comparison core/modules/system/src/Tests/Image/ToolkitTestBase.php @ 0:4c8ae668cc8c

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