3 namespace Drupal\FunctionalTests\Image;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
11 * Base class for image manipulation testing.
13 abstract class ToolkitTestBase extends BrowserTestBase {
15 use TestFileCreationTrait {
16 getTestFiles as drupalGetTestFiles;
23 public static $modules = ['image_test'];
26 * The URI for the file.
33 * The image factory service.
35 * @var \Drupal\Core\Image\ImageFactory
37 protected $imageFactory;
40 * The image object for the test file.
42 * @var \Drupal\Core\Image\ImageInterface
46 protected function setUp() {
49 // Set the image factory service.
50 $this->imageFactory = $this->container->get('image.factory');
52 // Pick a file for testing.
53 $file = current($this->drupalGetTestFiles('image'));
54 $this->file = $file->uri;
56 // Setup a dummy image to work with.
57 $this->image = $this->getImage();
59 // Clear out any hook calls.
60 $this->imageTestReset();
64 * Sets up an image with the custom toolkit.
66 * @return \Drupal\Core\Image\ImageInterface
69 protected function getImage() {
70 $image = $this->imageFactory->get($this->file, 'test');
71 $this->assertTrue($image->isValid(), 'Image file was parsed.');
76 * Assert that all of the specified image toolkit operations were called
77 * exactly once once, other values result in failure.
80 * Array with string containing with the operation name, e.g. 'load',
81 * 'save', 'crop', etc.
83 public function assertToolkitOperationsCalled(array $expected) {
84 // If one of the image operations is expected, apply should be expected as
97 if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
98 $expected[] = 'apply';
101 // Determine which operations were called.
102 $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
104 // Determine if there were any expected that were not called.
105 $uncalled = array_diff($expected, $actual);
106 if (count($uncalled)) {
107 $this->assertTrue(FALSE, SafeMarkup::format('Expected operations %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
110 $this->assertTrue(TRUE, SafeMarkup::format('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)]));
113 // Determine if there were any unexpected calls.
114 // If all unexpected calls are operations and apply was expected, we do not
115 // count it as an error.
116 $unexpected = array_diff($actual, $expected);
117 if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
118 $this->assertTrue(FALSE, SafeMarkup::format('Unexpected operations were called: %unexpected.', ['%unexpected' => implode(', ', $unexpected)]));
121 $this->assertTrue(TRUE, 'No unexpected operations were called.');
126 * Resets/initializes the history of calls to the test toolkit functions.
128 protected function imageTestReset() {
129 // Keep track of calls to these operations
141 'scale_and_crop' => [],
144 \Drupal::state()->set('image_test.results', $results);
148 * Gets an array of calls to the test toolkit.
151 * An array keyed by operation name ('parseFile', 'save', 'settings',
152 * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of
153 * parameters passed to each call.
155 protected function imageTestGetAllCalls() {
156 return \Drupal::state()->get('image_test.results') ?: [];