You are here

function OptionsWidgetsTest::testCheckBoxes in Zircon Profile 8.0

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

Tests the 'options_buttons' widget (multiple select).

File

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

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\options\Tests

Code

function testCheckBoxes() {

  // Create an instance of the 'multiple values' field.
  $field = entity_create('field_config', array(
    'field_storage' => $this->card2,
    'bundle' => 'entity_test',
  ));
  $field
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->card2
    ->getName(), array(
    'type' => 'options_buttons',
  ))
    ->save();

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

  // Display form: with no field data, nothing is checked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertNoFieldChecked('edit-card-2-0');
  $this
    ->assertNoFieldChecked('edit-card-2-1');
  $this
    ->assertNoFieldChecked('edit-card-2-2');
  $this
    ->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');

  // Submit form: select first and third options.
  $edit = array(
    'card_2[0]' => TRUE,
    'card_2[1]' => FALSE,
    'card_2[2]' => TRUE,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', array(
    0,
    2,
  ));

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

  // Submit form: select only first option.
  $edit = array(
    'card_2[0]' => TRUE,
    'card_2[1]' => FALSE,
    'card_2[2]' => FALSE,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_2', array(
    0,
  ));

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

  // Submit form: select the three options while the field accepts only 2.
  $edit = array(
    'card_2[0]' => TRUE,
    'card_2[1]' => TRUE,
    'card_2[2]' => TRUE,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');

  // Submit form: uncheck all options.
  $edit = array(
    'card_2[0]' => FALSE,
    'card_2[1]' => FALSE,
    'card_2[2]' => FALSE,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Check that the value was saved.
  $this
    ->assertFieldValues($entity_init, 'card_2', array());

  // Required checkbox with one option is auto-selected.
  $this->card2
    ->setSetting('allowed_values', [
    99 => 'Only allowed value',
  ]);
  $this->card2
    ->save();
  $field
    ->setRequired(TRUE);
  $field
    ->save();
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertFieldChecked('edit-card-2-99');
}