3 namespace Drupal\system\Tests\Image;
5 @trigger_error(__FILE__ . ' is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use Drupal\FunctionalTests\Image\ToolkitTestBase instead. See https://www.drupal.org/node/2862641.', E_USER_DEPRECATED);
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\Component\Utility\SafeMarkup;
11 * Base class for image manipulation testing.
13 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
14 * Use Drupal\FunctionalTests\Image\ToolkitTestBase instead.
16 * @see https://www.drupal.org/node/2862641
18 abstract class ToolkitTestBase extends WebTestBase {
25 public static $modules = ['image_test'];
28 * The URI for the file.
35 * The image factory service.
37 * @var \Drupal\Core\Image\ImageFactory
39 protected $imageFactory;
42 * The image object for the test file.
44 * @var \Drupal\Core\Image\ImageInterface
48 protected function setUp() {
51 // Set the image factory service.
52 $this->imageFactory = $this->container->get('image.factory');
54 // Pick a file for testing.
55 $file = current($this->drupalGetTestFiles('image'));
56 $this->file = $file->uri;
58 // Setup a dummy image to work with.
59 $this->image = $this->getImage();
61 // Clear out any hook calls.
62 $this->imageTestReset();
66 * Sets up an image with the custom toolkit.
68 * @return \Drupal\Core\Image\ImageInterface
71 protected function getImage() {
72 $image = $this->imageFactory->get($this->file, 'test');
73 $this->assertTrue($image->isValid(), 'Image file was parsed.');
78 * Assert that all of the specified image toolkit operations were called
79 * exactly once once, other values result in failure.
82 * Array with string containing with the operation name, e.g. 'load',
83 * 'save', 'crop', etc.
85 public function assertToolkitOperationsCalled(array $expected) {
86 // If one of the image operations is expected, apply should be expected as
99 if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
100 $expected[] = 'apply';
103 // Determine which operations were called.
104 $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
106 // Determine if there were any expected that were not called.
107 $uncalled = array_diff($expected, $actual);
108 if (count($uncalled)) {
109 $this->assertTrue(FALSE, SafeMarkup::format('Expected operations %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
112 $this->assertTrue(TRUE, SafeMarkup::format('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)]));
115 // Determine if there were any unexpected calls.
116 // If all unexpected calls are operations and apply was expected, we do not
117 // count it as an error.
118 $unexpected = array_diff($actual, $expected);
119 if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
120 $this->assertTrue(FALSE, SafeMarkup::format('Unexpected operations were called: %unexpected.', ['%unexpected' => implode(', ', $unexpected)]));
123 $this->assertTrue(TRUE, 'No unexpected operations were called.');
128 * Resets/initializes the history of calls to the test toolkit functions.
130 public function imageTestReset() {
131 // Keep track of calls to these operations
143 'scale_and_crop' => [],
146 \Drupal::state()->set('image_test.results', $results);
150 * Gets an array of calls to the test toolkit.
153 * An array keyed by operation name ('parseFile', 'save', 'settings',
154 * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of
155 * parameters passed to each call.
157 public function imageTestGetAllCalls() {
158 return \Drupal::state()->get('image_test.results') ?: [];