function ImageStyleTest::testGetPathToken

Same name and namespace in other branches
  1. 9 core/modules/image/tests/src/Unit/ImageStyleTest.php \Drupal\Tests\image\Unit\ImageStyleTest::testGetPathToken()
  2. 8.9.x core/modules/image/tests/src/Unit/ImageStyleTest.php \Drupal\Tests\image\Unit\ImageStyleTest::testGetPathToken()
  3. 11.x 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 153

Class

ImageStyleTest
@coversDefaultClass \Drupal\image\Entity\ImageStyle[[api-linebreak]]

Namespace

Drupal\Tests\image\Unit

Code

public function testGetPathToken() : void {
  $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([
    [],
    $image_effect_id,
    [],
    $logger,
  ])
    ->getMock();
  $image_effect->expects($this->any())
    ->method('getDerivativeExtension')
    ->willReturn('png');
  $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, [
    'getPrivateKey',
    'getHashSalt',
  ]);
  $image_style->expects($this->any())
    ->method('getPrivateKey')
    ->willReturn($private_key);
  $image_style->expects($this->any())
    ->method('getHashSalt')
    ->willReturn($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([
    [],
    $image_effect_id,
    [],
    $logger,
  ])
    ->getMock();
  $image_effect->expects($this->any())
    ->method('getDerivativeExtension')
    ->willReturnArgument(0);
  $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, [
    'getPrivateKey',
    'getHashSalt',
  ]);
  $image_style->expects($this->any())
    ->method('getPrivateKey')
    ->willReturn($private_key);
  $image_style->expects($this->any())
    ->method('getHashSalt')
    ->willReturn($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'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.