pagination.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Pagination class tests
  15. *
  16. * @group Core
  17. * @group Pagination
  18. */
  19. class Test_Pagination extends TestCase
  20. {
  21. protected function set_request($uri)
  22. {
  23. // set Request::$main
  24. $this->request = \Request::forge($uri);
  25. $rp = new \ReflectionProperty($this->request, 'main');
  26. $rp->setAccessible(true);
  27. $rp->setValue($this->request, $this->request);
  28. // set Request::$active
  29. $rp = new \ReflectionProperty($this->request, 'active');
  30. $rp->setAccessible(true);
  31. $rp->setValue($this->request, $this->request);
  32. }
  33. public function tearDown()
  34. {
  35. // reset Request::$main
  36. $request = \Request::forge();
  37. $rp = new \ReflectionProperty($request, 'main');
  38. $rp->setAccessible(true);
  39. $rp->setValue($request, false);
  40. // reset Request::$active
  41. $rp = new \ReflectionProperty($request, 'active');
  42. $rp->setAccessible(true);
  43. $rp->setValue($request, false);
  44. }
  45. /**********************************
  46. * Tests for URI Segment Pagination
  47. **********************************/
  48. protected function set_uri_segment_config()
  49. {
  50. $this->config = array(
  51. 'uri_segment' => 3,
  52. 'pagination_url' => 'http://docs.fuelphp.com/welcome/index/',
  53. 'total_items' => 100,
  54. 'per_page' => 10,
  55. );
  56. }
  57. public function test_uri_segment_auto_detect_pagination_url()
  58. {
  59. // set base_url
  60. Config::set('base_url', 'http://docs.fuelphp.com/');
  61. // set Request::$main & $active
  62. $this->set_request('welcome/index/5');
  63. $this->set_uri_segment_config();
  64. $this->config['pagination_url'] = null;
  65. $pagination = Pagination::forge(__METHOD__, $this->config);
  66. // set _make_link() accessible
  67. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  68. $_make_link->setAccessible(true);
  69. $test = $_make_link->invoke($pagination, 1);
  70. $expected = 'http://docs.fuelphp.com/welcome/index/1';
  71. $this->assertEquals($expected, $test);
  72. // reset base_url
  73. Config::set('base_url', null);
  74. }
  75. public function test_uri_segment_set_pagination_url_after_forging_fail()
  76. {
  77. // set Request::$main & $active
  78. $this->set_request('welcome/index/3');
  79. $this->set_uri_segment_config();
  80. $pagination = Pagination::forge(__METHOD__, $this->config);
  81. $pagination->pagination_url = 'http://example.com/page/';
  82. // set _make_link() accessible
  83. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  84. $_make_link->setAccessible(true);
  85. // not enough segments in the URI to add the page number
  86. $this->setExpectedException('RunTimeException');
  87. $test = $_make_link->invoke($pagination, 1);
  88. }
  89. public function test_uri_segment_set_pagination_url_after_forging_success()
  90. {
  91. // set Request::$main & $active
  92. $this->set_request('welcome/index/3');
  93. $this->set_uri_segment_config();
  94. $pagination = Pagination::forge(__METHOD__, $this->config);
  95. $pagination->pagination_url = 'http://example.com/this/page/';
  96. // set _make_link() accessible
  97. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  98. $_make_link->setAccessible(true);
  99. $test = $_make_link->invoke($pagination, 1);
  100. $expected = 'http://example.com/this/page/1';
  101. $this->assertEquals($expected, $test);
  102. }
  103. public function test_uri_segment_get_total_pages()
  104. {
  105. // set Request::$main & $active
  106. $this->set_request('welcome/index/');
  107. $this->set_uri_segment_config();
  108. $pagination = Pagination::forge(__METHOD__, $this->config);
  109. $test = $pagination->total_pages;
  110. $expected = 10;
  111. $this->assertEquals($expected, $test);
  112. }
  113. /**
  114. * first page
  115. *
  116. */
  117. public function test_uri_segment_first_page()
  118. {
  119. // set Request::$main & $active
  120. $this->set_request('welcome/index/');
  121. $this->set_uri_segment_config();
  122. $pagination = Pagination::forge(__METHOD__, $this->config);
  123. $output = $pagination->previous();
  124. $output = str_replace(array("\n", "\t"), "", $output);
  125. $expected = '<span class="previous-inactive"><a href="#">&laquo;</a></span>';
  126. $this->assertEquals($expected, $output);
  127. $output = $pagination->pages_render();
  128. $output = str_replace(array("\n", "\t"), "", $output);
  129. $expected = '<span class="active"><a href="#">1</a></span><span><a href="http://docs.fuelphp.com/welcome/index/2">2</a></span><span><a href="http://docs.fuelphp.com/welcome/index/3">3</a></span><span><a href="http://docs.fuelphp.com/welcome/index/4">4</a></span><span><a href="http://docs.fuelphp.com/welcome/index/5">5</a></span><span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span>';
  130. $this->assertEquals($expected, $output);
  131. $output = $pagination->next();
  132. $output = str_replace(array("\n", "\t"), "", $output);
  133. $expected = '<span class="next"><a href="http://docs.fuelphp.com/welcome/index/2">&raquo;</a></span>';
  134. $this->assertEquals($expected, $output);
  135. $output = $pagination->render();
  136. $output = str_replace(array("\n", "\t"), "", $output);
  137. $expected = '<div class="pagination"><span class="previous-inactive"><a href="#">&laquo;</a></span><span class="active"><a href="#">1</a></span><span><a href="http://docs.fuelphp.com/welcome/index/2">2</a></span><span><a href="http://docs.fuelphp.com/welcome/index/3">3</a></span><span><a href="http://docs.fuelphp.com/welcome/index/4">4</a></span><span><a href="http://docs.fuelphp.com/welcome/index/5">5</a></span><span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span><span class="next"><a href="http://docs.fuelphp.com/welcome/index/2">&raquo;</a></span></div>';
  138. $this->assertEquals($expected, $output);
  139. }
  140. /**
  141. * last page
  142. *
  143. */
  144. public function test_uri_segment_nextlink_inactive()
  145. {
  146. // set Request::$main & $active
  147. $this->set_request('welcome/index/10');
  148. $this->set_uri_segment_config();
  149. $pagination = Pagination::forge(__METHOD__, $this->config);
  150. $output = $pagination->next();
  151. $output = str_replace(array("\n", "\t"), "", $output);
  152. $expected = '<span class="next-inactive"><a href="#">&raquo;</a></span>';
  153. $this->assertEquals($expected, $output);
  154. $output = $pagination->pages_render();
  155. $output = str_replace(array("\n", "\t"), "", $output);
  156. $expected = '<span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span><span><a href="http://docs.fuelphp.com/welcome/index/7">7</a></span><span><a href="http://docs.fuelphp.com/welcome/index/8">8</a></span><span><a href="http://docs.fuelphp.com/welcome/index/9">9</a></span><span class="active"><a href="#">10</a></span>';
  157. $this->assertEquals($expected, $output);
  158. $output = $pagination->previous();
  159. $output = str_replace(array("\n", "\t"), "", $output);
  160. $expected = '<span class="previous"><a href="http://docs.fuelphp.com/welcome/index/9">&laquo;</a></span>';
  161. $this->assertEquals($expected, $output);
  162. $output = $pagination->render();
  163. $output = str_replace(array("\n", "\t"), "", $output);
  164. $expected = '<div class="pagination"><span class="previous"><a href="http://docs.fuelphp.com/welcome/index/9">&laquo;</a></span><span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span><span><a href="http://docs.fuelphp.com/welcome/index/7">7</a></span><span><a href="http://docs.fuelphp.com/welcome/index/8">8</a></span><span><a href="http://docs.fuelphp.com/welcome/index/9">9</a></span><span class="active"><a href="#">10</a></span><span class="next-inactive"><a href="#">&raquo;</a></span></div>';
  165. $this->assertEquals($expected, $output);
  166. }
  167. /**
  168. * total page is 1
  169. *
  170. */
  171. public function test_uri_segment_total_page_is_one()
  172. {
  173. // set Request::$main & $active
  174. $this->set_request('welcome/index/10');
  175. $this->set_uri_segment_config();
  176. $this->config['per_page'] = 1000;
  177. $pagination = Pagination::forge(__METHOD__, $this->config);
  178. $output = $pagination->pages_render();
  179. $output = str_replace(array("\n", "\t"), "", $output);
  180. $expected = '';
  181. $this->assertEquals($expected, $output);
  182. $output = $pagination->render();
  183. $output = str_replace(array("\n", "\t"), "", $output);
  184. $expected = '';
  185. $this->assertEquals($expected, $output);
  186. }
  187. public function test_uri_segment_make_link_with_no_query_string_ending_page_number()
  188. {
  189. // set Request::$main & $active
  190. $this->set_request('welcome/index/10');
  191. $this->set_uri_segment_config();
  192. $this->config['pagination_url'] = 'http://docs.fuelphp.com/welcome/index/55';
  193. $pagination = Pagination::forge(__METHOD__, $this->config);
  194. // set _make_link() accessible
  195. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  196. $_make_link->setAccessible(true);
  197. $test = $_make_link->invoke($pagination, 1);
  198. $expected = 'http://docs.fuelphp.com/welcome/index/1';
  199. $this->assertEquals($expected, $test);
  200. $test = $_make_link->invoke($pagination, 99);
  201. $expected = 'http://docs.fuelphp.com/welcome/index/99';
  202. $this->assertEquals($expected, $test);
  203. }
  204. public function test_uri_segment_make_link_with_no_query_string_ending_slash()
  205. {
  206. // set Request::$main & $active
  207. $this->set_request('welcome/index/');
  208. $this->set_uri_segment_config();
  209. $this->config['pagination_url'] = 'http://docs.fuelphp.com/welcome/index/';
  210. $pagination = Pagination::forge(__METHOD__, $this->config);
  211. // set _make_link() accessible
  212. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  213. $_make_link->setAccessible(true);
  214. $test = $_make_link->invoke($pagination, 1);
  215. $expected = 'http://docs.fuelphp.com/welcome/index/1';
  216. $this->assertEquals($expected, $test);
  217. $test = $_make_link->invoke($pagination, 99);
  218. $expected = 'http://docs.fuelphp.com/welcome/index/99';
  219. $this->assertEquals($expected, $test);
  220. }
  221. public function test_uri_segment_make_link_with_query_string_ending_page_number()
  222. {
  223. // set Request::$main & $active
  224. $this->set_request('welcome/index/55?foo=bar&fuel[]=php1&fuel[]=php2&');
  225. $this->set_uri_segment_config();
  226. // no define pagination_url
  227. $this->config['pagination_url'] = null;
  228. $pagination = Pagination::forge(__METHOD__, $this->config);
  229. // set _make_link() accessible
  230. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  231. $_make_link->setAccessible(true);
  232. $test = $_make_link->invoke($pagination, 1);
  233. $expected = '/welcome/index/1?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
  234. $this->assertEquals($expected, $test);
  235. $test = $_make_link->invoke($pagination, 99);
  236. $expected = '/welcome/index/99?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
  237. $this->assertEquals($expected, $test);
  238. }
  239. public function test_uri_segment_make_link_with_query_string_ending_slash()
  240. {
  241. // set Request::$main & $active
  242. $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&');
  243. $this->set_uri_segment_config();
  244. // no define pagination_url
  245. $this->config['pagination_url'] = null;
  246. $pagination = Pagination::forge(__METHOD__, $this->config);
  247. // set _make_link() accessible
  248. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  249. $_make_link->setAccessible(true);
  250. $test = $_make_link->invoke($pagination, 1);
  251. $expected = '/welcome/index/1?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
  252. $this->assertEquals($expected, $test);
  253. $test = $_make_link->invoke($pagination, 99);
  254. $expected = '/welcome/index/99?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
  255. $this->assertEquals($expected, $test);
  256. }
  257. /***********************************
  258. * Tests for Query String Pagination
  259. ***********************************/
  260. protected function set_query_string_config()
  261. {
  262. $this->config = array(
  263. 'uri_segment' => 'p',
  264. 'pagination_url' => 'http://docs.fuelphp.com/',
  265. 'total_items' => 100,
  266. 'per_page' => 10,
  267. );
  268. }
  269. public function test_query_string_auto_detect_pagination_url()
  270. {
  271. // set base_url
  272. Config::set('base_url', 'http://docs.fuelphp.com/');
  273. // set Request::$main & $active
  274. $this->set_request('/');
  275. $this->set_query_string_config();
  276. $this->config['pagination_url'] = null;
  277. $pagination = Pagination::forge(__METHOD__, $this->config);
  278. // set _make_link() accessible
  279. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  280. $_make_link->setAccessible(true);
  281. $test = $_make_link->invoke($pagination, 1);
  282. $expected = 'http://docs.fuelphp.com/?p=1';
  283. $this->assertEquals($expected, $test);
  284. // reset base_url
  285. Config::set('base_url', null);
  286. }
  287. public function test_query_string_get_total_pages()
  288. {
  289. // set Request::$main & $active
  290. $this->set_request('/');
  291. $this->set_query_string_config();
  292. $pagination = Pagination::forge(__METHOD__, $this->config);
  293. $test = $pagination->total_pages;
  294. $expected = 10;
  295. $this->assertEquals($expected, $test);
  296. }
  297. /**
  298. * first page
  299. *
  300. */
  301. public function test_query_string_first_page()
  302. {
  303. // set Request::$main & $active
  304. $this->set_request('/');
  305. $this->set_query_string_config();
  306. $pagination = Pagination::forge(__METHOD__, $this->config);
  307. $pagination->current_page = 1;
  308. $output = $pagination->previous();
  309. $output = str_replace(array("\n", "\t"), "", $output);
  310. $expected = '<span class="previous-inactive"><a href="#">&laquo;</a></span>';
  311. $this->assertEquals($expected, $output);
  312. $output = $pagination->pages_render();
  313. $output = str_replace(array("\n", "\t"), "", $output);
  314. $expected = '<span class="active"><a href="#">1</a></span><span><a href="http://docs.fuelphp.com/?p=2">2</a></span><span><a href="http://docs.fuelphp.com/?p=3">3</a></span><span><a href="http://docs.fuelphp.com/?p=4">4</a></span><span><a href="http://docs.fuelphp.com/?p=5">5</a></span><span><a href="http://docs.fuelphp.com/?p=6">6</a></span>';
  315. $this->assertEquals($expected, $output);
  316. $output = $pagination->next();
  317. $output = str_replace(array("\n", "\t"), "", $output);
  318. $expected = '<span class="next"><a href="http://docs.fuelphp.com/?p=2">&raquo;</a></span>';
  319. $this->assertEquals($expected, $output);
  320. }
  321. /**
  322. * last page
  323. *
  324. */
  325. public function test_query_string_nextlink_inactive()
  326. {
  327. // set Request::$main & $active
  328. $this->set_request('/');
  329. $this->set_query_string_config();
  330. $pagination = Pagination::forge(__METHOD__, $this->config);
  331. $pagination->current_page = 10;
  332. $output = $pagination->next();
  333. $output = str_replace(array("\n", "\t"), "", $output);
  334. $expected = '<span class="next-inactive"><a href="#">&raquo;</a></span>';
  335. $this->assertEquals($expected, $output);
  336. $output = $pagination->pages_render();
  337. $output = str_replace(array("\n", "\t"), "", $output);
  338. $expected = '<span><a href="http://docs.fuelphp.com/?p=6">6</a></span><span><a href="http://docs.fuelphp.com/?p=7">7</a></span><span><a href="http://docs.fuelphp.com/?p=8">8</a></span><span><a href="http://docs.fuelphp.com/?p=9">9</a></span><span class="active"><a href="#">10</a></span>';
  339. $this->assertEquals($expected, $output);
  340. $output = $pagination->previous();
  341. $output = str_replace(array("\n", "\t"), "", $output);
  342. $expected = '<span class="previous"><a href="http://docs.fuelphp.com/?p=9">&laquo;</a></span>';
  343. $this->assertEquals($expected, $output);
  344. }
  345. public function test_query_string_make_link_by_request()
  346. {
  347. // set Request::$main & $active
  348. $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&p=40');
  349. $this->set_query_string_config();
  350. $this->config['pagination_url'] = null;
  351. $pagination = Pagination::forge(__METHOD__, $this->config);
  352. // set _make_link() accessible
  353. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  354. $_make_link->setAccessible(true);
  355. $test = $_make_link->invoke($pagination, 1);
  356. $expected = 'welcome/index/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=1';
  357. $this->assertEquals($expected, $test);
  358. $test = $_make_link->invoke($pagination, 99);
  359. $expected = 'welcome/index/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=99';
  360. $this->assertEquals($expected, $test);
  361. }
  362. public function test_query_string_make_link_by_pagination_url()
  363. {
  364. // set Request::$main & $active
  365. $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&p=40');
  366. $this->set_query_string_config();
  367. $this->config['pagination_url'] = 'http://docs.fuelphp.com/?foo=bar&fuel[]=php1&fuel[]=php2';
  368. $pagination = Pagination::forge(__METHOD__, $this->config);
  369. // set _make_link() accessible
  370. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  371. $_make_link->setAccessible(true);
  372. $test = $_make_link->invoke($pagination, 1);
  373. $expected = 'http://docs.fuelphp.com/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=1';
  374. $this->assertEquals($expected, $test);
  375. $test = $_make_link->invoke($pagination, 99);
  376. $expected = 'http://docs.fuelphp.com/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=99';
  377. $this->assertEquals($expected, $test);
  378. }
  379. public function test_query_string_make_link_by_pagination_url_include_page_number()
  380. {
  381. // set Request::$main & $active
  382. $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&p=40');
  383. $this->set_query_string_config();
  384. $this->config['pagination_url'] = 'http://docs.fuelphp.com/?foo=bar&p=123&fuel[]=php1&fuel[]=php2';
  385. $pagination = Pagination::forge(__METHOD__, $this->config);
  386. // set _make_link() accessible
  387. $_make_link = new \ReflectionMethod($pagination, '_make_link');
  388. $_make_link->setAccessible(true);
  389. $test = $_make_link->invoke($pagination, 1);
  390. $expected = 'http://docs.fuelphp.com/?foo=bar&p=1&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
  391. $this->assertEquals($expected, $test);
  392. $test = $_make_link->invoke($pagination, 99);
  393. $expected = 'http://docs.fuelphp.com/?foo=bar&p=99&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
  394. $this->assertEquals($expected, $test);
  395. }
  396. }