function OptionsFieldUIAllowedValuesTest::testOptionsAllowedValues

Same name and namespace in other branches
  1. 11.x core/modules/options/tests/src/FunctionalJavascript/OptionsFieldUIAllowedValuesTest.php \Drupal\Tests\options\FunctionalJavascript\OptionsFieldUIAllowedValuesTest::testOptionsAllowedValues()

Tests option types allowed values.

@dataProvider providerTestOptionsAllowedValues

File

core/modules/options/tests/src/FunctionalJavascript/OptionsFieldUIAllowedValuesTest.php, line 88

Class

OptionsFieldUIAllowedValuesTest
Tests the Options field allowed values UI functionality.

Namespace

Drupal\Tests\options\FunctionalJavascript

Code

public function testOptionsAllowedValues($option_type, $options, $is_string_option, string $add_row_method) : void {
  $assert = $this->assertSession();
  $this->fieldName = 'field_options_text';
  $this->createOptionsField($option_type);
  $page = $this->getSession()
    ->getPage();
  $this->drupalGet($this->adminPath);
  $i = 0;
  $expected_rows = 1;
  $this->assertAllowValuesRowCount(1);
  foreach ($options as $option_key => $option_label) {
    $enter_element_name = $label_element_name = "field_storage[subform][settings][allowed_values][table][{$i}][item][label]";
    $page->fillField($label_element_name, $option_label);
    $this->assertSession()
      ->assertWaitOnAjaxRequest();
    $key_element_name = "field_storage[subform][settings][allowed_values][table][{$i}][item][key]";
    // Add keys if not string option list.
    if (!$is_string_option) {
      $this->pressEnterOnElement("[name=\"{$label_element_name}\"]");
      // Assert that pressing enter on label field does not create the new
      // row if the key field is visible.
      $this->assertAllowValuesRowCount($expected_rows);
      $enter_element_name = $key_element_name;
      $this->assertHasFocusByAttribute('name', $key_element_name);
      $page->fillField($key_element_name, $option_key);
      $this->assertSession()
        ->assertWaitOnAjaxRequest();
    }
    else {
      $this->assertFalse($assert->fieldExists($key_element_name)
        ->isVisible());
    }
    switch ($add_row_method) {
      case 'Press button':
        $page->pressButton('Add another item');
        break;

      case 'Enter button':
        $button = $assert->buttonExists('Add another item');
        $this->pressEnterOnElement('[data-drupal-selector="' . $button->getAttribute('data-drupal-selector') . '"]');
        break;

      case 'Enter element':
        // If testing using the "enter" key while focused on element there a
        // few different scenarios to test.
        switch ($i) {
          case 0:
            // For string options the machine name input can be exposed which
            // will mean the label input will no longer create the next row.
            if ($is_string_option) {
              $this->exposeOptionMachineName($expected_rows);
              $this->pressEnterOnElement("[name=\"{$enter_element_name}\"]");
              $this->assertHasFocusByAttribute('name', $key_element_name);
              // Ensure that pressing enter while focused on the label input
              // did not create a new row if the machine name field is
              // visible.
              $this->assertAllowValuesRowCount($expected_rows);
              $enter_element_name = $key_element_name;
            }
            break;

        }
        $this->pressEnterOnElement("[name=\"{$enter_element_name}\"]");
        break;

      default:
        throw new \UnexpectedValueException("Unknown method {$add_row_method}");
    }
    $i++;
    $expected_rows++;
    $this->assertSession()
      ->waitForElementVisible('css', "[name='field_storage[subform][settings][allowed_values][table][{$i}][item][label]']");
    $this->assertHasFocusByAttribute('name', "field_storage[subform][settings][allowed_values][table][{$i}][item][label]");
    $this->assertAllowValuesRowCount($expected_rows);
    if ($is_string_option) {
      // Expose the key input for string options for the previous row to test
      // shifting focus from the label to key inputs on the previous row by
      // pressing enter.
      $this->exposeOptionMachineName($expected_rows - 1);
    }
    // Test that pressing enter on the label input on previous row will shift
    // focus to key input of that row.
    $this->pressEnterOnElement("[name=\"{$label_element_name}\"]");
    $this->assertHasFocusByAttribute('name', $key_element_name);
    $this->assertAllowValuesRowCount($expected_rows);
  }
  $page->pressButton('Save');
  // Test the order of the option list on node form.
  $this->drupalGet($this->nodeFormPath);
  $this->assertNodeFormOrder([
    '- None -',
    'First',
    'Second',
    'Third',
  ]);
  // Test the order of the option list on admin path.
  $this->drupalGet($this->adminPath);
  $this->assertOrder([
    'First',
    'Second',
    'Third',
    '',
  ], $is_string_option);
  $drag_handle = $page->find('css', '[data-drupal-selector="edit-field-storage-subform-settings-allowed-values-table-0"] .tabledrag-handle');
  $target = $page->find('css', '[data-drupal-selector="edit-field-storage-subform-settings-allowed-values-table-2"]');
  // Change the order the items appear.
  $drag_handle->dragTo($target);
  $this->assertOrder([
    'Second',
    'Third',
    'First',
    '',
  ], $is_string_option);
  $page->pressButton('Save');
  $this->drupalGet($this->nodeFormPath);
  $this->assertNodeFormOrder([
    '- None -',
    'Second',
    'Third',
    'First',
  ]);
  $this->drupalGet($this->adminPath);
  // Confirm the change in order was saved.
  $this->assertOrder([
    'Second',
    'Third',
    'First',
    '',
  ], $is_string_option);
  // Delete an item.
  $page->pressButton('remove_row_button__1');
  $this->assertSession()
    ->assertWaitOnAjaxRequest();
  $this->assertOrder([
    'Second',
    'First',
    '',
  ], $is_string_option);
  $page->pressButton('Save');
  $this->drupalGet($this->nodeFormPath);
  $this->assertNodeFormOrder([
    '- None -',
    'Second',
    'First',
  ]);
  $this->drupalGet($this->adminPath);
  // Confirm the item removal was saved.
  $this->assertOrder([
    'Second',
    'First',
    '',
  ], $is_string_option);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.