function MediaRevisionTest::testRevisionDelete

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

Tests deleting a revision.

File

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

Class

MediaRevisionTest
Tests the revisions of media entities.

Namespace

Drupal\Tests\media\Functional

Code

public function testRevisionDelete() : void {
    $user = $this->drupalCreateUser([
        'edit any test media',
        'view any test media revisions',
        'delete any test media revisions',
    ]);
    $this->drupalLogin($user);
    $media = $this->createMedia('Sample media');
    $this->createMediaWithRevision($media);
    $originalRevisionId = $media->getRevisionId();
    // Cannot delete latest revision.
    $this->drupalGet($media->toUrl('revision-delete-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 delete it in the UI.
    $revision = \Drupal::entityTypeManager()->getStorage('media')
        ->loadRevision($originalRevisionId);
    $this->drupalGet($revision->toUrl('revision-delete-form'));
    $this->assertSession()
        ->pageTextContains('Are you sure you want to delete the revision from Sun, 01/11/2009 - 16:00?');
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->pageTextNotContains("First revision");
    $this->assertSession()
        ->pageTextContains("Second revision");
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->addressEquals(sprintf('media/%s/revisions', $media->id()));
    $this->assertSession()
        ->pageTextContains('Revision from Sun, 01/11/2009 - 16:00 of test 1st changed title has been deleted.');
    // Check that only two revisions exists, i.e. the original and the latest
    // revision.
    $this->assertSession()
        ->elementsCount('css', 'table tbody tr', 2);
}

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