3 namespace Drupal\field\Tests\Views;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\views\Views;
10 * Tests the UI of the field field handler.
13 * @see \Drupal\field\Plugin\views\field\Field
15 class FieldUITest extends FieldTestBase {
18 * Views used by this test.
22 public static $testViews = ['test_view_fieldapi'];
29 public static $modules = ['views_ui'];
32 * A user with the 'administer views' permission.
34 * @var \Drupal\user\UserInterface
41 protected function setUp() {
44 $this->account = $this->drupalCreateUser(['administer views']);
45 $this->drupalLogin($this->account);
47 $this->setUpFieldStorages(1, 'text');
52 * Tests basic field handler settings in the UI.
54 public function testHandlerUI() {
55 $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
56 $this->drupalGet($url);
58 // Tests the available formatter options.
59 $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-type']);
60 $options = array_map(function ($item) {
61 return (string) $item->attributes()->value[0];
63 // @todo Replace this sort by assertArray once it's in.
64 sort($options, SORT_STRING);
65 $this->assertEqual($options, ['text_default', 'text_trimmed'], 'The text formatters for a simple text field appear as expected.');
67 $this->drupalPostForm(NULL, ['options[type]' => 'text_trimmed'], t('Apply'));
69 $this->drupalGet($url);
70 $this->assertOptionSelected('edit-options-type', 'text_trimmed');
72 $random_number = rand(100, 400);
73 $this->drupalPostForm(NULL, ['options[settings][trim_length]' => $random_number], t('Apply'));
74 $this->drupalGet($url);
75 $this->assertFieldByName('options[settings][trim_length]', $random_number, 'The formatter setting got saved.');
77 // Save the view and test whether the settings are saved.
78 $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save'));
79 $view = Views::getView('test_view_fieldapi');
80 $view->initHandlers();
81 $this->assertEqual($view->field['field_name_0']->options['type'], 'text_trimmed');
82 $this->assertEqual($view->field['field_name_0']->options['settings']['trim_length'], $random_number);
84 // Now change the formatter back to 'default' which doesn't have any
85 // settings. We want to ensure that the settings are empty then.
86 $edit['options[type]'] = 'text_default';
87 $this->drupalPostForm('admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0', $edit, t('Apply'));
88 $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save'));
89 $view = Views::getView('test_view_fieldapi');
90 $view->initHandlers();
91 $this->assertEqual($view->field['field_name_0']->options['type'], 'text_default');
92 $this->assertEqual($view->field['field_name_0']->options['settings'], []);
94 // Ensure that the view depends on the field storage.
95 $dependencies = \Drupal::service('config.manager')->findConfigEntityDependents('config', [$this->fieldStorages[0]->getConfigDependencyName()]);
96 $this->assertTrue(isset($dependencies['views.view.test_view_fieldapi']), 'The view is dependent on the field storage.');
100 * Tests the basic field handler form when aggregation is enabled.
102 public function testHandlerUIAggregation() {
103 // Enable aggregation.
104 $edit = ['group_by' => '1'];
105 $this->drupalPostForm('admin/structure/views/nojs/display/test_view_fieldapi/default/group_by', $edit, t('Apply'));
107 $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
108 $this->drupalGet($url);
109 $this->assertResponse(200);
111 // Test the click sort column options.
112 // Tests the available formatter options.
113 $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-click-sort-column']);
114 $options = array_map(function ($item) {
115 return (string) $item->attributes()->value[0];
117 sort($options, SORT_STRING);
119 $this->assertEqual($options, ['format', 'value'], 'The expected sort field options were found.');
123 * Tests adding a boolean field filter handler.
125 public function testBooleanFilterHandler() {
126 // Create a boolean field.
127 $field_name = 'field_boolean';
128 $field_storage = FieldStorageConfig::create([
129 'field_name' => $field_name,
130 'entity_type' => 'node',
133 $field_storage->save();
134 $field = FieldConfig::create([
135 'field_storage' => $field_storage,
140 $url = "admin/structure/views/nojs/add-handler/test_view_fieldapi/default/filter";
141 $this->drupalPostForm($url, ['name[node__' . $field_name . '.' . $field_name . '_value]' => TRUE], t('Add and configure @handler', ['@handler' => t('filter criteria')]));
142 $this->assertResponse(200);
143 // Verify that using a boolean field as a filter also results in using the
145 $option = $this->xpath('//label[@for="edit-options-value-1"]');
146 $this->assertEqual(t('True'), (string) $option[0]);
147 $option = $this->xpath('//label[@for="edit-options-value-0"]');
148 $this->assertEqual(t('False'), (string) $option[0]);
150 // Expose the filter and see if the 'Any' option is added and if we can save
152 $this->drupalPostForm(NULL, [], 'Expose filter');
153 $option = $this->xpath('//label[@for="edit-options-value-all"]');
154 $this->assertEqual(t('- Any -'), (string) $option[0]);
155 $this->drupalPostForm(NULL, ['options[value]' => 'All', 'options[expose][required]' => FALSE], 'Apply');
156 $this->drupalPostForm(NULL, [], 'Save');
157 $this->drupalGet('/admin/structure/views/nojs/handler/test_view_fieldapi/default/filter/field_boolean_value');
158 $this->assertFieldChecked('edit-options-value-all');