views_plugin_style.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsPluginStyleTestCase.
  5. */
  6. /**
  7. * Tests some general style plugin related functionality.
  8. */
  9. class ViewsPluginStyleTestCase extends ViewsPluginStyleTestBase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Styles',
  13. 'description' => 'Test general style functionality.',
  14. 'group' => 'Views Plugins',
  15. );
  16. }
  17. /**
  18. * Tests the grouping legacy features of styles.
  19. */
  20. function testGroupingLegacy() {
  21. $view = $this->getBasicView();
  22. // Setup grouping by the job.
  23. $view->init_display();
  24. $view->init_style();
  25. $view->style_plugin->options['grouping'] = 'job';
  26. // Reduce the amount of items to make the test a bit easier.
  27. // Set up the pager.
  28. $view->display['default']->handler->override_option('pager', array(
  29. 'type' => 'some',
  30. 'options' => array('items_per_page' => 3),
  31. ));
  32. // Add the job field .
  33. $view->display['default']->handler->override_option('fields', array(
  34. 'name' => array(
  35. 'id' => 'name',
  36. 'table' => 'views_test',
  37. 'field' => 'name',
  38. 'relationship' => 'none',
  39. ),
  40. 'job' => array(
  41. 'id' => 'job',
  42. 'table' => 'views_test',
  43. 'field' => 'job',
  44. 'relationship' => 'none',
  45. ),
  46. ));
  47. // Now run the query and groupby the result.
  48. $this->executeView($view);
  49. // This is the old way to call it.
  50. $sets = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping']);
  51. $expected = array();
  52. // Use Job: as label, so be sure that the label is used for groupby as well.
  53. $expected['Job: Singer'] = array();
  54. $expected['Job: Singer'][0] = new StdClass();
  55. $expected['Job: Singer'][0]->views_test_name = 'John';
  56. $expected['Job: Singer'][0]->views_test_job = 'Singer';
  57. $expected['Job: Singer'][0]->views_test_id = '1';
  58. $expected['Job: Singer'][1] = new StdClass();
  59. $expected['Job: Singer'][1]->views_test_name = 'George';
  60. $expected['Job: Singer'][1]->views_test_job = 'Singer';
  61. $expected['Job: Singer'][1]->views_test_id = '2';
  62. $expected['Job: Drummer'] = array();
  63. $expected['Job: Drummer'][2] = new StdClass();
  64. $expected['Job: Drummer'][2]->views_test_name = 'Ringo';
  65. $expected['Job: Drummer'][2]->views_test_job = 'Drummer';
  66. $expected['Job: Drummer'][2]->views_test_id = '3';
  67. $this->assertEqual($sets, $expected, t('The style plugin should proper group the results with grouping by the rendered output.'));
  68. $expected = array();
  69. $expected['Job: Singer'] = array();
  70. $expected['Job: Singer']['group'] = 'Job: Singer';
  71. $expected['Job: Singer']['level'] = '0';
  72. $expected['Job: Singer']['rows'][0] = new StdClass();
  73. $expected['Job: Singer']['rows'][0]->views_test_name = 'John';
  74. $expected['Job: Singer']['rows'][0]->views_test_job = 'Singer';
  75. $expected['Job: Singer']['rows'][0]->views_test_id = '1';
  76. $expected['Job: Singer']['rows'][1] = new StdClass();
  77. $expected['Job: Singer']['rows'][1]->views_test_name = 'George';
  78. $expected['Job: Singer']['rows'][1]->views_test_job = 'Singer';
  79. $expected['Job: Singer']['rows'][1]->views_test_id = '2';
  80. $expected['Job: Drummer'] = array();
  81. $expected['Job: Drummer']['group'] = 'Job: Drummer';
  82. $expected['Job: Drummer']['level'] = '0';
  83. $expected['Job: Drummer']['rows'][2] = new StdClass();
  84. $expected['Job: Drummer']['rows'][2]->views_test_name = 'Ringo';
  85. $expected['Job: Drummer']['rows'][2]->views_test_job = 'Drummer';
  86. $expected['Job: Drummer']['rows'][2]->views_test_id = '3';
  87. // The newer api passes the value of the grouping as well.
  88. $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE);
  89. $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE);
  90. $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper group the results with grouping by the rendered output.'));
  91. // Reorder the group structure to group by value.
  92. $expected['Singer'] = $expected['Job: Singer'];
  93. $expected['Drummer'] = $expected['Job: Drummer'];
  94. unset($expected['Job: Singer']);
  95. unset($expected['Job: Drummer']);
  96. $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper group the results with grouping by the value.'));
  97. }
  98. function testGrouping() {
  99. $this->_testGrouping(FALSE);
  100. $this->_testGrouping(TRUE);
  101. }
  102. /**
  103. * Tests the grouping features of styles.
  104. */
  105. function _testGrouping($stripped = FALSE) {
  106. $view = $this->getBasicView();
  107. // Setup grouping by the job and the age field.
  108. $view->init_display();
  109. $view->init_style();
  110. $view->style_plugin->options['grouping'] = array(
  111. array('field' => 'job'),
  112. array('field' => 'age'),
  113. );
  114. // Reduce the amount of items to make the test a bit easier.
  115. // Set up the pager.
  116. $view->display['default']->handler->override_option('pager', array(
  117. 'type' => 'some',
  118. 'options' => array('items_per_page' => 3),
  119. ));
  120. // Add the job and age field.
  121. $view->display['default']->handler->override_option('fields', array(
  122. 'name' => array(
  123. 'id' => 'name',
  124. 'table' => 'views_test',
  125. 'field' => 'name',
  126. 'relationship' => 'none',
  127. ),
  128. 'job' => array(
  129. 'id' => 'job',
  130. 'table' => 'views_test',
  131. 'field' => 'job',
  132. 'relationship' => 'none',
  133. ),
  134. 'age' => array(
  135. 'id' => 'age',
  136. 'table' => 'views_test',
  137. 'field' => 'age',
  138. 'relationship' => 'none',
  139. ),
  140. ));
  141. // Now run the query and groupby the result.
  142. $this->executeView($view);
  143. $expected = array();
  144. $expected['Job: Singer'] = array();
  145. $expected['Job: Singer']['group'] = 'Job: Singer';
  146. $expected['Job: Singer']['level'] = 0;
  147. $expected['Job: Singer']['rows']['Age: 25'] = array();
  148. $expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25';
  149. $expected['Job: Singer']['rows']['Age: 25']['level'] = 1;
  150. $expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new StdClass();
  151. $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_name = 'John';
  152. $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_job = 'Singer';
  153. $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_age = '25';
  154. $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_id = '1';
  155. $expected['Job: Singer']['rows']['Age: 27'] = array();
  156. $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
  157. $expected['Job: Singer']['rows']['Age: 27']['level'] = 1;
  158. $expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new StdClass();
  159. $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_name = 'George';
  160. $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_job = 'Singer';
  161. $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_age = '27';
  162. $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_id = '2';
  163. $expected['Job: Drummer'] = array();
  164. $expected['Job: Drummer']['group'] = 'Job: Drummer';
  165. $expected['Job: Drummer']['level'] = 0;
  166. $expected['Job: Drummer']['rows']['Age: 28'] = array();
  167. $expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28';
  168. $expected['Job: Drummer']['rows']['Age: 28']['level'] = 1;
  169. $expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new StdClass();
  170. $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_name = 'Ringo';
  171. $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_job = 'Drummer';
  172. $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_age = '28';
  173. $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_id = '3';
  174. // Alter the results to support the stripped case.
  175. if ($stripped) {
  176. // Add some html to the result and expected value.
  177. $rand = '<a data="' . $this->randomName() . '" />';
  178. $view->result[0]->views_test_job .= $rand;
  179. $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_job = 'Singer' . $rand;
  180. $expected['Job: Singer']['group'] = 'Job: Singer';
  181. $rand = '<a data="' . $this->randomName() . '" />';
  182. $view->result[1]->views_test_job .= $rand;
  183. $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_job = 'Singer' . $rand;
  184. $rand = '<a data="' . $this->randomName() . '" />';
  185. $view->result[2]->views_test_job .= $rand;
  186. $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_job = 'Drummer' . $rand;
  187. $expected['Job: Drummer']['group'] = 'Job: Drummer';
  188. $view->style_plugin->options['grouping'][0] = array('field' => 'job', 'rendered' => TRUE, 'rendered_strip' => TRUE);
  189. $view->style_plugin->options['grouping'][1] = array('field' => 'age', 'rendered' => TRUE, 'rendered_strip' => TRUE);
  190. }
  191. // The newer api passes the value of the grouping as well.
  192. $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE);
  193. $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper group the results with grouping by the rendered output.'));
  194. // Don't test stripped case, because the actual value is not stripped.
  195. if (!$stripped) {
  196. $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE);
  197. // Reorder the group structure to grouping by value.
  198. $expected['Singer'] = $expected['Job: Singer'];
  199. $expected['Singer']['rows']['25'] = $expected['Job: Singer']['rows']['Age: 25'];
  200. $expected['Singer']['rows']['27'] = $expected['Job: Singer']['rows']['Age: 27'];
  201. $expected['Drummer'] = $expected['Job: Drummer'];
  202. $expected['Drummer']['rows']['28'] = $expected['Job: Drummer']['rows']['Age: 28'];
  203. unset($expected['Job: Singer']);
  204. unset($expected['Singer']['rows']['Age: 25']);
  205. unset($expected['Singer']['rows']['Age: 27']);
  206. unset($expected['Job: Drummer']);
  207. unset($expected['Drummer']['rows']['Age: 28']);
  208. $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper group the results with grouping by the value.'));
  209. }
  210. }
  211. /**
  212. * Tests custom css classes.
  213. */
  214. function testCustomRowClasses() {
  215. $view = $this->getBasicView();
  216. // Setup some random css class.
  217. $view->init_display();
  218. $view->init_style();
  219. $random_name = drupal_html_class($this->randomName());
  220. $view->style_plugin->options['row_class'] = $random_name . " test-token-[name]";
  221. $rendered_output = $view->preview();
  222. $this->storeViewPreview($rendered_output);
  223. $rows = $this->elements->body->div->div->div;
  224. $count = 0;
  225. foreach ($rows as $row) {
  226. $attributes = $row->attributes();
  227. $class = (string) $attributes['class'][0];
  228. $this->assertTrue(strpos($class, $random_name) !== FALSE, 'Make sure that a custom css class is added to the output.');
  229. // Check token replacement.
  230. $name = drupal_clean_css_identifier($view->field['name']->get_value($view->result[$count]));
  231. $this->assertTrue(strpos($class, "test-token-$name") !== FALSE, 'Make sure that a token in custom css class is replaced.');
  232. $count++;
  233. }
  234. }
  235. }