3 namespace Drupal\Tests\views_ui\Functional;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\views\Views;
9 * Tests the UI and functionality for the Custom boolean field handler options.
12 * @see \Drupal\views\Plugin\views\field\Boolean
14 class CustomBooleanTest extends UITestBase {
17 * Views used by this test.
21 public static $testViews = ['test_view'];
24 * \Drupal\views\Tests\ViewTestBase::viewsData().
26 public function viewsData() {
27 $data = parent::viewsData();
28 $data['views_test_data']['age']['field']['id'] = 'boolean';
35 public function dataSet() {
36 $data = parent::dataSet();
43 * Tests the setting and output of custom labels for boolean values.
45 public function testCustomOption() {
46 // Add the boolean field handler to the test view.
47 $view = Views::getView('test_view');
50 $view->displayHandlers->get('default')->overrideOption('fields', [
53 'table' => 'views_test_data',
55 'relationship' => 'none',
56 'plugin_id' => 'boolean',
61 $this->executeView($view);
64 $custom_false = 'Nay';
66 // Set up some custom value mappings for different types.
69 'true' => $custom_true,
70 'false' => $custom_false,
71 'test' => 'assertTrue',
74 'true' => '<p>' . $custom_true . '</p>',
75 'false' => '<p>' . $custom_false . '</p>',
76 'test' => 'assertTrue',
79 'true' => '<script>' . $custom_true . '</script>',
80 'false' => '<script>' . $custom_false . '</script>',
81 'test' => 'assertFalse',
85 // Run the same tests on each type.
86 foreach ($custom_values as $type => $values) {
88 'options[type]' => 'custom',
89 'options[type_custom_true]' => $values['true'],
90 'options[type_custom_false]' => $values['false'],
92 $this->drupalPostForm('admin/structure/views/nojs/handler/test_view/default/field/age', $options, 'Apply');
95 $this->drupalPostForm('admin/structure/views/view/test_view', [], 'Save');
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']), new FormattableMarkup('Expected custom boolean TRUE value %value in output for %type', ['%value' => $values['true'], '%type' => $type]));
101 $this->{$values['test']}(strpos($output, $values['false']), new FormattableMarkup('Expected custom boolean FALSE value %value in output for %type', ['%value' => $values['false'], '%type' => $type]));
106 * Tests the setting and output of custom labels for boolean values.
108 public function testCustomOptionTemplate() {
109 // Install theme to test with template system.
110 \Drupal::service('theme_handler')->install(['views_test_theme']);
112 // Set the default theme for Views preview.
113 $this->config('system.theme')
114 ->set('default', 'views_test_theme')
116 $this->assertEqual($this->config('system.theme')->get('default'), 'views_test_theme');
118 // Add the boolean field handler to the test view.
119 $view = Views::getView('test_view');
122 $view->displayHandlers->get('default')->overrideOption('fields', [
125 'table' => 'views_test_data',
127 'relationship' => 'none',
128 'plugin_id' => 'boolean',
133 $this->executeView($view);
135 $custom_true = 'Yay';
136 $custom_false = 'Nay';
138 // Set up some custom value mappings for different types.
141 'true' => $custom_true,
142 'false' => $custom_false,
143 'test' => 'assertTrue',
146 'true' => '<p>' . $custom_true . '</p>',
147 'false' => '<p>' . $custom_false . '</p>',
148 'test' => 'assertTrue',
150 'disallowed tag' => [
151 'true' => '<script>' . $custom_true . '</script>',
152 'false' => '<script>' . $custom_false . '</script>',
153 'test' => 'assertFalse',
157 // Run the same tests on each type.
158 foreach ($custom_values as $type => $values) {
160 'options[type]' => 'custom',
161 'options[type_custom_true]' => $values['true'],
162 'options[type_custom_false]' => $values['false'],
164 $this->drupalPostForm('admin/structure/views/nojs/handler/test_view/default/field/age', $options, 'Apply');
167 $this->drupalPostForm('admin/structure/views/view/test_view', [], 'Save');
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']), new FormattableMarkup('Expected custom boolean TRUE value %value in output for %type', ['%value' => $values['true'], '%type' => $type]));
173 $this->{$values['test']}(strpos($output, $values['false']), new FormattableMarkup('Expected custom boolean FALSE value %value in output for %type', ['%value' => $values['false'], '%type' => $type]));
175 // Assert that we are using the correct template.
176 $this->assertContains('llama', (string) $output);