Verified Commit a6509130 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3038141 by hooroomoo, bnjmnm, lauriii, pooja saraah, uzlov, Charchil...

Issue #3038141 by hooroomoo, bnjmnm, lauriii, pooja saraah, uzlov, Charchil Khandelwal, Pancho, smustgrave, joachim: Machine name field expands after failing validation
parent 2fe6ae3d
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -47,6 +47,11 @@
      function clickEditHandler(e) {
        const data = e.data;
        data.$wrapper.removeClass('hidden');
        if (data.$target.attr('data-machine-name-require-when-visible')) {
          data.$target
            .attr('required', true)
            .removeAttr('data-machine-name-require-when-visible');
        }
        data.$target.trigger('focus');
        data.$suffix.hide();
        data.$source.off('.machineName');
@@ -110,14 +115,25 @@
        ) {
          return;
        }
        // Skip processing upon a form validation error on the machine name.
        if ($target.hasClass('error')) {
        // Skip processing upon a form validation error on a non-empty
        // machine name.
        if (
          $target.hasClass('error') &&
          $target[0].value &&
          $target[0].value.trim().length
        ) {
          return;
        }
        // Figure out the maximum length for the machine name.
        options.maxlength = $target.attr('maxlength');
        // Hide the form item container of the machine name form element.
        $wrapper.addClass('hidden');
        if ($target.attr('required')) {
          $target
            .removeAttr('required')
            .attr('data-machine-name-require-when-visible');
        }

        // Initial machine name from the target field default value.
        const machine = $target[0].value;
        // Append the machine name preview to the source field.
@@ -176,10 +192,6 @@
            // Initialize machine name preview.
            .trigger('formUpdated.machineName');
        }

        // Add a listener for an invalid event on the machine name input
        // to show its container and focus it.
        $target.on('invalid', eventData, clickEditHandler);
      });
    },

+15 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#title' => 'Machine name 1',
      '#description' => 'A machine name.',
      '#machine_name' => [
        'exists' => [$this, 'load'],
        'source' => ['machine_name_1_label'],
      ],
    ];
@@ -45,6 +46,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#title' => 'Machine name 2',
      '#description' => 'Another machine name.',
      '#machine_name' => [
        'exists' => [$this, 'load'],
        'source' => ['machine_name_2_label'],
      ],
    ];
@@ -58,6 +60,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#title' => 'Machine name 3',
      '#description' => 'Another machine name.',
      '#machine_name' => [
        'exists' => [$this, 'load'],
        'source' => ['machine_name_3_label'],
      ],
    ];
@@ -75,4 +78,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->setResponse(new JsonResponse($form_state->getValues()));
  }

  /**
   * Loading stub for machine name.
   *
   * @param $machine_name
   *   The machine name.
   *
   * @return bool
   */
  public function load($machine_name) {
    return str_contains($machine_name, 'duplicate');
  }

}
+55 −1
Original line number Diff line number Diff line
@@ -120,6 +120,51 @@ public function testMachineName() {
    // Validate if the element contains the correct value.
    $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');

    // Test that machine name generation still occurs after an HTML 5
    // validation failure.
    $this->drupalGet('form-test/machine-name');
    $this->assertSession()->buttonExists('Submit')->press();

    // Assert all fields are initialized correctly.
    $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
    $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
    $this->assertNotEmpty($machine_name_3_value, 'Machine name field 3 must be initialized');

    // Assert that a machine name based on a default value is initialized.
    $this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "yet_another_machine_name"');

    // Test each value for conversion to a machine name.
    foreach ($test_values as $test_info) {
      // Set the value for the field, triggering the machine name update.
      $title_1->setValue($test_info['input']);

      // Wait the set timeout for fetching the machine name.
      $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');

      // Validate the generated machine name.
      $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);

      // Validate the second machine name field is empty.
      $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
    }

    // Validate the machine name field is hidden. Elements are visually hidden
    // using positioning, isVisible() will therefore not work.
    $this->assertTrue($machine_name_1_wrapper->hasClass('hidden'), 'The ID field must not be visible');
    $this->assertTrue($machine_name_2_wrapper->hasClass('hidden'), 'The ID field must not be visible');

    // Test switching back to the manual editing mode by clicking the edit link.
    $button_1->click();

    // Validate the visibility of the machine name field.
    $this->assertFalse($machine_name_1_wrapper->hasClass('hidden'), 'The ID field must now be visible');

    // Validate the visibility of the second machine name field.
    $this->assertTrue($machine_name_2_wrapper->hasClass('hidden'), 'The ID field must not be visible');

    // Validate if the element contains the correct value.
    $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');

    $assert = $this->assertSession();
    $this->drupalGet('/form-test/form-test-machine-name-validation');

@@ -132,7 +177,16 @@ public function testMachineName() {

    // Test a successful submit after using AJAX.
    $assert->fieldExists('Name')->setValue('test 1');
    $assert->fieldExists('id')->setValue('test_1');
    $machine_name_value = $page->find('css', '#edit-name-machine-name-suffix .machine-name-value');
    $this->assertNotEmpty($machine_name_value, 'Machine name field must be initialized');
    $this->assertJsCondition('jQuery("#edit-name-machine-name-suffix .machine-name-value").html() == "' . 'test_1' . '"');

    // Ensure that machine name generation still occurs after a non-HTML 5
    // validation failure.
    $this->assertEquals('test_1', $machine_name_value->getHtml(), $test_values[1]['message']);
    $machine_name_wrapper = $page->find('css', '#edit-id')->getParent();
    // Machine name field should not expand after failing validation.
    $this->assertTrue($machine_name_wrapper->hasClass('hidden'), 'The ID field must not be visible');
    $assert->selectExists('snack')->selectOption('apple');
    $assert->assertWaitOnAjaxRequest();
    $assert->buttonExists('Save')->press();