You are here

public function MediaUiFunctionalTest::testMediaWithOnlyOneBundle in Media entity 8

Tests the media actions (add/edit/delete).

File

tests/src/Functional/MediaUiFunctionalTest.php, line 43

Class

MediaUiFunctionalTest
Ensures that media UI works correctly.

Namespace

Drupal\Tests\media_entity\Functional

Code

public function testMediaWithOnlyOneBundle() {
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $assert_session = $this
    ->assertSession();

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $bundle = $this
    ->drupalCreateMediaBundle();

  // Assert that media item list is empty.
  $this
    ->drupalGet('admin/content/media');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('No content available.');
  $this
    ->drupalGet('media/add');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->addressEquals('media/add/' . $bundle
    ->id());
  $assert_session
    ->checkboxChecked('edit-revision');

  // Tests media item add form.
  $media_name = $this
    ->randomMachineName();
  $page
    ->fillField('name[0][value]', $media_name);
  $revision_log_message = $this
    ->randomString();
  $page
    ->fillField('revision_log', $revision_log_message);
  $page
    ->pressButton('Save and publish');
  $media_id = $this->container
    ->get('entity.query')
    ->get('media')
    ->execute();
  $media_id = reset($media_id);

  /** @var \Drupal\media_entity\MediaInterface $media */
  $media = $this->container
    ->get('entity_type.manager')
    ->getStorage('media')
    ->loadUnchanged($media_id);
  $this
    ->assertEquals($media
    ->getRevisionLogMessage(), $revision_log_message);
  $assert_session
    ->titleEquals($media
    ->label() . ' | Drupal');

  // Test if the media list contains exactly 1 media bundle.
  $this
    ->drupalGet('admin/content/media');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($media
    ->label());

  // Tests media edit form.
  $media_name2 = $this
    ->randomMachineName();
  $this
    ->drupalGet('media/' . $media_id . '/edit');
  $assert_session
    ->checkboxNotChecked('edit-revision');
  $media_name = $this
    ->randomMachineName();
  $page
    ->fillField('name[0][value]', $media_name2);
  $page
    ->pressButton('Save and keep published');
  $assert_session
    ->titleEquals($media_name2 . ' | Drupal');

  // Assert that the media list updates after an edit.
  $this
    ->drupalGet('admin/content/media');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($media_name2);

  // Test that there is no empty vertical tabs element, if the container is
  // empty (see #2750697).
  // Make the "Publisher ID" and "Created" fields hidden.
  $this
    ->drupalGet('/admin/structure/media/manage/' . $bundle->id . '/form-display');
  $page
    ->selectFieldOption('fields[created][parent]', 'hidden');
  $page
    ->selectFieldOption('fields[uid][parent]', 'hidden');
  $page
    ->pressButton('Save');

  // Assure we are testing with a user without permission to manage revisions.
  $this
    ->drupalLogin($this->nonAdminUser);

  // Check the container is not present.
  $this
    ->drupalGet('media/' . $media_id . '/edit');

  // An empty tab container would look like this.
  $raw_html = '<div data-drupal-selector="edit-advanced" data-vertical-tabs-panes><input class="vertical-tabs__active-tab" data-drupal-selector="edit-advanced-active-tab" type="hidden" name="advanced__active_tab" value="" />' . "\n" . '</div>';
  $assert_session
    ->responseNotContains($raw_html);

  // Continue testing as admin.
  $this
    ->drupalLogin($this->adminUser);

  // Enable revisions by default.
  $bundle
    ->setNewRevision(TRUE);
  $bundle
    ->save();
  $this
    ->drupalGet('media/' . $media_id . '/edit');
  $assert_session
    ->checkboxChecked('edit-revision');
  $page
    ->fillField('name[0][value]', $media_name);
  $page
    ->fillField('revision_log', $revision_log_message);
  $page
    ->pressButton('Save and keep published');
  $assert_session
    ->titleEquals($media_name . ' | Drupal');

  /** @var \Drupal\media_entity\MediaInterface $media */
  $media = $this->container
    ->get('entity_type.manager')
    ->getStorage('media')
    ->loadUnchanged($media_id);
  $this
    ->assertEquals($media
    ->getRevisionLogMessage(), $revision_log_message);

  // Tests media delete form.
  $this
    ->drupalGet('media/' . $media_id . '/edit');
  $page
    ->clickLink('Delete');
  $assert_session
    ->pageTextContains('This action cannot be undone');
  $page
    ->pressButton('Delete');
  $media_id = \Drupal::entityQuery('media')
    ->execute();
  $this
    ->assertFalse($media_id);

  // Assert that the media list is empty after deleting the media item.
  $this
    ->drupalGet('admin/content/media');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains($media_name);
  $assert_session
    ->pageTextContains('No content available.');
}