function MediaAccessTest::testMediaAnonymousUserAccess

Same name in other branches
  1. 9 core/modules/media/tests/src/Functional/MediaAccessTest.php \Drupal\Tests\media\Functional\MediaAccessTest::testMediaAnonymousUserAccess()
  2. 10 core/modules/media/tests/src/Functional/MediaAccessTest.php \Drupal\Tests\media\Functional\MediaAccessTest::testMediaAnonymousUserAccess()
  3. 11.x core/modules/media/tests/src/Functional/MediaAccessTest.php \Drupal\Tests\media\Functional\MediaAccessTest::testMediaAnonymousUserAccess()

Tests media access of anonymous user.

File

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

Class

MediaAccessTest
Basic access tests for Media.

Namespace

Drupal\Tests\media\Functional

Code

public function testMediaAnonymousUserAccess() {
    \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');
    // Create media as anonymous user.
    $user_media = Media::create([
        'bundle' => $media_type->id(),
        'name' => 'Unnamed',
        'uid' => 0,
    ]);
    $user_media->save();
    $role = Role::load(RoleInterface::ANONYMOUS_ID);
    $this->grantPermissions($role, [
        'view media',
        'view own unpublished media',
    ]);
    $this->drupalLogout();
    // Make sure anonymous users can access published media.
    $user_media->setPublished()
        ->save();
    $this->drupalGet('media/' . $user_media->id());
    $assert_session->statusCodeEquals(200);
    // Make sure anonymous users can not access unpublished media
    // even though role has 'view own unpublished media' permission.
    $user_media->setUnpublished()
        ->save();
    $this->drupalGet('media/' . $user_media->id());
    $assert_session->statusCodeEquals(403);
    $this->assertCacheContext('user');
}

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