PaginatorHelperTest.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Core\Configure;
  19. use Cake\Network\Request;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper\PaginatorHelper;
  23. use Cake\View\View;
  24. /**
  25. * PaginatorHelperTest class
  26. *
  27. */
  28. class PaginatorHelperTest extends TestCase {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. Configure::write('Config.language', 'eng');
  37. $controller = null;
  38. $this->View = new View($controller);
  39. $this->Paginator = new PaginatorHelper($this->View);
  40. $this->Paginator->Js = $this->getMock('Cake\View\Helper\PaginatorHelper', array(), array($this->View));
  41. $this->Paginator->request = new Request();
  42. $this->Paginator->request->addParams(array(
  43. 'paging' => array(
  44. 'Article' => array(
  45. 'page' => 1,
  46. 'current' => 9,
  47. 'count' => 62,
  48. 'prevPage' => false,
  49. 'nextPage' => true,
  50. 'pageCount' => 7,
  51. 'sort' => null,
  52. 'direction' => null,
  53. 'limit' => null,
  54. )
  55. )
  56. ));
  57. Configure::write('Routing.prefixes', array());
  58. Router::reload();
  59. Router::connect('/:controller/:action/*');
  60. Router::connect('/:plugin/:controller/:action/*');
  61. }
  62. /**
  63. * tearDown method
  64. *
  65. * @return void
  66. */
  67. public function tearDown() {
  68. parent::tearDown();
  69. unset($this->View, $this->Paginator);
  70. }
  71. /**
  72. * Test the templates method.
  73. *
  74. * @return void
  75. */
  76. public function testTemplates() {
  77. $result = $this->Paginator->templates([
  78. 'test' => 'val'
  79. ]);
  80. $this->assertNull($result, 'Setting should return null');
  81. $result = $this->Paginator->templates();
  82. $this->assertArrayHasKey('test', $result);
  83. $this->assertEquals('val', $result['test']);
  84. $this->assertEquals('val', $this->Paginator->templates('test'));
  85. }
  86. /**
  87. * testHasPrevious method
  88. *
  89. * @return void
  90. */
  91. public function testHasPrevious() {
  92. $this->assertFalse($this->Paginator->hasPrev());
  93. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  94. $this->assertTrue($this->Paginator->hasPrev());
  95. $this->Paginator->request->params['paging']['Article']['prevPage'] = false;
  96. }
  97. /**
  98. * testHasNext method
  99. *
  100. * @return void
  101. */
  102. public function testHasNext() {
  103. $this->assertTrue($this->Paginator->hasNext());
  104. $this->Paginator->request->params['paging']['Article']['nextPage'] = false;
  105. $this->assertFalse($this->Paginator->hasNext());
  106. $this->Paginator->request->params['paging']['Article']['nextPage'] = true;
  107. }
  108. /**
  109. * testSortLinks method
  110. *
  111. * @return void
  112. */
  113. public function testSortLinks() {
  114. Router::setRequestInfo(array(
  115. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'url' => array('url' => 'accounts/')),
  116. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  117. ));
  118. $this->Paginator->options(array('url' => array('param')));
  119. $this->Paginator->request['paging'] = array(
  120. 'Article' => array(
  121. 'current' => 9,
  122. 'count' => 62,
  123. 'prevPage' => false,
  124. 'nextPage' => true,
  125. 'pageCount' => 7,
  126. 'sort' => 'date',
  127. 'direction' => 'asc',
  128. 'page' => 1,
  129. )
  130. );
  131. $result = $this->Paginator->sort('title');
  132. $expected = array(
  133. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=asc'),
  134. 'Title',
  135. '/a'
  136. );
  137. $this->assertTags($result, $expected);
  138. $result = $this->Paginator->sort('date');
  139. $expected = array(
  140. 'a' => array('href' => '/accounts/index/param?sort=date&amp;direction=desc', 'class' => 'asc'),
  141. 'Date',
  142. '/a'
  143. );
  144. $this->assertTags($result, $expected);
  145. $result = $this->Paginator->sort('title', 'TestTitle');
  146. $expected = array(
  147. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=asc'),
  148. 'TestTitle',
  149. '/a'
  150. );
  151. $this->assertTags($result, $expected);
  152. $result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'));
  153. $expected = array(
  154. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=asc'),
  155. 'ascending',
  156. '/a'
  157. );
  158. $this->assertTags($result, $expected);
  159. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  160. $result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'));
  161. $expected = array(
  162. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'),
  163. 'descending',
  164. '/a'
  165. );
  166. $this->assertTags($result, $expected);
  167. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  168. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  169. $result = $this->Paginator->sort('title');
  170. $expected = array(
  171. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=asc', 'class' => 'desc'),
  172. 'Title',
  173. '/a'
  174. );
  175. $this->assertTags($result, $expected);
  176. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  177. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  178. $result = $this->Paginator->sort('title');
  179. $expected = array(
  180. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'),
  181. 'Title',
  182. '/a'
  183. );
  184. $this->assertTags($result, $expected);
  185. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  186. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  187. $result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc'));
  188. $expected = array(
  189. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=asc', 'class' => 'desc'),
  190. 'Title',
  191. '/a'
  192. );
  193. $this->assertTags($result, $expected);
  194. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  195. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  196. $result = $this->Paginator->sort('title', 'Title', array('direction' => 'asc'));
  197. $expected = array(
  198. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=asc', 'class' => 'desc'),
  199. 'Title',
  200. '/a'
  201. );
  202. $this->assertTags($result, $expected);
  203. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  204. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  205. $result = $this->Paginator->sort('title', 'Title', array('direction' => 'asc'));
  206. $expected = array(
  207. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'),
  208. 'Title',
  209. '/a'
  210. );
  211. $this->assertTags($result, $expected);
  212. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  213. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  214. $result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc'));
  215. $expected = array(
  216. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'),
  217. 'Title',
  218. '/a'
  219. );
  220. $this->assertTags($result, $expected);
  221. }
  222. /**
  223. * test sort() with escape option
  224. */
  225. public function testSortEscape() {
  226. $result = $this->Paginator->sort('title', 'TestTitle >');
  227. $expected = array(
  228. 'a' => array('href' => '/index?sort=title&amp;direction=asc'),
  229. 'TestTitle &gt;',
  230. '/a'
  231. );
  232. $this->assertTags($result, $expected);
  233. $result = $this->Paginator->sort('title', 'TestTitle >', ['escape' => true]);
  234. $this->assertTags($result, $expected);
  235. $result = $this->Paginator->sort('title', 'TestTitle >', ['escape' => false]);
  236. $expected = array(
  237. 'a' => array('href' => '/index?sort=title&amp;direction=asc'),
  238. 'TestTitle >',
  239. '/a'
  240. );
  241. $this->assertTags($result, $expected);
  242. }
  243. /**
  244. * test that sort() works with virtual field order options.
  245. *
  246. * @return void
  247. */
  248. public function testSortLinkWithVirtualField() {
  249. Router::setRequestInfo(array(
  250. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
  251. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  252. ));
  253. $this->Paginator->request->params['paging']['Article']['sort'] = 'full_name';
  254. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  255. $result = $this->Paginator->sort('Article.full_name');
  256. $expected = array(
  257. 'a' => array('href' => '/accounts/index?sort=Article.full_name&amp;direction=desc', 'class' => 'asc'),
  258. 'Article Full Name',
  259. '/a'
  260. );
  261. $this->assertTags($result, $expected);
  262. $result = $this->Paginator->sort('full_name');
  263. $expected = array(
  264. 'a' => array('href' => '/accounts/index?sort=full_name&amp;direction=desc', 'class' => 'asc'),
  265. 'Full Name',
  266. '/a'
  267. );
  268. $this->assertTags($result, $expected);
  269. $this->Paginator->request->params['paging']['Article']['sort'] = 'full_name';
  270. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  271. $result = $this->Paginator->sort('Article.full_name');
  272. $expected = array(
  273. 'a' => array('href' => '/accounts/index?sort=Article.full_name&amp;direction=asc', 'class' => 'desc'),
  274. 'Article Full Name',
  275. '/a'
  276. );
  277. $this->assertTags($result, $expected);
  278. $result = $this->Paginator->sort('full_name');
  279. $expected = array(
  280. 'a' => array('href' => '/accounts/index?sort=full_name&amp;direction=asc', 'class' => 'desc'),
  281. 'Full Name',
  282. '/a'
  283. );
  284. $this->assertTags($result, $expected);
  285. }
  286. /**
  287. * testSortLinksUsingDirectionOption method
  288. *
  289. * @return void
  290. */
  291. public function testSortLinksUsingDirectionOption() {
  292. Router::setRequestInfo(array(
  293. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index',
  294. 'url' => array('url' => 'accounts/', 'mod_rewrite' => 'true')),
  295. array('base' => '/', 'here' => '/accounts/', 'webroot' => '/')
  296. ));
  297. $this->Paginator->options(array('url' => array('param')));
  298. $result = $this->Paginator->sort('title', 'TestTitle', array('direction' => 'desc'));
  299. $expected = array(
  300. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=desc'),
  301. 'TestTitle',
  302. '/a'
  303. );
  304. $this->assertTags($result, $expected);
  305. $result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'), array('direction' => 'desc'));
  306. $expected = array(
  307. 'a' => array('href' => '/accounts/index/param?sort=title&amp;direction=desc'),
  308. 'descending',
  309. '/a'
  310. );
  311. $this->assertTags($result, $expected);
  312. }
  313. /**
  314. * testSortLinksUsingDotNotation method
  315. *
  316. * @return void
  317. */
  318. public function testSortLinksUsingDotNotation() {
  319. Router::setRequestInfo(array(
  320. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array()),
  321. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  322. ));
  323. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  324. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  325. $result = $this->Paginator->sort('Article.title');
  326. $expected = array(
  327. 'a' => array('href' => '/accounts/index?sort=Article.title&amp;direction=asc', 'class' => 'desc'),
  328. 'Article Title',
  329. '/a'
  330. );
  331. $this->assertTags($result, $expected);
  332. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  333. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  334. $result = $this->Paginator->sort('Article.title', 'Title');
  335. $expected = array(
  336. 'a' => array('href' => '/accounts/index?sort=Article.title&amp;direction=asc', 'class' => 'desc'),
  337. 'Title',
  338. '/a'
  339. );
  340. $this->assertTags($result, $expected);
  341. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  342. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  343. $result = $this->Paginator->sort('Article.title', 'Title');
  344. $expected = array(
  345. 'a' => array('href' => '/accounts/index?sort=Article.title&amp;direction=desc', 'class' => 'asc'),
  346. 'Title',
  347. '/a'
  348. );
  349. $this->assertTags($result, $expected);
  350. $this->Paginator->request->params['paging']['Article']['sort'] = 'Account.title';
  351. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  352. $result = $this->Paginator->sort('title');
  353. $expected = array(
  354. 'a' => array('href' => '/accounts/index?sort=title&amp;direction=asc'),
  355. 'Title',
  356. '/a'
  357. );
  358. $this->assertTags($result, $expected);
  359. }
  360. /**
  361. * testSortKey method
  362. *
  363. * @return void
  364. */
  365. public function testSortKey() {
  366. $result = $this->Paginator->sortKey('Article', array('sort' => 'Article.title'));
  367. $this->assertEquals('Article.title', $result);
  368. $result = $this->Paginator->sortKey('Article', array('sort' => 'Article'));
  369. $this->assertEquals('Article', $result);
  370. }
  371. /**
  372. * Test that sortKey falls back to the default sorting options set
  373. * in the $params which are the default pagination options.
  374. *
  375. * @return void
  376. */
  377. public function testSortKeyFallbackToParams() {
  378. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  379. $result = $this->Paginator->sortKey();
  380. $this->assertEquals('Article.body', $result);
  381. $result = $this->Paginator->sortKey('Article');
  382. $this->assertEquals('Article.body', $result);
  383. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  384. $this->Paginator->request->params['paging']['Article']['order'] = 'DESC';
  385. $result = $this->Paginator->sortKey();
  386. $this->assertEquals('Article.body', $result);
  387. $result = $this->Paginator->sortKey('Article');
  388. $this->assertEquals('Article.body', $result);
  389. }
  390. /**
  391. * testSortDir method
  392. *
  393. * @return void
  394. */
  395. public function testSortDir() {
  396. $result = $this->Paginator->sortDir();
  397. $expected = 'asc';
  398. $this->assertEquals($expected, $result);
  399. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  400. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  401. $result = $this->Paginator->sortDir();
  402. $this->assertEquals('desc', $result);
  403. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  404. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  405. $result = $this->Paginator->sortDir();
  406. $this->assertEquals('asc', $result);
  407. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  408. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  409. $result = $this->Paginator->sortDir();
  410. $this->assertEquals('desc', $result);
  411. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  412. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  413. $result = $this->Paginator->sortDir();
  414. $this->assertEquals('asc', $result);
  415. unset($this->Paginator->request->params['paging']['Article']['direction']);
  416. $result = $this->Paginator->sortDir('Article', array('direction' => 'asc'));
  417. $this->assertEquals('asc', $result);
  418. $result = $this->Paginator->sortDir('Article', array('direction' => 'desc'));
  419. $this->assertEquals('desc', $result);
  420. $result = $this->Paginator->sortDir('Article', array('direction' => 'asc'));
  421. $this->assertEquals('asc', $result);
  422. }
  423. /**
  424. * Test that sortDir falls back to the default sorting options set
  425. * in the $params which are the default pagination options.
  426. *
  427. * @return void
  428. */
  429. public function testSortDirFallbackToParams() {
  430. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  431. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  432. $result = $this->Paginator->sortDir();
  433. $this->assertEquals('asc', $result);
  434. $result = $this->Paginator->sortDir('Article');
  435. $this->assertEquals('asc', $result);
  436. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  437. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  438. $result = $this->Paginator->sortDir();
  439. $this->assertEquals('desc', $result);
  440. $result = $this->Paginator->sortDir('Article');
  441. $this->assertEquals('desc', $result);
  442. }
  443. /**
  444. * testSortAdminLinks method
  445. *
  446. * @return void
  447. */
  448. public function testSortAdminLinks() {
  449. Configure::write('Routing.prefixes', array('admin'));
  450. Router::reload();
  451. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  452. Router::setRequestInfo(array(
  453. array('controller' => 'users', 'plugin' => null, 'action' => 'index', 'prefix' => 'admin'),
  454. array('base' => '', 'here' => '/admin/users', 'webroot' => '/')
  455. ));
  456. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  457. $result = $this->Paginator->next('Next');
  458. $expected = array(
  459. 'li' => array('class' => 'next'),
  460. 'a' => array('href' => '/admin/users/index?page=2', 'rel' => 'next'),
  461. 'Next',
  462. '/a',
  463. '/li'
  464. );
  465. $this->assertTags($result, $expected);
  466. $this->Paginator->options(array('url' => array('param')));
  467. $result = $this->Paginator->sort('title');
  468. $expected = array(
  469. 'a' => array('href' => '/admin/users/index/param?sort=title&amp;direction=asc'),
  470. 'Title',
  471. '/a'
  472. );
  473. $this->assertTags($result, $expected);
  474. $this->Paginator->options(array('url' => array('param')));
  475. $result = $this->Paginator->sort('Article.title', 'Title');
  476. $expected = array(
  477. 'a' => array('href' => '/admin/users/index/param?sort=Article.title&amp;direction=asc'),
  478. 'Title',
  479. '/a'
  480. );
  481. $this->assertTags($result, $expected);
  482. }
  483. /**
  484. * testUrlGeneration method
  485. *
  486. * @return void
  487. */
  488. public function testUrlGeneration() {
  489. $result = $this->Paginator->sort('controller');
  490. $expected = array(
  491. 'a' => array('href' => '/index?sort=controller&amp;direction=asc'),
  492. 'Controller',
  493. '/a'
  494. );
  495. $this->assertTags($result, $expected);
  496. $result = $this->Paginator->url();
  497. $this->assertEquals('/index', $result);
  498. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  499. $result = $this->Paginator->url();
  500. $this->assertEquals('/index?page=2', $result);
  501. $options = array('sort' => 'Article', 'direction' => 'desc');
  502. $result = $this->Paginator->url($options);
  503. $this->assertEquals('/index?page=2&amp;sort=Article&amp;direction=desc', $result);
  504. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  505. $options = array('sort' => 'Article.name', 'direction' => 'desc');
  506. $result = $this->Paginator->url($options);
  507. $this->assertEquals('/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
  508. }
  509. /**
  510. * test URL generation with prefix routes
  511. *
  512. * @return void
  513. */
  514. public function testUrlGenerationWithPrefixes() {
  515. Configure::write('Routing.prefixes', array('members'));
  516. Router::reload();
  517. Router::connect('/members/:controller/:action/*', array('prefix' => 'members'));
  518. Router::connect('/:controller/:action/*');
  519. Router::setRequestInfo(array(
  520. array('controller' => 'posts', 'action' => 'index', 'plugin' => null),
  521. array('base' => '', 'here' => 'posts/index', 'webroot' => '/')
  522. ));
  523. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  524. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  525. $options = array('prefix' => 'members');
  526. $result = $this->Paginator->url($options);
  527. $expected = '/members/posts/index?page=2';
  528. $this->assertEquals($expected, $result);
  529. $result = $this->Paginator->sort('name', null, array('url' => $options));
  530. $expected = array(
  531. 'a' => array('href' => '/members/posts/index?page=2&amp;sort=name&amp;direction=asc'),
  532. 'Name',
  533. '/a'
  534. );
  535. $this->assertTags($result, $expected);
  536. $result = $this->Paginator->next('next', array('url' => $options));
  537. $expected = array(
  538. 'li' => array('class' => 'next'),
  539. 'a' => array('href' => '/members/posts/index?page=3', 'rel' => 'next'),
  540. 'next',
  541. '/a',
  542. '/li'
  543. );
  544. $this->assertTags($result, $expected);
  545. $result = $this->Paginator->prev('prev', array('url' => $options));
  546. $expected = array(
  547. 'li' => array('class' => 'prev'),
  548. 'a' => array('href' => '/members/posts/index', 'rel' => 'prev'),
  549. 'prev',
  550. '/a',
  551. '/li'
  552. );
  553. $this->assertTags($result, $expected);
  554. $options = array('prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc');
  555. $result = $this->Paginator->url($options);
  556. $expected = '/members/posts/index?page=2&amp;sort=name&amp;direction=desc';
  557. $this->assertEquals($expected, $result);
  558. $options = array('controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc');
  559. $result = $this->Paginator->url($options);
  560. $expected = '/posts/index?page=2&amp;sort=Article.name&amp;direction=desc';
  561. $this->assertEquals($expected, $result);
  562. }
  563. /**
  564. * testOptions method
  565. *
  566. * @return void
  567. */
  568. public function testOptions() {
  569. $this->Paginator->options = array();
  570. $this->Paginator->request->params = array();
  571. $options = array('paging' => array('Article' => array(
  572. 'direction' => 'desc',
  573. 'sort' => 'title'
  574. )));
  575. $this->Paginator->options($options);
  576. $expected = array('Article' => array(
  577. 'direction' => 'desc',
  578. 'sort' => 'title'
  579. ));
  580. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  581. $this->Paginator->options = array();
  582. $this->Paginator->request->params = array();
  583. $options = array('Article' => array(
  584. 'direction' => 'desc',
  585. 'sort' => 'title'
  586. ));
  587. $this->Paginator->options($options);
  588. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  589. $options = array('paging' => array('Article' => array(
  590. 'direction' => 'desc',
  591. 'sort' => 'Article.title'
  592. )));
  593. $this->Paginator->options($options);
  594. $expected = array('Article' => array(
  595. 'direction' => 'desc',
  596. 'sort' => 'Article.title'
  597. ));
  598. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  599. }
  600. /**
  601. * testPassedArgsMergingWithUrlOptions method
  602. *
  603. * @return void
  604. */
  605. public function testPassedArgsMergingWithUrlOptions() {
  606. Router::setRequestInfo(array(
  607. array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => array('2')),
  608. array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
  609. ));
  610. $this->Paginator->request->params['paging'] = array(
  611. 'Article' => array(
  612. 'page' => 1, 'current' => 3, 'count' => 13,
  613. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  614. 'sort' => null, 'direction' => null,
  615. )
  616. );
  617. $this->Paginator->request->params['pass'] = array(2);
  618. $this->Paginator->request->query = array('page' => 1, 'foo' => 'bar', 'x' => 'y');
  619. $this->Paginator->beforeRender(null, 'posts/index');
  620. $result = $this->Paginator->sort('title');
  621. $expected = array(
  622. 'a' => array('href' => '/articles/index/2?foo=bar&amp;x=y&amp;sort=title&amp;direction=asc'),
  623. 'Title',
  624. '/a'
  625. );
  626. $this->assertTags($result, $expected);
  627. $result = $this->Paginator->numbers();
  628. $expected = array(
  629. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  630. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=2&amp;foo=bar&amp;x=y')), '2', '/a', '/li',
  631. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=3&amp;foo=bar&amp;x=y')), '3', '/a', '/li',
  632. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=4&amp;foo=bar&amp;x=y')), '4', '/a', '/li',
  633. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=5&amp;foo=bar&amp;x=y')), '5', '/a', '/li',
  634. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=6&amp;foo=bar&amp;x=y')), '6', '/a', '/li',
  635. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=7&amp;foo=bar&amp;x=y')), '7', '/a', '/li',
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Paginator->next('Next');
  639. $expected = array(
  640. 'li' => array('class' => 'next'),
  641. 'a' => array('href' => '/articles/index/2?page=2&amp;foo=bar&amp;x=y', 'rel' => 'next'),
  642. 'Next',
  643. '/a',
  644. '/li'
  645. );
  646. $this->assertTags($result, $expected);
  647. }
  648. /**
  649. * Test that generated URLs don't include sort and direction parameters
  650. *
  651. * @return void
  652. */
  653. public function testDefaultSortRemovedFromUrl() {
  654. Router::setRequestInfo(array(
  655. array('plugin' => null, 'controller' => 'articles', 'action' => 'index'),
  656. array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
  657. ));
  658. $this->Paginator->request->params['paging'] = array(
  659. 'Article' => array(
  660. 'page' => 1, 'current' => 3, 'count' => 13,
  661. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  662. 'sort' => 'Article.title', 'direction' => 'ASC',
  663. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
  664. )
  665. );
  666. $result = $this->Paginator->next('Next');
  667. $expected = array(
  668. 'li' => array('class' => 'next'),
  669. 'a' => array('rel' => 'next', 'href' => '/articles/index?page=2'),
  670. 'Next',
  671. '/a',
  672. '/li'
  673. );
  674. $this->assertTags($result, $expected);
  675. }
  676. /**
  677. * Test the prev() method.
  678. *
  679. * @return void
  680. */
  681. public function testPrev() {
  682. $this->Paginator->request->params['paging'] = array(
  683. 'Client' => array(
  684. 'page' => 1,
  685. 'current' => 3,
  686. 'count' => 13,
  687. 'prevPage' => false,
  688. 'nextPage' => true,
  689. 'pageCount' => 5,
  690. )
  691. );
  692. $result = $this->Paginator->prev('<< Previous');
  693. $expected = array(
  694. 'li' => array('class' => 'prev disabled'),
  695. 'span' => array(),
  696. '&lt;&lt; Previous',
  697. '/span',
  698. '/li'
  699. );
  700. $this->assertTags($result, $expected);
  701. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => 'Prev']);
  702. $expected = array(
  703. 'li' => array('class' => 'prev disabled'),
  704. 'span' => array(),
  705. 'Prev',
  706. '/span',
  707. '/li'
  708. );
  709. $this->assertTags($result, $expected);
  710. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => false]);
  711. $this->assertEquals('', $result, 'disabled + no text = no link');
  712. $this->Paginator->request->params['paging']['Client']['page'] = 2;
  713. $this->Paginator->request->params['paging']['Client']['prevPage'] = true;
  714. $result = $this->Paginator->prev('<< Previous');
  715. $expected = array(
  716. 'li' => array('class' => 'prev'),
  717. 'a' => array('href' => '/index', 'rel' => 'prev'),
  718. '&lt;&lt; Previous',
  719. '/a',
  720. '/li'
  721. );
  722. $this->assertTags($result, $expected);
  723. }
  724. /**
  725. * Test that prev() and the shared implementation underneath picks up from optins
  726. *
  727. * @return void
  728. */
  729. public function testPrevWithOptions() {
  730. $this->Paginator->request->params['paging'] = array(
  731. 'Client' => array(
  732. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
  733. 'nextPage' => false, 'pageCount' => 2,
  734. 'limit' => 10,
  735. )
  736. );
  737. $this->Paginator->options(array('url' => array(12, 'page' => 3)));
  738. $result = $this->Paginator->prev('Prev', array('url' => array('foo' => 'bar')));
  739. $expected = array(
  740. 'li' => array('class' => 'prev'),
  741. 'a' => array('href' => '/index/12?limit=10&amp;foo=bar', 'rel' => 'prev'),
  742. 'Prev',
  743. '/a',
  744. '/li'
  745. );
  746. $this->assertTags($result, $expected);
  747. }
  748. /**
  749. * test the next() method.
  750. *
  751. * @return void
  752. */
  753. public function testNext() {
  754. $result = $this->Paginator->next('Next >>');
  755. $expected = array(
  756. 'li' => array('class' => 'next'),
  757. 'a' => array('href' => '/index?page=2', 'rel' => 'next'),
  758. 'Next &gt;&gt;',
  759. '/a',
  760. '/li'
  761. );
  762. $this->assertTags($result, $expected);
  763. $result = $this->Paginator->next('Next >>', ['escape' => false]);
  764. $expected = array(
  765. 'li' => array('class' => 'next'),
  766. 'a' => array('href' => '/index?page=2', 'rel' => 'next'),
  767. 'preg:/Next >>/',
  768. '/a',
  769. '/li'
  770. );
  771. $this->assertTags($result, $expected);
  772. }
  773. /**
  774. * test next() with disabled links
  775. *
  776. * @return void
  777. */
  778. public function testNextDisabled() {
  779. $this->Paginator->request->params['paging'] = array(
  780. 'Client' => array(
  781. 'page' => 5,
  782. 'current' => 3,
  783. 'count' => 13,
  784. 'prevPage' => true,
  785. 'nextPage' => false,
  786. 'pageCount' => 5,
  787. )
  788. );
  789. $result = $this->Paginator->next('Next >>');
  790. $expected = array(
  791. 'li' => array('class' => 'next disabled'),
  792. 'span' => array(),
  793. 'Next &gt;&gt;',
  794. '/span',
  795. '/li'
  796. );
  797. $this->assertTags($result, $expected);
  798. $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']);
  799. $expected = array(
  800. 'li' => array('class' => 'next disabled'),
  801. 'span' => array(),
  802. 'Next',
  803. '/span',
  804. '/li'
  805. );
  806. $this->assertTags($result, $expected);
  807. $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]);
  808. $this->assertEquals('', $result, 'disabled + no text = no link');
  809. }
  810. /**
  811. * Test next() with a model argument.
  812. *
  813. * @return void
  814. */
  815. public function testNextAndPrevNonDefaultModel() {
  816. $this->Paginator->request->params['paging'] = array(
  817. 'Client' => array(
  818. 'page' => 1,
  819. 'current' => 3,
  820. 'count' => 13,
  821. 'prevPage' => false,
  822. 'nextPage' => true,
  823. 'pageCount' => 5,
  824. ),
  825. 'Server' => array(
  826. 'page' => 5,
  827. 'current' => 1,
  828. 'count' => 5,
  829. 'prevPage' => true,
  830. 'nextPage' => false,
  831. 'pageCount' => 5,
  832. )
  833. );
  834. $result = $this->Paginator->next('Next', [
  835. 'model' => 'Client'
  836. ]);
  837. $expected = array(
  838. 'li' => array('class' => 'next'),
  839. 'a' => array('href' => '/index?page=2', 'rel' => 'next'),
  840. 'Next',
  841. '/a',
  842. '/li'
  843. );
  844. $this->assertTags($result, $expected);
  845. $result = $this->Paginator->prev('Prev', [
  846. 'model' => 'Client'
  847. ]);
  848. $expected = '<li class="prev disabled"><span>Prev</span></li>';
  849. $this->assertEquals($expected, $result);
  850. $result = $this->Paginator->next('Next', [
  851. 'model' => 'Server'
  852. ]);
  853. $expected = '<li class="next disabled"><span>Next</span></li>';
  854. $this->assertEquals($expected, $result);
  855. $result = $this->Paginator->prev('Prev', [
  856. 'model' => 'Server'
  857. ]);
  858. $expected = array(
  859. 'li' => array('class' => 'prev'),
  860. 'a' => array('href' => '/index?page=4', 'rel' => 'prev'),
  861. 'Prev',
  862. '/a',
  863. '/li'
  864. );
  865. $this->assertTags($result, $expected);
  866. }
  867. /**
  868. * testNumbers method
  869. *
  870. * @return void
  871. */
  872. public function testNumbers() {
  873. $this->Paginator->request->params['paging'] = array(
  874. 'Client' => array(
  875. 'page' => 8,
  876. 'current' => 3,
  877. 'count' => 30,
  878. 'prevPage' => false,
  879. 'nextPage' => 2,
  880. 'pageCount' => 15,
  881. )
  882. );
  883. $result = $this->Paginator->numbers();
  884. $expected = array(
  885. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  886. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  887. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  888. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  889. array('li' => array('class' => 'active')), '<span', '8', '/span', '/li',
  890. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  891. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  892. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  893. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  894. );
  895. $this->assertTags($result, $expected);
  896. $result = $this->Paginator->numbers(true);
  897. $expected = array(
  898. array('li' => array('class' => 'first')), array('a' => array('href' => '/index')), 'first', '/a', '/li',
  899. array('li' => array('class' => 'ellipsis')), '...', '/li',
  900. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  901. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  902. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  903. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  904. array('li' => array('class' => 'active')), '<span', '8', '/span', '/li',
  905. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  906. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  907. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  908. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  909. array('li' => array('class' => 'ellipsis')), '...', '/li',
  910. array('li' => array('class' => 'last')), array('a' => array('href' => '/index?page=15')), 'last', '/a', '/li',
  911. );
  912. $this->assertTags($result, $expected);
  913. $this->Paginator->request->params['paging'] = array(
  914. 'Client' => array(
  915. 'page' => 1,
  916. 'current' => 3,
  917. 'count' => 30,
  918. 'prevPage' => false,
  919. 'nextPage' => 2,
  920. 'pageCount' => 15,
  921. )
  922. );
  923. $result = $this->Paginator->numbers();
  924. $expected = array(
  925. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  926. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  927. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  928. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  929. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  930. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  931. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  932. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  933. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  934. );
  935. $this->assertTags($result, $expected);
  936. $this->Paginator->request->params['paging'] = array(
  937. 'Client' => array(
  938. 'page' => 14,
  939. 'current' => 3,
  940. 'count' => 30,
  941. 'prevPage' => false,
  942. 'nextPage' => 2,
  943. 'pageCount' => 15,
  944. )
  945. );
  946. $result = $this->Paginator->numbers();
  947. $expected = array(
  948. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  949. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  950. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  951. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  952. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  953. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  954. array('li' => array()), array('a' => array('href' => '/index?page=13')), '13', '/a', '/li',
  955. array('li' => array('class' => 'active')), '<span', '14', '/span', '/li',
  956. array('li' => array()), array('a' => array('href' => '/index?page=15')), '15', '/a', '/li',
  957. );
  958. $this->assertTags($result, $expected);
  959. $this->Paginator->request->params['paging'] = array(
  960. 'Client' => array(
  961. 'page' => 2,
  962. 'current' => 3,
  963. 'count' => 27,
  964. 'prevPage' => false,
  965. 'nextPage' => 2,
  966. 'pageCount' => 9,
  967. )
  968. );
  969. $result = $this->Paginator->numbers(array('first' => 1));
  970. $expected = array(
  971. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  972. array('li' => array('class' => 'active')), '<span', '2', '/span', '/li',
  973. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  974. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  975. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  976. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  977. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  978. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  979. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  980. );
  981. $this->assertTags($result, $expected);
  982. $result = $this->Paginator->numbers(array('last' => 1));
  983. $expected = array(
  984. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  985. array('li' => array('class' => 'active')), '<span', '2', '/span', '/li',
  986. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  987. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  988. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  989. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  990. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  991. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  992. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  993. );
  994. $this->assertTags($result, $expected);
  995. $this->Paginator->request->params['paging'] = array(
  996. 'Client' => array(
  997. 'page' => 15,
  998. 'current' => 3,
  999. 'count' => 30,
  1000. 'prevPage' => false,
  1001. 'nextPage' => 2,
  1002. 'pageCount' => 15,
  1003. )
  1004. );
  1005. $result = $this->Paginator->numbers(array('first' => 1));
  1006. $expected = array(
  1007. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1008. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1009. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1010. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1011. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1012. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  1013. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  1014. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  1015. array('li' => array()), array('a' => array('href' => '/index?page=13')), '13', '/a', '/li',
  1016. array('li' => array()), array('a' => array('href' => '/index?page=14')), '14', '/a', '/li',
  1017. array('li' => array('class' => 'active')), '<span', '15', '/span', '/li',
  1018. );
  1019. $this->assertTags($result, $expected);
  1020. $this->Paginator->request->params['paging'] = array(
  1021. 'Client' => array(
  1022. 'page' => 10,
  1023. 'current' => 3,
  1024. 'count' => 30,
  1025. 'prevPage' => false,
  1026. 'nextPage' => 2,
  1027. 'pageCount' => 15,
  1028. )
  1029. );
  1030. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  1031. $expected = array(
  1032. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1033. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1034. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1035. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1036. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1037. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1038. array('li' => array('class' => 'active')), '<span', '10', '/span', '/li',
  1039. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  1040. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  1041. array('li' => array()), array('a' => array('href' => '/index?page=13')), '13', '/a', '/li',
  1042. array('li' => array()), array('a' => array('href' => '/index?page=14')), '14', '/a', '/li',
  1043. array('li' => array()), array('a' => array('href' => '/index?page=15')), '15', '/a', '/li',
  1044. );
  1045. $this->assertTags($result, $expected);
  1046. $this->Paginator->request->params['paging'] = array(
  1047. 'Client' => array(
  1048. 'page' => 6,
  1049. 'current' => 15,
  1050. 'count' => 623,
  1051. 'prevPage' => 1,
  1052. 'nextPage' => 1,
  1053. 'pageCount' => 42,
  1054. )
  1055. );
  1056. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  1057. $expected = array(
  1058. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1059. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1060. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1061. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1062. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1063. array('li' => array('class' => 'active')), '<span', '6', '/span', '/li',
  1064. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1065. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1066. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1067. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  1068. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1069. array('li' => array()), array('a' => array('href' => '/index?page=42')), '42', '/a', '/li',
  1070. );
  1071. $this->assertTags($result, $expected);
  1072. $this->Paginator->request->params['paging'] = array(
  1073. 'Client' => array(
  1074. 'page' => 37,
  1075. 'current' => 15,
  1076. 'count' => 623,
  1077. 'prevPage' => 1,
  1078. 'nextPage' => 1,
  1079. 'pageCount' => 42,
  1080. )
  1081. );
  1082. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  1083. $expected = array(
  1084. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1085. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1086. array('li' => array()), array('a' => array('href' => '/index?page=33')), '33', '/a', '/li',
  1087. array('li' => array()), array('a' => array('href' => '/index?page=34')), '34', '/a', '/li',
  1088. array('li' => array()), array('a' => array('href' => '/index?page=35')), '35', '/a', '/li',
  1089. array('li' => array()), array('a' => array('href' => '/index?page=36')), '36', '/a', '/li',
  1090. array('li' => array('class' => 'active')), '<span', '37', '/span', '/li',
  1091. array('li' => array()), array('a' => array('href' => '/index?page=38')), '38', '/a', '/li',
  1092. array('li' => array()), array('a' => array('href' => '/index?page=39')), '39', '/a', '/li',
  1093. array('li' => array()), array('a' => array('href' => '/index?page=40')), '40', '/a', '/li',
  1094. array('li' => array()), array('a' => array('href' => '/index?page=41')), '41', '/a', '/li',
  1095. array('li' => array()), array('a' => array('href' => '/index?page=42')), '42', '/a', '/li',
  1096. );
  1097. $this->assertTags($result, $expected);
  1098. }
  1099. /**
  1100. * Test modulus option for numbers()
  1101. *
  1102. * @return void
  1103. */
  1104. public function testNumbersModulus() {
  1105. $this->Paginator->request->params['paging'] = array(
  1106. 'Client' => array(
  1107. 'page' => 1,
  1108. 'current' => 10,
  1109. 'count' => 30,
  1110. 'prevPage' => false,
  1111. 'nextPage' => 2,
  1112. 'pageCount' => 3,
  1113. )
  1114. );
  1115. $options = array('modulus' => 10);
  1116. $result = $this->Paginator->numbers($options);
  1117. $expected = array(
  1118. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  1119. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1120. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1121. );
  1122. $this->assertTags($result, $expected);
  1123. $result = $this->Paginator->numbers(array('modulus' => 3));
  1124. $expected = array(
  1125. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  1126. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1127. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $this->Paginator->request->params['paging'] = array(
  1131. 'Client' => array(
  1132. 'page' => 4895,
  1133. 'current' => 10,
  1134. 'count' => 48962,
  1135. 'prevPage' => 1,
  1136. 'nextPage' => 1,
  1137. 'pageCount' => 4897,
  1138. )
  1139. );
  1140. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1141. $expected = array(
  1142. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1143. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1144. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1145. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1146. array('li' => array('class' => 'active')), '<span', '4895', '/span', '/li',
  1147. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1148. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1149. );
  1150. $this->assertTags($result, $expected);
  1151. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1152. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1153. $expected = array(
  1154. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1155. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1156. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1157. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1158. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1159. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1160. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1161. );
  1162. $this->assertTags($result, $expected);
  1163. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1164. $expected = array(
  1165. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1166. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1167. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1168. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1169. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1170. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1171. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1172. );
  1173. $this->assertTags($result, $expected);
  1174. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 5, 'last' => 5));
  1175. $expected = array(
  1176. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1177. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1178. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1179. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1180. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1181. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1182. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1183. array('li' => array()), array('a' => array('href' => '/index?page=4893')), '4893', '/a', '/li',
  1184. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1185. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1186. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1187. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1188. );
  1189. $this->assertTags($result, $expected);
  1190. $this->Paginator->request->params['paging']['Client']['page'] = 4893;
  1191. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5));
  1192. $expected = array(
  1193. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1194. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1195. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1196. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1197. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1198. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1199. array('li' => array()), array('a' => array('href' => '/index?page=4891')), '4891', '/a', '/li',
  1200. array('li' => array()), array('a' => array('href' => '/index?page=4892')), '4892', '/a', '/li',
  1201. array('li' => array('class' => 'active')), '<span', '4893', '/span', '/li',
  1202. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1203. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1204. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1205. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1206. );
  1207. $this->assertTags($result, $expected);
  1208. $this->Paginator->request->params['paging']['Client']['page'] = 58;
  1209. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5));
  1210. $expected = array(
  1211. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1212. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1213. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1214. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1215. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1216. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1217. array('li' => array()), array('a' => array('href' => '/index?page=56')), '56', '/a', '/li',
  1218. array('li' => array()), array('a' => array('href' => '/index?page=57')), '57', '/a', '/li',
  1219. array('li' => array('class' => 'active')), '<span', '58', '/span', '/li',
  1220. array('li' => array()), array('a' => array('href' => '/index?page=59')), '59', '/a', '/li',
  1221. array('li' => array()), array('a' => array('href' => '/index?page=60')), '60', '/a', '/li',
  1222. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1223. array('li' => array()), array('a' => array('href' => '/index?page=4893')), '4893', '/a', '/li',
  1224. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1225. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1226. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1227. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1228. );
  1229. $this->assertTags($result, $expected);
  1230. $this->Paginator->request->params['paging']['Client']['page'] = 5;
  1231. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5));
  1232. $expected = array(
  1233. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1234. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1235. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1236. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1237. array('li' => array('class' => 'active')), '<span', '5', '/span', '/li',
  1238. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1239. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1240. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1241. array('li' => array()), array('a' => array('href' => '/index?page=4893')), '4893', '/a', '/li',
  1242. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1243. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1244. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1245. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1246. );
  1247. $this->assertTags($result, $expected);
  1248. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1249. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1250. $expected = array(
  1251. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1252. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1253. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1254. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1255. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1256. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1257. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1258. );
  1259. $this->assertTags($result, $expected);
  1260. }
  1261. /**
  1262. * test numbers() with routing parameters.
  1263. *
  1264. * @return void
  1265. */
  1266. public function testNumbersRouting() {
  1267. $this->Paginator->request->params['paging'] = array(
  1268. 'Client' => array(
  1269. 'page' => 2,
  1270. 'current' => 2,
  1271. 'count' => 30,
  1272. 'prevPage' => false,
  1273. 'nextPage' => 3,
  1274. 'pageCount' => 3,
  1275. )
  1276. );
  1277. $request = new Request();
  1278. $request->addParams(array(
  1279. 'controller' => 'clients', 'action' => 'index', 'plugin' => null
  1280. ));
  1281. $request->base = '';
  1282. $request->here = '/clients/index?page=2';
  1283. $request->webroot = '/';
  1284. Router::setRequestInfo($request);
  1285. $result = $this->Paginator->numbers();
  1286. $expected = array(
  1287. array('li' => array()), array('a' => array('href' => '/clients/index')), '1', '/a', '/li',
  1288. array('li' => array('class' => 'active')), '<span', '2', '/span', '/li',
  1289. array('li' => array()), array('a' => array('href' => '/clients/index?page=3')), '3', '/a', '/li',
  1290. );
  1291. $this->assertTags($result, $expected);
  1292. }
  1293. /**
  1294. * Test that numbers() works with the non default model.
  1295. *
  1296. * @return void
  1297. */
  1298. public function testNumbersNonDefaultModel() {
  1299. $this->Paginator->request->params['paging'] = array(
  1300. 'Client' => array(
  1301. 'page' => 1,
  1302. 'current' => 3,
  1303. 'count' => 13,
  1304. 'prevPage' => false,
  1305. 'nextPage' => true,
  1306. 'pageCount' => 5,
  1307. ),
  1308. 'Server' => array(
  1309. 'page' => 5,
  1310. 'current' => 1,
  1311. 'count' => 5,
  1312. 'prevPage' => true,
  1313. 'nextPage' => false,
  1314. 'pageCount' => 5,
  1315. )
  1316. );
  1317. $result = $this->Paginator->numbers(['model' => 'Server']);
  1318. $this->assertContains('<li class="active"><span>5</span></li>', $result);
  1319. $this->assertNotContains('<li class="active"><span>1</span></li>', $result);
  1320. $result = $this->Paginator->numbers(['model' => 'Client']);
  1321. $this->assertContains('<li class="active"><span>1</span></li>', $result);
  1322. $this->assertNotContains('<li class="active"><span>5</span></li>', $result);
  1323. }
  1324. /**
  1325. * test first() and last() with tag options
  1326. *
  1327. * @return void
  1328. */
  1329. public function testFirstAndLastTag() {
  1330. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1331. $result = $this->Paginator->first('<<');
  1332. $expected = array(
  1333. 'li' => array('class' => 'first'),
  1334. 'a' => array('href' => '/index'),
  1335. '&lt;&lt;',
  1336. '/a',
  1337. '/li'
  1338. );
  1339. $this->assertTags($result, $expected);
  1340. $result = $this->Paginator->last(2);
  1341. $expected = array(
  1342. '<li',
  1343. array('a' => array('href' => '/index?page=6')), '6', '/a',
  1344. '/li',
  1345. '<li',
  1346. array('a' => array('href' => '/index?page=7')), '7', '/a',
  1347. '/li',
  1348. );
  1349. $this->assertTags($result, $expected);
  1350. }
  1351. /**
  1352. * test that on the last page you don't get a link ot the last page.
  1353. *
  1354. * @return void
  1355. */
  1356. public function testLastNoOutput() {
  1357. $this->Paginator->request->params['paging']['Article']['page'] = 15;
  1358. $this->Paginator->request->params['paging']['Article']['pageCount'] = 15;
  1359. $result = $this->Paginator->last();
  1360. $expected = '';
  1361. $this->assertEquals($expected, $result);
  1362. }
  1363. /**
  1364. * test first() with a the model parameter.
  1365. *
  1366. * @return void
  1367. */
  1368. public function testFirstNonDefaultModel() {
  1369. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1370. $this->Paginator->request->params['paging']['Client'] = array(
  1371. 'page' => 3,
  1372. 'current' => 3,
  1373. 'count' => 13,
  1374. 'prevPage' => false,
  1375. 'nextPage' => true,
  1376. 'pageCount' => 5,
  1377. );
  1378. $result = $this->Paginator->first('first', ['model' => 'Article:']);
  1379. $this->assertEquals('', $result);
  1380. $result = $this->Paginator->first('first', ['model' => 'Client']);
  1381. $expected = array(
  1382. 'li' => array('class' => 'first'),
  1383. 'a' => array('href' => '/index'),
  1384. 'first',
  1385. '/a',
  1386. '/li'
  1387. );
  1388. $this->assertTags($result, $expected);
  1389. }
  1390. /**
  1391. * test first() on the first page.
  1392. *
  1393. * @return void
  1394. */
  1395. public function testFirstEmpty() {
  1396. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1397. $result = $this->Paginator->first();
  1398. $expected = '';
  1399. $this->assertEquals($expected, $result);
  1400. }
  1401. /**
  1402. * test first() and options()
  1403. *
  1404. * @return void
  1405. */
  1406. public function testFirstFullBaseUrl() {
  1407. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1408. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  1409. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1410. $this->Paginator->options(array('url' => array('_full' => true)));
  1411. $result = $this->Paginator->first();
  1412. $expected = array(
  1413. 'li' => ['class' => 'first'],
  1414. array('a' => array(
  1415. 'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC'
  1416. )),
  1417. '&lt;&lt; first',
  1418. '/a',
  1419. '/li',
  1420. );
  1421. $this->assertTags($result, $expected);
  1422. }
  1423. /**
  1424. * test first() on the fence-post
  1425. *
  1426. * @return void
  1427. */
  1428. public function testFirstBoundaries() {
  1429. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1430. $result = $this->Paginator->first();
  1431. $expected = array(
  1432. 'li' => ['class' => 'first'],
  1433. 'a' => array('href' => '/index'),
  1434. '&lt;&lt; first',
  1435. '/a',
  1436. '/li'
  1437. );
  1438. $this->assertTags($result, $expected);
  1439. $result = $this->Paginator->first(2);
  1440. $expected = array(
  1441. '<li',
  1442. array('a' => array('href' => '/index')), '1', '/a',
  1443. '/li',
  1444. '<li',
  1445. array('a' => array('href' => '/index?page=2')), '2', '/a',
  1446. '/li'
  1447. );
  1448. $this->assertTags($result, $expected);
  1449. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1450. $result = $this->Paginator->first(3);
  1451. $this->assertEquals('', $result, 'When inside the first links range, no links should be made');
  1452. }
  1453. /**
  1454. * test params() method
  1455. *
  1456. * @return void
  1457. */
  1458. public function testParams() {
  1459. $result = $this->Paginator->params();
  1460. $this->assertArrayHasKey('page', $result);
  1461. $this->assertArrayHasKey('pageCount', $result);
  1462. }
  1463. /**
  1464. * test param() method
  1465. *
  1466. * @return void
  1467. */
  1468. public function testParam() {
  1469. $result = $this->Paginator->param('count');
  1470. $this->assertSame(62, $result);
  1471. $result = $this->Paginator->param('imaginary');
  1472. $this->assertNull($result);
  1473. }
  1474. /**
  1475. * test last() method
  1476. *
  1477. * @return void
  1478. */
  1479. public function testLast() {
  1480. $result = $this->Paginator->last();
  1481. $expected = array(
  1482. 'li' => ['class' => 'last'],
  1483. 'a' => array('href' => '/index?page=7'),
  1484. 'last &gt;&gt;',
  1485. '/a',
  1486. '/li'
  1487. );
  1488. $this->assertTags($result, $expected);
  1489. $result = $this->Paginator->last(1);
  1490. $expected = array(
  1491. '<li',
  1492. 'a' => array('href' => '/index?page=7'),
  1493. '7',
  1494. '/a',
  1495. '/li'
  1496. );
  1497. $this->assertTags($result, $expected);
  1498. $this->Paginator->request->params['paging']['Article']['page'] = 6;
  1499. $result = $this->Paginator->last(2);
  1500. $expected = array(
  1501. '<li',
  1502. array('a' => array('href' => '/index?page=6')), '6', '/a',
  1503. '/li',
  1504. '<li',
  1505. array('a' => array('href' => '/index?page=7')), '7', '/a',
  1506. '/li',
  1507. );
  1508. $this->assertTags($result, $expected);
  1509. $result = $this->Paginator->last(3);
  1510. $this->assertEquals('', $result, 'When inside the last links range, no links should be made');
  1511. }
  1512. /**
  1513. * test the options for last()
  1514. *
  1515. * @return void
  1516. */
  1517. public function testLastOptions() {
  1518. $this->Paginator->request->params['paging'] = array(
  1519. 'Client' => array(
  1520. 'page' => 4,
  1521. 'current' => 3,
  1522. 'count' => 30,
  1523. 'prevPage' => false,
  1524. 'nextPage' => 2,
  1525. 'pageCount' => 15,
  1526. 'sort' => 'Client.name',
  1527. 'direction' => 'DESC',
  1528. )
  1529. );
  1530. $result = $this->Paginator->last();
  1531. $expected = array(
  1532. 'li' => ['class' => 'last'],
  1533. 'a' => array(
  1534. 'href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC',
  1535. ),
  1536. 'last &gt;&gt;', '/a',
  1537. '/li',
  1538. );
  1539. $this->assertTags($result, $expected);
  1540. $result = $this->Paginator->last(1);
  1541. $expected = array(
  1542. '<li',
  1543. array('a' => array('href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC')), '15', '/a',
  1544. '/li',
  1545. );
  1546. $this->assertTags($result, $expected);
  1547. $result = $this->Paginator->last(2);
  1548. $expected = array(
  1549. '<li',
  1550. array('a' => array('href' => '/index?page=14&amp;sort=Client.name&amp;direction=DESC')), '14', '/a',
  1551. '/li',
  1552. '<li',
  1553. array('a' => array('href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC')), '15', '/a',
  1554. '/li',
  1555. );
  1556. $this->assertTags($result, $expected);
  1557. }
  1558. /**
  1559. * test last() with a the model parameter.
  1560. *
  1561. * @return void
  1562. */
  1563. public function testLastNonDefaultModel() {
  1564. $this->Paginator->request->params['paging']['Article']['page'] = 7;
  1565. $this->Paginator->request->params['paging']['Client'] = array(
  1566. 'page' => 3,
  1567. 'current' => 3,
  1568. 'count' => 13,
  1569. 'prevPage' => false,
  1570. 'nextPage' => true,
  1571. 'pageCount' => 5,
  1572. );
  1573. $result = $this->Paginator->last('last', ['model' => 'Article:']);
  1574. $this->assertEquals('', $result);
  1575. $result = $this->Paginator->last('last', ['model' => 'Client']);
  1576. $expected = array(
  1577. 'li' => array('class' => 'last'),
  1578. 'a' => array('href' => '/index?page=5'),
  1579. 'last',
  1580. '/a',
  1581. '/li'
  1582. );
  1583. $this->assertTags($result, $expected);
  1584. }
  1585. /**
  1586. * testCounter method
  1587. *
  1588. * @return void
  1589. */
  1590. public function testCounter() {
  1591. $this->Paginator->request->params['paging'] = array(
  1592. 'Client' => array(
  1593. 'page' => 1,
  1594. 'current' => 3,
  1595. 'count' => 13,
  1596. 'prevPage' => false,
  1597. 'nextPage' => true,
  1598. 'pageCount' => 5,
  1599. 'limit' => 3,
  1600. 'sort' => 'Client.name',
  1601. 'order' => 'DESC',
  1602. )
  1603. );
  1604. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  1605. $input .= 'starting on record {{start}}, ending on {{end}}';
  1606. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
  1607. $expected .= 'ending on 3';
  1608. $result = $this->Paginator->counter($input);
  1609. $this->assertEquals($expected, $result);
  1610. $result = $this->Paginator->counter(array('format' => 'pages'));
  1611. $expected = '1 of 5';
  1612. $this->assertEquals($expected, $result);
  1613. $result = $this->Paginator->counter(array('format' => 'range'));
  1614. $expected = '1 - 3 of 13';
  1615. $this->assertEquals($expected, $result);
  1616. $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}');
  1617. $this->assertEquals('Showing 1 of 5 clients', $result);
  1618. }
  1619. /**
  1620. * testHasPage method
  1621. *
  1622. * @return void
  1623. */
  1624. public function testHasPage() {
  1625. $result = $this->Paginator->hasPage('Article', 15);
  1626. $this->assertFalse($result);
  1627. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  1628. $this->assertFalse($result);
  1629. $result = $this->Paginator->hasPage('Article', 2);
  1630. $this->assertTrue($result);
  1631. $result = $this->Paginator->hasPage(2);
  1632. $this->assertTrue($result);
  1633. }
  1634. /**
  1635. * testNextLinkUsingDotNotation method
  1636. *
  1637. * @return void
  1638. */
  1639. public function testNextLinkUsingDotNotation() {
  1640. Router::setRequestInfo(array(
  1641. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array()),
  1642. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  1643. ));
  1644. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1645. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  1646. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1647. $test = array('url' => array(
  1648. 'page' => '1',
  1649. 'sort' => 'Article.title',
  1650. 'direction' => 'asc',
  1651. ));
  1652. $this->Paginator->options($test);
  1653. $result = $this->Paginator->next('Next');
  1654. $expected = array(
  1655. 'li' => array('class' => 'next'),
  1656. 'a' => array(
  1657. 'href' => '/accounts/index?page=2&amp;sort=Article.title&amp;direction=asc',
  1658. 'rel' => 'next'
  1659. ),
  1660. 'Next',
  1661. '/a',
  1662. '/li',
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. }
  1666. /**
  1667. * test the current() method
  1668. *
  1669. * @return void
  1670. */
  1671. public function testCurrent() {
  1672. $result = $this->Paginator->current();
  1673. $this->assertEquals($this->Paginator->request->params['paging']['Article']['page'], $result);
  1674. $result = $this->Paginator->current('Incorrect');
  1675. $this->assertEquals(1, $result);
  1676. }
  1677. /**
  1678. * test the defaultModel() method
  1679. *
  1680. * @return void
  1681. */
  1682. public function testNoDefaultModel() {
  1683. $this->Paginator->request = new Request();
  1684. $this->assertNull($this->Paginator->defaultModel());
  1685. }
  1686. /**
  1687. * test the numbers() method when there is only one page
  1688. *
  1689. * @return void
  1690. */
  1691. public function testWithOnePage() {
  1692. $this->Paginator->request['paging'] = array(
  1693. 'Article' => array(
  1694. 'page' => 1,
  1695. 'current' => 2,
  1696. 'count' => 2,
  1697. 'prevPage' => false,
  1698. 'nextPage' => true,
  1699. 'pageCount' => 1,
  1700. )
  1701. );
  1702. $this->assertFalse($this->Paginator->numbers());
  1703. $this->assertFalse($this->Paginator->first());
  1704. $this->assertFalse($this->Paginator->last());
  1705. }
  1706. /**
  1707. * test the numbers() method when there is only one page
  1708. *
  1709. * @return void
  1710. */
  1711. public function testWithZeroPages() {
  1712. $this->Paginator->request['paging'] = array(
  1713. 'Article' => array(
  1714. 'page' => 0,
  1715. 'current' => 0,
  1716. 'count' => 0,
  1717. 'prevPage' => false,
  1718. 'nextPage' => false,
  1719. 'pageCount' => 0,
  1720. 'limit' => 10,
  1721. )
  1722. );
  1723. $result = $this->Paginator->counter(array('format' => 'pages'));
  1724. $expected = '0 of 1';
  1725. $this->assertEquals($expected, $result);
  1726. }
  1727. }