views_groupby.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * @file
  4. * Definitions of ViewsQueryGroupByTest and ViewsUiGroupbyTestCase.
  5. */
  6. /**
  7. * Tests aggregate functionality of views, for example count.
  8. */
  9. class ViewsQueryGroupByTest extends ViewsSqlTest {
  10. /**
  11. * Test meta data.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Groupby',
  16. 'description' => 'Tests aggregate functionality of views, for example count.',
  17. 'group' => 'Views',
  18. );
  19. }
  20. // tests ambiguous group by column error (postgresql)
  21. public function testAggregateAmbiguity() {
  22. // Create 4 nodes of type1
  23. $type1 = $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. $view = $this->viewsAggregateAmbiguityView();
  32. $output = $view->execute_display();
  33. $this->assertEqual(count($view->result), 1, 'Make sure there are no ambiguity problems with the group by operation.');
  34. }
  35. public function viewsAggregateAmbiguityView() {
  36. $view = new view();
  37. $view->name = 'aggregate_ambiguity';
  38. $view->description = '';
  39. $view->tag = 'default';
  40. $view->base_table = 'node';
  41. $view->human_name = '';
  42. $view->core = 7;
  43. $view->api_version = '3.0';
  44. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  45. /* Display: Master */
  46. $handler = $view->new_display('default', 'Master', 'default');
  47. $handler->display->display_options['use_more_always'] = FALSE;
  48. $handler->display->display_options['group_by'] = TRUE;
  49. $handler->display->display_options['access']['type'] = 'none';
  50. $handler->display->display_options['cache']['type'] = 'none';
  51. $handler->display->display_options['query']['type'] = 'views_query';
  52. $handler->display->display_options['exposed_form']['type'] = 'basic';
  53. $handler->display->display_options['pager']['type'] = 'full';
  54. $handler->display->display_options['style_plugin'] = 'default';
  55. $handler->display->display_options['row_plugin'] = 'fields';
  56. /* Field: COUNT(Content revision: Nid) */
  57. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  58. $handler->display->display_options['fields']['nid']['table'] = 'node_revision';
  59. $handler->display->display_options['fields']['nid']['field'] = 'nid';
  60. $handler->display->display_options['fields']['nid']['group_type'] = 'count';
  61. $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
  62. $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
  63. $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
  64. $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
  65. $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
  66. $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
  67. $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
  68. $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
  69. $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
  70. /* Field: Content: Nid */
  71. $handler->display->display_options['fields']['nid_1']['id'] = 'nid_1';
  72. $handler->display->display_options['fields']['nid_1']['table'] = 'node';
  73. $handler->display->display_options['fields']['nid_1']['field'] = 'nid';
  74. $handler->display->display_options['fields']['nid_1']['alter']['alter_text'] = 0;
  75. $handler->display->display_options['fields']['nid_1']['alter']['make_link'] = 0;
  76. $handler->display->display_options['fields']['nid_1']['alter']['word_boundary'] = 1;
  77. $handler->display->display_options['fields']['nid_1']['alter']['ellipsis'] = 1;
  78. $handler->display->display_options['fields']['nid_1']['alter']['strip_tags'] = 0;
  79. $handler->display->display_options['fields']['nid_1']['alter']['trim'] = 0;
  80. $handler->display->display_options['fields']['nid_1']['alter']['html'] = 0;
  81. $handler->display->display_options['fields']['nid_1']['hide_empty'] = 0;
  82. $handler->display->display_options['fields']['nid_1']['empty_zero'] = 0;
  83. /* Contextual filter: Content: Type */
  84. $handler->display->display_options['arguments']['type']['id'] = 'type';
  85. $handler->display->display_options['arguments']['type']['table'] = 'node';
  86. $handler->display->display_options['arguments']['type']['field'] = 'type';
  87. $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
  88. $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
  89. $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
  90. return $view;
  91. }
  92. /**
  93. * Tests aggregate count feature.
  94. */
  95. public function testAggregateCount() {
  96. // Create 2 nodes of type1 and 3 nodes of type2
  97. $type1 = $this->drupalCreateContentType();
  98. $type2 = $this->drupalCreateContentType();
  99. $node_1 = array(
  100. 'type' => $type1->type,
  101. );
  102. $this->drupalCreateNode($node_1);
  103. $this->drupalCreateNode($node_1);
  104. $this->drupalCreateNode($node_1);
  105. $this->drupalCreateNode($node_1);
  106. $node_2 = array(
  107. 'type' => $type2->type,
  108. );
  109. $this->drupalCreateNode($node_2);
  110. $this->drupalCreateNode($node_2);
  111. $this->drupalCreateNode($node_2);
  112. $view = $this->viewsAggregateCountView();
  113. $output = $view->execute_display();
  114. $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
  115. $types = array();
  116. foreach ($view->result as $item) {
  117. // 'num_records' is a alias for nid.
  118. $types[$item->node_type] = $item->num_records;
  119. }
  120. $this->assertEqual($types[$type1->type], 4);
  121. $this->assertEqual($types[$type2->type], 3);
  122. }
  123. /**
  124. *
  125. */
  126. // public function testAggregateSum() {
  127. // }
  128. /**
  129. *
  130. */
  131. public function viewsAggregateCountView() {
  132. $view = new view;
  133. $view->name = 'aggregate_count';
  134. $view->description = '';
  135. $view->tag = '';
  136. $view->base_table = 'node';
  137. $view->human_name = '';
  138. $view->core = 7;
  139. $view->api_version = '3.0';
  140. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  141. /* Display: Master */
  142. $handler = $view->new_display('default', 'Master', 'default');
  143. $handler->display->display_options['group_by'] = TRUE;
  144. $handler->display->display_options['access']['type'] = 'none';
  145. $handler->display->display_options['cache']['type'] = 'none';
  146. $handler->display->display_options['query']['type'] = 'views_query';
  147. $handler->display->display_options['query']['options']['query_comment'] = FALSE;
  148. $handler->display->display_options['exposed_form']['type'] = 'basic';
  149. $handler->display->display_options['pager']['type'] = 'some';
  150. $handler->display->display_options['style_plugin'] = 'default';
  151. $handler->display->display_options['row_plugin'] = 'fields';
  152. /* Field: Content: Title */
  153. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  154. $handler->display->display_options['fields']['nid']['table'] = 'node';
  155. $handler->display->display_options['fields']['nid']['field'] = 'title';
  156. $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
  157. $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
  158. $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
  159. $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
  160. $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
  161. $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
  162. $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
  163. $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
  164. $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
  165. $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
  166. /* Contextual filter: Content: Type */
  167. $handler->display->display_options['arguments']['type']['id'] = 'type';
  168. $handler->display->display_options['arguments']['type']['table'] = 'node';
  169. $handler->display->display_options['arguments']['type']['field'] = 'type';
  170. $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
  171. $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
  172. $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
  173. return $view;
  174. }
  175. /**
  176. * @param string|null $group_by
  177. * (optional) Which group_by function should be used, for example sum or
  178. * count. If omitted, the aggregation is tested with no group function.
  179. * @param array|null $values
  180. * (optional) Expected values.
  181. */
  182. function GroupByTestHelper($group_by = NULL, $values = NULL) {
  183. // Create 4 nodes of type1 and 3 nodes of type2
  184. $type1 = $this->drupalCreateContentType();
  185. $type2 = $this->drupalCreateContentType();
  186. $node_1 = array(
  187. 'type' => $type1->type,
  188. );
  189. // Nids from 1 to 4.
  190. $this->drupalCreateNode($node_1);
  191. $this->drupalCreateNode($node_1);
  192. $this->drupalCreateNode($node_1);
  193. $this->drupalCreateNode($node_1);
  194. $node_2 = array(
  195. 'type' => $type2->type,
  196. );
  197. // Nids from 5 to 7.
  198. $this->drupalCreateNode($node_2);
  199. $this->drupalCreateNode($node_2);
  200. $this->drupalCreateNode($node_2);
  201. $view = $this->viewsGroupByViewHelper($group_by);
  202. $output = $view->execute_display();
  203. $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
  204. $results = array();
  205. // There's no need for a function in order to have aggregation.
  206. if (empty($group_by)) {
  207. $types = array($type1->type, $type2->type);
  208. $results = array_map(function ($item) {
  209. return $item->node_type;
  210. }, $view->result);
  211. sort($types);
  212. sort($results);
  213. $this->assertIdentical($results, $types);
  214. // Exit here with no aggregation function.
  215. return;
  216. }
  217. // Group by nodetype to identify the right count.
  218. foreach ($view->result as $item) {
  219. $results[$item->node_type] = $item->nid;
  220. }
  221. $this->assertEqual($results[$type1->type], $values[0]);
  222. $this->assertEqual($results[$type2->type], $values[1]);
  223. }
  224. /**
  225. *
  226. */
  227. function viewsGroupByViewHelper($group_by = NULL) {
  228. $view = new view;
  229. $view->name = 'group_by_count';
  230. $view->description = '';
  231. $view->tag = '';
  232. $view->view_php = '';
  233. $view->base_table = 'node';
  234. $view->is_cacheable = FALSE;
  235. $view->api_version = 2;
  236. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  237. /* Display: Master */
  238. $handler = $view->new_display('default', 'Master', 'default');
  239. $handler->display->display_options['group_by'] = TRUE;
  240. $handler->display->display_options['access']['type'] = 'none';
  241. $handler->display->display_options['cache']['type'] = 'none';
  242. $handler->display->display_options['exposed_form']['type'] = 'basic';
  243. $handler->display->display_options['pager']['type'] = 'some';
  244. $handler->display->display_options['style_plugin'] = 'default';
  245. $handler->display->display_options['row_plugin'] = 'fields';
  246. // The test view has 2 fields ('nid' and 'type'). Don't add 'nid' when
  247. // having no aggregation function. We just want to aggregate on node type.
  248. if (!empty($group_by)) {
  249. /* Field: Content: Nid */
  250. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  251. $handler->display->display_options['fields']['nid']['table'] = 'node';
  252. $handler->display->display_options['fields']['nid']['field'] = 'nid';
  253. $handler->display->display_options['fields']['nid']['group_type'] = $group_by;
  254. $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
  255. $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
  256. $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
  257. $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
  258. $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
  259. $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
  260. $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
  261. $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
  262. $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
  263. $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
  264. }
  265. /* Field: Content: Type */
  266. $handler->display->display_options['fields']['type']['id'] = 'type';
  267. $handler->display->display_options['fields']['type']['table'] = 'node';
  268. $handler->display->display_options['fields']['type']['field'] = 'type';
  269. $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
  270. $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
  271. $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
  272. $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
  273. $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
  274. $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
  275. $handler->display->display_options['fields']['type']['alter']['html'] = 0;
  276. $handler->display->display_options['fields']['type']['hide_empty'] = 0;
  277. $handler->display->display_options['fields']['type']['empty_zero'] = 0;
  278. $handler->display->display_options['fields']['type']['link_to_node'] = 0;
  279. return $view;
  280. }
  281. /**
  282. *
  283. */
  284. public function testGroupByCount() {
  285. $this->GroupByTestHelper('count', array(4, 3));
  286. }
  287. /**
  288. *
  289. */
  290. public function testGroupBySum() {
  291. $this->GroupByTestHelper('sum', array(10, 18));
  292. }
  293. /**
  294. *
  295. */
  296. public function testGroupByAverage() {
  297. $this->GroupByTestHelper('avg', array(2.5, 6));
  298. }
  299. /**
  300. *
  301. */
  302. public function testGroupByMin() {
  303. $this->GroupByTestHelper('min', array(1, 5));
  304. }
  305. /**
  306. * {@inheritdoc}
  307. */
  308. public function testGroupByMax() {
  309. $this->GroupByTestHelper('max', array(4, 7));
  310. }
  311. /**
  312. *
  313. */
  314. public function testGroupByNone() {
  315. $this->GroupByTestHelper();
  316. }
  317. /**
  318. *
  319. */
  320. public function testGroupByCountOnlyFilters() {
  321. // Check if GROUP BY and HAVING are included when a view
  322. // Doesn't display SUM, COUNT, MAX... functions in SELECT statment
  323. $type1 = $this->drupalCreateContentType();
  324. $node_1 = array(
  325. 'type' => $type1->type,
  326. );
  327. for ($x = 0; $x < 10; $x++) {
  328. $this->drupalCreateNode($node_1);
  329. }
  330. $view = $this->viewsGroupByCountViewOnlyFilters();
  331. $output = $view->execute_display();
  332. $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), t('Make sure that GROUP BY is in the query'));
  333. $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), t('Make sure that HAVING is in the query'));
  334. }
  335. /**
  336. *
  337. */
  338. function viewsGroupByCountViewOnlyFilters() {
  339. $view = new view;
  340. $view->name = 'group_by_in_filters';
  341. $view->description = '';
  342. $view->tag = '';
  343. $view->view_php = '';
  344. $view->base_table = 'node';
  345. $view->is_cacheable = FALSE;
  346. $view->api_version = 2;
  347. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  348. /* Display: Master */
  349. $handler = $view->new_display('default', 'Master', 'default');
  350. $handler->display->display_options['group_by'] = TRUE;
  351. $handler->display->display_options['access']['type'] = 'none';
  352. $handler->display->display_options['cache']['type'] = 'none';
  353. $handler->display->display_options['exposed_form']['type'] = 'basic';
  354. $handler->display->display_options['pager']['type'] = 'some';
  355. $handler->display->display_options['style_plugin'] = 'default';
  356. $handler->display->display_options['row_plugin'] = 'fields';
  357. /* Field: Nodo: Tipo */
  358. $handler->display->display_options['fields']['type']['id'] = 'type';
  359. $handler->display->display_options['fields']['type']['table'] = 'node';
  360. $handler->display->display_options['fields']['type']['field'] = 'type';
  361. $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
  362. $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
  363. $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
  364. $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
  365. $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
  366. $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
  367. $handler->display->display_options['fields']['type']['alter']['html'] = 0;
  368. $handler->display->display_options['fields']['type']['hide_empty'] = 0;
  369. $handler->display->display_options['fields']['type']['empty_zero'] = 0;
  370. $handler->display->display_options['fields']['type']['link_to_node'] = 0;
  371. /* Filtrar: Nodo: Nid */
  372. $handler->display->display_options['filters']['nid']['id'] = 'nid';
  373. $handler->display->display_options['filters']['nid']['table'] = 'node';
  374. $handler->display->display_options['filters']['nid']['field'] = 'nid';
  375. $handler->display->display_options['filters']['nid']['group_type'] = 'count';
  376. $handler->display->display_options['filters']['nid']['operator'] = '>';
  377. $handler->display->display_options['filters']['nid']['value']['value'] = '3';
  378. return $view;
  379. }
  380. }
  381. /**
  382. * Tests UI of aggregate functionality.
  383. */
  384. class ViewsUiGroupbyTestCase extends DrupalWebTestCase {
  385. /**
  386. * {@inheritdoc}
  387. */
  388. function setUp() {
  389. // Enable views_ui.
  390. parent::setUp('views_ui', 'views_test');
  391. // Create and log in a user with administer views permission.
  392. $views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions'));
  393. $this->drupalLogin($views_admin);
  394. }
  395. /**
  396. * Test meta data.
  397. */
  398. public static function getInfo() {
  399. return array(
  400. 'name' => 'Groupby UI',
  401. 'description' => 'Tests UI of aggregate functionality.',
  402. 'group' => 'Views UI',
  403. );
  404. }
  405. /**
  406. * Tests whether basic saving works.
  407. *
  408. * @todo: this should check the change of the settings as well.
  409. */
  410. function testGroupBySave() {
  411. $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
  412. $edit = array(
  413. 'group_by' => TRUE,
  414. );
  415. $this->drupalPost('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by', $edit, t('Apply'));
  416. $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit');
  417. $this->drupalPost('admin/structure/views/view/test_views_groupby_save/edit', array(), t('Save'));
  418. $this->drupalGet('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by');
  419. }
  420. }