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