You are here

public function MediaEmbedFilterTest::testAccessUnpublished in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaEmbedFilterTest.php \Drupal\Tests\media\Kernel\MediaEmbedFilterTest::testAccessUnpublished()
  2. 10 core/modules/media/tests/src/Kernel/MediaEmbedFilterTest.php \Drupal\Tests\media\Kernel\MediaEmbedFilterTest::testAccessUnpublished()

Tests that entity access is respected by embedding an unpublished entity.

@dataProvider providerAccessUnpublished

File

core/modules/media/tests/src/Kernel/MediaEmbedFilterTest.php, line 132

Class

MediaEmbedFilterTest
@coversDefaultClass \Drupal\media\Plugin\Filter\MediaEmbed @group media

Namespace

Drupal\Tests\media\Kernel

Code

public function testAccessUnpublished($allowed_to_view_unpublished, $expected_rendered, CacheableMetadata $expected_cacheability, array $expected_attachments) {

  // Unpublish the embedded entity so we can test variations in behavior.
  $this->embeddedEntity
    ->setUnpublished()
    ->save();

  // Are we testing as a user who is allowed to view the embedded entity?
  if ($allowed_to_view_unpublished) {
    $this->container
      ->get('current_user')
      ->addRole($this
      ->drupalCreateRole([
      'view own unpublished media',
    ]));
  }
  $content = $this
    ->createEmbedCode([
    'data-entity-type' => 'media',
    'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID,
  ]);
  $result = $this
    ->applyFilter($content);
  if (!$expected_rendered) {
    $this
      ->assertEmpty($this
      ->getRawContent());
  }
  else {
    $this
      ->assertCount(1, $this
      ->cssSelect('div[data-media-embed-test-view-mode="default"]'));
  }
  $this
    ->assertSame($expected_cacheability
    ->getCacheTags(), $result
    ->getCacheTags());
  $this
    ->assertSame($expected_cacheability
    ->getCacheContexts(), $result
    ->getCacheContexts());
  $this
    ->assertSame($expected_cacheability
    ->getCacheMaxAge(), $result
    ->getCacheMaxAge());
  $this
    ->assertSame($expected_attachments, $result
    ->getAttachments());
}