3 namespace Drupal\Tests\media\Functional;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\media\MediaInterface;
8 use Drupal\user\Entity\Role;
9 use Drupal\user\RoleInterface;
12 * Tests the revisionability of media entities.
16 class MediaRevisionTest extends MediaFunctionalTestBase {
19 * Checks media revision operations.
21 public function testRevisions() {
22 $assert = $this->assertSession();
24 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */
25 $media_storage = $this->container->get('entity.manager')->getStorage('media');
27 // Create a media type and media item.
28 $media_type = $this->createMediaType('test');
29 $media = $media_storage->create([
30 'bundle' => $media_type->id(),
35 // You can't access the revision page when there is only 1 revision.
36 $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
37 $assert->statusCodeEquals(403);
39 // Create some revisions.
40 $media_revisions = [];
41 $media_revisions[] = clone $media;
43 for ($i = 0; $i < $revision_count; $i++) {
44 $media->revision_log = $this->randomMachineName(32);
45 $media = $this->createMediaRevision($media);
46 $media_revisions[] = clone $media;
49 // Get the last revision for simple checks.
50 /** @var \Drupal\media\MediaInterface $media */
51 $media = end($media_revisions);
54 $this->drupalLogin($this->nonAdminUser);
55 /** @var \Drupal\user\RoleInterface $role */
56 $role = Role::load(RoleInterface::AUTHENTICATED_ID);
58 // Test 'view all media revisions' permission ('view media' permission is
60 user_role_revoke_permissions($role->id(), ['view media', 'view all media revisions']);
61 $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
62 $assert->statusCodeEquals(403);
63 $this->grantPermissions($role, ['view media', 'view all media revisions']);
64 $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
65 $assert->statusCodeEquals(200);
67 // Confirm the revision page shows the correct title.
68 $assert->pageTextContains($media->getName());
70 // Confirm that the last revision is the default revision.
71 $this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.');
75 * Tests creating revisions of a File media item.
77 public function testFileMediaRevision() {
78 $assert = $this->assertSession();
80 $uri = 'temporary://foo.txt';
81 file_put_contents($uri, $this->randomString(128));
83 $this->createMediaType('file', ['id' => 'file', 'new_revision' => TRUE]);
85 // Create a media item.
86 $this->drupalGet('/media/add/file');
87 $page = $this->getSession()->getPage();
88 $page->fillField('Name', 'Foobar');
89 $page->attachFileToField('File', $this->container->get('file_system')->realpath($uri));
90 $page->pressButton('Save');
91 $assert->addressEquals('admin/content/media');
93 // The media item was just created, so it should only have one revision.
94 $media = $this->container
95 ->get('entity_type.manager')
98 $this->assertRevisionCount($media, 1);
100 // If we edit the item, we should get a new revision.
101 $this->drupalGet('/media/1/edit');
102 $assert->checkboxChecked('Create new revision');
103 $page = $this->getSession()->getPage();
104 $page->fillField('Name', 'Foobaz');
105 $page->pressButton('Save');
106 $this->assertRevisionCount($media, 2);
108 // Confirm the correct revision title appears on "view revisions" page.
109 $media = $this->container->get('entity_type.manager')
110 ->getStorage('media')
112 $this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
113 $assert->pageTextContains('Foobaz');
117 * Tests creating revisions of a Image media item.
119 public function testImageMediaRevision() {
120 $assert = $this->assertSession();
122 $this->createMediaType('image', ['id' => 'image', 'new_revision' => TRUE]);
124 /** @var \Drupal\field\FieldConfigInterface $field */
125 // Disable the alt text field, because this is not a JavaScript test and
126 // the alt text field will therefore not appear without a full page refresh.
127 $field = FieldConfig::load('media.image.field_media_image');
128 $settings = $field->getSettings();
129 $settings['alt_field'] = FALSE;
130 $settings['alt_field_required'] = FALSE;
131 $field->set('settings', $settings);
134 // Create a media item.
135 $this->drupalGet('/media/add/image');
136 $page = $this->getSession()->getPage();
137 $page->fillField('Name', 'Foobar');
138 $page->attachFileToField('Image', $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
139 $page->pressButton('Save');
140 $assert->addressEquals('admin/content/media');
142 // The media item was just created, so it should only have one revision.
143 $media = $this->container
144 ->get('entity_type.manager')
145 ->getStorage('media')
147 $this->assertRevisionCount($media, 1);
149 // If we edit the item, we should get a new revision.
150 $this->drupalGet('/media/1/edit');
151 $assert->checkboxChecked('Create new revision');
152 $page = $this->getSession()->getPage();
153 $page->fillField('Name', 'Foobaz');
154 $page->pressButton('Save');
155 $this->assertRevisionCount($media, 2);
157 // Confirm the correct revision title appears on "view revisions" page.
158 $media = $this->container->get('entity_type.manager')
159 ->getStorage('media')
161 $this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
162 $assert->pageTextContains('Foobaz');
166 * Creates a new revision for a given media item.
168 * @param \Drupal\media\MediaInterface $media
171 * @return \Drupal\media\MediaInterface
172 * A media object with up to date revision information.
174 protected function createMediaRevision(MediaInterface $media) {
175 $media->setName($this->randomMachineName());
176 $media->setNewRevision();
182 * Asserts that an entity has a certain number of revisions.
184 * @param \Drupal\Core\Entity\EntityInterface $entity
185 * The entity in question.
186 * @param int $expected_revisions
187 * The expected number of revisions.
189 protected function assertRevisionCount(EntityInterface $entity, $expected_revisions) {
190 $entity_type = $entity->getEntityType();
192 $count = $this->container
193 ->get('entity_type.manager')
194 ->getStorage($entity_type->id())
198 ->condition($entity_type->getKey('id'), $entity->id())
201 $this->assertSame($expected_revisions, (int) $count);