StyleTest.php 13.8 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\Plugin\StyleTest.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Tests\Plugin;
9

10 11 12 13
use Drupal\views\Tests\ViewTestBase;
use Drupal\views_test_data\Plugin\views\row\RowTest;
use Drupal\views_test_data\Plugin\views\style\StyleTest as StyleTestPlugin;

merlinofchaos's avatar
merlinofchaos committed
14 15
/**
 * Tests some general style plugin related functionality.
16 17
 *
 * @see Drupal\views_test_data\Plugin\views\style\StyleTest.
merlinofchaos's avatar
merlinofchaos committed
18
 */
19 20
class StyleTest extends ViewTestBase {

21 22 23 24 25 26 27
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view');

28 29 30 31 32 33
  /**
   * Stores the SimpleXML representation of the output.
   *
   * @var \SimpleXMLElement
   */
  protected $elements;
34

merlinofchaos's avatar
merlinofchaos committed
35 36
  public static function getInfo() {
    return array(
37
      'name' => 'Style: General',
merlinofchaos's avatar
merlinofchaos committed
38 39 40 41 42
      'description' => 'Test general style functionality.',
      'group' => 'Views Plugins',
    );
  }

43 44 45 46 47 48
  protected function setUp() {
    parent::setUp();

    $this->enableViewsTestModule();
  }

49 50 51 52
  /**
   * Tests the general renderering of styles.
   */
  public function testStyle() {
53 54
    $view = views_get_view('test_view');
    $view->setDisplay();
55 56 57
    $style = $view->display_handler->getOption('style');
    $style['type'] = 'test_style';
    $view->display_handler->setOption('style', $style);
58 59
    $view->initDisplay();
    $view->initStyle();
60
    $this->assertTrue($view->style_plugin instanceof StyleTestPlugin, 'Make sure the right style plugin class is loaded.');
61 62 63 64 65 66 67 68 69

    $random_text = $this->randomName();
    // Set some custom text to the output and make sure that this value is
    // rendered.
    $view->style_plugin->setOutput($random_text);
    $output = $view->preview();
    $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Take sure that the rendering of the style plugin appears in the output of the view.');

    // This run use the test row plugin and render with it.
70 71
    $view = views_get_view('test_view');
    $view->setDisplay();
72 73 74 75 76 77
    $style = $view->display_handler->getOption('style');
    $style['type'] = 'test_style';
    $view->display_handler->setOption('style', $style);
    $row = $view->display_handler->getOption('row');
    $row['type'] = 'test_row';
    $view->display_handler->setOption('row', $row);
78 79 80 81
    $view->initDisplay();
    $view->initStyle();
    $view->style_plugin->setUsesRowPlugin(TRUE);
    // Reinitialize the style as it supports row plugins now.
82
    $view->style_plugin->init($view, $view->display_handler, array());
83
    $this->assertTrue($view->style_plugin->row_plugin instanceof RowTest, 'Make sure the right row plugin class is loaded.');
84 85 86 87 88 89 90 91

    $random_text = $this->randomName();
    $view->style_plugin->row_plugin->setOutput($random_text);

    $output = $view->preview();
    $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Take sure that the rendering of the row plugin appears in the output of the view.');
  }

merlinofchaos's avatar
merlinofchaos committed
92 93 94 95
  /**
   * Tests the grouping legacy features of styles.
   */
  function testGroupingLegacy() {
96
    $view = views_get_view('test_view');
merlinofchaos's avatar
merlinofchaos committed
97
    // Setup grouping by the job.
98
    $view->setDisplay();
99
    $view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
100 101 102 103
    $view->style_plugin->options['grouping'] = 'job';

    // Reduce the amount of items to make the test a bit easier.
    // Set up the pager.
104
    $view->displayHandlers['default']->overrideOption('pager', array(
merlinofchaos's avatar
merlinofchaos committed
105 106 107 108 109
      'type' => 'some',
      'options' => array('items_per_page' => 3),
    ));

    // Add the job field .
110
    $view->displayHandlers['default']->overrideOption('fields', array(
merlinofchaos's avatar
merlinofchaos committed
111 112
      'name' => array(
        'id' => 'name',
113
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
114 115 116 117 118
        'field' => 'name',
        'relationship' => 'none',
      ),
      'job' => array(
        'id' => 'job',
119
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133
        'field' => 'job',
        'relationship' => 'none',
      ),
    ));

    // Now run the query and groupby the result.
    $this->executeView($view);

    // This is the old way to call it.
    $sets = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping']);

    $expected = array();
    // Use Job: as label, so be sure that the label is used for groupby as well.
    $expected['Job: Singer'] = array();
134
    $expected['Job: Singer'][0] = new \stdClass();
135 136 137
    $expected['Job: Singer'][0]->views_test_data_name = 'John';
    $expected['Job: Singer'][0]->views_test_data_job = 'Singer';
    $expected['Job: Singer'][0]->views_test_data_id = '1';
138
    $expected['Job: Singer'][1] = new \stdClass();
139 140 141
    $expected['Job: Singer'][1]->views_test_data_name = 'George';
    $expected['Job: Singer'][1]->views_test_data_job = 'Singer';
    $expected['Job: Singer'][1]->views_test_data_id = '2';
merlinofchaos's avatar
merlinofchaos committed
142
    $expected['Job: Drummer'] = array();
143
    $expected['Job: Drummer'][2] = new \stdClass();
144 145 146
    $expected['Job: Drummer'][2]->views_test_data_name = 'Ringo';
    $expected['Job: Drummer'][2]->views_test_data_job = 'Drummer';
    $expected['Job: Drummer'][2]->views_test_data_id = '3';
merlinofchaos's avatar
merlinofchaos committed
147 148 149 150 151 152

    $this->assertEqual($sets, $expected, t('The style plugin should proper group the results with grouping by the rendered output.'));

    $expected = array();
    $expected['Job: Singer'] = array();
    $expected['Job: Singer']['group'] = 'Job: Singer';
153
    $expected['Job: Singer']['rows'][0] = new \stdClass();
154 155 156
    $expected['Job: Singer']['rows'][0]->views_test_data_name = 'John';
    $expected['Job: Singer']['rows'][0]->views_test_data_job = 'Singer';
    $expected['Job: Singer']['rows'][0]->views_test_data_id = '1';
157
    $expected['Job: Singer']['rows'][1] = new \stdClass();
158 159 160
    $expected['Job: Singer']['rows'][1]->views_test_data_name = 'George';
    $expected['Job: Singer']['rows'][1]->views_test_data_job = 'Singer';
    $expected['Job: Singer']['rows'][1]->views_test_data_id = '2';
merlinofchaos's avatar
merlinofchaos committed
161 162
    $expected['Job: Drummer'] = array();
    $expected['Job: Drummer']['group'] = 'Job: Drummer';
163
    $expected['Job: Drummer']['rows'][2] = new \stdClass();
164 165 166
    $expected['Job: Drummer']['rows'][2]->views_test_data_name = 'Ringo';
    $expected['Job: Drummer']['rows'][2]->views_test_data_job = 'Drummer';
    $expected['Job: Drummer']['rows'][2]->views_test_data_id = '3';
merlinofchaos's avatar
merlinofchaos committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

    // The newer api passes the value of the grouping as well.
    $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE);
    $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE);

    $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper group the results with grouping by the rendered output.'));

    // Reorder the group structure to group by value.
    $expected['Singer'] = $expected['Job: Singer'];
    $expected['Drummer'] = $expected['Job: Drummer'];
    unset($expected['Job: Singer']);
    unset($expected['Job: Drummer']);

    $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper group the results with grouping by the value.'));
  }

  function testGrouping() {
    $this->_testGrouping(FALSE);
    $this->_testGrouping(TRUE);
  }

  /**
   * Tests the grouping features of styles.
   */
  function _testGrouping($stripped = FALSE) {
192 193
    $view = views_get_view('test_view');
    $view->setDisplay();
merlinofchaos's avatar
merlinofchaos committed
194
    // Setup grouping by the job and the age field.
195
    $view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
196 197 198 199 200 201 202
    $view->style_plugin->options['grouping'] = array(
      array('field' => 'job'),
      array('field' => 'age'),
    );

    // Reduce the amount of items to make the test a bit easier.
    // Set up the pager.
203
    $view->displayHandlers['default']->overrideOption('pager', array(
merlinofchaos's avatar
merlinofchaos committed
204 205 206 207 208
      'type' => 'some',
      'options' => array('items_per_page' => 3),
    ));

    // Add the job and age field.
209
    $view->displayHandlers['default']->overrideOption('fields', array(
merlinofchaos's avatar
merlinofchaos committed
210 211
      'name' => array(
        'id' => 'name',
212
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
213 214 215 216 217
        'field' => 'name',
        'relationship' => 'none',
      ),
      'job' => array(
        'id' => 'job',
218
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
219 220 221 222 223
        'field' => 'job',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
224
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
225 226 227 228 229 230 231 232 233 234 235 236 237
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));

    // Now run the query and groupby the result.
    $this->executeView($view);

    $expected = array();
    $expected['Job: Singer'] = array();
    $expected['Job: Singer']['group'] = 'Job: Singer';
    $expected['Job: Singer']['rows']['Age: 25'] = array();
    $expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25';
238
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new \stdClass();
239 240 241 242
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_name = 'John';
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer';
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_age = '25';
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_id = '1';
merlinofchaos's avatar
merlinofchaos committed
243 244
    $expected['Job: Singer']['rows']['Age: 27'] = array();
    $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
245
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new \stdClass();
246 247 248 249
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_name = 'George';
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer';
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_age = '27';
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_id = '2';
merlinofchaos's avatar
merlinofchaos committed
250 251 252 253
    $expected['Job: Drummer'] = array();
    $expected['Job: Drummer']['group'] = 'Job: Drummer';
    $expected['Job: Drummer']['rows']['Age: 28'] = array();
    $expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28';
254
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new \stdClass();
255 256 257 258
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_name = 'Ringo';
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer';
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_age = '28';
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_id = '3';
merlinofchaos's avatar
merlinofchaos committed
259 260 261 262 263 264 265


    // Alter the results to support the stripped case.
    if ($stripped) {

      // Add some html to the result and expected value.
      $rand = '<a data="' . $this->randomName() . '" />';
266 267
      $view->result[0]->views_test_data_job .= $rand;
      $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer' . $rand;
merlinofchaos's avatar
merlinofchaos committed
268 269
      $expected['Job: Singer']['group'] = 'Job: Singer';
      $rand = '<a data="' . $this->randomName() . '" />';
270 271
      $view->result[1]->views_test_data_job .= $rand;
      $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer' . $rand;
merlinofchaos's avatar
merlinofchaos committed
272
      $rand = '<a data="' . $this->randomName() . '" />';
273 274
      $view->result[2]->views_test_data_job .= $rand;
      $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer' . $rand;
merlinofchaos's avatar
merlinofchaos committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
      $expected['Job: Drummer']['group'] = 'Job: Drummer';

      $view->style_plugin->options['grouping'][0] = array('field' => 'job', 'rendered' => TRUE, 'rendered_strip' => TRUE);
      $view->style_plugin->options['grouping'][1] = array('field' => 'age', 'rendered' => TRUE, 'rendered_strip' => TRUE);
    }


    // The newer api passes the value of the grouping as well.
    $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE);

    $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper group the results with grouping by the rendered output.'));

    // Don't test stripped case, because the actual value is not stripped.
    if (!$stripped) {
      $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE);

      // Reorder the group structure to grouping by value.
      $expected['Singer'] = $expected['Job: Singer'];
      $expected['Singer']['rows']['25'] = $expected['Job: Singer']['rows']['Age: 25'];
      $expected['Singer']['rows']['27'] = $expected['Job: Singer']['rows']['Age: 27'];
      $expected['Drummer'] = $expected['Job: Drummer'];
      $expected['Drummer']['rows']['28'] = $expected['Job: Drummer']['rows']['Age: 28'];
      unset($expected['Job: Singer']);
      unset($expected['Singer']['rows']['Age: 25']);
      unset($expected['Singer']['rows']['Age: 27']);
      unset($expected['Job: Drummer']);
      unset($expected['Drummer']['rows']['Age: 28']);

      $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper group the results with grouping by the value.'));
    }
  }

  /**
   * Tests custom css classes.
   */
  function testCustomRowClasses() {
311 312
    $view = views_get_view('test_view');
    $view->setDisplay();
merlinofchaos's avatar
merlinofchaos committed
313 314

    // Setup some random css class.
315
    $view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    $random_name = $this->randomName();
    $view->style_plugin->options['row_class'] = $random_name . " test-token-[name]";

    $rendered_output = $view->preview();
    $this->storeViewPreview($rendered_output);

    $rows = $this->elements->body->div->div->div;
    $count = 0;
    foreach ($rows as $row) {
      $attributes = $row->attributes();
      $class = (string) $attributes['class'][0];
      $this->assertTrue(strpos($class, $random_name) !== FALSE, 'Take sure that a custom css class is added to the output.');

      // Check token replacement.
      $name = $view->field['name']->get_value($view->result[$count]);
      $this->assertTrue(strpos($class, "test-token-$name") !== FALSE, 'Take sure that a token in custom css class is replaced.');

      $count++;
    }
  }
336

337 338 339 340 341 342 343 344 345 346 347 348 349
  /**
   * Stores a view output in the elements.
   */
  protected function storeViewPreview($output) {
    $htmlDom = new \DOMDocument();
    @$htmlDom->loadHTML($output);
    if ($htmlDom) {
      // It's much easier to work with simplexml than DOM, luckily enough
      // we can just simply import our DOM tree.
      $this->elements = simplexml_import_dom($htmlDom);
    }
  }

merlinofchaos's avatar
merlinofchaos committed
350
}