You are here

public function QuickEditMediaBrowserTest::testEditAndRemove in Lightning Media 8.3

Tests if media can be edited and removed with Quick Edit.

File

tests/src/FunctionalJavascript/QuickEditMediaBrowserTest.php, line 149

Class

QuickEditMediaBrowserTest
@group lightning_media

Namespace

Drupal\Tests\lightning_media\FunctionalJavascript

Code

public function testEditAndRemove() {
  $assert_session = $this
    ->assertSession();

  // Initialize Quick Edit.
  $this
    ->awaitQuickEditForPage();
  $this
    ->startQuickEditViaToolbar('node', $this->nodeId, 0);

  // Click the image field.
  $assert_session
    ->waitForElementVisible('css', sprintf('[data-quickedit-field-id="node/%d/field_image/en/full"]', $this->nodeId));
  $this
    ->click(sprintf('[data-quickedit-field-id="node/%d/field_image/en/full"]', $this->nodeId));

  // Click edit.
  $edit = $assert_session
    ->waitForElement('css', 'input[value="Edit"]');
  $edit
    ->click();

  // Change name.
  $assert_session
    ->waitForElement('css', '.ui-dialog');
  $name = $assert_session
    ->waitForField('Name');
  $name
    ->setValue('Bar');

  // Remove image.
  $remove = $assert_session
    ->waitForButton('image_0_remove_button');
  $remove
    ->click();

  // Add new image.
  $uri = $this
    ->getRandomGenerator()
    ->image('public://test_image_2.png', '240x240', '640x480');
  $path = $this->container
    ->get('file_system')
    ->realpath($uri);
  $file = $assert_session
    ->waitForField('Image');
  $file
    ->attachFile($path);

  // Add alternative text.
  $alt = $assert_session
    ->waitForField('Alternative text');
  $alt
    ->setValue('Test Alt 2');

  // Click save.
  $save = $assert_session
    ->waitForElement('css', '.ui-dialog-buttonset .button');
  $save
    ->click();

  // Assert image has changed.
  $this
    ->assertJsCondition('document.querySelector(".ui-dialog") === null');
  $image = Media::load(1);
  $this
    ->assertSame('Bar', $image
    ->getName());

  // Save Quick Edit.
  $this
    ->saveQuickEdit();
  $this
    ->assertJsCondition("Drupal.quickedit.collections.entities.get('node/{$this->nodeId}[0]').get('state') === 'closed'");

  // Assert new image is displayed.
  $assert_session
    ->elementNotExists('css', 'img[alt="Test Alt 1"]');
  $assert_session
    ->elementExists('css', 'img[alt="Test Alt 2"]');
  $assert_session
    ->elementNotExists('css', 'img[src*="test_image.png"]');
  $assert_session
    ->elementExists('css', 'img[src*="test_image_2.png"]');

  // Restart Quick Edit.
  $this
    ->click('#toolbar-bar .contextual-toolbar-tab button');
  $this
    ->startQuickEditViaToolbar('node', $this->nodeId, 0);

  // Click the image field.
  $assert_session
    ->waitForElementVisible('css', sprintf('[data-quickedit-field-id="node/%d/field_image/en/full"]', $this->nodeId));
  $this
    ->click(sprintf('[data-quickedit-field-id="node/%d/field_image/en/full"]', $this->nodeId));

  // Remove the image.
  $remove = $assert_session
    ->waitForButton('Remove');
  $remove
    ->click();

  // Save Quick Edit.
  $assert_session
    ->waitForButton('Place');
  $this
    ->saveQuickEdit();
  $this
    ->assertJsCondition("Drupal.quickedit.collections.entities.get('node/{$this->nodeId}[0]').get('state') === 'closed'");

  // Assert image is removed from node.
  $assert_session
    ->elementNotExists('css', 'img[src*="test_image_2.png"]');
  $node = Node::load($this->nodeId);
  $this
    ->assertEmpty($node
    ->get('field_image')
    ->getValue());
}