You are here

public function TestBase::testEmptyOption in Select (or other) 8

Same name and namespace in other branches
  1. 4.x tests/src/Functional/TestBase.php \Drupal\Tests\select_or_other\Functional\TestBase::testEmptyOption()

Make sure an empty option is present when relevant.

2 calls to TestBase::testEmptyOption()
ListTest::testEmptyOption in tests/src/Functional/ListTest.php
Make sure an empty option is present when relevant.
ReferenceTest::testEmptyOption in tests/src/Functional/ReferenceTest.php
Make sure an empty option is present when relevant.
2 methods override TestBase::testEmptyOption()
ListTest::testEmptyOption in tests/src/Functional/ListTest.php
Make sure an empty option is present when relevant.
ReferenceTest::testEmptyOption in tests/src/Functional/ReferenceTest.php
Make sure an empty option is present when relevant.

File

tests/src/Functional/TestBase.php, line 45

Class

TestBase
Base test class for select or other widgets.

Namespace

Drupal\Tests\select_or_other\Functional

Code

public function testEmptyOption($other_option = '') {
  foreach ($this->fields as $field_name => $field) {
    $this
      ->drupalGet('node/add/' . $this
      ->getFieldContentType($field_name));
    $select_type = $field['select_type'];
    $multiple = $field['cardinality'] !== 1;
    $required = $field['required'];

    // First test empty behaviour. Only single select boxes should always have
    // an empty value.
    if ($select_type === 'select_or_other_select' && !$multiple) {
      if ($required) {
        $this
          ->assertText(t('- Select -'));
      }
      else {
        $this
          ->assertText(t('- None -'));
      }
    }
    else {
      $this
        ->assertNoText(t('- Select -'));
      $this
        ->assertNoText(t('- None -'));
    }

    // Test non-empty behaviour. Once again only single cardinality elements
    // should have empty options to allow for the removal of values if the
    // field is not required.
    if ($other_option !== '') {

      // We set the other option, because we can set that in the same way
      // always.
      $this
        ->setFieldValue($field_name, 'select_or_other', $other_option);
      $this
        ->clickLink(t('Edit'));
      if (!$multiple && !$required) {
        $this
          ->assertText(t('- None -'));
      }
      else {
        $this
          ->assertNoText(t('- Select -'));
        $this
          ->assertNoText(t('- None -'));
      }
    }
    else {
      $this
        ->fail('No $other_option provided, this test should be overridden and calling itself from individual test classes.');
    }
  }
}