views_groupby.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * @file
  4. * Tests aggregate functionality of Views.
  5. */
  6. /**
  7. * Tests aggregate functionality of views, for example count.
  8. */
  9. class ViewsQueryGroupByTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Groupby',
  13. 'description' => 'Tests aggregate functionality of views, for example count.',
  14. 'group' => 'Views',
  15. );
  16. }
  17. /**
  18. * Tests aggregate count feature.
  19. */
  20. public function testAggregateCount() {
  21. // Create 2 nodes of type1 and 3 nodes of type2
  22. $type1 = $this->drupalCreateContentType();
  23. $type2 = $this->drupalCreateContentType();
  24. $node_1 = array(
  25. 'type' => $type1->type,
  26. );
  27. $this->drupalCreateNode($node_1);
  28. $this->drupalCreateNode($node_1);
  29. $this->drupalCreateNode($node_1);
  30. $this->drupalCreateNode($node_1);
  31. $node_2 = array(
  32. 'type' => $type2->type,
  33. );
  34. $this->drupalCreateNode($node_2);
  35. $this->drupalCreateNode($node_2);
  36. $this->drupalCreateNode($node_2);
  37. $view = $this->viewsAggregateCountView();
  38. $output = $view->execute_display();
  39. $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
  40. $types = array();
  41. foreach ($view->result as $item) {
  42. // num_records is a alias for nid.
  43. $types[$item->node_type] = $item->num_records;
  44. }
  45. $this->assertEqual($types[$type1->type], 4);
  46. $this->assertEqual($types[$type2->type], 3);
  47. }
  48. //public function testAggregateSum() {
  49. //}
  50. public function viewsAggregateCountView() {
  51. $view = new view;
  52. $view->name = 'aggregate_count';
  53. $view->description = '';
  54. $view->tag = '';
  55. $view->base_table = 'node';
  56. $view->human_name = '';
  57. $view->core = 7;
  58. $view->api_version = '3.0';
  59. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  60. /* Display: Master */
  61. $handler = $view->new_display('default', 'Master', 'default');
  62. $handler->display->display_options['group_by'] = TRUE;
  63. $handler->display->display_options['access']['type'] = 'none';
  64. $handler->display->display_options['cache']['type'] = 'none';
  65. $handler->display->display_options['query']['type'] = 'views_query';
  66. $handler->display->display_options['query']['options']['query_comment'] = FALSE;
  67. $handler->display->display_options['exposed_form']['type'] = 'basic';
  68. $handler->display->display_options['pager']['type'] = 'some';
  69. $handler->display->display_options['style_plugin'] = 'default';
  70. $handler->display->display_options['row_plugin'] = 'fields';
  71. /* Field: Content: Title */
  72. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  73. $handler->display->display_options['fields']['nid']['table'] = 'node';
  74. $handler->display->display_options['fields']['nid']['field'] = 'title';
  75. $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
  76. $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
  77. $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
  78. $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
  79. $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
  80. $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
  81. $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
  82. $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
  83. $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
  84. $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
  85. /* Contextual filter: Content: Type */
  86. $handler->display->display_options['arguments']['type']['id'] = 'type';
  87. $handler->display->display_options['arguments']['type']['table'] = 'node';
  88. $handler->display->display_options['arguments']['type']['field'] = 'type';
  89. $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
  90. $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
  91. $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
  92. return $view;
  93. }
  94. /**
  95. * @param $group_by
  96. * Which group_by function should be used, for example sum or count.
  97. */
  98. function GroupByTestHelper($group_by, $values) {
  99. // Create 2 nodes of type1 and 3 nodes of type2
  100. $type1 = $this->drupalCreateContentType();
  101. $type2 = $this->drupalCreateContentType();
  102. $node_1 = array(
  103. 'type' => $type1->type,
  104. );
  105. // Nids from 1 to 4.
  106. $this->drupalCreateNode($node_1);
  107. $this->drupalCreateNode($node_1);
  108. $this->drupalCreateNode($node_1);
  109. $this->drupalCreateNode($node_1);
  110. $node_2 = array(
  111. 'type' => $type2->type,
  112. );
  113. // Nids from 5 to 7.
  114. $this->drupalCreateNode($node_2);
  115. $this->drupalCreateNode($node_2);
  116. $this->drupalCreateNode($node_2);
  117. $view = $this->viewsGroupByViewHelper($group_by);
  118. $output = $view->execute_display();
  119. $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
  120. // Group by nodetype to identify the right count.
  121. foreach ($view->result as $item) {
  122. $results[$item->node_type] = $item->nid;
  123. }
  124. $this->assertEqual($results[$type1->type], $values[0]);
  125. $this->assertEqual($results[$type2->type], $values[1]);
  126. }
  127. function viewsGroupByViewHelper($group_by) {
  128. $view = new view;
  129. $view->name = 'group_by_count';
  130. $view->description = '';
  131. $view->tag = '';
  132. $view->view_php = '';
  133. $view->base_table = 'node';
  134. $view->is_cacheable = FALSE;
  135. $view->api_version = 2;
  136. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  137. /* Display: Master */
  138. $handler = $view->new_display('default', 'Master', 'default');
  139. $handler->display->display_options['group_by'] = TRUE;
  140. $handler->display->display_options['access']['type'] = 'none';
  141. $handler->display->display_options['cache']['type'] = 'none';
  142. $handler->display->display_options['exposed_form']['type'] = 'basic';
  143. $handler->display->display_options['pager']['type'] = 'some';
  144. $handler->display->display_options['style_plugin'] = 'default';
  145. $handler->display->display_options['row_plugin'] = 'fields';
  146. /* Field: Content: Nid */
  147. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  148. $handler->display->display_options['fields']['nid']['table'] = 'node';
  149. $handler->display->display_options['fields']['nid']['field'] = 'nid';
  150. $handler->display->display_options['fields']['nid']['group_type'] = $group_by;
  151. $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
  152. $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
  153. $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
  154. $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
  155. $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
  156. $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
  157. $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
  158. $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
  159. $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
  160. $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
  161. /* Field: Content: Type */
  162. $handler->display->display_options['fields']['type']['id'] = 'type';
  163. $handler->display->display_options['fields']['type']['table'] = 'node';
  164. $handler->display->display_options['fields']['type']['field'] = 'type';
  165. $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
  166. $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
  167. $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
  168. $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
  169. $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
  170. $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
  171. $handler->display->display_options['fields']['type']['alter']['html'] = 0;
  172. $handler->display->display_options['fields']['type']['hide_empty'] = 0;
  173. $handler->display->display_options['fields']['type']['empty_zero'] = 0;
  174. $handler->display->display_options['fields']['type']['link_to_node'] = 0;
  175. return $view;
  176. }
  177. public function testGroupByCount() {
  178. $this->GroupByTestHelper('count', array(4, 3));
  179. }
  180. function testGroupBySum() {
  181. $this->GroupByTestHelper('sum', array(10, 18));
  182. }
  183. function testGroupByAverage() {
  184. $this->GroupByTestHelper('avg', array(2.5, 6));
  185. }
  186. function testGroupByMin() {
  187. $this->GroupByTestHelper('min', array(1, 5));
  188. }
  189. function testGroupByMax() {
  190. $this->GroupByTestHelper('max', array(4, 7));
  191. }
  192. public function testGroupByCountOnlyFilters() {
  193. // Check if GROUP BY and HAVING are included when a view
  194. // Doesn't display SUM, COUNT, MAX... functions in SELECT statment
  195. $type1 = $this->drupalCreateContentType();
  196. $node_1 = array(
  197. 'type' => $type1->type,
  198. );
  199. for ($x = 0; $x < 10; $x++) {
  200. $this->drupalCreateNode($node_1);
  201. }
  202. $view = $this->viewsGroupByCountViewOnlyFilters();
  203. $output = $view->execute_display();
  204. $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), t('Make sure that GROUP BY is in the query'));
  205. $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), t('Make sure that HAVING is in the query'));
  206. }
  207. function viewsGroupByCountViewOnlyFilters() {
  208. $view = new view;
  209. $view->name = 'group_by_in_filters';
  210. $view->description = '';
  211. $view->tag = '';
  212. $view->view_php = '';
  213. $view->base_table = 'node';
  214. $view->is_cacheable = FALSE;
  215. $view->api_version = 2;
  216. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  217. /* Display: Master */
  218. $handler = $view->new_display('default', 'Master', 'default');
  219. $handler->display->display_options['group_by'] = TRUE;
  220. $handler->display->display_options['access']['type'] = 'none';
  221. $handler->display->display_options['cache']['type'] = 'none';
  222. $handler->display->display_options['exposed_form']['type'] = 'basic';
  223. $handler->display->display_options['pager']['type'] = 'some';
  224. $handler->display->display_options['style_plugin'] = 'default';
  225. $handler->display->display_options['row_plugin'] = 'fields';
  226. /* Field: Nodo: Tipo */
  227. $handler->display->display_options['fields']['type']['id'] = 'type';
  228. $handler->display->display_options['fields']['type']['table'] = 'node';
  229. $handler->display->display_options['fields']['type']['field'] = 'type';
  230. $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
  231. $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
  232. $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
  233. $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
  234. $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
  235. $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
  236. $handler->display->display_options['fields']['type']['alter']['html'] = 0;
  237. $handler->display->display_options['fields']['type']['hide_empty'] = 0;
  238. $handler->display->display_options['fields']['type']['empty_zero'] = 0;
  239. $handler->display->display_options['fields']['type']['link_to_node'] = 0;
  240. /* Filtrar: Nodo: Nid */
  241. $handler->display->display_options['filters']['nid']['id'] = 'nid';
  242. $handler->display->display_options['filters']['nid']['table'] = 'node';
  243. $handler->display->display_options['filters']['nid']['field'] = 'nid';
  244. $handler->display->display_options['filters']['nid']['group_type'] = 'count';
  245. $handler->display->display_options['filters']['nid']['operator'] = '>';
  246. $handler->display->display_options['filters']['nid']['value']['value'] = '3';
  247. return $view;
  248. }
  249. }
  250. /**
  251. * Tests UI of aggregate functionality..
  252. */
  253. class viewsUiGroupbyTestCase extends DrupalWebTestCase {
  254. function setUp() {
  255. // Enable views_ui.
  256. parent::setUp('views_ui', 'views_test');
  257. // Create and log in a user with administer views permission.
  258. $views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions'));
  259. $this->drupalLogin($views_admin);
  260. }
  261. public static function getInfo() {
  262. return array(
  263. 'name' => 'Groupby UI',
  264. 'description' => 'Tests UI of aggregate functionality.',
  265. 'group' => 'Views UI',
  266. );
  267. }
  268. /**
  269. * Tests whether basic saving works.
  270. *
  271. * @todo: this should check the change of the settings as well.
  272. */
  273. function testGroupBySave() {
  274. $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
  275. $edit = array(
  276. 'group_by' => TRUE,
  277. );
  278. $this->drupalPost('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by', $edit, t('Apply'));
  279. $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
  280. $this->drupalPost('admin/structure/views/view/test_views_groupby_save/edit', array(), t('Save'));
  281. $this->drupalGet('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by');
  282. }
  283. }