You are here

function OptionsWidgetsTest::testRadioButtons 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::testRadioButtons()

Tests the 'options_buttons' widget (single select).

File

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

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\options\Tests

Code

function testRadioButtons() {

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

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

  // With no field data, no buttons are checked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertNoFieldChecked('edit-card-1-0');
  $this
    ->assertNoFieldChecked('edit-card-1-1');
  $this
    ->assertNoFieldChecked('edit-card-1-2');
  $this
    ->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
  $this
    ->assertRaw('Some HTML encoded markup with &lt; &amp; &gt;');

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

  // Check that the selected button is checked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertFieldChecked('edit-card-1-0');
  $this
    ->assertNoFieldChecked('edit-card-1-1');
  $this
    ->assertNoFieldChecked('edit-card-1-2');

  // Unselect option.
  $edit = array(
    'card_1' => '_none',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'card_1', array());

  // Check that required radios with one option is auto-selected.
  $this->card1
    ->setSetting('allowed_values', [
    99 => 'Only allowed value',
  ]);
  $this->card1
    ->save();
  $field
    ->setRequired(TRUE);
  $field
    ->save();
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertFieldChecked('edit-card-1-99');
}