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