Loading core/modules/system/tests/modules/system_test/src/Form/CopyFieldValueTestForm.php 0 → 100644 +53 −0 Original line number Diff line number Diff line <?php namespace Drupal\system_test\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; /** * Provides a form to test Drupal.behaviors.copyFieldValue. */ class CopyFieldValueTestForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'copy_field_value_test_form'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['#attached']['library'][] = 'system/drupal.system'; $form['#attached']['drupalSettings']['copyFieldValue']['edit-source-field'] = ['edit-target-field']; $form['source_field'] = [ '#type' => 'textfield', '#title' => $this->t('Source Field'), '#default_value' => '', '#description' => $this->t('Source input field to provide text value.'), '#required' => TRUE, ]; $form['target_field'] = [ '#type' => 'textfield', '#title' => $this->t('Target Field'), '#default_value' => '', '#description' => $this->t('Target input field to get value from source field.'), '#required' => TRUE, ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // We are only testing the JavaScript part of form. We are not submitting // form. } } core/modules/system/tests/modules/system_test/system_test.routing.yml +8 −0 Original line number Diff line number Diff line Loading @@ -213,6 +213,14 @@ system_test.custom_cache_control: requirements: _access: 'TRUE' system_test.copy_field_value: path: '/system-test/copy-field-value-test-form' defaults: _form: '\Drupal\system_test\Form\CopyFieldValueTestForm' _title: 'Copy Field Value Test Form' requirements: _access: 'TRUE' system_test.install_profile: path: '/system-test/get-install-profile' defaults: Loading core/modules/system/tests/src/FunctionalJavascript/CopyFieldValueTest.php 0 → 100644 +48 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\system\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** * Tests copy field value functionality. * * @see Drupal.behaviors.copyFieldValue. * * @group system */ class CopyFieldValueTest extends WebDriverTestBase { /** * {@inheritdoc} */ protected static $modules = ['system_test']; /** * {@inheritdoc} */ protected $defaultTheme = 'starterkit_theme'; /** * Tests copy field value JavaScript functionality. */ public function testCopyFieldValue() { $this->drupalGet('/system-test/copy-field-value-test-form'); $page = $this->getSession()->getPage(); $source_field_selector = 'edit-source-field'; $target_field = $page->find('css', '#edit-target-field'); $random_string = $this->randomString(); // Ensure that after source field has been filled, target field is filled // with the same value. $page->fillField($source_field_selector, $random_string); $target_field->focus(); $this->assertEquals($target_field->getValue(), $random_string); // Ensure that the target value doesn't change after it has been focused. $page->fillField($source_field_selector, ''); $target_field->focus(); $this->assertEquals($target_field->getValue(), $random_string); } } Loading
core/modules/system/tests/modules/system_test/src/Form/CopyFieldValueTestForm.php 0 → 100644 +53 −0 Original line number Diff line number Diff line <?php namespace Drupal\system_test\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; /** * Provides a form to test Drupal.behaviors.copyFieldValue. */ class CopyFieldValueTestForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'copy_field_value_test_form'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['#attached']['library'][] = 'system/drupal.system'; $form['#attached']['drupalSettings']['copyFieldValue']['edit-source-field'] = ['edit-target-field']; $form['source_field'] = [ '#type' => 'textfield', '#title' => $this->t('Source Field'), '#default_value' => '', '#description' => $this->t('Source input field to provide text value.'), '#required' => TRUE, ]; $form['target_field'] = [ '#type' => 'textfield', '#title' => $this->t('Target Field'), '#default_value' => '', '#description' => $this->t('Target input field to get value from source field.'), '#required' => TRUE, ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // We are only testing the JavaScript part of form. We are not submitting // form. } }
core/modules/system/tests/modules/system_test/system_test.routing.yml +8 −0 Original line number Diff line number Diff line Loading @@ -213,6 +213,14 @@ system_test.custom_cache_control: requirements: _access: 'TRUE' system_test.copy_field_value: path: '/system-test/copy-field-value-test-form' defaults: _form: '\Drupal\system_test\Form\CopyFieldValueTestForm' _title: 'Copy Field Value Test Form' requirements: _access: 'TRUE' system_test.install_profile: path: '/system-test/get-install-profile' defaults: Loading
core/modules/system/tests/src/FunctionalJavascript/CopyFieldValueTest.php 0 → 100644 +48 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\system\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** * Tests copy field value functionality. * * @see Drupal.behaviors.copyFieldValue. * * @group system */ class CopyFieldValueTest extends WebDriverTestBase { /** * {@inheritdoc} */ protected static $modules = ['system_test']; /** * {@inheritdoc} */ protected $defaultTheme = 'starterkit_theme'; /** * Tests copy field value JavaScript functionality. */ public function testCopyFieldValue() { $this->drupalGet('/system-test/copy-field-value-test-form'); $page = $this->getSession()->getPage(); $source_field_selector = 'edit-source-field'; $target_field = $page->find('css', '#edit-target-field'); $random_string = $this->randomString(); // Ensure that after source field has been filled, target field is filled // with the same value. $page->fillField($source_field_selector, $random_string); $target_field->focus(); $this->assertEquals($target_field->getValue(), $random_string); // Ensure that the target value doesn't change after it has been focused. $page->fillField($source_field_selector, ''); $target_field->focus(); $this->assertEquals($target_field->getValue(), $random_string); } }