PaginatorHelperTest.php 66 KB

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