More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / field / src / Tests / Views / FieldUITest.php
1 <?php
2
3 namespace Drupal\field\Tests\Views;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\views\Views;
8
9 /**
10  * Tests the UI of the field field handler.
11  *
12  * @group field
13  * @see \Drupal\field\Plugin\views\field\Field
14  */
15 class FieldUITest extends FieldTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_view_fieldapi'];
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['views_ui'];
30
31   /**
32    * A user with the 'administer views' permission.
33    *
34    * @var \Drupal\user\UserInterface
35    */
36   protected $account;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp($import_test_views = TRUE) {
42     parent::setUp($import_test_views);
43
44     $this->account = $this->drupalCreateUser(['administer views']);
45     $this->drupalLogin($this->account);
46
47     $this->setUpFieldStorages(1, 'text');
48     $this->setUpFields();
49   }
50
51   /**
52    * Tests basic field handler settings in the UI.
53    */
54   public function testHandlerUI() {
55     $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
56     $this->drupalGet($url);
57
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];
62     }, $result);
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.');
66
67     $this->drupalPostForm(NULL, ['options[type]' => 'text_trimmed'], t('Apply'));
68
69     $this->drupalGet($url);
70     $this->assertOptionSelected('edit-options-type', 'text_trimmed');
71
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.');
76
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);
83
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'], []);
93
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.');
97   }
98
99   /**
100    * Tests the basic field handler form when aggregation is enabled.
101    */
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'));
106
107     $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
108     $this->drupalGet($url);
109     $this->assertResponse(200);
110
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];
116     }, $result);
117     sort($options, SORT_STRING);
118
119     $this->assertEqual($options, ['format', 'value'], 'The expected sort field options were found.');
120   }
121
122   /**
123    * Tests adding a boolean field filter handler.
124    */
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',
131       'type' => 'boolean',
132     ]);
133     $field_storage->save();
134     $field = FieldConfig::create([
135       'field_storage' => $field_storage,
136       'bundle' => 'page',
137     ]);
138     $field->save();
139
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
144     // boolean plugin.
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]);
149
150     // Expose the filter and see if the 'Any' option is added and if we can save
151     // it.
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');
159   }
160
161 }