You are here

public function MediaAccessTest::testUnpublishedMediaUserAccess in Drupal 10

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

Tests unpublished media access.

File

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

Class

MediaAccessTest
Basic access tests for Media.

Namespace

Drupal\Tests\media\Functional

Code

public function testUnpublishedMediaUserAccess() {
  \Drupal::configFactory()
    ->getEditable('media.settings')
    ->set('standalone_url', TRUE)
    ->save(TRUE);
  $this->container
    ->get('router.builder')
    ->rebuild();
  $assert_session = $this
    ->assertSession();
  $media_type = $this
    ->createMediaType('test');
  $permissions = [
    'view media',
    'view own unpublished media',
  ];
  $user_one = $this
    ->drupalCreateUser($permissions);
  $user_two = $this
    ->drupalCreateUser($permissions);

  // Create media as user one.
  $user_media = Media::create([
    'bundle' => $media_type
      ->id(),
    'name' => 'Unnamed',
    'uid' => $user_one
      ->id(),
  ]);
  $user_media
    ->setUnpublished()
    ->save();

  // Make sure user two can't access unpublished media.
  $this
    ->drupalLogin($user_two);
  $this
    ->drupalGet('media/' . $user_media
    ->id());
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->assertCacheContext('user');
  $this
    ->drupalLogout();

  // Make sure user one can access own unpublished media.
  $this
    ->drupalLogin($user_one);
  $this
    ->drupalGet('media/' . $user_media
    ->id());
  $assert_session
    ->statusCodeEquals(200);
  $this
    ->assertCacheContext('user');
}