commit bfd211d00f92a58112305ad6be5a3576d860d800 Author: Joel Pittet Date: Wed Sep 23 16:13:37 2015 +0200 boolean diff --git a/core/modules/views/src/Plugin/views/field/Boolean.php b/core/modules/views/src/Plugin/views/field/Boolean.php index 1e0eab0..37001ec 100644 --- a/core/modules/views/src/Plugin/views/field/Boolean.php +++ b/core/modules/views/src/Plugin/views/field/Boolean.php @@ -7,7 +7,9 @@ namespace Drupal\views\Plugin\views\field; +use Drupal\Component\Utility\Xss as UtilityXss; use Drupal\Core\Form\FormStateInterface; +use Drupal\views\Render\ViewsRenderPipelineSafeString; use Drupal\views\ResultRow; use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -117,13 +119,8 @@ public function render(ResultRow $values) { } if ($this->options['type'] == 'custom') { - if ($value) { - $build = ['#markup' => $this->options['type_custom_true']]; - } - else { - $build = ['#markup' => $this->options['type_custom_false']]; - } - return \Drupal::service('renderer')->renderPlain($build); + $custom_value = $value ? UtilityXss::filterAdmin($this->options['type_custom_true']) : UtilityXss::filterAdmin($this->options['type_custom_false']); + return ViewsRenderPipelineSafeString::create($custom_value); } elseif (isset($this->formats[$this->options['type']])) { return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1]; diff --git a/core/modules/views_ui/src/Tests/CustomBooleanTest.php b/core/modules/views_ui/src/Tests/CustomBooleanTest.php index 9910973..b0ba60a 100644 --- a/core/modules/views_ui/src/Tests/CustomBooleanTest.php +++ b/core/modules/views_ui/src/Tests/CustomBooleanTest.php @@ -7,6 +7,7 @@ namespace Drupal\views_ui\Tests; +use Drupal\Component\Utility\SafeMarkup; use Drupal\views\Views; /** @@ -101,17 +102,62 @@ public function testCustomOption() { $view = Views::getView('test_view'); $output = $view->preview(); $output = \Drupal::service('renderer')->renderRoot($output); - $this->{$values['test']}(strpos($output, $values['true']), 'Expected custom boolean TRUE value ' . $values['true'] . ' in output for ' . $type); - $this->{$values['test']}(strpos($output, $values['false']), 'Expected custom boolean FALSE value ' . $values['false'] . ' in output for ' . $type); + $this->{$values['test']}(strpos($output, $values['true']), SafeMarkup::format('Expected custom boolean TRUE value %value in output for %type', ['%value' => $values['true'], '%type' => $type])); + $this->{$values['test']}(strpos($output, $values['false']), SafeMarkup::format('Expected custom boolean FALSE value %value in output for %type', ['%value' => $values['false'], '%type' => $type])); } + } + /** + * Tests the setting and output of custom labels for boolean values. + */ + public function testCustomOptionTemplate() { // Install theme to test with template system. - \Drupal::service('theme_handler')->install(array('views_test_theme')); + \Drupal::service('theme_handler')->install(['views_test_theme']); - // Make base theme default then test for hook invocations. + // Set the default theme for Views preview. $this->config('system.theme') ->set('default', 'views_test_theme') ->save(); + $this->assertEqual($this->config('system.theme')->get('default'), 'views_test_theme'); + + // Add the boolean field handler to the test view. + $view = Views::getView('test_view'); + $view->setDisplay(); + + $view->displayHandlers->get('default')->overrideOption('fields', [ + 'age' => [ + 'id' => 'age', + 'table' => 'views_test_data', + 'field' => 'age', + 'relationship' => 'none', + 'plugin_id' => 'boolean', + ], + ]); + $view->save(); + + $this->executeView($view); + + $custom_true = 'Yay'; + $custom_false = 'Nay'; + + // Set up some custom value mappings for different types. + $custom_values = array( + 'plain' => array( + 'true' => $custom_true, + 'false' => $custom_false, + 'test' => 'assertTrue', + ), + 'allowed tag' => array( + 'true' => '

' . $custom_true . '

', + 'false' => '

' . $custom_false . '

', + 'test' => 'assertTrue', + ), + 'disallowed tag' => array( + 'true' => '', + 'false' => '', + 'test' => 'assertFalse', + ), + ); // Run the same tests on each type. foreach ($custom_values as $type => $values) { @@ -128,8 +174,12 @@ public function testCustomOption() { $view = Views::getView('test_view'); $output = $view->preview(); $output = \Drupal::service('renderer')->renderRoot($output); - $this->{$values['test']}(strpos($output, $values['true']), 'Expected custom boolean TRUE value ' . $values['true'] . ' in output for ' . $type); - $this->{$values['test']}(strpos($output, $values['false']), 'Expected custom boolean FALSE value ' . $values['false'] . ' in output for ' . $type); + $this->{$values['test']}(strpos($output, $values['true']), SafeMarkup::format('Expected custom boolean TRUE value %value in output for %type', ['%value' => $values['true'], '%type' => $type])); + $this->{$values['test']}(strpos($output, $values['false']), SafeMarkup::format('Expected custom boolean FALSE value %value in output for %type', ['%value' => $values['false'], '%type' => $type])); + + // Assert that we are using the correct template. + $this->setRawContent($output); + $this->assertText('llama', 'Loaded the correct views-view-field.html.twig template'); } }