265e50fcb84fafcc7d59b5cff07d49e4a8b1cff4
[yaffs-website] / web / core / modules / media / tests / src / Functional / MediaRevisionTest.php
1 <?php
2
3 namespace Drupal\Tests\media\Functional;
4
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;
10
11 /**
12  * Tests the revisionability of media entities.
13  *
14  * @group media
15  */
16 class MediaRevisionTest extends MediaFunctionalTestBase {
17
18   /**
19    * Checks media revision operations.
20    */
21   public function testRevisions() {
22     $assert = $this->assertSession();
23
24     /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */
25     $media_storage = $this->container->get('entity.manager')->getStorage('media');
26
27     // Create a media type and media item.
28     $media_type = $this->createMediaType();
29     $media = $media_storage->create([
30       'bundle' => $media_type->id(),
31       'name' => 'Unnamed',
32     ]);
33     $media->save();
34
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);
38
39     // Create some revisions.
40     $media_revisions = [];
41     $media_revisions[] = clone $media;
42     $revision_count = 3;
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;
47     }
48
49     // Get the last revision for simple checks.
50     /** @var \Drupal\media\MediaInterface $media */
51     $media = end($media_revisions);
52
53     // Test permissions.
54     $this->drupalLogin($this->nonAdminUser);
55     /** @var \Drupal\user\RoleInterface $role */
56     $role = Role::load(RoleInterface::AUTHENTICATED_ID);
57
58     // Test 'view all media revisions' permission ('view media' permission is
59     // needed as well).
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);
66
67     // Confirm the revision page shows the correct title.
68     $assert->pageTextContains($media->getName());
69
70     // Confirm that the last revision is the default revision.
71     $this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.');
72   }
73
74   /**
75    * Tests creating revisions of a File media item.
76    */
77   public function testFileMediaRevision() {
78     $assert = $this->assertSession();
79
80     $uri = 'temporary://foo.txt';
81     file_put_contents($uri, $this->randomString(128));
82
83     $this->createMediaType(['bundle' => 'file', 'new_revision' => TRUE], 'file');
84
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->addressMatches('/^\/media\/[0-9]+$/');
92
93     // The media item was just created, so it should only have one revision.
94     $media = $this->container
95       ->get('entity_type.manager')
96       ->getStorage('media')
97       ->load(1);
98     $this->assertRevisionCount($media, 1);
99
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);
107
108     // Confirm the correct revision title appears on "view revisions" page.
109     $media = $this->container->get('entity_type.manager')
110       ->getStorage('media')
111       ->loadUnchanged(1);
112     $this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
113     $assert->pageTextContains('Foobaz');
114   }
115
116   /**
117    * Tests creating revisions of a Image media item.
118    */
119   public function testImageMediaRevision() {
120     $assert = $this->assertSession();
121
122     $this->createMediaType(['bundle' => 'image', 'new_revision' => TRUE], 'image');
123
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);
132     $field->save();
133
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', \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg');
139     $page->pressButton('Save');
140     $assert->addressMatches('/^\/media\/[0-9]+$/');
141
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')
146       ->load(1);
147     $this->assertRevisionCount($media, 1);
148
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);
156
157     // Confirm the correct revision title appears on "view revisions" page.
158     $media = $this->container->get('entity_type.manager')
159       ->getStorage('media')
160       ->loadUnchanged(1);
161     $this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
162     $assert->pageTextContains('Foobaz');
163   }
164
165   /**
166    * Creates a new revision for a given media item.
167    *
168    * @param \Drupal\media\MediaInterface $media
169    *   A media object.
170    *
171    * @return \Drupal\media\MediaInterface
172    *   A media object with up to date revision information.
173    */
174   protected function createMediaRevision(MediaInterface $media) {
175     $media->setName($this->randomMachineName());
176     $media->setNewRevision();
177     $media->save();
178     return $media;
179   }
180
181   /**
182    * Asserts that an entity has a certain number of revisions.
183    *
184    * @param \Drupal\Core\Entity\EntityInterface $entity
185    *   The entity in question.
186    * @param int $expected_revisions
187    *   The expected number of revisions.
188    */
189   protected function assertRevisionCount(EntityInterface $entity, $expected_revisions) {
190     $entity_type = $entity->getEntityType();
191
192     $count = $this->container
193       ->get('entity.query')
194       ->get($entity_type->id())
195       ->count()
196       ->allRevisions()
197       ->condition($entity_type->getKey('id'), $entity->id())
198       ->execute();
199
200     $this->assertSame($expected_revisions, (int) $count);
201   }
202
203 }