You are here

public function InsertImageCKEditorTest::testCaption in Insert 8.2

File

tests/src/FunctionalJavaScript/InsertImageCKEditorTest.php, line 15

Class

InsertImageCKEditorTest
Tests Insert module's functionality on CKEditor instances.

Namespace

Drupal\Tests\insert\FunctionalJavascript

Code

public function testCaption() {
  $fieldName = strtolower($this
    ->randomMachineName());
  $this
    ->createImageField($fieldName, [
    'alt_field' => '0',
    'title_field' => '1',
  ]);
  $this
    ->updateInsertSettings($fieldName, [
    'styles' => [
      'image' => 'image',
    ],
    'default' => 'image',
    'caption' => TRUE,
  ]);
  $images = $this
    ->drupalGetTestFiles('image');
  $this
    ->drupalGet('node/add/article');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->attachFileToField('files[' . $fieldName . '_0]', \Drupal::service('file_system')
    ->realpath($images[0]->uri));
  $this
    ->assertSession()
    ->waitForField($fieldName . '[0][fids]');
  $titleField = $page
    ->findField($fieldName . '[0][title]');
  $titleField
    ->setValue('some title');
  $page
    ->findButton('Insert')
    ->click();
  $instancesQuery = 'CKEDITOR.instances["edit-body-0-value"].widgets.instances';
  $hasCaption = $this
    ->getSession()
    ->evaluateScript($instancesQuery . '[0].data.hasCaption');
  $this
    ->assertTrue($hasCaption, 'Verified caption being inserted: ' . (string) $hasCaption);

  // For some reason, $titleField->setValue('') empties the whole editor, so
  // resetting title field needs to be simulated using JavaScript.
  $this
    ->getSession()
    ->executeScript('jQuery(\'[name$="[title]"]\').val("").trigger("input")');
  $hasCaption = $this
    ->getSession()
    ->evaluateScript($instancesQuery . '[1].data.hasCaption');
  $this
    ->assertFalse($hasCaption, 'Verified caption being removed when erasing title field: ' . (string) $hasCaption);
  $this
    ->drupalGet('node/add/article');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->attachFileToField('files[' . $fieldName . '_0]', \Drupal::service('file_system')
    ->realpath($images[0]->uri));
  $this
    ->assertSession()
    ->waitForField($fieldName . '[0][fids]');
  $page
    ->findButton('Insert')
    ->click();
  $hasCaption = $this
    ->getSession()
    ->evaluateScript($instancesQuery . '[0].data.hasCaption');
  $this
    ->assertFalse($hasCaption, 'Verified no caption being inserted when title is empty: ' . (string) $hasCaption);
  $this
    ->getSession()
    ->executeScript('jQuery(\'[name$="[title]"]\').val("some title").trigger("input")');
  $hasCaption = $this
    ->getSession()
    ->evaluateScript($instancesQuery . '[1].data.hasCaption');
  $this
    ->assertTrue($hasCaption, 'Verified caption being inserted on images already placed: ' . (string) $hasCaption);
}