3 namespace Drupal\system\Tests\Image;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Component\Utility\SafeMarkup;
9 * Base class for image manipulation testing.
11 abstract class ToolkitTestBase extends WebTestBase {
18 public static $modules = ['image_test'];
21 * The URI for the file.
28 * The image factory service.
30 * @var \Drupal\Core\Image\ImageFactory
32 protected $imageFactory;
35 * The image object for the test file.
37 * @var \Drupal\Core\Image\ImageInterface
41 protected function setUp() {
44 // Set the image factory service.
45 $this->imageFactory = $this->container->get('image.factory');
47 // Pick a file for testing.
48 $file = current($this->drupalGetTestFiles('image'));
49 $this->file = $file->uri;
51 // Setup a dummy image to work with.
52 $this->image = $this->getImage();
54 // Clear out any hook calls.
55 $this->imageTestReset();
59 * Sets up an image with the custom toolkit.
61 * @return \Drupal\Core\Image\ImageInterface
64 protected function getImage() {
65 $image = $this->imageFactory->get($this->file, 'test');
66 $this->assertTrue($image->isValid(), 'Image file was parsed.');
71 * Assert that all of the specified image toolkit operations were called
72 * exactly once once, other values result in failure.
75 * Array with string containing with the operation name, e.g. 'load',
76 * 'save', 'crop', etc.
78 public function assertToolkitOperationsCalled(array $expected) {
79 // If one of the image operations is expected, apply should be expected as
92 if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
93 $expected[] = 'apply';
96 // Determine which operations were called.
97 $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
99 // Determine if there were any expected that were not called.
100 $uncalled = array_diff($expected, $actual);
101 if (count($uncalled)) {
102 $this->assertTrue(FALSE, SafeMarkup::format('Expected operations %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
105 $this->assertTrue(TRUE, SafeMarkup::format('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)]));
108 // Determine if there were any unexpected calls.
109 // If all unexpected calls are operations and apply was expected, we do not
110 // count it as an error.
111 $unexpected = array_diff($actual, $expected);
112 if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
113 $this->assertTrue(FALSE, SafeMarkup::format('Unexpected operations were called: %unexpected.', ['%unexpected' => implode(', ', $unexpected)]));
116 $this->assertTrue(TRUE, 'No unexpected operations were called.');
121 * Resets/initializes the history of calls to the test toolkit functions.
123 public function imageTestReset() {
124 // Keep track of calls to these operations
136 'scale_and_crop' => [],
139 \Drupal::state()->set('image_test.results', $results);
143 * Gets an array of calls to the test toolkit.
146 * An array keyed by operation name ('parseFile', 'save', 'settings',
147 * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of
148 * parameters passed to each call.
150 public function imageTestGetAllCalls() {
151 return \Drupal::state()->get('image_test.results') ?: [];