You are here

public function InsertFileTest::testMultipleInsertOperations in Insert 8.2

File

tests/src/FunctionalJavaScript/InsertFileTest.php, line 184

Class

InsertFileTest
Tests Insert module's generic file insert capability.

Namespace

Drupal\Tests\insert\FunctionalJavascript

Code

public function testMultipleInsertOperations() {
  $fieldName = strtolower($this
    ->randomMachineName());
  $this
    ->createFileField($fieldName);
  $this
    ->updateInsertSettings($fieldName, [
    'styles' => [
      'link' => 1,
      'icon_link' => 1,
    ],
    'default' => 'link',
  ]);
  $files = $this
    ->drupalGetTestFiles('text');
  $this
    ->drupalGet('node/add/article');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->attachFileToField('files[' . $fieldName . '_0]', \Drupal::service('file_system')
    ->realpath($files[0]->uri));
  $this
    ->assertSession()
    ->waitForField($fieldName . '[0][fids]');
  $body = $page
    ->findField('body[0][value]');
  $page
    ->findButton('Insert')
    ->click();
  $this
    ->assertEquals(1, preg_match_all('!<a [^>]+>[^<]+</a>!', $body
    ->getValue()), 'Verified inserted HTML after inserting once: "' . $body
    ->getValue() . '"');
  $page
    ->findButton('Insert')
    ->click();
  $this
    ->assertEquals(2, preg_match_all('!<a [^>]+>[^<]+</a>!', $body
    ->getValue()), 'Verified inserted HTML after inserting twice: "' . $body
    ->getValue() . '"');
  $body
    ->setValue($body
    ->getValue() . 'insert after');

  // Simulate updated caret position:
  $this
    ->getSession()
    ->executeScript("var textarea = jQuery('#edit-body-0-value').get(0); textarea.selectionStart = textarea.selectionEnd = textarea.selectionStart + 'insert after'.length;");
  $page
    ->findButton('Insert')
    ->click();
  $this
    ->assertEquals(1, preg_match('!^<a [^>]+>[^<]+</a><a [^>]+>[^<]+</a>insert after<a [^>]+>[^<]+</a>$!', $body
    ->getValue()), 'Verified HTML after inserting three times: "' . $body
    ->getValue() . '"');
  $body
    ->setValue('insert before');
  $this
    ->getSession()
    ->executeScript("var textarea = jQuery('#edit-body-0-value').get(0); textarea.selectionStart = textarea.selectionEnd = 0;");
  $page
    ->findButton('Insert')
    ->click();
  $this
    ->assertEquals(1, preg_match('!^<a [^>]+>[^<]+</a>insert before$!', $body
    ->getValue()), 'Verified HTML inserted before existing content: "' . $body
    ->getValue() . '"');
}