|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\media_diff_ui\Functional; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\EntityInterface; |
| 6 | +use Drupal\Tests\media\Functional\MediaFunctionalTestBase; |
| 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 MediaRevisionDiffTest extends MediaFunctionalTestBase { |
| 17 | + |
| 18 | + /** |
| 19 | + * {@inheritdoc} |
| 20 | + */ |
| 21 | + protected $defaultTheme = 'stark'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Modules to enable. |
| 25 | + * |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + protected static $modules = [ |
| 29 | + 'system', |
| 30 | + 'node', |
| 31 | + 'field_ui', |
| 32 | + 'views_ui', |
| 33 | + 'media', |
| 34 | + 'media_test_source', |
| 35 | + 'entity_diff_ui', |
| 36 | + 'media_diff_ui', |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * Checks media revision operations. |
| 41 | + */ |
| 42 | + public function testRevisions() { |
| 43 | + $assert = $this->assertSession(); |
| 44 | + |
| 45 | + /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */ |
| 46 | + $media_storage = $this->container->get('entity_type.manager')->getStorage('media'); |
| 47 | + |
| 48 | + // Create a media type and media item. |
| 49 | + $media_type = $this->createMediaType('test'); |
| 50 | + $media = $media_storage->create([ |
| 51 | + 'bundle' => $media_type->id(), |
| 52 | + 'name' => 'Unnamed', |
| 53 | + ]); |
| 54 | + $media->save(); |
| 55 | + |
| 56 | + // Anonymouse must not have access to revision overview page. |
| 57 | + $this->drupalGet('media/' . $media->id() . '/revisions'); |
| 58 | + $assert->statusCodeEquals(403); |
| 59 | + |
| 60 | + // Create some revisions. |
| 61 | + $media_revisions = []; |
| 62 | + $media_revisions[] = clone $media; |
| 63 | + $revision_count = 3; |
| 64 | + for ($i = 0; $i < $revision_count; $i++) { |
| 65 | + $media->revision_log = $this->randomMachineName(32); |
| 66 | + $media = $this->createMediaRevision($media); |
| 67 | + $media_revisions[] = clone $media; |
| 68 | + } |
| 69 | + |
| 70 | + // Get the last revision for simple checks. |
| 71 | + /** @var \Drupal\media\MediaInterface $media */ |
| 72 | + $media = end($media_revisions); |
| 73 | + |
| 74 | + // Test permissions. |
| 75 | + $this->drupalLogin($this->nonAdminUser); |
| 76 | + /** @var \Drupal\user\RoleInterface $role */ |
| 77 | + $role = Role::load(RoleInterface::AUTHENTICATED_ID); |
| 78 | + |
| 79 | + // Test 'view all media revisions' permission ('view media' permission is |
| 80 | + // needed as well). |
| 81 | + user_role_revoke_permissions($role->id(), [ |
| 82 | + 'view media', |
| 83 | + 'view all media revisions', |
| 84 | + ]); |
| 85 | + $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view'); |
| 86 | + $assert->statusCodeEquals(403); |
| 87 | + $this->grantPermissions($role, ['view media', 'view all media revisions']); |
| 88 | + $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view'); |
| 89 | + $assert->statusCodeEquals(200); |
| 90 | + |
| 91 | + // Confirm the revision page shows the correct title. |
| 92 | + $assert->pageTextContains($media->getName()); |
| 93 | + |
| 94 | + // Confirm that the last revision is the default revision. |
| 95 | + $this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.'); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a new revision for a given media item. |
| 100 | + * |
| 101 | + * @param \Drupal\media\MediaInterface $media |
| 102 | + * A media object. |
| 103 | + * |
| 104 | + * @return \Drupal\media\MediaInterface |
| 105 | + * A media object with up to date revision information. |
| 106 | + */ |
| 107 | + protected function createMediaRevision(MediaInterface $media) { |
| 108 | + $media->setName($this->randomMachineName()); |
| 109 | + $media->setNewRevision(); |
| 110 | + $media->save(); |
| 111 | + return $media; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Asserts that an entity has a certain number of revisions. |
| 116 | + * |
| 117 | + * @param \Drupal\Core\Entity\EntityInterface $entity |
| 118 | + * The entity in question. |
| 119 | + * @param int $expected_revisions |
| 120 | + * The expected number of revisions. |
| 121 | + * |
| 122 | + * @internal |
| 123 | + */ |
| 124 | + protected function assertRevisionCount(EntityInterface $entity, int $expected_revisions): void { |
| 125 | + $entity_type = $entity->getEntityType(); |
| 126 | + |
| 127 | + $count = $this->container |
| 128 | + ->get('entity_type.manager') |
| 129 | + ->getStorage($entity_type->id()) |
| 130 | + ->getQuery() |
| 131 | + ->accessCheck(FALSE) |
| 132 | + ->count() |
| 133 | + ->allRevisions() |
| 134 | + ->condition($entity_type->getKey('id'), $entity->id()) |
| 135 | + ->execute(); |
| 136 | + |
| 137 | + $this->assertSame($expected_revisions, (int) $count); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments