0051e94117397d12ad58842680d718b77700092f
[yaffs-website] / web / core / modules / system / src / Tests / Image / ToolkitTestBase.php
1 <?php
2
3 namespace Drupal\system\Tests\Image;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Component\Utility\SafeMarkup;
7
8 /**
9  * Base class for image manipulation testing.
10  */
11 abstract class ToolkitTestBase extends WebTestBase {
12
13   /**
14    * Modules to enable.
15    *
16    * @var array
17    */
18   public static $modules = ['image_test'];
19
20   /**
21    * The URI for the file.
22    *
23    * @var string
24    */
25   protected $file;
26
27   /**
28    * The image factory service.
29    *
30    * @var \Drupal\Core\Image\ImageFactory
31    */
32   protected $imageFactory;
33
34   /**
35    * The image object for the test file.
36    *
37    * @var \Drupal\Core\Image\ImageInterface
38    */
39   protected $image;
40
41   protected function setUp() {
42     parent::setUp();
43
44     // Set the image factory service.
45     $this->imageFactory = $this->container->get('image.factory');
46
47     // Pick a file for testing.
48     $file = current($this->drupalGetTestFiles('image'));
49     $this->file = $file->uri;
50
51     // Setup a dummy image to work with.
52     $this->image = $this->getImage();
53
54     // Clear out any hook calls.
55     $this->imageTestReset();
56   }
57
58   /**
59    * Sets up an image with the custom toolkit.
60    *
61    * @return \Drupal\Core\Image\ImageInterface
62    *   The image object.
63    */
64   protected function getImage() {
65     $image = $this->imageFactory->get($this->file, 'test');
66     $this->assertTrue($image->isValid(), 'Image file was parsed.');
67     return $image;
68   }
69
70   /**
71    * Assert that all of the specified image toolkit operations were called
72    * exactly once once, other values result in failure.
73    *
74    * @param $expected
75    *   Array with string containing with the operation name, e.g. 'load',
76    *   'save', 'crop', etc.
77    */
78   public function assertToolkitOperationsCalled(array $expected) {
79     // If one of the image operations is expected, apply should be expected as
80     // well.
81     $operations = [
82       'resize',
83       'rotate',
84       'crop',
85       'desaturate',
86       'create_new',
87       'scale',
88       'scale_and_crop',
89       'my_operation',
90       'convert',
91     ];
92     if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
93       $expected[] = 'apply';
94     }
95
96     // Determine which operations were called.
97     $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
98
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)]));
103     }
104     else {
105       $this->assertTrue(TRUE, SafeMarkup::format('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)]));
106     }
107
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)]));
114     }
115     else {
116       $this->assertTrue(TRUE, 'No unexpected operations were called.');
117     }
118   }
119
120   /**
121    * Resets/initializes the history of calls to the test toolkit functions.
122    */
123   public function imageTestReset() {
124     // Keep track of calls to these operations
125     $results = [
126       'parseFile' => [],
127       'save' => [],
128       'settings' => [],
129       'apply' => [],
130       'resize' => [],
131       'rotate' => [],
132       'crop' => [],
133       'desaturate' => [],
134       'create_new' => [],
135       'scale' => [],
136       'scale_and_crop' => [],
137       'convert' => [],
138     ];
139     \Drupal::state()->set('image_test.results', $results);
140   }
141
142   /**
143    * Gets an array of calls to the test toolkit.
144    *
145    * @return array
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.
149    */
150   public function imageTestGetAllCalls() {
151     return \Drupal::state()->get('image_test.results') ?: [];
152   }
153
154 }