MediaRevisionTest.php 6.91 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\media\Functional;

use Drupal\Core\Entity\EntityInterface;
6
use Drupal\field\Entity\FieldConfig;
7 8 9
use Drupal\media\MediaInterface;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
10 11 12 13 14 15 16 17

/**
 * Tests the revisionability of media entities.
 *
 * @group media
 */
class MediaRevisionTest extends MediaFunctionalTestBase {

18 19 20 21 22 23 24 25 26 27
  /**
   * Checks media revision operations.
   */
  public function testRevisions() {
    $assert = $this->assertSession();

    /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */
    $media_storage = $this->container->get('entity.manager')->getStorage('media');

    // Create a media type and media item.
28
    $media_type = $this->createMediaType('test');
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    $media = $media_storage->create([
      'bundle' => $media_type->id(),
      'name' => 'Unnamed',
    ]);
    $media->save();

    // You can't access the revision page when there is only 1 revision.
    $this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
    $assert->statusCodeEquals(403);

    // Create some revisions.
    $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;
    }

    // Get the last revision for simple checks.
    /** @var \Drupal\media\MediaInterface $media */
    $media = end($media_revisions);

    // Test permissions.
    $this->drupalLogin($this->nonAdminUser);
    /** @var \Drupal\user\RoleInterface $role */
    $role = Role::load(RoleInterface::AUTHENTICATED_ID);

    // Test 'view all media revisions' permission ('view media' permission is
    // needed as well).
    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);

    // Confirm the revision page shows the correct title.
68
    $assert->pageTextContains($media->getName());
69 70 71 72 73

    // Confirm that the last revision is the default revision.
    $this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.');
  }

74 75 76 77 78 79 80 81 82
  /**
   * Tests creating revisions of a File media item.
   */
  public function testFileMediaRevision() {
    $assert = $this->assertSession();

    $uri = 'temporary://foo.txt';
    file_put_contents($uri, $this->randomString(128));

83
    $this->createMediaType('file', ['id' => 'file', 'new_revision' => TRUE]);
84

85 86 87 88 89
    // Create a media item.
    $this->drupalGet('/media/add/file');
    $page = $this->getSession()->getPage();
    $page->fillField('Name', 'Foobar');
    $page->attachFileToField('File', $this->container->get('file_system')->realpath($uri));
90
    $page->pressButton('Save');
91
    $assert->addressEquals('admin/content/media');
92 93 94 95 96 97 98

    // 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);
99 100 101 102 103 104

    // 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', 'Foobaz');
105
    $page->pressButton('Save');
106
    $this->assertRevisionCount($media, 2);
107 108 109 110 111 112 113

    // 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('Foobaz');
114 115 116 117 118 119 120 121
  }

  /**
   * Tests creating revisions of a Image media item.
   */
  public function testImageMediaRevision() {
    $assert = $this->assertSession();

122
    $this->createMediaType('image', ['id' => 'image', 'new_revision' => TRUE]);
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137
    /** @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');
138
    $page->attachFileToField('Image', $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
139
    $page->pressButton('Save');
140
    $assert->addressEquals('admin/content/media');
141 142 143 144 145 146 147

    // 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);
148 149 150 151 152 153

    // 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', 'Foobaz');
154
    $page->pressButton('Save');
155
    $this->assertRevisionCount($media, 2);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    // 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('Foobaz');
  }

  /**
   * Creates a new revision for a given media item.
   *
   * @param \Drupal\media\MediaInterface $media
   *   A media object.
   *
   * @return \Drupal\media\MediaInterface
   *   A media object with up to date revision information.
   */
  protected function createMediaRevision(MediaInterface $media) {
175
    $media->setName($this->randomMachineName());
176 177 178
    $media->setNewRevision();
    $media->save();
    return $media;
179 180 181 182 183 184 185 186 187 188 189 190 191 192
  }

  /**
   * Asserts that an entity has a certain number of revisions.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity in question.
   * @param int $expected_revisions
   *   The expected number of revisions.
   */
  protected function assertRevisionCount(EntityInterface $entity, $expected_revisions) {
    $entity_type = $entity->getEntityType();

    $count = $this->container
193 194 195
      ->get('entity_type.manager')
      ->getStorage($entity_type->id())
      ->getQuery()
196 197 198 199 200 201 202 203 204
      ->count()
      ->allRevisions()
      ->condition($entity_type->getKey('id'), $entity->id())
      ->execute();

    $this->assertSame($expected_revisions, (int) $count);
  }

}