CustomBooleanTest.php 5.76 KB
Newer Older
1 2
<?php

3
namespace Drupal\views_ui\Tests;
4

5
use Drupal\Component\Utility\SafeMarkup;
6 7
use Drupal\views\Views;

8 9 10
/**
 * Tests the UI and functionality for the Custom boolean field handler options.
 *
11
 * @group views_ui
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * @see \Drupal\views\Plugin\views\field\Boolean
 */
class CustomBooleanTest extends UITestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view');

  /**
   * \Drupal\views\Tests\ViewTestBase::viewsData().
   */
  public function viewsData() {
    $data = parent::viewsData();
    $data['views_test_data']['age']['field']['id'] = 'boolean';
    return $data;
  }

  /**
33
   * {@inheritdoc}
34 35 36 37 38 39 40 41 42 43 44 45 46
   */
  public function dataSet() {
    $data = parent::dataSet();
    $data[0]['age'] = 0;
    $data[3]['age'] = 0;
    return $data;
  }

  /**
   * Tests the setting and output of custom labels for boolean values.
   */
  public function testCustomOption() {
    // Add the boolean field handler to the test view.
47
    $view = Views::getView('test_view');
48 49 50 51 52 53 54 55
    $view->setDisplay();

    $view->displayHandlers->get('default')->overrideOption('fields', array(
      'age' => array(
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
        'relationship' => 'none',
56
        'plugin_id' => 'boolean',
57 58
      ),
    ));
59
    $view->save();
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    $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' => '<p>' . $custom_true . '</p>',
        'false' => '<p>' . $custom_false . '</p>',
        'test' => 'assertTrue',
      ),
      'disallowed tag' => array(
        'true' => '<script>' . $custom_true . '</script>',
        'false' => '<script>' . $custom_false . '</script>',
        'test' => 'assertFalse',
      ),
    );

    // Run the same tests on each type.
    foreach ($custom_values as $type => $values) {
      $options = array(
        'options[type]' => 'custom',
        'options[type_custom_true]' => $values['true'],
        'options[type_custom_false]' => $values['false'],
      );
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', array(), 'Save');
96

97
      $view = Views::getView('test_view');
98
      $output = $view->preview();
99
      $output = \Drupal::service('renderer')->renderRoot($output);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      $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(['views_test_theme']);

    // 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');

118
    // Add the boolean field handler to the test view.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    $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' => '<p>' . $custom_true . '</p>',
        'false' => '<p>' . $custom_false . '</p>',
        'test' => 'assertTrue',
      ),
      'disallowed tag' => array(
        'true' => '<script>' . $custom_true . '</script>',
        'false' => '<script>' . $custom_false . '</script>',
        'test' => 'assertFalse',
      ),
    );
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    // Run the same tests on each type.
    foreach ($custom_values as $type => $values) {
      $options = array(
        'options[type]' => 'custom',
        'options[type_custom_true]' => $values['true'],
        'options[type_custom_false]' => $values['false'],
      );
      $this->drupalPostForm('admin/structure/views/nojs/handler/test_view/default/field/age', $options, 'Apply');

      // Save the view.
      $this->drupalPostForm('admin/structure/views/view/test_view', array(), 'Save');

      $view = Views::getView('test_view');
      $output = $view->preview();
      $output = \Drupal::service('renderer')->renderRoot($output);
      $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');
178 179 180 181
    }
  }

}