Verified Commit f88d58b0 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #2944089 by msankhala, smustgrave, lauriii, Ada Hernandez, TanujJain-TJ,...

Issue #2944089 by msankhala, smustgrave, lauriii, Ada Hernandez, TanujJain-TJ, mrinalini9, Abhisheksingh27, alexpott, longwave: Test Drupal.behaviors.copyFieldValue javascript
parent 13123747
Loading
Loading
Loading
Loading
+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.
  }

}
+8 −0
Original line number Diff line number Diff line
@@ -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:
+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);
  }

}