You are here

public function MediaRevisionTest::testFileMediaRevision in Drupal 10

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

Tests creating revisions of a File media item.

File

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

Class

MediaRevisionTest
Tests the revisionability of media entities.

Namespace

Drupal\Tests\media\Functional

Code

public function testFileMediaRevision() {
  $assert = $this
    ->assertSession();
  $uri = 'temporary://foo.txt';
  file_put_contents($uri, $this
    ->randomString(128));
  $this
    ->createMediaType('file', [
    'id' => 'document',
    'new_revision' => TRUE,
  ]);

  // Create a media item.
  $this
    ->drupalGet('/media/add/document');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField('Name', 'Foobar');
  $page
    ->attachFileToField('File', $this->container
    ->get('file_system')
    ->realpath($uri));
  $page
    ->pressButton('Save');
  $assert
    ->addressEquals('admin/content/media');

  // The media item was just created, so it should only have one revision.
  $media = $this->container
    ->get('entity_type.manager')
    ->getStorage('media')
    ->load(1);
  $this
    ->assertRevisionCount($media, 1);

  // If we edit the item, we should get a new revision.
  $this
    ->drupalGet('/media/1/edit');
  $assert
    ->checkboxChecked('Create new revision');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField('Name', 'Foo');
  $page
    ->pressButton('Save');
  $this
    ->assertRevisionCount($media, 2);

  // Confirm the correct revision title appears on "view revisions" page.
  $media = $this->container
    ->get('entity_type.manager')
    ->getStorage('media')
    ->loadUnchanged(1);
  $this
    ->drupalGet("media/" . $media
    ->id() . "/revisions/" . $media
    ->getRevisionId() . "/view");
  $assert
    ->pageTextContains('Foo');
}