/core/modules/image/tests/src/Unit/ImageStyleTest.php

https://gitlab.com/geeta7/drupal · PHP · 242 lines · 146 code · 30 blank · 66 comment · 1 complexity · ab9935dbad34b7ffd09ee07bccc7366a MD5 · raw file

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