function OptionsWidgetsTest::testOnOffCheckbox

Tests the 'options_onoff' widget.

File

drupal/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php, line 454
Definition of Drupal\options\Tests\OptionsWidgetsTest.

Class

OptionsWidgetsTest
Test the Options widgets.

Namespace

Drupal\options\Tests

Code

function testOnOffCheckbox() {

  // Create an instance of the 'boolean' field.
  $instance = array(
    'field_name' => $this->bool['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  $instance = field_create_instance($instance);
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->bool['field_name'], array(
    'type' => 'options_onoff',
  ))
    ->save();
  $langcode = Language::LANGCODE_NOT_SPECIFIED;

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

  // Display form: with no field data, option is unchecked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertNoFieldChecked("edit-bool-{$langcode}");
  $this
    ->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');

  // Submit form: check the option.
  $edit = array(
    "bool[{$langcode}]" => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'bool', $langcode, array(
    1,
  ));

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

  // Submit form: uncheck the option.
  $edit = array(
    "bool[{$langcode}]" => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->assertFieldValues($entity_init, 'bool', $langcode, array(
    0,
  ));

  // Display form: with 'off' value, option is unchecked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this
    ->assertNoFieldChecked("edit-bool-{$langcode}");

  // Create Basic page node type.
  $this
    ->drupalCreateContentType(array(
    'type' => 'page',
    'name' => 'Basic page',
  ));

  // Create admin user.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access content',
    'administer content types',
    'administer node fields',
    'administer taxonomy',
  ));
  $this
    ->drupalLogin($admin_user);

  // Create a test field instance.
  $fieldUpdate = $this->bool;
  $fieldUpdate['settings']['allowed_values'] = array(
    0 => 0,
    1 => 'MyOnValue',
  );
  field_update_field($fieldUpdate);
  $instance = array(
    'field_name' => $this->bool['field_name'],
    'entity_type' => 'node',
    'bundle' => 'page',
  );
  field_create_instance($instance);
  entity_get_form_display('node', 'page', 'default')
    ->setComponent($this->bool['field_name'], array(
    'type' => 'options_onoff',
  ))
    ->save();

  // Go to the edit page and check if the default settings works as expected
  $fieldEditUrl = 'admin/structure/types/manage/page/fields/node.page.bool';
  $this
    ->drupalGet($fieldEditUrl);
  $this
    ->assertText('Use field label instead of the "On value" as label', t('Display setting checkbox available.'));
  $this
    ->assertFieldByXPath('*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue"]', TRUE, t('Default case shows "On value"'));

  // Enable setting
  $edit = array(
    'instance[widget][settings][display_label]' => 1,
  );

  // Save the new Settings
  $this
    ->drupalPost($fieldEditUrl, $edit, t('Save settings'));

  // Go again to the edit page and check if the setting
  // is stored and has the expected effect
  $this
    ->drupalGet($fieldEditUrl);
  $this
    ->assertText('Use field label instead of the "On value" as label', t('Display setting checkbox is available'));
  $this
    ->assertFieldChecked('edit-instance-widget-settings-display-label', t('Display settings checkbox checked'));
  $this
    ->assertFieldByXPath('*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . '"]', TRUE, t('Display label changes label of the checkbox'));
}