views_pager.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsPagerTest.
  5. */
  6. /**
  7. * Tests the pluggable pager system.
  8. */
  9. class ViewsPagerTest extends ViewsSqlTest {
  10. /**
  11. *
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Pager',
  16. 'description' => 'Test the pluggable pager system',
  17. 'group' => 'Views Plugins',
  18. );
  19. }
  20. /**
  21. *
  22. */
  23. public function setUp() {
  24. parent::setUp('views', 'views_ui', 'views_test');
  25. }
  26. /**
  27. * Pagers was sometimes not stored.
  28. *
  29. * @see http://drupal.org/node/652712
  30. */
  31. public function testStorePagerSettings() {
  32. $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
  33. $this->drupalLogin($admin_user);
  34. // Test behaviour described in
  35. // http://drupal.org/node/652712#comment-2354918.
  36. $this->drupalGet('admin/structure/views/view/frontpage/edit');
  37. $edit = array(
  38. 'pager_options[items_per_page]' => 20,
  39. );
  40. $this->drupalPost('admin/structure/views/nojs/display/frontpage/default/pager_options', $edit, t('Apply'));
  41. $this->assertText('20 items');
  42. // Change type and check whether the type is new type is stored.
  43. $edit = array();
  44. $edit = array(
  45. 'pager[type]' => 'mini',
  46. );
  47. $this->drupalPost('admin/structure/views/nojs/display/frontpage/default/pager', $edit, t('Apply'));
  48. $this->drupalGet('admin/structure/views/view/frontpage/edit');
  49. $this->assertText('Mini', 'Changed pager plugin, should change some text');
  50. // Test behaviour described in http://drupal.org/node/652712#comment-2354400
  51. $view = $this->viewsStorePagerSettings();
  52. // Make it editable in the admin interface.
  53. $view->save();
  54. $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit');
  55. $edit = array();
  56. $edit = array(
  57. 'pager[type]' => 'full',
  58. );
  59. $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager', $edit, t('Apply'));
  60. $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit');
  61. $this->assertText('Full');
  62. $edit = array(
  63. 'pager_options[items_per_page]' => 20,
  64. );
  65. $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager_options', $edit, t('Apply'));
  66. $this->assertText('20 items');
  67. // Add new display and test the settings again, by override it.
  68. $edit = array();
  69. // Add a display and override the pager settings.
  70. $this->drupalPost('admin/structure/views/view/test_store_pager_settings/edit', $edit, t('Add Page'));
  71. $edit = array(
  72. 'override[dropdown]' => 'page_1',
  73. );
  74. $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/page_1/pager', $edit, t('Apply'));
  75. $edit = array(
  76. 'pager[type]' => 'mini',
  77. );
  78. $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/page_1/pager', $edit, t('Apply'));
  79. $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit');
  80. $this->assertText('Mini', 'Changed pager plugin, should change some text');
  81. $edit = array(
  82. 'pager_options[items_per_page]' => 10,
  83. );
  84. $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager_options', $edit, t('Apply'));
  85. $this->assertText('20 items');
  86. }
  87. /**
  88. *
  89. */
  90. public function viewsStorePagerSettings() {
  91. $view = new view();
  92. $view->name = 'test_store_pager_settings';
  93. $view->description = '';
  94. $view->tag = '';
  95. $view->view_php = '';
  96. $view->base_table = 'node';
  97. $view->is_cacheable = FALSE;
  98. $view->api_version = 3;
  99. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  100. /* Display: Master */
  101. $handler = $view->new_display('default', 'Master', 'default');
  102. $handler->display->display_options['access']['type'] = 'none';
  103. $handler->display->display_options['cache']['type'] = 'none';
  104. $handler->display->display_options['exposed_form']['type'] = 'basic';
  105. $handler->display->display_options['pager']['type'] = 'none';
  106. $handler->display->display_options['style_plugin'] = 'default';
  107. $handler->display->display_options['row_plugin'] = 'node';
  108. return $view;
  109. }
  110. /**
  111. * Tests the none-pager-query.
  112. */
  113. public function testNoLimit() {
  114. // Create 11 nodes and make sure that everyone is returned. We create 11
  115. // nodes, because the default pager plugin had 10 items per page.
  116. for ($i = 0; $i < 11; $i++) {
  117. $this->drupalCreateNode();
  118. }
  119. $view = $this->viewsPagerNoLimit();
  120. $view->set_display('default');
  121. $this->executeView($view);
  122. $this->assertEqual(count($view->result), 11, 'Make sure that every item is returned in the result');
  123. $view->destroy();
  124. // Setup and test a offset.
  125. $view = $this->viewsPagerNoLimit();
  126. $view->set_display('default');
  127. $pager = array(
  128. 'type' => 'none',
  129. 'options' => array(
  130. 'offset' => 3,
  131. ),
  132. );
  133. $view->display_handler->set_option('pager', $pager);
  134. $this->executeView($view);
  135. $this->assertEqual(count($view->result), 8, 'Make sure that every item beside the first three is returned in the result');
  136. // Check some public functions.
  137. $this->assertFalse($view->query->pager->use_pager());
  138. $this->assertFalse($view->query->pager->use_count_query());
  139. $this->assertEqual($view->query->pager->get_items_per_page(), 0);
  140. }
  141. /**
  142. *
  143. */
  144. public function viewsPagerNoLimit() {
  145. $view = new view();
  146. $view->name = 'test_pager_none';
  147. $view->description = '';
  148. $view->tag = '';
  149. $view->view_php = '';
  150. $view->base_table = 'node';
  151. $view->is_cacheable = FALSE;
  152. $view->api_version = 3;
  153. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  154. /* Display: Master */
  155. $handler = $view->new_display('default', 'Master', 'default');
  156. $handler->display->display_options['access']['type'] = 'none';
  157. $handler->display->display_options['cache']['type'] = 'none';
  158. $handler->display->display_options['exposed_form']['type'] = 'basic';
  159. $handler->display->display_options['pager']['type'] = 'none';
  160. $handler->display->display_options['style_plugin'] = 'default';
  161. $handler->display->display_options['row_plugin'] = 'node';
  162. return $view;
  163. }
  164. /**
  165. *
  166. */
  167. public function testViewTotalRowsWithoutPager() {
  168. $this->createNodes(23);
  169. $view = $this->viewsPagerNoLimit();
  170. $view->get_total_rows = TRUE;
  171. $view->set_display('default');
  172. $this->executeView($view);
  173. $this->assertEqual($view->total_rows, 23, "'total_rows' is calculated when pager type is 'none' and 'get_total_rows' is TRUE.");
  174. }
  175. /**
  176. *
  177. */
  178. public function createNodes($count) {
  179. if ($count >= 0) {
  180. for ($i = 0; $i < $count; $i++) {
  181. $this->drupalCreateNode();
  182. }
  183. }
  184. }
  185. /**
  186. * Tests the some pager plugin.
  187. */
  188. public function testLimit() {
  189. // Create 11 nodes and make sure that everyone is returned. We create 11
  190. // nodes, because the default pager plugin had 10 items per page.
  191. for ($i = 0; $i < 11; $i++) {
  192. $this->drupalCreateNode();
  193. }
  194. $view = $this->viewsPagerLimit();
  195. $view->set_display('default');
  196. $this->executeView($view);
  197. $this->assertEqual(count($view->result), 5, 'Make sure that only a certain count of items is returned');
  198. $view->destroy();
  199. // Setup and test a offset.
  200. $view = $this->viewsPagerLimit();
  201. $view->set_display('default');
  202. $pager = array(
  203. 'type' => 'none',
  204. 'options' => array(
  205. 'offset' => 8,
  206. 'items_per_page' => 5,
  207. ),
  208. );
  209. $view->display_handler->set_option('pager', $pager);
  210. $this->executeView($view);
  211. $this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned');
  212. // Check some public functions.
  213. $this->assertFalse($view->query->pager->use_pager());
  214. $this->assertFalse($view->query->pager->use_count_query());
  215. }
  216. /**
  217. *
  218. */
  219. public function viewsPagerLimit() {
  220. $view = new view();
  221. $view->name = 'test_pager_some';
  222. $view->description = '';
  223. $view->tag = '';
  224. $view->view_php = '';
  225. $view->base_table = 'node';
  226. $view->is_cacheable = FALSE;
  227. $view->api_version = 3;
  228. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  229. /* Display: Master */
  230. $handler = $view->new_display('default', 'Master', 'default');
  231. $handler->display->display_options['access']['type'] = 'none';
  232. $handler->display->display_options['cache']['type'] = 'none';
  233. $handler->display->display_options['exposed_form']['type'] = 'basic';
  234. $handler->display->display_options['pager']['type'] = 'some';
  235. $handler->display->display_options['pager']['options']['offset'] = 0;
  236. $handler->display->display_options['pager']['options']['items_per_page'] = 5;
  237. $handler->display->display_options['style_plugin'] = 'default';
  238. $handler->display->display_options['row_plugin'] = 'node';
  239. return $view;
  240. }
  241. /**
  242. * Tests the normal pager.
  243. */
  244. public function testNormalPager() {
  245. // Create 11 nodes and make sure that everyone is returned. We create 11
  246. // nodes, because the default pager plugin had 10 items per page.
  247. for ($i = 0; $i < 11; $i++) {
  248. $this->drupalCreateNode();
  249. }
  250. $view = $this->viewsPagerFull();
  251. $view->set_display('default');
  252. $this->executeView($view);
  253. $this->assertEqual(count($view->result), 5, 'Make sure that only a certain count of items is returned');
  254. $view->destroy();
  255. // Setup and test a offset.
  256. $view = $this->viewsPagerFull();
  257. $view->set_display('default');
  258. $pager = array(
  259. 'type' => 'full',
  260. 'options' => array(
  261. 'offset' => 8,
  262. 'items_per_page' => 5,
  263. ),
  264. );
  265. $view->display_handler->set_option('pager', $pager);
  266. $this->executeView($view);
  267. $this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned');
  268. // Test items per page = 0
  269. $view = $this->viewPagerFullZeroItemsPerPage();
  270. $view->set_display('default');
  271. $this->executeView($view);
  272. $this->assertEqual(count($view->result), 11, 'All items are return');
  273. // @todo test number of pages.
  274. // Test items per page = 0.
  275. $view->destroy();
  276. // Setup and test a offset.
  277. $view = $this->viewsPagerFull();
  278. $view->set_display('default');
  279. $pager = array(
  280. 'type' => 'full',
  281. 'options' => array(
  282. 'offset' => 0,
  283. 'items_per_page' => 0,
  284. ),
  285. );
  286. $view->display_handler->set_option('pager', $pager);
  287. $this->executeView($view);
  288. $this->assertEqual($view->query->pager->get_items_per_page(), 0);
  289. $this->assertEqual(count($view->result), 11);
  290. }
  291. /**
  292. *
  293. */
  294. function viewPagerFullZeroItemsPerPage() {
  295. $view = new view();
  296. $view->name = 'view_pager_full_zero_items_per_page';
  297. $view->description = '';
  298. $view->tag = '';
  299. $view->view_php = '';
  300. $view->base_table = 'node';
  301. $view->is_cacheable = FALSE;
  302. $view->api_version = 3;
  303. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  304. /* Display: Master */
  305. $handler = $view->new_display('default', 'Master', 'default');
  306. $handler->display->display_options['access']['type'] = 'none';
  307. $handler->display->display_options['cache']['type'] = 'none';
  308. $handler->display->display_options['exposed_form']['type'] = 'basic';
  309. $handler->display->display_options['pager']['type'] = 'full';
  310. $handler->display->display_options['pager']['options']['items_per_page'] = '0';
  311. $handler->display->display_options['pager']['options']['offset'] = '0';
  312. $handler->display->display_options['pager']['options']['id'] = '0';
  313. $handler->display->display_options['style_plugin'] = 'default';
  314. $handler->display->display_options['row_plugin'] = 'fields';
  315. /* Field: Content: Title */
  316. $handler->display->display_options['fields']['title']['id'] = 'title';
  317. $handler->display->display_options['fields']['title']['table'] = 'node';
  318. $handler->display->display_options['fields']['title']['field'] = 'title';
  319. $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
  320. $handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
  321. $handler->display->display_options['fields']['title']['alter']['trim'] = 0;
  322. $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1;
  323. $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1;
  324. $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
  325. $handler->display->display_options['fields']['title']['alter']['html'] = 0;
  326. $handler->display->display_options['fields']['title']['hide_empty'] = 0;
  327. $handler->display->display_options['fields']['title']['empty_zero'] = 0;
  328. $handler->display->display_options['fields']['title']['link_to_node'] = 0;
  329. return $view;
  330. }
  331. /**
  332. *
  333. */
  334. function viewsPagerFull() {
  335. $view = new view();
  336. $view->name = 'test_pager_full';
  337. $view->description = '';
  338. $view->tag = '';
  339. $view->view_php = '';
  340. $view->base_table = 'node';
  341. $view->is_cacheable = FALSE;
  342. $view->api_version = 3;
  343. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  344. /* Display: Master */
  345. $handler = $view->new_display('default', 'Master', 'default');
  346. $handler->display->display_options['access']['type'] = 'none';
  347. $handler->display->display_options['cache']['type'] = 'none';
  348. $handler->display->display_options['exposed_form']['type'] = 'basic';
  349. $handler->display->display_options['pager']['type'] = 'full';
  350. $handler->display->display_options['pager']['options']['items_per_page'] = '5';
  351. $handler->display->display_options['pager']['options']['offset'] = '0';
  352. $handler->display->display_options['pager']['options']['id'] = '0';
  353. $handler->display->display_options['style_plugin'] = 'default';
  354. $handler->display->display_options['row_plugin'] = 'node';
  355. return $view;
  356. }
  357. /**
  358. *
  359. */
  360. function viewsPagerFullFields() {
  361. $view = new view();
  362. $view->name = 'test_pager_full';
  363. $view->description = '';
  364. $view->tag = '';
  365. $view->view_php = '';
  366. $view->base_table = 'node';
  367. $view->is_cacheable = FALSE;
  368. $view->api_version = 3;
  369. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  370. /* Display: Master */
  371. $handler = $view->new_display('default', 'Master', 'default');
  372. $handler->display->display_options['access']['type'] = 'none';
  373. $handler->display->display_options['cache']['type'] = 'none';
  374. $handler->display->display_options['exposed_form']['type'] = 'basic';
  375. $handler->display->display_options['pager']['type'] = 'full';
  376. $handler->display->display_options['pager']['options']['items_per_page'] = '5';
  377. $handler->display->display_options['pager']['options']['offset'] = '0';
  378. $handler->display->display_options['pager']['options']['id'] = '0';
  379. $handler->display->display_options['style_plugin'] = 'default';
  380. $handler->display->display_options['row_plugin'] = 'fields';
  381. $handler->display->display_options['fields']['title']['id'] = 'title';
  382. $handler->display->display_options['fields']['title']['table'] = 'node';
  383. $handler->display->display_options['fields']['title']['field'] = 'title';
  384. $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
  385. $handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
  386. $handler->display->display_options['fields']['title']['alter']['trim'] = 0;
  387. $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1;
  388. $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1;
  389. $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
  390. $handler->display->display_options['fields']['title']['alter']['html'] = 0;
  391. $handler->display->display_options['fields']['title']['hide_empty'] = 0;
  392. $handler->display->display_options['fields']['title']['empty_zero'] = 0;
  393. $handler->display->display_options['fields']['title']['link_to_node'] = 0;
  394. return $view;
  395. }
  396. /**
  397. * Tests the minipager.
  398. */
  399. public function testMiniPager() {
  400. // The functionality is the same as the normal pager, so i don't know what
  401. // to test here.
  402. }
  403. /**
  404. * Tests rendering with NULL pager.
  405. */
  406. public function testRenderNullPager() {
  407. // Create 11 nodes and make sure that everyone is returned. We create 11
  408. // nodes, because the default pager plugin had 10 items per page.
  409. for ($i = 0; $i < 11; $i++) {
  410. $this->drupalCreateNode();
  411. }
  412. $view = $this->viewsPagerFullFields();
  413. $view->set_display('default');
  414. $this->executeView($view);
  415. $view->use_ajax = TRUE;
  416. // force the value again here.
  417. $view->query->pager = NULL;
  418. $output = $view->render();
  419. $this->assertEqual(preg_match('/<ul class="pager">/', $output), 0, t('The pager is not rendered.'));
  420. }
  421. /**
  422. * Test the api functions on the view object.
  423. */
  424. function testPagerApi() {
  425. $view = $this->viewsPagerFull();
  426. // On the first round don't initialize the pager.
  427. $this->assertEqual($view->get_items_per_page(), NULL, 'If the pager is not initialized and no manual override there is no items per page.');
  428. $rand_number = rand(1, 5);
  429. $view->set_items_per_page($rand_number);
  430. $this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
  431. $this->assertEqual($view->get_offset(), NULL, 'If the pager is not initialized and no manual override there is no offset.');
  432. $rand_number = rand(1, 5);
  433. $view->set_offset($rand_number);
  434. $this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
  435. $this->assertEqual($view->get_current_page(), NULL, 'If the pager is not initialized and no manual override there is no current page.');
  436. $rand_number = rand(1, 5);
  437. $view->set_current_page($rand_number);
  438. $this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
  439. $view->destroy();
  440. // On this round enable the pager.
  441. $view->init_display();
  442. $view->init_query();
  443. $view->init_pager();
  444. $this->assertEqual($view->get_items_per_page(), 5, 'Per default the view has 5 items per page.');
  445. $rand_number = rand(1, 5);
  446. $view->set_items_per_page($rand_number);
  447. $rand_number = rand(6, 11);
  448. $view->query->pager->set_items_per_page($rand_number);
  449. $this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
  450. $this->assertEqual($view->get_offset(), 0, 'Per default a view has a 0 offset.');
  451. $rand_number = rand(1, 5);
  452. $view->set_offset($rand_number);
  453. $rand_number = rand(6, 11);
  454. $view->query->pager->set_offset($rand_number);
  455. $this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
  456. $this->assertEqual($view->get_current_page(), 0, 'Per default the current page is 0.');
  457. $rand_number = rand(1, 5);
  458. $view->set_current_page($rand_number);
  459. $rand_number = rand(6, 11);
  460. $view->query->pager->set_current_page($rand_number);
  461. $this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
  462. }
  463. }