BooleanFieldTest.php 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\field\Tests\Boolean\BooleanFieldTest.
 */

namespace Drupal\field\Tests\Boolean;

10
use Drupal\Component\Utility\Unicode;
11
use Drupal\field\Entity\FieldStorageConfig;
12
use Drupal\field\Entity\FieldConfig;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use Drupal\simpletest\WebTestBase;

/**
 * Tests boolean field functionality.
 *
 * @group field
 */
class BooleanFieldTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('entity_test', 'field_ui', 'options');

  /**
   * A field to use in this test class.
   *
32
   * @var \Drupal\field\Entity\FieldStorageConfig
33
   */
34
  protected $fieldStorage;
35 36

  /**
37
   * The field used in this test class.
38
   *
39
   * @var \Drupal\field\Entity\FieldConfig
40
   */
41
  protected $field;
42 43 44 45

  /**
   * {@inheritdoc}
   */
46
  protected function setUp() {
47 48
    parent::setUp();

49
    $this->drupalLogin($this->drupalCreateUser(array(
50 51 52 53
      'view test entity',
      'administer entity_test content',
      'administer entity_test form display',
      'administer entity_test fields',
54
    )));
55 56 57 58 59 60
  }

  /**
   * Tests boolean field.
   */
  function testBooleanField() {
61 62 63
    $on = $this->randomMachineName();
    $off = $this->randomMachineName();
    $label = $this->randomMachineName();
64 65

    // Create a field with settings to validate.
66
    $field_name = Unicode::strtolower($this->randomMachineName());
67
    $this->fieldStorage = FieldStorageConfig::create(array(
68
      'field_name' => $field_name,
69 70 71
      'entity_type' => 'entity_test',
      'type' => 'boolean',
    ));
72
    $this->fieldStorage->save();
73
    $this->field = FieldConfig::create(array(
74 75 76 77 78
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'label' => $label,
      'required' => TRUE,
79 80 81 82
      'settings' => array(
        'on_label' => $on,
        'off_label' => $off,
      ),
83
    ));
84
    $this->field->save();
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    // Create a form display for the default form mode.
    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent($field_name, array(
        'type' => 'boolean_checkbox',
      ))
      ->save();
    // Create a display for the full view mode.
    entity_get_display('entity_test', 'entity_test', 'full')
      ->setComponent($field_name, array(
        'type' => 'boolean',
      ))
      ->save();

    // Display creation form.
    $this->drupalGet('entity_test/add');
    $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
    $this->assertRaw($on);

    // Submit and ensure it is accepted.
    $edit = array(
      "{$field_name}[value]" => 1,
    );
    $this->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
    $id = $match[1];
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));

    // Verify that boolean value is displayed.
    $entity = entity_load('entity_test', $id);
    $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
    $content = $display->build($entity);
117
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
118 119
    $this->assertRaw('<div class="field-item">' . $on . '</div>');

120 121 122
    // Test if we can change the on label.
    $on = $this->randomMachineName();
    $edit = array(
123
      'settings[on_label]' => $on,
124 125 126 127 128 129
    );
    $this->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name, $edit, t('Save settings'));
    // Check if we see the updated labels in the creation form.
    $this->drupalGet('entity_test/add');
    $this->assertRaw($on);

130 131 132 133 134 135 136 137 138 139 140 141 142
    // Test the display_label option.
    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent($field_name, array(
        'type' => 'boolean_checkbox',
        'settings' => array(
          'display_label' => TRUE,
        )
      ))
      ->save();

    $this->drupalGet('entity_test/add');
    $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
    $this->assertNoRaw($on);
143
    $this->assertText($this->field->label());
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    // Go to the form display page and check if the default settings works as
    // expected.
    $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
    $this->drupalGet($fieldEditUrl);

    // Click on the widget settings button to open the widget settings form.
    $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");

    $this->assertText(
      'Use field label instead of the "On label" as label',
      t('Display setting checkbox available.')
    );

    // Enable setting.
    $edit = array('fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1);
    $this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update");
    $this->drupalPostForm(NULL, NULL, 'Save');

    // Go again to the form display page and check if the setting
    // is stored and has the expected effect.
    $this->drupalGet($fieldEditUrl);
    $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');

    $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
    $this->assertText(
      'Use field label instead of the "On label" as label',
      t('Display setting checkbox is available')
    );
    $this->assertFieldByXPath(
174
      '*//input[starts-with(@id, "edit-fields-' . $field_name . '-settings-edit-form-settings-display-label") and @value="1"]',
175 176 177 178 179
      TRUE,
      t('Display label changes label of the checkbox')
    );

    // Test the boolean field settings.
180
    $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
181 182
    $this->assertFieldById('edit-settings-on-label', $on);
    $this->assertFieldById('edit-settings-off-label', $off);
183 184 185
  }

}