You are here

function OptionsWidgetsTest::testSelectListSingle in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/options/src/Tests/OptionsWidgetsTest.php \Drupal\options\Tests\OptionsWidgetsTest::testSelectListSingle()

Tests the 'options_select' widget (single select).

File

core/modules/options/src/Tests/OptionsWidgetsTest.php, line 235
Contains \Drupal\options\Tests\OptionsWidgetsTest.

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\options\Tests

Code

function testSelectListSingle() {

  // Create an instance of the 'single value' field.
  $field = entity_create('field_config', array(
    'field_storage' => $this->card1,
    'bundle' => 'entity_test',
    'required' => TRUE,
  ));
  $field
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->card1
    ->getName(), array(
    'type' => 'options_select',
  ))
    ->save();

  // Create an entity.
  $entity = entity_create('entity_test', array(
    'user_id' => 1,
    'name' => $this
      ->randomMachineName(),
  ));
  $entity
    ->save();
  $entity_init = clone $entity;

  // Display form.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // A required field without any value has a "none" option.
  $this
    ->assertTrue($this
    ->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(
    ':id' => 'edit-card-1',
    ':label' => t('- Select a value -'),
  )), 'A required select list has a "Select a value" choice.');

  // With no field data, nothing is selected.
  $this
    ->assertNoOptionSelected('edit-card-1', '_none');
  $this
    ->assertNoOptionSelected('edit-card-1', 0);
  $this
    ->assertNoOptionSelected('edit-card-1', 1);
  $this
    ->assertNoOptionSelected('edit-card-1', 2);
  $this
    ->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');

  // Submit form: select invalid 'none' option.
  $edit = array(
    'card_1' => '_none',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('@title field is required.', array(
    '@title' => $field
      ->getName(),
  )), 'Cannot save a required field when selecting "none" from the select list.');

  // Submit form: select first option.
  $edit = array(
    'card_1' => 0,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_1', array(
    0,
  ));

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // A required field with a value has no 'none' option.
  $this
    ->assertFalse($this
    ->xpath('//select[@id=:id]//option[@value="_none"]', array(
    ':id' => 'edit-card-1',
  )), 'A required select list with an actual value has no "none" choice.');
  $this
    ->assertOptionSelected('edit-card-1', 0);
  $this
    ->assertNoOptionSelected('edit-card-1', 1);
  $this
    ->assertNoOptionSelected('edit-card-1', 2);

  // Make the field non required.
  $field
    ->setRequired(FALSE);
  $field
    ->save();

  // Display form.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');

  // A non-required field has a 'none' option.
  $this
    ->assertTrue($this
    ->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(
    ':id' => 'edit-card-1',
    ':label' => t('- None -'),
  )), 'A non-required select list has a "None" choice.');

  // Submit form: Unselect the option.
  $edit = array(
    'card_1' => '_none',
  );
  $this
    ->drupalPostForm('entity_test/manage/' . $entity
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_1', array());

  // Test optgroups.
  $this->card1
    ->setSetting('allowed_values', []);
  $this->card1
    ->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
  $this->card1
    ->save();

  // Display form: with no field data, nothing is selected
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertNoOptionSelected('edit-card-1', 0);
  $this
    ->assertNoOptionSelected('edit-card-1', 1);
  $this
    ->assertNoOptionSelected('edit-card-1', 2);
  $this
    ->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
  $this
    ->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.');
  $this
    ->assertRaw('Group 1', 'Option groups are displayed.');

  // Submit form: select first option.
  $edit = array(
    'card_1' => 0,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_1', array(
    0,
  ));

  // Display form: check that the right options are selected.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertOptionSelected('edit-card-1', 0);
  $this
    ->assertNoOptionSelected('edit-card-1', 1);
  $this
    ->assertNoOptionSelected('edit-card-1', 2);

  // Submit form: Unselect the option.
  $edit = array(
    'card_1' => '_none',
  );
  $this
    ->drupalPostForm('entity_test/manage/' . $entity
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_1', array());
}