You are here

public function AutofillJavascriptTest::testAutofillFromAnotherField in Autofill 8

Tests the autofill of a new field based on the node title.

File

tests/src/FunctionalJavascript/AutofillJavascriptTest.php, line 52

Class

AutofillJavascriptTest
Tests the Autofill module.

Namespace

Drupal\Tests\autofill\FunctionalJavascript

Code

public function testAutofillFromAnotherField() {
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->configureAutofillFields();

  // Start the actual test.
  $this
    ->drupalGet('node/add/' . $this->contentType
    ->id());
  $this
    ->getSession()
    ->getPage()
    ->fillField('title[0][value]', $this
    ->randomString());

  // Set the source fields and check the target fields.
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_source_1[0][value]', 'Test value 1');
  $this
    ->assertSession()
    ->fieldValueEquals('field_target_1[0][value]', 'Test value 1');
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_source_2[0][value]', 'Test value 2');
  $this
    ->assertSession()
    ->fieldValueEquals('field_target_2[0][value]', 'Test value 2');

  // Finally save the node.
  $this
    ->getSession()
    ->getPage()
    ->findButton('Save')
    ->click();

  // Open the created node again. When changing the source field, the target
  // field should change since values are identical.
  $this
    ->drupalGet('node/1/edit');
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_source_1[0][value]', 'My adjusted value 1');
  $this
    ->assertSession()
    ->fieldValueEquals('field_target_1[0][value]', 'My adjusted value 1');
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_source_2[0][value]', 'My adjusted value 2');
  $this
    ->assertSession()
    ->fieldValueEquals('field_target_2[0][value]', 'My adjusted value 2');

  // If the target field was manipulated once it should not be autofilled
  // anymore. Manipulation is done by pressing backspace in the textfield.
  $this
    ->drupalGet('node/add/' . $this->contentType
    ->id());
  $target_field = $this
    ->getSession()
    ->getPage()
    ->findField('field_target_1[0][value]');
  $target_field
    ->keyPress(8);
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_source_1[0][value]', 'My adjusted value');
  $this
    ->assertSession()
    ->fieldValueEquals('field_target_1[0][value]', '');

  // The second target field is still not manipulated, so copying should work.
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_source_2[0][value]', 'My adjusted value 2');
  $this
    ->assertSession()
    ->fieldValueEquals('field_target_2[0][value]', 'My adjusted value 2');
}