StyleTest.php 10.9 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

use stdClass;

merlinofchaos's avatar
merlinofchaos committed
12 13 14
/**
 * Tests some general style plugin related functionality.
 */
15
class StyleTest extends StyleTestBase {
16

merlinofchaos's avatar
merlinofchaos committed
17 18
  public static function getInfo() {
    return array(
19
      'name' => 'Style: General',
merlinofchaos's avatar
merlinofchaos committed
20 21 22 23 24 25 26 27 28
      'description' => 'Test general style functionality.',
      'group' => 'Views Plugins',
    );
  }

  /**
   * Tests the grouping legacy features of styles.
   */
  function testGroupingLegacy() {
29
    $view = $this->view->cloneView();
merlinofchaos's avatar
merlinofchaos committed
30
    // Setup grouping by the job.
31 32
    $view->initDisplay();
    $view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
33 34 35 36
    $view->style_plugin->options['grouping'] = 'job';

    // Reduce the amount of items to make the test a bit easier.
    // Set up the pager.
37
    $view->display['default']->handler->overrideOption('pager', array(
merlinofchaos's avatar
merlinofchaos committed
38 39 40 41 42
      'type' => 'some',
      'options' => array('items_per_page' => 3),
    ));

    // Add the job field .
43
    $view->display['default']->handler->overrideOption('fields', array(
merlinofchaos's avatar
merlinofchaos committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'job' => array(
        'id' => 'job',
        'table' => 'views_test',
        '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();
67
    $expected['Job: Singer'][0] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
68 69 70
    $expected['Job: Singer'][0]->views_test_name = 'John';
    $expected['Job: Singer'][0]->views_test_job = 'Singer';
    $expected['Job: Singer'][0]->views_test_id = '1';
71
    $expected['Job: Singer'][1] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
72 73 74 75
    $expected['Job: Singer'][1]->views_test_name = 'George';
    $expected['Job: Singer'][1]->views_test_job = 'Singer';
    $expected['Job: Singer'][1]->views_test_id = '2';
    $expected['Job: Drummer'] = array();
76
    $expected['Job: Drummer'][2] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
77 78 79 80 81 82 83 84 85
    $expected['Job: Drummer'][2]->views_test_name = 'Ringo';
    $expected['Job: Drummer'][2]->views_test_job = 'Drummer';
    $expected['Job: Drummer'][2]->views_test_id = '3';

    $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';
86
    $expected['Job: Singer']['rows'][0] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
87 88 89
    $expected['Job: Singer']['rows'][0]->views_test_name = 'John';
    $expected['Job: Singer']['rows'][0]->views_test_job = 'Singer';
    $expected['Job: Singer']['rows'][0]->views_test_id = '1';
90
    $expected['Job: Singer']['rows'][1] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
91 92 93 94 95
    $expected['Job: Singer']['rows'][1]->views_test_name = 'George';
    $expected['Job: Singer']['rows'][1]->views_test_job = 'Singer';
    $expected['Job: Singer']['rows'][1]->views_test_id = '2';
    $expected['Job: Drummer'] = array();
    $expected['Job: Drummer']['group'] = 'Job: Drummer';
96
    $expected['Job: Drummer']['rows'][2] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    $expected['Job: Drummer']['rows'][2]->views_test_name = 'Ringo';
    $expected['Job: Drummer']['rows'][2]->views_test_job = 'Drummer';
    $expected['Job: Drummer']['rows'][2]->views_test_id = '3';

    // 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) {
125
    $view = $this->getView();
merlinofchaos's avatar
merlinofchaos committed
126
    // Setup grouping by the job and the age field.
127 128
    $view->initDisplay();
    $view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
129 130 131 132 133 134 135
    $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.
136
    $view->display['default']->handler->overrideOption('pager', array(
merlinofchaos's avatar
merlinofchaos committed
137 138 139 140 141
      'type' => 'some',
      'options' => array('items_per_page' => 3),
    ));

    // Add the job and age field.
142
    $view->display['default']->handler->overrideOption('fields', array(
merlinofchaos's avatar
merlinofchaos committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'job' => array(
        'id' => 'job',
        'table' => 'views_test',
        'field' => 'job',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        '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';
171
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
172 173 174 175 176 177
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_name = 'John';
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_job = 'Singer';
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_age = '25';
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_id = '1';
    $expected['Job: Singer']['rows']['Age: 27'] = array();
    $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
178
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
179 180 181 182 183 184 185 186
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_name = 'George';
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_job = 'Singer';
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_age = '27';
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_id = '2';
    $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';
187
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_name = 'Ringo';
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_job = 'Drummer';
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_age = '28';
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_id = '3';


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

      // Add some html to the result and expected value.
      $rand = '<a data="' . $this->randomName() . '" />';
      $view->result[0]->views_test_job .= $rand;
      $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_job = 'Singer' . $rand;
      $expected['Job: Singer']['group'] = 'Job: Singer';
      $rand = '<a data="' . $this->randomName() . '" />';
      $view->result[1]->views_test_job .= $rand;
      $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_job = 'Singer' . $rand;
      $rand = '<a data="' . $this->randomName() . '" />';
      $view->result[2]->views_test_job .= $rand;
      $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_job = 'Drummer' . $rand;
      $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() {
244
    $view = $this->view->cloneView();
merlinofchaos's avatar
merlinofchaos committed
245 246

    // Setup some random css class.
247 248
    $view->initDisplay();
    $view->initStyle();
merlinofchaos's avatar
merlinofchaos committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    $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++;
    }
  }
269

merlinofchaos's avatar
merlinofchaos committed
270
}