You are here

public function MediaUiFunctionalTest::testMediaWithMultipleBundles in Media entity 8

Tests the "media/add" and "admin/content/media" pages.

Tests if the "media/add" page gives you a selecting option if there are multiple media bundles available.

File

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

Class

MediaUiFunctionalTest
Ensures that media UI works correctly.

Namespace

Drupal\Tests\media_entity\Functional

Code

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

  // Test access to media overview page.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('admin/content/media');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/content');

  // Test there is a media tab in the menu.
  $page
    ->clickLink('Media');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('No content available.');

  // Tests and creates the first media bundle.
  $first_media_bundle = $this
    ->drupalCreateMediaBundle([
    'description' => $this
      ->randomMachineName(32),
  ]);

  // Test and create a second media bundle.
  $second_media_bundle = $this
    ->drupalCreateMediaBundle([
    'description' => $this
      ->randomMachineName(32),
  ]);

  // Test if media/add displays two media bundle options.
  $this
    ->drupalGet('media/add');

  // Checks for the first media bundle.
  $assert_session
    ->pageTextContains($first_media_bundle
    ->label());
  $assert_session
    ->pageTextContains($first_media_bundle->description);

  // Checks for the second media bundle.
  $assert_session
    ->pageTextContains($second_media_bundle
    ->label());
  $assert_session
    ->pageTextContains($second_media_bundle->description);

  // Continue testing media bundle filter.
  $first_media_item = Media::create([
    'bundle' => $first_media_bundle
      ->id(),
  ]);
  $first_media_item
    ->save();
  $second_media_item = Media::create([
    'bundle' => $second_media_bundle
      ->id(),
  ]);
  $second_media_item
    ->save();

  // Go to media item list.
  $this
    ->drupalGet('admin/content/media');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->linkExists('Add media');

  // Assert that all available media items are in the list.
  $assert_session
    ->pageTextContains($first_media_item
    ->label());
  $assert_session
    ->pageTextContains($first_media_bundle
    ->label());
  $assert_session
    ->pageTextContains($second_media_item
    ->label());
  $assert_session
    ->pageTextContains($second_media_bundle
    ->label());

  // Filter for each bundle and assert that the list has been updated.
  $this
    ->drupalGet('admin/content/media', [
    'query' => [
      'provider' => $first_media_bundle
        ->id(),
    ],
  ]);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($first_media_item
    ->label());
  $assert_session
    ->pageTextNotContains($second_media_item
    ->label());

  // Filter all and check for all items again.
  $this
    ->drupalGet('admin/content/media', [
    'query' => [
      'provider' => 'All',
    ],
  ]);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($first_media_item
    ->label());
  $assert_session
    ->pageTextContains($first_media_bundle
    ->label());
  $assert_session
    ->pageTextContains($second_media_item
    ->label());
  $assert_session
    ->pageTextContains($second_media_bundle
    ->label());
}