Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views_ui / tests / src / Functional / CustomBooleanTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Functional;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the UI and functionality for the Custom boolean field handler options.
10  *
11  * @group views_ui
12  * @see \Drupal\views\Plugin\views\field\Boolean
13  */
14 class CustomBooleanTest extends UITestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_view'];
22
23   /**
24    * \Drupal\views\Tests\ViewTestBase::viewsData().
25    */
26   public function viewsData() {
27     $data = parent::viewsData();
28     $data['views_test_data']['age']['field']['id'] = 'boolean';
29     return $data;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function dataSet() {
36     $data = parent::dataSet();
37     $data[0]['age'] = 0;
38     $data[3]['age'] = 0;
39     return $data;
40   }
41
42   /**
43    * Tests the setting and output of custom labels for boolean values.
44    */
45   public function testCustomOption() {
46     // Add the boolean field handler to the test view.
47     $view = Views::getView('test_view');
48     $view->setDisplay();
49
50     $view->displayHandlers->get('default')->overrideOption('fields', [
51       'age' => [
52         'id' => 'age',
53         'table' => 'views_test_data',
54         'field' => 'age',
55         'relationship' => 'none',
56         'plugin_id' => 'boolean',
57       ],
58     ]);
59     $view->save();
60
61     $this->executeView($view);
62
63     $custom_true = 'Yay';
64     $custom_false = 'Nay';
65
66     // Set up some custom value mappings for different types.
67     $custom_values = [
68       'plain' => [
69         'true' => $custom_true,
70         'false' => $custom_false,
71         'test' => 'assertTrue',
72       ],
73       'allowed tag' => [
74         'true' => '<p>' . $custom_true . '</p>',
75         'false' => '<p>' . $custom_false . '</p>',
76         'test' => 'assertTrue',
77       ],
78       'disallowed tag' => [
79         'true' => '<script>' . $custom_true . '</script>',
80         'false' => '<script>' . $custom_false . '</script>',
81         'test' => 'assertFalse',
82       ],
83     ];
84
85     // Run the same tests on each type.
86     foreach ($custom_values as $type => $values) {
87       $options = [
88         'options[type]' => 'custom',
89         'options[type_custom_true]' => $values['true'],
90         'options[type_custom_false]' => $values['false'],
91       ];
92       $this->drupalPostForm('admin/structure/views/nojs/handler/test_view/default/field/age', $options, 'Apply');
93
94       // Save the view.
95       $this->drupalPostForm('admin/structure/views/view/test_view', [], 'Save');
96
97       $view = Views::getView('test_view');
98       $output = $view->preview();
99       $output = \Drupal::service('renderer')->renderRoot($output);
100       $this->{$values['test']}(strpos($output, $values['true']), SafeMarkup::format('Expected custom boolean TRUE value %value in output for %type', ['%value' => $values['true'], '%type' => $type]));
101       $this->{$values['test']}(strpos($output, $values['false']), SafeMarkup::format('Expected custom boolean FALSE value %value in output for %type', ['%value' => $values['false'], '%type' => $type]));
102     }
103   }
104
105   /**
106    * Tests the setting and output of custom labels for boolean values.
107    */
108   public function testCustomOptionTemplate() {
109     // Install theme to test with template system.
110     \Drupal::service('theme_handler')->install(['views_test_theme']);
111
112     // Set the default theme for Views preview.
113     $this->config('system.theme')
114       ->set('default', 'views_test_theme')
115       ->save();
116     $this->assertEqual($this->config('system.theme')->get('default'), 'views_test_theme');
117
118     // Add the boolean field handler to the test view.
119     $view = Views::getView('test_view');
120     $view->setDisplay();
121
122     $view->displayHandlers->get('default')->overrideOption('fields', [
123       'age' => [
124         'id' => 'age',
125         'table' => 'views_test_data',
126         'field' => 'age',
127         'relationship' => 'none',
128         'plugin_id' => 'boolean',
129       ],
130     ]);
131     $view->save();
132
133     $this->executeView($view);
134
135     $custom_true = 'Yay';
136     $custom_false = 'Nay';
137
138     // Set up some custom value mappings for different types.
139     $custom_values = [
140       'plain' => [
141         'true' => $custom_true,
142         'false' => $custom_false,
143         'test' => 'assertTrue',
144       ],
145       'allowed tag' => [
146         'true' => '<p>' . $custom_true . '</p>',
147         'false' => '<p>' . $custom_false . '</p>',
148         'test' => 'assertTrue',
149       ],
150       'disallowed tag' => [
151         'true' => '<script>' . $custom_true . '</script>',
152         'false' => '<script>' . $custom_false . '</script>',
153         'test' => 'assertFalse',
154       ],
155     ];
156
157     // Run the same tests on each type.
158     foreach ($custom_values as $type => $values) {
159       $options = [
160         'options[type]' => 'custom',
161         'options[type_custom_true]' => $values['true'],
162         'options[type_custom_false]' => $values['false'],
163       ];
164       $this->drupalPostForm('admin/structure/views/nojs/handler/test_view/default/field/age', $options, 'Apply');
165
166       // Save the view.
167       $this->drupalPostForm('admin/structure/views/view/test_view', [], 'Save');
168
169       $view = Views::getView('test_view');
170       $output = $view->preview();
171       $output = \Drupal::service('renderer')->renderRoot($output);
172       $this->{$values['test']}(strpos($output, $values['true']), SafeMarkup::format('Expected custom boolean TRUE value %value in output for %type', ['%value' => $values['true'], '%type' => $type]));
173       $this->{$values['test']}(strpos($output, $values['false']), SafeMarkup::format('Expected custom boolean FALSE value %value in output for %type', ['%value' => $values['false'], '%type' => $type]));
174
175       // Assert that we are using the correct template.
176       $this->assertContains('llama', (string) $output);
177     }
178   }
179
180 }