PaginatorHelperTest.php 80 KB

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