function MediaRevisionTest::testImageMediaRevision

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

Tests creating revisions of an Image media item.

File

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

Class

MediaRevisionTest
Tests the revisions of media entities.

Namespace

Drupal\Tests\media\Functional

Code

public function testImageMediaRevision() : void {
    $assert = $this->assertSession();
    $this->createMediaType('image', [
        'id' => 'image',
        'new_revision' => TRUE,
    ]);
    
    /** @var \Drupal\field\FieldConfigInterface $field */
    // Disable the alt text field, because this is not a JavaScript test and
    // the alt text field will therefore not appear without a full page refresh.
    $field = FieldConfig::load('media.image.field_media_image');
    $settings = $field->getSettings();
    $settings['alt_field'] = FALSE;
    $settings['alt_field_required'] = FALSE;
    $field->set('settings', $settings);
    $field->save();
    // Create a media item.
    $this->drupalGet('/media/add/image');
    $page = $this->getSession()
        ->getPage();
    $page->fillField('Name', 'Foobar');
    $page->attachFileToField('Image', $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
    $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');
}

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