You are here

public function MediaImageFieldTest::test in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/FunctionalJavascript/MediaImageFieldTest.php \Drupal\Tests\lightning_media\FunctionalJavascript\MediaImageFieldTest::test()

Tests clearing an image field on an existing media item.

File

tests/src/FunctionalJavascript/MediaImageFieldTest.php, line 51

Class

MediaImageFieldTest
Tests interactions with image fields attached to media types.

Namespace

Drupal\Tests\lightning_media\FunctionalJavascript

Code

public function test() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $field_name = 'field_test' . mb_strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'media',
    'type' => 'image',
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'video',
    'label' => 'Image',
  ])
    ->save();
  $this
    ->drupalPlaceBlock('local_tasks_block');
  $form_display = lightning_media_entity_get_form_display('media', 'video');

  // Add field_image to the display and save it; lightning_media_image will
  // default it to the image browser widget.
  $form_display
    ->setComponent($field_name, [
    'type' => 'image_image',
  ])
    ->save();

  // Then switch it to a standard image widget.
  $form_display
    ->setComponent($field_name, [
    'type' => 'image_image',
    'weight' => 4,
    'settings' => [
      'preview_image_style' => 'thumbnail',
      'progress_indicator' => 'throbber',
    ],
    'region' => 'content',
  ])
    ->save();
  $account = $this
    ->createUser([
    'create media',
    'update media',
  ]);
  $this
    ->drupalLogin($account);
  $name = $this
    ->randomString();
  $this
    ->drupalGet('/media/add/video');
  $page
    ->fillField('Name', $name);
  $page
    ->fillField('Video URL', 'https://www.youtube.com/watch?v=z9qY4VUZzcY');
  $this
    ->assertNotEmpty($assert_session
    ->waitForField('Image'));
  $path = realpath(__DIR__ . '/../../files/test.jpg');
  $this
    ->assertNotEmpty($path);
  $page
    ->attachFileToField('Image', $path);
  $this
    ->assertNotEmpty($assert_session
    ->waitForField('Alternative text'));
  $page
    ->fillField('Alternative text', 'This is a beauty.');
  $page
    ->pressButton('Save');
  $page
    ->clickLink('Edit');
  $page
    ->pressButton("{$field_name}_0_remove_button");
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Ensure that the widget has actually been cleared. This test was written
  // because the AJAX operation would fail due to a 500 error at the server,
  // which would prevent the widget from being cleared.
  $assert_session
    ->buttonNotExists("{$field_name}_0_remove_button");
}