Mercurial > hg > isophonics-drupal-site
comparison core/modules/image/tests/src/Unit/ImageStyleTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\image\Unit; | |
4 | |
5 use Drupal\Tests\UnitTestCase; | |
6 use Drupal\Component\Utility\Crypt; | |
7 | |
8 /** | |
9 * @coversDefaultClass \Drupal\image\Entity\ImageStyle | |
10 * | |
11 * @group Image | |
12 */ | |
13 class ImageStyleTest extends UnitTestCase { | |
14 | |
15 /** | |
16 * The entity type used for testing. | |
17 * | |
18 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject | |
19 */ | |
20 protected $entityType; | |
21 | |
22 /** | |
23 * The entity manager used for testing. | |
24 * | |
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject | |
26 */ | |
27 protected $entityManager; | |
28 | |
29 /** | |
30 * The ID of the type of the entity under test. | |
31 * | |
32 * @var string | |
33 */ | |
34 protected $entityTypeId; | |
35 | |
36 /** | |
37 * Gets a mocked image style for testing. | |
38 * | |
39 * @param string $image_effect_id | |
40 * The image effect ID. | |
41 * @param \Drupal\image\ImageEffectInterface|\PHPUnit_Framework_MockObject_MockObject $image_effect | |
42 * The image effect used for testing. | |
43 * | |
44 * @return \Drupal\image\ImageStyleInterface | |
45 * The mocked image style. | |
46 */ | |
47 protected function getImageStyleMock($image_effect_id, $image_effect, $stubs = []) { | |
48 $effectManager = $this->getMockBuilder('\Drupal\image\ImageEffectManager') | |
49 ->disableOriginalConstructor() | |
50 ->getMock(); | |
51 $effectManager->expects($this->any()) | |
52 ->method('createInstance') | |
53 ->with($image_effect_id) | |
54 ->will($this->returnValue($image_effect)); | |
55 $default_stubs = [ | |
56 'getImageEffectPluginManager', | |
57 'fileUriScheme', | |
58 'fileUriTarget', | |
59 'fileDefaultScheme', | |
60 ]; | |
61 $image_style = $this->getMockBuilder('\Drupal\image\Entity\ImageStyle') | |
62 ->setConstructorArgs([ | |
63 ['effects' => [$image_effect_id => ['id' => $image_effect_id]]], | |
64 $this->entityTypeId, | |
65 ]) | |
66 ->setMethods(array_merge($default_stubs, $stubs)) | |
67 ->getMock(); | |
68 | |
69 $image_style->expects($this->any()) | |
70 ->method('getImageEffectPluginManager') | |
71 ->will($this->returnValue($effectManager)); | |
72 $image_style->expects($this->any()) | |
73 ->method('fileUriScheme') | |
74 ->will($this->returnCallback([$this, 'fileUriScheme'])); | |
75 $image_style->expects($this->any()) | |
76 ->method('fileUriTarget') | |
77 ->will($this->returnCallback([$this, 'fileUriTarget'])); | |
78 $image_style->expects($this->any()) | |
79 ->method('fileDefaultScheme') | |
80 ->will($this->returnCallback([$this, 'fileDefaultScheme'])); | |
81 | |
82 return $image_style; | |
83 } | |
84 | |
85 /** | |
86 * {@inheritdoc} | |
87 */ | |
88 protected function setUp() { | |
89 $this->entityTypeId = $this->randomMachineName(); | |
90 $this->provider = $this->randomMachineName(); | |
91 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); | |
92 $this->entityType->expects($this->any()) | |
93 ->method('getProvider') | |
94 ->will($this->returnValue($this->provider)); | |
95 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); | |
96 $this->entityManager->expects($this->any()) | |
97 ->method('getDefinition') | |
98 ->with($this->entityTypeId) | |
99 ->will($this->returnValue($this->entityType)); | |
100 } | |
101 | |
102 /** | |
103 * @covers ::getDerivativeExtension | |
104 */ | |
105 public function testGetDerivativeExtension() { | |
106 $image_effect_id = $this->randomMachineName(); | |
107 $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); | |
108 $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase') | |
109 ->setConstructorArgs([[], $image_effect_id, [], $logger]) | |
110 ->getMock(); | |
111 $image_effect->expects($this->any()) | |
112 ->method('getDerivativeExtension') | |
113 ->will($this->returnValue('png')); | |
114 | |
115 $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); | |
116 | |
117 $extensions = ['jpeg', 'gif', 'png']; | |
118 foreach ($extensions as $extension) { | |
119 $extensionReturned = $image_style->getDerivativeExtension($extension); | |
120 $this->assertEquals($extensionReturned, 'png'); | |
121 } | |
122 } | |
123 | |
124 /** | |
125 * @covers ::buildUri | |
126 */ | |
127 public function testBuildUri() { | |
128 // Image style that changes the extension. | |
129 $image_effect_id = $this->randomMachineName(); | |
130 $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); | |
131 $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase') | |
132 ->setConstructorArgs([[], $image_effect_id, [], $logger]) | |
133 ->getMock(); | |
134 $image_effect->expects($this->any()) | |
135 ->method('getDerivativeExtension') | |
136 ->will($this->returnValue('png')); | |
137 | |
138 $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); | |
139 $this->assertEquals($image_style->buildUri('public://test.jpeg'), 'public://styles/' . $image_style->id() . '/public/test.jpeg.png'); | |
140 | |
141 // Image style that doesn't change the extension. | |
142 $image_effect_id = $this->randomMachineName(); | |
143 $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase') | |
144 ->setConstructorArgs([[], $image_effect_id, [], $logger]) | |
145 ->getMock(); | |
146 $image_effect->expects($this->any()) | |
147 ->method('getDerivativeExtension') | |
148 ->will($this->returnArgument(0)); | |
149 | |
150 $image_style = $this->getImageStyleMock($image_effect_id, $image_effect); | |
151 $this->assertEquals($image_style->buildUri('public://test.jpeg'), 'public://styles/' . $image_style->id() . '/public/test.jpeg'); | |
152 } | |
153 | |
154 /** | |
155 * @covers ::getPathToken | |
156 */ | |
157 public function testGetPathToken() { | |
158 $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); | |
159 $private_key = $this->randomMachineName(); | |
160 $hash_salt = $this->randomMachineName(); | |
161 | |
162 // Image style that changes the extension. | |
163 $image_effect_id = $this->randomMachineName(); | |
164 $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase') | |
165 ->setConstructorArgs([[], $image_effect_id, [], $logger]) | |
166 ->getMock(); | |
167 $image_effect->expects($this->any()) | |
168 ->method('getDerivativeExtension') | |
169 ->will($this->returnValue('png')); | |
170 | |
171 $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, ['getPrivateKey', 'getHashSalt']); | |
172 $image_style->expects($this->any()) | |
173 ->method('getPrivateKey') | |
174 ->will($this->returnValue($private_key)); | |
175 $image_style->expects($this->any()) | |
176 ->method('getHashSalt') | |
177 ->will($this->returnValue($hash_salt)); | |
178 | |
179 // Assert the extension has been added to the URI before creating the token. | |
180 $this->assertEquals($image_style->getPathToken('public://test.jpeg.png'), $image_style->getPathToken('public://test.jpeg')); | |
181 $this->assertEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg.png', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg')); | |
182 $this->assertNotEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg')); | |
183 | |
184 // Image style that doesn't change the extension. | |
185 $image_effect_id = $this->randomMachineName(); | |
186 $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase') | |
187 ->setConstructorArgs([[], $image_effect_id, [], $logger]) | |
188 ->getMock(); | |
189 $image_effect->expects($this->any()) | |
190 ->method('getDerivativeExtension') | |
191 ->will($this->returnArgument(0)); | |
192 | |
193 $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, ['getPrivateKey', 'getHashSalt']); | |
194 $image_style->expects($this->any()) | |
195 ->method('getPrivateKey') | |
196 ->will($this->returnValue($private_key)); | |
197 $image_style->expects($this->any()) | |
198 ->method('getHashSalt') | |
199 ->will($this->returnValue($hash_salt)); | |
200 // Assert no extension has been added to the uri before creating the token. | |
201 $this->assertNotEquals($image_style->getPathToken('public://test.jpeg.png'), $image_style->getPathToken('public://test.jpeg')); | |
202 $this->assertNotEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg.png', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg')); | |
203 $this->assertEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg')); | |
204 } | |
205 | |
206 /** | |
207 * Mock function for ImageStyle::fileUriScheme(). | |
208 */ | |
209 public function fileUriScheme($uri) { | |
210 if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) { | |
211 // The scheme will always be the last element in the matches array. | |
212 return array_pop($matches); | |
213 } | |
214 | |
215 return FALSE; | |
216 } | |
217 | |
218 /** | |
219 * Mock function for ImageStyle::fileUriTarget(). | |
220 */ | |
221 public function fileUriTarget($uri) { | |
222 // Remove the scheme from the URI and remove erroneous leading or trailing, | |
223 // forward-slashes and backslashes. | |
224 $target = trim(preg_replace('/^[\w\-]+:\/\/|^data:/', '', $uri), '\/'); | |
225 | |
226 // If nothing was replaced, the URI doesn't have a valid scheme. | |
227 return $target !== $uri ? $target : FALSE; | |
228 } | |
229 | |
230 /** | |
231 * Mock function for ImageStyle::fileDefaultScheme(). | |
232 */ | |
233 public function fileDefaultScheme() { | |
234 return 'public'; | |
235 } | |
236 | |
237 } |