function MediaRevisionTest::testRevisionRevert

Same name in other branches
  1. 10 core/modules/media/tests/src/Functional/MediaRevisionTest.php \Drupal\Tests\media\Functional\MediaRevisionTest::testRevisionRevert()

Tests reverting a revision.

File

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

Class

MediaRevisionTest
Tests the revisions of media entities.

Namespace

Drupal\Tests\media\Functional

Code

public function testRevisionRevert() : void {
    
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->drupalCreateUser([
        'edit any test media',
        'view any test media revisions',
        'revert any test media revisions',
    ]);
    $this->drupalLogin($user);
    $media = $this->createMedia('Initial title');
    $this->createMediaWithRevision($media);
    $originalRevisionId = $media->getRevisionId();
    $originalRevisionLabel = $media->getName();
    // Cannot revert latest revision.
    $this->drupalGet($media->toUrl('revision-revert-form'));
    $this->assertSession()
        ->statusCodeEquals(403);
    // Create a new revision.
    $media->setNewRevision();
    $media->setRevisionLogMessage('Second revision')
        ->setRevisionCreationTime((new \DateTimeImmutable('12 March 2012 5pm'))->getTimestamp())
        ->setName('Sample media updated')
        ->save();
    $this->drupalGet($media->toUrl('version-history'));
    $this->assertSession()
        ->pageTextContains("First revision");
    $this->assertSession()
        ->pageTextContains("Second revision");
    $this->assertSession()
        ->elementsCount('css', 'table tbody tr', 3);
    // Reload the previous revision, and ensure we can revert to it in the UI.
    $revision = \Drupal::entityTypeManager()->getStorage('media')
        ->loadRevision($originalRevisionId);
    $this->drupalGet($revision->toUrl('revision-revert-form'));
    $this->assertSession()
        ->pageTextContains('Are you sure you want to revert to the revision from Sun, 11 Jan 2009 - 16:00?');
    $this->submitForm([], 'Revert');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('Copy of the revision from Sun, 11 Jan 2009 - 16:00');
    $this->assertSession()
        ->addressEquals(sprintf('media/%s/revisions', $media->id()));
    $this->assertSession()
        ->pageTextContains(sprintf('test %s has been reverted to the revision from Sun, 11 Jan 2009 - 16:00.', $originalRevisionLabel));
    $this->assertSession()
        ->elementsCount('css', 'table tbody tr', 4);
    $this->drupalGet($media->toUrl('edit-form'));
    // Check if the title is changed to the reverted revision.
    $this->assertSession()
        ->pageTextContains('1st changed title');
}

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