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