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