View source
<?php
namespace Drupal\Tests\media\Functional;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\media\MediaInterface;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class MediaRevisionTest extends MediaFunctionalTestBase {
protected $defaultTheme = 'stark';
public function testRevisions() {
$assert = $this
->assertSession();
$media_storage = $this->container
->get('entity_type.manager')
->getStorage('media');
$media_type = $this
->createMediaType('test');
$media = $media_storage
->create([
'bundle' => $media_type
->id(),
'name' => 'Unnamed',
]);
$media
->save();
$this
->drupalGet('media/' . $media
->id() . '/revisions/' . $media
->getRevisionId() . '/view');
$assert
->statusCodeEquals(200);
$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;
}
$media = end($media_revisions);
$this
->drupalLogin($this->nonAdminUser);
$role = Role::load(RoleInterface::AUTHENTICATED_ID);
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);
$assert
->pageTextContains($media
->getName());
$this
->assertTrue($media
->isDefaultRevision(), 'Last revision is the default.');
}
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,
]);
$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');
$media = $this->container
->get('entity_type.manager')
->getStorage('media')
->load(1);
$this
->assertRevisionCount($media, 1);
$this
->drupalGet('/media/1/edit');
$assert
->checkboxChecked('Create new revision');
$page = $this
->getSession()
->getPage();
$page
->fillField('Name', 'Foobaz');
$page
->pressButton('Save');
$this
->assertRevisionCount($media, 2);
$media = $this->container
->get('entity_type.manager')
->getStorage('media')
->loadUnchanged(1);
$this
->drupalGet("media/" . $media
->id() . "/revisions/" . $media
->getRevisionId() . "/view");
$assert
->pageTextContains('Foobaz');
}
public function testImageMediaRevision() {
$assert = $this
->assertSession();
$this
->createMediaType('image', [
'id' => 'image',
'new_revision' => TRUE,
]);
$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();
$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');
$media = $this->container
->get('entity_type.manager')
->getStorage('media')
->load(1);
$this
->assertRevisionCount($media, 1);
$this
->drupalGet('/media/1/edit');
$assert
->checkboxChecked('Create new revision');
$page = $this
->getSession()
->getPage();
$page
->fillField('Name', 'Foobaz');
$page
->pressButton('Save');
$this
->assertRevisionCount($media, 2);
$media = $this->container
->get('entity_type.manager')
->getStorage('media')
->loadUnchanged(1);
$this
->drupalGet("media/" . $media
->id() . "/revisions/" . $media
->getRevisionId() . "/view");
$assert
->pageTextContains('Foobaz');
}
protected function createMediaRevision(MediaInterface $media) {
$media
->setName($this
->randomMachineName());
$media
->setNewRevision();
$media
->save();
return $media;
}
protected function assertRevisionCount(EntityInterface $entity, $expected_revisions) {
$entity_type = $entity
->getEntityType();
$count = $this->container
->get('entity_type.manager')
->getStorage($entity_type
->id())
->getQuery()
->accessCheck(FALSE)
->count()
->allRevisions()
->condition($entity_type
->getKey('id'), $entity
->id())
->execute();
$this
->assertSame($expected_revisions, (int) $count);
}
}