You are here

public function ImageStyleTest::testGetPathToken in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/image/tests/src/Unit/ImageStyleTest.php \Drupal\Tests\image\Unit\ImageStyleTest::testGetPathToken()

@covers ::getPathToken

File

core/modules/image/tests/src/Unit/ImageStyleTest.php, line 162
Contains \Drupal\Tests\image\Unit\ImageStyleTest.

Class

ImageStyleTest
@coversDefaultClass \Drupal\image\Entity\ImageStyle

Namespace

Drupal\Tests\image\Unit

Code

public function testGetPathToken() {
  $logger = $this
    ->getMockBuilder('\\Psr\\Log\\LoggerInterface')
    ->getMock();
  $private_key = $this
    ->randomMachineName();
  $hash_salt = $this
    ->randomMachineName();

  // Image style that changes the extension.
  $image_effect_id = $this
    ->randomMachineName();
  $image_effect = $this
    ->getMockBuilder('\\Drupal\\image\\ImageEffectBase')
    ->setConstructorArgs(array(
    array(),
    $image_effect_id,
    array(),
    $logger,
  ))
    ->getMock();
  $image_effect
    ->expects($this
    ->any())
    ->method('getDerivativeExtension')
    ->will($this
    ->returnValue('png'));
  $image_style = $this
    ->getImageStyleMock($image_effect_id, $image_effect, array(
    'getPrivateKey',
    'getHashSalt',
  ));
  $image_style
    ->expects($this
    ->any())
    ->method('getPrivateKey')
    ->will($this
    ->returnValue($private_key));
  $image_style
    ->expects($this
    ->any())
    ->method('getHashSalt')
    ->will($this
    ->returnValue($hash_salt));

  // Assert the extension has been added to the URI before creating the token.
  $this
    ->assertEquals($image_style
    ->getPathToken('public://test.jpeg.png'), $image_style
    ->getPathToken('public://test.jpeg'));
  $this
    ->assertEquals(substr(Crypt::hmacBase64($image_style
    ->id() . ':' . 'public://test.jpeg.png', $private_key . $hash_salt), 0, 8), $image_style
    ->getPathToken('public://test.jpeg'));
  $this
    ->assertNotEquals(substr(Crypt::hmacBase64($image_style
    ->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style
    ->getPathToken('public://test.jpeg'));

  // Image style that doesn't change the extension.
  $image_effect_id = $this
    ->randomMachineName();
  $image_effect = $this
    ->getMockBuilder('\\Drupal\\image\\ImageEffectBase')
    ->setConstructorArgs(array(
    array(),
    $image_effect_id,
    array(),
    $logger,
  ))
    ->getMock();
  $image_effect
    ->expects($this
    ->any())
    ->method('getDerivativeExtension')
    ->will($this
    ->returnArgument(0));
  $image_style = $this
    ->getImageStyleMock($image_effect_id, $image_effect, array(
    'getPrivateKey',
    'getHashSalt',
  ));
  $image_style
    ->expects($this
    ->any())
    ->method('getPrivateKey')
    ->will($this
    ->returnValue($private_key));
  $image_style
    ->expects($this
    ->any())
    ->method('getHashSalt')
    ->will($this
    ->returnValue($hash_salt));

  // Assert no extension has been added to the uri before creating the token.
  $this
    ->assertNotEquals($image_style
    ->getPathToken('public://test.jpeg.png'), $image_style
    ->getPathToken('public://test.jpeg'));
  $this
    ->assertNotEquals(substr(Crypt::hmacBase64($image_style
    ->id() . ':' . 'public://test.jpeg.png', $private_key . $hash_salt), 0, 8), $image_style
    ->getPathToken('public://test.jpeg'));
  $this
    ->assertEquals(substr(Crypt::hmacBase64($image_style
    ->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style
    ->getPathToken('public://test.jpeg'));
}