You are here

public function SoundcloudEmbedFormatterTest::testSoundcloudEmbedFormatter in Media entity Soundcloud 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/SoundcloudEmbedFormatterTest.php \Drupal\Tests\media_entity_soundcloud\Functional\SoundcloudEmbedFormatterTest::testSoundcloudEmbedFormatter()

Tests adding and editing a soundcloud embed formatter.

File

tests/src/Functional/SoundcloudEmbedFormatterTest.php, line 62

Class

SoundcloudEmbedFormatterTest
Tests for Soundcloud embed formatter.

Namespace

Drupal\Tests\media_entity_soundcloud\Functional

Code

public function testSoundcloudEmbedFormatter() {

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface */
  $entity_display_repository = \Drupal::service('entity_display.repository');
  $media_type = $this
    ->createMediaType('soundcloud', [
    'id' => 'soundcloud',
  ]);
  $source_field = $media_type
    ->getSource()
    ->getSourceFieldDefinition($media_type);
  $this
    ->assertSame('field_media_soundcloud', $source_field
    ->getName());
  $this
    ->assertSame('string', $source_field
    ->getType());

  // Set form and view displays.
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('media', $media_type
    ->id(), 'default')
    ->setComponent('field_media_soundcloud', [
    'type' => 'string_textfield',
  ])
    ->save();
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('media', $media_type
    ->id(), 'full')
    ->setComponent('field_media_soundcloud', [
    'type' => 'soundcloud_embed',
  ])
    ->save();

  // Create a soundcloud media entity.
  $this
    ->drupalGet('media/add/' . $media_type
    ->id());
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField('name[0][value]', 'Soundcloud');
  $page
    ->fillField('field_media_soundcloud[0][value]', 'https://soundcloud.com/winguy/billie-jean-remix-ft');
  $page
    ->pressButton('Save');

  // Assert "has been created" text.
  $assert = $this
    ->assertSession();
  $assert
    ->pageTextContains('has been created');

  // Get to the media page.
  $medias = \Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadByProperties([
    'name' => 'Soundcloud',
  ]);

  /** @var \Drupal\media\MediaInterface */
  $media = reset($medias);
  $this
    ->drupalGet(Url::fromRoute('entity.media.canonical', [
    'media' => $media
      ->id(),
  ])
    ->toString());
  $assert
    ->statusCodeEquals(200);

  // Assert that the formatter exists on this page.
  $assert
    ->elementExists('css', 'iframe[src*="soundcloud"]');
}