You are here

public function InsertFileWidgetTest::testFocus in Insert 8

File

tests/src/FunctionalJavaScript/InsertFileWidgetTest.php, line 128

Class

InsertFileWidgetTest
@group insert

Namespace

Drupal\Tests\insert\FunctionalJavascript

Code

public function testFocus() {
  $longText_field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createTextField($longText_field_name, 'article');
  $field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createFileField($field_name, 'article');
  $files = $this
    ->drupalGetTestFiles('text');
  $this
    ->drupalGet('node/add/article');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->attachFileToField('files[' . $field_name . '_0]', \Drupal::service('file_system')
    ->realpath($files[0]->uri));
  $this
    ->assertSession()
    ->waitForField($field_name . '[0][fids]');
  $page
    ->find('css', '#edit-' . $longText_field_name . '-0-value')
    ->focus();
  $page
    ->findButton('Insert')
    ->click();
  $body_value = $page
    ->find('css', '#edit-body-0-value')
    ->getValue();
  $long_text_value = $page
    ->find('css', '#edit-' . $longText_field_name . '-0-value')
    ->getValue();
  $this
    ->assertEquals('', $body_value, 'Body is empty');
  $this
    ->assertEquals(1, preg_match('!^<a [^>]+>text-0.txt</a>$!', $long_text_value), 'Inserted HTML into focused text area');
  $page
    ->find('css', '#edit-body-0-value')
    ->focus();
  $page
    ->findButton('Insert')
    ->click();
  $body_value = $page
    ->find('css', '#edit-body-0-value')
    ->getValue();
  $long_text_value = $page
    ->find('css', '#edit-' . $longText_field_name . '-0-value')
    ->getValue();
  $this
    ->assertEquals(1, preg_match('!^<a [^>]+>text-0.txt</a>$!', $body_value), 'Inserted HTML into body after refocusing: ' . $body_value);
  $this
    ->assertEquals(1, preg_match('!^<a [^>]+>text-0.txt</a>$!', $long_text_value), 'Still, second text area has HTML inserted once: ' . $body_value);
}