PaginatorHelperTest.php 66 KB

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