You are here

public function AccessTest::testDrupalImage in Twig Tweak 8.2

Test callback.

File

tests/src/Kernel/AccessTest.php, line 424

Class

AccessTest
Tests for the Twig Tweak access control.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testDrupalImage() {

  // @codingStandardsIgnoreStart
  $create_image = function ($uri) {
    $file = new class([
      'uri' => $uri,
    ], 'file') extends File {
      public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
        $is_public = parse_url($this
          ->getFileUri(), PHP_URL_SCHEME) == 'public';
        $result = AccessResult::allowedIf($is_public);
        $result
          ->cachePerUser();
        $result
          ->addCacheTags([
          'tag_for_' . $this
            ->getFileUri(),
        ]);
        $result
          ->setCacheMaxAge(123);
        return $return_as_object ? $result : $result
          ->isAllowed();
      }
      public function getPlugin() {
        return NULL;
      }

    };
    $file
      ->setFileUri($uri);
    return $file;
  };

  // @codingStandardsIgnoreEnd
  $storage = $this
    ->createMock(EntityStorageInterface::class);
  $map = [
    [
      [
        'uri' => 'public://ocean.jpg',
      ],
      [
        $create_image('public://ocean.jpg'),
      ],
    ],
    [
      [
        'uri' => 'private://sea.jpg',
      ],
      [
        $create_image('private://sea.jpg'),
      ],
    ],
  ];
  $storage
    ->method('loadByProperties')
    ->will($this
    ->returnValueMap($map));
  $entity_type_manager = $this
    ->createMock(EntityTypeManagerInterface::class);
  $entity_type_manager
    ->method('getStorage')
    ->willReturn($storage);
  $this->container
    ->set('entity_type.manager', $entity_type_manager);

  // -- Public image with access check.
  $build = $this->twigExtension
    ->drupalImage('public://ocean.jpg');
  $expected_build = [
    '#uri' => 'public://ocean.jpg',
    '#attributes' => [],
    '#theme' => 'image',
    '#cache' => [
      'contexts' => [
        'user',
      ],
      'tags' => [
        'tag_for_public://ocean.jpg',
      ],
      'max-age' => 123,
    ],
  ];
  self::assertSame($expected_build, $build);

  // -- Public image without access check.
  $build = $this->twigExtension
    ->drupalImage('public://ocean.jpg', NULL, [], NULL, FALSE);
  $expected_build = [
    '#uri' => 'public://ocean.jpg',
    '#attributes' => [],
    '#theme' => 'image',
    '#cache' => [
      'contexts' => [],
      'tags' => [],
      'max-age' => Cache::PERMANENT,
    ],
  ];
  self::assertSame($expected_build, $build);

  // -- Private image with access check.
  $build = $this->twigExtension
    ->drupalImage('private://sea.jpg');
  self::assertNull($build);

  // -- Private image without access check.
  $build = $this->twigExtension
    ->drupalImage('private://sea.jpg', NULL, [], NULL, FALSE);
  $expected_build = [
    '#uri' => 'private://sea.jpg',
    '#attributes' => [],
    '#theme' => 'image',
    '#cache' => [
      'contexts' => [],
      'tags' => [],
      'max-age' => Cache::PERMANENT,
    ],
  ];
  self::assertSame($expected_build, $build);
}