You are here

public function MediaAccessTest::testReferencedRendering in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/Functional/MediaAccessTest.php \Drupal\Tests\media\Functional\MediaAccessTest::testReferencedRendering()

Tests access for embedded medias.

File

core/modules/media/tests/src/Functional/MediaAccessTest.php, line 323

Class

MediaAccessTest
Basic access tests for Media.

Namespace

Drupal\Tests\media\Functional

Code

public function testReferencedRendering() {
  \Drupal::configFactory()
    ->getEditable('media.settings')
    ->set('standalone_url', TRUE)
    ->save(TRUE);
  $this->container
    ->get('router.builder')
    ->rebuild();

  // Create a media type and a entity reference to itself.
  $media_type = $this
    ->createMediaType('test');
  FieldStorageConfig::create([
    'field_name' => 'field_reference',
    'entity_type' => 'media',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_reference',
    'entity_type' => 'media',
    'bundle' => $media_type
      ->id(),
  ])
    ->save();
  $author = $this
    ->drupalCreateUser([
    'view media',
    'view own unpublished media',
  ]);
  $other_user = $this
    ->drupalCreateUser([
    'view media',
    'view own unpublished media',
  ]);
  $view_user = $this
    ->drupalCreateUser([
    'view media',
  ]);
  $child_title = 'Child media';
  $media_child = Media::create([
    'name' => $child_title,
    'bundle' => $media_type
      ->id(),
    'uid' => $author
      ->id(),
  ]);
  $media_child
    ->setUnpublished()
    ->save();
  $media_parent = Media::create([
    'name' => 'Parent media',
    'bundle' => $media_type
      ->id(),
    'field_reference' => $media_child
      ->id(),
  ]);
  $media_parent
    ->save();
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('media', $media_type
    ->id(), 'full')
    ->set('content', [])
    ->setComponent('title', [
    'type' => 'string',
  ])
    ->setComponent('field_reference', [
    'type' => 'entity_reference_label',
  ])
    ->save();
  $assert_session = $this
    ->assertSession();

  // The author of the child media items should have access to both the parent
  // and child.
  $this
    ->drupalLogin($author);
  $this
    ->drupalGet($media_parent
    ->toUrl());
  $this
    ->assertCacheContext('user');
  $assert_session
    ->pageTextContains($child_title);

  // Other users with the 'view own unpublished media' permission should not
  // be able to see the unpublished child media item. The 'user' cache context
  // should be added in this case.
  $this
    ->drupalLogin($other_user);
  $this
    ->drupalGet($media_parent
    ->toUrl());
  $this
    ->assertCacheContext('user');
  $assert_session
    ->pageTextNotContains($child_title);

  // User with just the 'view media' permission should not be able to see the
  // child media item. The 'user' cache context should not be added in this
  // case.
  $this
    ->drupalLogin($view_user);
  $this
    ->drupalGet($media_parent
    ->toUrl());
  $this
    ->assertNoCacheContext('user');
  $assert_session
    ->pageTextNotContains($child_title);
}