function MediaRevisionTest::testRevisions

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

Checks media revision operations.

File

core/modules/media/tests/src/Functional/MediaRevisionTest.php, line 26

Class

MediaRevisionTest
Tests the revisionability of media entities.

Namespace

Drupal\Tests\media\Functional

Code

public function testRevisions() {
    $assert = $this->assertSession();
    
    /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */
    $media_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('media');
    // Create a media type and media item.
    $media_type = $this->createMediaType('test');
    $media = $media_storage->create([
        'bundle' => $media_type->id(),
        'name' => 'Unnamed',
    ]);
    $media->save();
    // You can access the revision page when there is only 1 revision.
    $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
    $assert->statusCodeEquals(200);
    // Create some revisions.
    $media_revisions = [];
    $media_revisions[] = clone $media;
    $revision_count = 3;
    for ($i = 0; $i < $revision_count; $i++) {
        $media->revision_log = $this->randomMachineName(32);
        $media = $this->createMediaRevision($media);
        $media_revisions[] = clone $media;
    }
    // Get the last revision for simple checks.
    
    /** @var \Drupal\media\MediaInterface $media */
    $media = end($media_revisions);
    // Test permissions.
    $this->drupalLogin($this->nonAdminUser);
    
    /** @var \Drupal\user\RoleInterface $role */
    $role = Role::load(RoleInterface::AUTHENTICATED_ID);
    // Test 'view all media revisions' permission ('view media' permission is
    // needed as well).
    user_role_revoke_permissions($role->id(), [
        'view media',
        'view all media revisions',
    ]);
    $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
    $assert->statusCodeEquals(403);
    $this->grantPermissions($role, [
        'view media',
        'view all media revisions',
    ]);
    $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
    $assert->statusCodeEquals(200);
    // Confirm the revision page shows the correct title.
    $assert->pageTextContains($media->getName());
    // Confirm that the last revision is the default revision.
    $this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.');
}

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