View source
<?php
namespace Drupal\Tests\autofill\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class AutofillJavascriptTest extends WebDriverTestBase {
public static $modules = [
'field_ui',
'node',
'autofill',
];
protected $defaultTheme = 'stark';
protected $contentType;
protected function setUp() {
parent::setUp();
$this->contentType = $this
->drupalCreateContentType([
'type' => 'article',
]);
$this
->setupFields();
}
public function testAutofillFromAnotherField() {
$this
->drupalLogin($this->rootUser);
$this
->configureAutofillFields();
$this
->drupalGet('node/add/' . $this->contentType
->id());
$this
->getSession()
->getPage()
->fillField('title[0][value]', $this
->randomString());
$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');
$this
->getSession()
->getPage()
->findButton('Save')
->click();
$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');
$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]', '');
$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');
}
protected function setupFields() {
$fields = [
'field_source_1',
'field_target_1',
'field_source_2',
'field_target_2',
];
foreach ($fields as $field) {
FieldStorageConfig::create([
'field_name' => $field,
'entity_type' => 'node',
'type' => 'string',
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'field_name' => $field,
'bundle' => $this->contentType
->id(),
'label' => $field,
])
->save();
EntityFormDisplay::load('node.article.default')
->setComponent($field)
->save();
}
}
protected function configureAutofillFields() {
$fields = [
'field_source_1' => 'field_target_1',
'field_source_2' => 'field_target_2',
];
$this
->drupalGet('admin/structure/types/manage/' . $this->contentType
->id() . '/form-display');
foreach ($fields as $source => $target) {
$this
->click('[name="' . $target . '_settings_edit"]');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$autofill_enable_checkbox = $this
->assertSession()
->elementExists('css', 'input[name="fields[' . $target . '][settings_edit_form][third_party_settings][autofill][enabled]"]');
$autofill_enable_checkbox
->check();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$source_field_select = $this
->assertSession()
->elementExists('css', 'select[name="fields[' . $target . '][settings_edit_form][third_party_settings][autofill][source_field]"]');
$source_field_select
->selectOption($source);
$this
->getSession()
->getPage()
->findButton('Update')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$target_drupal_selector = 'edit-fields-' . str_replace('_', '-', $target);
$this
->assertSession()
->elementTextContains('css', 'tr[data-drupal-selector="' . $target_drupal_selector . '"]', 'Autofill from: ' . $source);
}
$this
->getSession()
->getPage()
->findButton('Save')
->click();
}
}