3 namespace Drupal\Tests\image\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Component\Utility\Crypt;
9 * @coversDefaultClass \Drupal\image\Entity\ImageStyle
13 class ImageStyleTest extends UnitTestCase {
16 * The entity type used for testing.
18 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $entityType;
23 * The entity manager used for testing.
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $entityManager;
30 * The ID of the type of the entity under test.
34 protected $entityTypeId;
37 * Gets a mocked image style for testing.
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.
44 * @return \Drupal\image\ImageStyleInterface
45 * The mocked image style.
47 protected function getImageStyleMock($image_effect_id, $image_effect, $stubs = []) {
48 $effectManager = $this->getMockBuilder('\Drupal\image\ImageEffectManager')
49 ->disableOriginalConstructor()
51 $effectManager->expects($this->any())
52 ->method('createInstance')
53 ->with($image_effect_id)
54 ->will($this->returnValue($image_effect));
56 'getImageEffectPluginManager',
61 $image_style = $this->getMockBuilder('\Drupal\image\Entity\ImageStyle')
62 ->setConstructorArgs([
63 ['effects' => [$image_effect_id => ['id' => $image_effect_id]]],
66 ->setMethods(array_merge($default_stubs, $stubs))
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']));
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));
103 * @covers ::getDerivativeExtension
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])
111 $image_effect->expects($this->any())
112 ->method('getDerivativeExtension')
113 ->will($this->returnValue('png'));
115 $image_style = $this->getImageStyleMock($image_effect_id, $image_effect);
117 $extensions = ['jpeg', 'gif', 'png'];
118 foreach ($extensions as $extension) {
119 $extensionReturned = $image_style->getDerivativeExtension($extension);
120 $this->assertEquals($extensionReturned, 'png');
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])
134 $image_effect->expects($this->any())
135 ->method('getDerivativeExtension')
136 ->will($this->returnValue('png'));
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');
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])
146 $image_effect->expects($this->any())
147 ->method('getDerivativeExtension')
148 ->will($this->returnArgument(0));
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');
155 * @covers ::getPathToken
157 public function testGetPathToken() {
158 $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock();
159 $private_key = $this->randomMachineName();
160 $hash_salt = $this->randomMachineName();
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])
167 $image_effect->expects($this->any())
168 ->method('getDerivativeExtension')
169 ->will($this->returnValue('png'));
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));
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'));
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])
189 $image_effect->expects($this->any())
190 ->method('getDerivativeExtension')
191 ->will($this->returnArgument(0));
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'));
207 * Mock function for ImageStyle::fileUriScheme().
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);
219 * Mock function for ImageStyle::fileUriTarget().
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), '\/');
226 // If nothing was replaced, the URI doesn't have a valid scheme.
227 return $target !== $uri ? $target : FALSE;
231 * Mock function for ImageStyle::fileDefaultScheme().
233 public function fileDefaultScheme() {