You are here

public function ImageFieldTest::testImageFieldUsage in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 tests/src/FunctionalJavascript/ImageFieldTest.php \Drupal\Tests\entity_browser\FunctionalJavascript\ImageFieldTest::testImageFieldUsage()

Tests basic usage for an image field.

File

tests/src/FunctionalJavascript/ImageFieldTest.php, line 127

Class

ImageFieldTest
Tests the image field widget.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

public function testImageFieldUsage() {
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertSession()
    ->linkExists('Select images');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Select images');
  $this
    ->getSession()
    ->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_view');
  $this
    ->getSession()
    ->getPage()
    ->checkField('entity_browser_select[file:' . $this->image
    ->id() . ']');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Select entities');
  $this
    ->waitForAjaxToFinish();
  $button = $this
    ->assertSession()
    ->waitForButton('Use selected');
  $this
    ->assertSession()
    ->pageTextContains('example.jpg');
  $button
    ->press();

  // Switch back to the main page.
  $this
    ->getSession()
    ->switchToIFrame();
  $this
    ->waitForAjaxToFinish();

  // Check if the image thumbnail exists.
  $this
    ->assertSession()
    ->waitForElementVisible('xpath', '//tr[@data-drupal-selector="edit-field-image-current-1"]');

  // Test if the image filename is present.
  $this
    ->assertSession()
    ->pageTextContains('example.jpg');

  // Test specifying Alt and Title texts and saving the node.
  $alt_text = 'Test alt text.';
  $title_text = 'Test title text.';
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_image[current][1][meta][alt]', $alt_text);
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_image[current][1][meta][title]', $title_text);
  $this
    ->getSession()
    ->getPage()
    ->fillField('title[0][value]', 'Node 1');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save');
  $this
    ->assertSession()
    ->pageTextContains('Article Node 1 has been created.');
  $node = Node::load(1);
  $saved_alt = $node
    ->get('field_image')[0]->alt;
  $this
    ->assertEquals($saved_alt, $alt_text);
  $saved_title = $node
    ->get('field_image')[0]->title;
  $this
    ->assertEquals($saved_title, $title_text);

  // Test the Delete functionality.
  $this
    ->drupalGet('node/1/edit');
  $this
    ->assertSession()
    ->buttonExists('Remove');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Remove');
  $this
    ->waitForAjaxToFinish();

  // Image filename should not be present.
  $this
    ->assertSession()
    ->pageTextNotContains('example.jpg');
  $this
    ->assertSession()
    ->linkExists('Select entities');

  // Test the Replace functionality.
  $test_files = $this
    ->getTestFiles('image');
  foreach ($test_files as $test_file) {
    if ($test_file->filename === 'image-test.jpg') {
      break;
    }
  }
  $file_system = $this->container
    ->get('file_system');
  $file_system
    ->copy($file_system
    ->realpath($test_file->uri), 'public://example2.jpg');
  $image2 = File::create([
    'uri' => 'public://example2.jpg',
  ]);
  $image2
    ->save();
  \Drupal::service('file.usage')
    ->add($image2, 'entity_browser', 'test', '1');
  $this
    ->drupalGet('node/1/edit');
  $this
    ->assertSession()
    ->buttonExists('Replace');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Replace');
  $this
    ->waitForAjaxToFinish();
  $this
    ->getSession()
    ->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_view');
  $this
    ->getSession()
    ->getPage()
    ->checkField('entity_browser_select[file:' . $image2
    ->id() . ']');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Select entities');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Use selected');
  $this
    ->getSession()
    ->wait(1000);
  $this
    ->getSession()
    ->switchToIFrame();
  $this
    ->waitForAjaxToFinish();

  // Initial image should not be present, the new one should be there instead.
  $this
    ->assertSession()
    ->pageTextNotContains('example.jpg');
  $this
    ->assertSession()
    ->pageTextContains('example2.jpg');
}