3 namespace Drupal\FunctionalTests\Image;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
10 * Base class for image manipulation testing.
12 abstract class ToolkitTestBase extends BrowserTestBase {
14 use TestFileCreationTrait {
15 getTestFiles as drupalGetTestFiles;
22 public static $modules = ['image_test'];
25 * The URI for the file.
32 * The image factory service.
34 * @var \Drupal\Core\Image\ImageFactory
36 protected $imageFactory;
39 * The image object for the test file.
41 * @var \Drupal\Core\Image\ImageInterface
45 protected function setUp() {
48 // Set the image factory service.
49 $this->imageFactory = $this->container->get('image.factory');
51 // Pick a file for testing.
52 $file = current($this->drupalGetTestFiles('image'));
53 $this->file = $file->uri;
55 // Setup a dummy image to work with.
56 $this->image = $this->getImage();
58 // Clear out any hook calls.
59 $this->imageTestReset();
63 * Sets up an image with the custom toolkit.
65 * @return \Drupal\Core\Image\ImageInterface
68 protected function getImage() {
69 $image = $this->imageFactory->get($this->file, 'test');
70 $this->assertTrue($image->isValid(), 'Image file was parsed.');
75 * Assert that all of the specified image toolkit operations were called
76 * exactly once once, other values result in failure.
79 * Array with string containing with the operation name, e.g. 'load',
80 * 'save', 'crop', etc.
82 public function assertToolkitOperationsCalled(array $expected) {
83 // If one of the image operations is expected, apply should be expected as
96 if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
97 $expected[] = 'apply';
100 // Determine which operations were called.
101 $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
103 // Determine if there were any expected that were not called.
104 $uncalled = array_diff($expected, $actual);
105 if (count($uncalled)) {
106 $this->assertTrue(FALSE, SafeMarkup::format('Expected operations %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
109 $this->assertTrue(TRUE, SafeMarkup::format('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)]));
112 // Determine if there were any unexpected calls.
113 // If all unexpected calls are operations and apply was expected, we do not
114 // count it as an error.
115 $unexpected = array_diff($actual, $expected);
116 if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
117 $this->assertTrue(FALSE, SafeMarkup::format('Unexpected operations were called: %unexpected.', ['%unexpected' => implode(', ', $unexpected)]));
120 $this->assertTrue(TRUE, 'No unexpected operations were called.');
125 * Resets/initializes the history of calls to the test toolkit functions.
127 protected function imageTestReset() {
128 // Keep track of calls to these operations
140 'scale_and_crop' => [],
143 \Drupal::state()->set('image_test.results', $results);
147 * Gets an array of calls to the test toolkit.
150 * An array keyed by operation name ('parseFile', 'save', 'settings',
151 * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of
152 * parameters passed to each call.
154 protected function imageTestGetAllCalls() {
155 return \Drupal::state()->get('image_test.results') ?: [];