PaginatorHelperTest.php 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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' => '', 'onclick' => 'return false;'],
  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' => '', 'onclick' => 'return false;'],
  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', 'num' => 0, 'empty' => ''];
  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;num=0&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&amp;num=0']], '2', '/a', '/li',
  722. ['li' => []], ['a' => ['href' => '/articles/index/2?page=3&amp;foo=bar&amp;x=y&amp;num=0']], '3', '/a', '/li',
  723. ['li' => []], ['a' => ['href' => '/articles/index/2?page=4&amp;foo=bar&amp;x=y&amp;num=0']], '4', '/a', '/li',
  724. ['li' => []], ['a' => ['href' => '/articles/index/2?page=5&amp;foo=bar&amp;x=y&amp;num=0']], '5', '/a', '/li',
  725. ['li' => []], ['a' => ['href' => '/articles/index/2?page=6&amp;foo=bar&amp;x=y&amp;num=0']], '6', '/a', '/li',
  726. ['li' => []], ['a' => ['href' => '/articles/index/2?page=7&amp;foo=bar&amp;x=y&amp;num=0']], '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&amp;num=0', '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' => '', 'onclick' => 'return false;'],
  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' => '', 'onclick' => 'return false;'],
  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. $result = $this->Paginator->prev('Prev', [
  817. 'templates' => [
  818. 'prevActive' => '<a rel="prev" href="{{url}}">{{text}}</a>'
  819. ]
  820. ]);
  821. $expected = [
  822. 'a' => ['href' => '/index', 'rel' => 'prev'],
  823. 'Prev',
  824. '/a',
  825. ];
  826. $this->assertHtml($expected, $result);
  827. }
  828. /**
  829. * Test that prev() and the shared implementation underneath picks up from optins
  830. *
  831. * @return void
  832. */
  833. public function testPrevWithOptions()
  834. {
  835. $this->Paginator->request->params['paging'] = [
  836. 'Client' => [
  837. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
  838. 'nextPage' => false, 'pageCount' => 2,
  839. 'limit' => 10,
  840. ]
  841. ];
  842. $this->Paginator->options(['url' => [12, 'page' => 3]]);
  843. $result = $this->Paginator->prev('Prev', ['url' => ['foo' => 'bar']]);
  844. $expected = [
  845. 'li' => ['class' => 'prev'],
  846. 'a' => ['href' => '/index/12?limit=10&amp;foo=bar', 'rel' => 'prev'],
  847. 'Prev',
  848. '/a',
  849. '/li'
  850. ];
  851. $this->assertHtml($expected, $result);
  852. }
  853. /**
  854. * test the next() method.
  855. *
  856. * @return void
  857. */
  858. public function testNext()
  859. {
  860. $result = $this->Paginator->next('Next >>');
  861. $expected = [
  862. 'li' => ['class' => 'next'],
  863. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  864. 'Next &gt;&gt;',
  865. '/a',
  866. '/li'
  867. ];
  868. $this->assertHtml($expected, $result);
  869. $result = $this->Paginator->next('Next', [
  870. 'templates' => [
  871. 'nextActive' => '<a rel="next" href="{{url}}">{{text}}</a>'
  872. ]
  873. ]);
  874. $expected = [
  875. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  876. 'Next',
  877. '/a',
  878. ];
  879. $this->assertHtml($expected, $result);
  880. $result = $this->Paginator->next('Next >>', ['escape' => false]);
  881. $expected = [
  882. 'li' => ['class' => 'next'],
  883. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  884. 'preg:/Next >>/',
  885. '/a',
  886. '/li'
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * test next() with disabled links
  892. *
  893. * @return void
  894. */
  895. public function testNextDisabled()
  896. {
  897. $this->Paginator->request->params['paging'] = [
  898. 'Client' => [
  899. 'page' => 5,
  900. 'current' => 3,
  901. 'count' => 13,
  902. 'prevPage' => true,
  903. 'nextPage' => false,
  904. 'pageCount' => 5,
  905. ]
  906. ];
  907. $result = $this->Paginator->next('Next >>');
  908. $expected = [
  909. 'li' => ['class' => 'next disabled'],
  910. 'a' => ['href' => '', 'onclick' => 'return false;'],
  911. 'Next &gt;&gt;',
  912. '/a',
  913. '/li'
  914. ];
  915. $this->assertHtml($expected, $result);
  916. $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']);
  917. $expected = [
  918. 'li' => ['class' => 'next disabled'],
  919. 'a' => ['href' => '', 'onclick' => 'return false;'],
  920. 'Next',
  921. '/a',
  922. '/li'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]);
  926. $this->assertEquals('', $result, 'disabled + no text = no link');
  927. }
  928. /**
  929. * Test next() with a model argument.
  930. *
  931. * @return void
  932. */
  933. public function testNextAndPrevNonDefaultModel()
  934. {
  935. $this->Paginator->request->params['paging'] = [
  936. 'Client' => [
  937. 'page' => 1,
  938. 'current' => 3,
  939. 'count' => 13,
  940. 'prevPage' => false,
  941. 'nextPage' => true,
  942. 'pageCount' => 5,
  943. ],
  944. 'Server' => [
  945. 'page' => 5,
  946. 'current' => 1,
  947. 'count' => 5,
  948. 'prevPage' => true,
  949. 'nextPage' => false,
  950. 'pageCount' => 5,
  951. ]
  952. ];
  953. $result = $this->Paginator->next('Next', [
  954. 'model' => 'Client'
  955. ]);
  956. $expected = [
  957. 'li' => ['class' => 'next'],
  958. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  959. 'Next',
  960. '/a',
  961. '/li'
  962. ];
  963. $this->assertHtml($expected, $result);
  964. $result = $this->Paginator->prev('Prev', [
  965. 'model' => 'Client'
  966. ]);
  967. $expected = '<li class="prev disabled"><a href="" onclick="return false;">Prev</a></li>';
  968. $this->assertEquals($expected, $result);
  969. $result = $this->Paginator->next('Next', [
  970. 'model' => 'Server'
  971. ]);
  972. $expected = '<li class="next disabled"><a href="" onclick="return false;">Next</a></li>';
  973. $this->assertEquals($expected, $result);
  974. $result = $this->Paginator->prev('Prev', [
  975. 'model' => 'Server'
  976. ]);
  977. $expected = [
  978. 'li' => ['class' => 'prev'],
  979. 'a' => ['href' => '/index?page=4', 'rel' => 'prev'],
  980. 'Prev',
  981. '/a',
  982. '/li'
  983. ];
  984. $this->assertHtml($expected, $result);
  985. }
  986. /**
  987. * testNumbers method
  988. *
  989. * @return void
  990. */
  991. public function testNumbers()
  992. {
  993. $this->Paginator->request->params['paging'] = [
  994. 'Client' => [
  995. 'page' => 8,
  996. 'current' => 3,
  997. 'count' => 30,
  998. 'prevPage' => false,
  999. 'nextPage' => 2,
  1000. 'pageCount' => 15,
  1001. ]
  1002. ];
  1003. $result = $this->Paginator->numbers();
  1004. $expected = [
  1005. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1006. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1007. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1008. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1009. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1010. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1011. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1012. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1013. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1014. ];
  1015. $this->assertHtml($expected, $result);
  1016. $result = $this->Paginator->numbers(['first' => 'first', 'last' => 'last']);
  1017. $expected = [
  1018. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], 'first', '/a', '/li',
  1019. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1020. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1021. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1022. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1023. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1024. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1025. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1026. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1027. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1028. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1029. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1030. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], 'last', '/a', '/li',
  1031. ];
  1032. $this->assertHtml($expected, $result);
  1033. $result = $this->Paginator->numbers(['first' => '2', 'last' => '8']);
  1034. $expected = [
  1035. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '2', '/a', '/li',
  1036. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1037. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1038. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1039. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1040. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1041. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1042. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1043. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1044. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1045. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1046. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1047. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
  1048. ];
  1049. $this->assertHtml($expected, $result);
  1050. $result = $this->Paginator->numbers(['first' => '8', 'last' => '8']);
  1051. $expected = [
  1052. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '8', '/a', '/li',
  1053. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1054. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1055. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1056. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1057. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1058. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1059. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1060. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1061. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1062. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1063. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1064. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
  1065. ];
  1066. $this->assertHtml($expected, $result);
  1067. $this->Paginator->request->params['paging'] = [
  1068. 'Client' => [
  1069. 'page' => 1,
  1070. 'current' => 3,
  1071. 'count' => 30,
  1072. 'prevPage' => false,
  1073. 'nextPage' => 2,
  1074. 'pageCount' => 15,
  1075. ]
  1076. ];
  1077. $result = $this->Paginator->numbers();
  1078. $expected = [
  1079. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1080. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1081. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1082. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1083. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1084. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1085. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1086. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1087. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1088. ];
  1089. $this->assertHtml($expected, $result);
  1090. $this->Paginator->request->params['paging'] = [
  1091. 'Client' => [
  1092. 'page' => 14,
  1093. 'current' => 3,
  1094. 'count' => 30,
  1095. 'prevPage' => false,
  1096. 'nextPage' => 2,
  1097. 'pageCount' => 15,
  1098. ]
  1099. ];
  1100. $result = $this->Paginator->numbers();
  1101. $expected = [
  1102. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1103. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1104. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1105. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1106. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1107. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1108. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1109. ['li' => ['class' => 'active']], '<a href=""', '14', '/a', '/li',
  1110. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1111. ];
  1112. $this->assertHtml($expected, $result);
  1113. $this->Paginator->request->params['paging'] = [
  1114. 'Client' => [
  1115. 'page' => 2,
  1116. 'current' => 3,
  1117. 'count' => 27,
  1118. 'prevPage' => false,
  1119. 'nextPage' => 2,
  1120. 'pageCount' => 9,
  1121. ]
  1122. ];
  1123. $result = $this->Paginator->numbers(['first' => 1]);
  1124. $expected = [
  1125. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1126. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1127. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1128. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1129. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1130. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1131. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1132. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1133. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1134. ];
  1135. $this->assertHtml($expected, $result);
  1136. $result = $this->Paginator->numbers(['last' => 1]);
  1137. $expected = [
  1138. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1139. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1140. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1141. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1142. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1143. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1144. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1145. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1146. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1147. ];
  1148. $this->assertHtml($expected, $result);
  1149. $this->Paginator->request->params['paging'] = [
  1150. 'Client' => [
  1151. 'page' => 15,
  1152. 'current' => 3,
  1153. 'count' => 30,
  1154. 'prevPage' => false,
  1155. 'nextPage' => 2,
  1156. 'pageCount' => 15,
  1157. ]
  1158. ];
  1159. $result = $this->Paginator->numbers(['first' => 1]);
  1160. $expected = [
  1161. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1162. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1163. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1164. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1165. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1166. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1167. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1168. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1169. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1170. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1171. ['li' => ['class' => 'active']], '<a href=""', '15', '/a', '/li',
  1172. ];
  1173. $this->assertHtml($expected, $result);
  1174. $this->Paginator->request->params['paging'] = [
  1175. 'Client' => [
  1176. 'page' => 10,
  1177. 'current' => 3,
  1178. 'count' => 30,
  1179. 'prevPage' => false,
  1180. 'nextPage' => 2,
  1181. 'pageCount' => 15,
  1182. ]
  1183. ];
  1184. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1185. $expected = [
  1186. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1187. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1188. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1189. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1190. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1191. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1192. ['li' => ['class' => 'active']], '<a href=""', '10', '/a', '/li',
  1193. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1194. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1195. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1196. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1197. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1198. ];
  1199. $this->assertHtml($expected, $result);
  1200. $this->Paginator->request->params['paging'] = [
  1201. 'Client' => [
  1202. 'page' => 6,
  1203. 'current' => 15,
  1204. 'count' => 623,
  1205. 'prevPage' => 1,
  1206. 'nextPage' => 1,
  1207. 'pageCount' => 42,
  1208. ]
  1209. ];
  1210. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1211. $expected = [
  1212. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1213. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1214. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1215. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1216. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1217. ['li' => ['class' => 'active']], '<a href=""', '6', '/a', '/li',
  1218. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1219. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1220. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1221. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1222. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1223. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1224. ];
  1225. $this->assertHtml($expected, $result);
  1226. $this->Paginator->request->params['paging'] = [
  1227. 'Client' => [
  1228. 'page' => 37,
  1229. 'current' => 15,
  1230. 'count' => 623,
  1231. 'prevPage' => 1,
  1232. 'nextPage' => 1,
  1233. 'pageCount' => 42,
  1234. ]
  1235. ];
  1236. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1237. $expected = [
  1238. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1239. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1240. ['li' => []], ['a' => ['href' => '/index?page=33']], '33', '/a', '/li',
  1241. ['li' => []], ['a' => ['href' => '/index?page=34']], '34', '/a', '/li',
  1242. ['li' => []], ['a' => ['href' => '/index?page=35']], '35', '/a', '/li',
  1243. ['li' => []], ['a' => ['href' => '/index?page=36']], '36', '/a', '/li',
  1244. ['li' => ['class' => 'active']], '<a href=""', '37', '/a', '/li',
  1245. ['li' => []], ['a' => ['href' => '/index?page=38']], '38', '/a', '/li',
  1246. ['li' => []], ['a' => ['href' => '/index?page=39']], '39', '/a', '/li',
  1247. ['li' => []], ['a' => ['href' => '/index?page=40']], '40', '/a', '/li',
  1248. ['li' => []], ['a' => ['href' => '/index?page=41']], '41', '/a', '/li',
  1249. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1250. ];
  1251. $this->assertHtml($expected, $result);
  1252. }
  1253. /**
  1254. * Test that numbers() lets you overwrite templates.
  1255. *
  1256. * The templates file has no li elements.
  1257. *
  1258. * @return void
  1259. */
  1260. public function testNumbersTemplates()
  1261. {
  1262. $this->Paginator->request->params['paging'] = [
  1263. 'Client' => [
  1264. 'page' => 8,
  1265. 'current' => 3,
  1266. 'count' => 30,
  1267. 'prevPage' => false,
  1268. 'nextPage' => 2,
  1269. 'pageCount' => 15,
  1270. ]
  1271. ];
  1272. $result = $this->Paginator->numbers(['templates' => 'htmlhelper_tags']);
  1273. $expected = [
  1274. ['a' => ['href' => '/index?page=4']], '4', '/a',
  1275. ['a' => ['href' => '/index?page=5']], '5', '/a',
  1276. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1277. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1278. 'span' => ['class' => 'active'], '8', '/span',
  1279. ['a' => ['href' => '/index?page=9']], '9', '/a',
  1280. ['a' => ['href' => '/index?page=10']], '10', '/a',
  1281. ['a' => ['href' => '/index?page=11']], '11', '/a',
  1282. ['a' => ['href' => '/index?page=12']], '12', '/a',
  1283. ];
  1284. $this->assertHtml($expected, $result);
  1285. $this->assertContains(
  1286. '<li',
  1287. $this->Paginator->templater()->get('current'),
  1288. 'Templates were not restored.'
  1289. );
  1290. }
  1291. /**
  1292. * Test modulus option for numbers()
  1293. *
  1294. * @return void
  1295. */
  1296. public function testNumbersModulus()
  1297. {
  1298. $this->Paginator->request->params['paging'] = [
  1299. 'Client' => [
  1300. 'page' => 1,
  1301. 'current' => 10,
  1302. 'count' => 30,
  1303. 'prevPage' => false,
  1304. 'nextPage' => 2,
  1305. 'pageCount' => 3,
  1306. ]
  1307. ];
  1308. $result = $this->Paginator->numbers(['modulus' => 10]);
  1309. $expected = [
  1310. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1311. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1312. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1313. ];
  1314. $this->assertHtml($expected, $result);
  1315. $result = $this->Paginator->numbers(['modulus' => 3]);
  1316. $expected = [
  1317. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1318. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1319. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1320. ];
  1321. $this->assertHtml($expected, $result);
  1322. $this->Paginator->request->params['paging'] = [
  1323. 'Client' => [
  1324. 'page' => 4895,
  1325. 'current' => 10,
  1326. 'count' => 48962,
  1327. 'prevPage' => 1,
  1328. 'nextPage' => 1,
  1329. 'pageCount' => 4897,
  1330. ]
  1331. ];
  1332. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1333. $expected = [
  1334. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1335. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1336. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1337. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1338. ['li' => ['class' => 'active']], '<a href=""', '4895', '/a', '/li',
  1339. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1340. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1341. ];
  1342. $this->assertHtml($expected, $result);
  1343. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1344. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1345. $expected = [
  1346. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1347. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1348. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1349. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1350. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1351. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1352. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1353. ];
  1354. $this->assertHtml($expected, $result);
  1355. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1356. $expected = [
  1357. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1358. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1359. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1360. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1361. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1362. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1363. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 5, 'last' => 5]);
  1367. $expected = [
  1368. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1369. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1370. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1371. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1372. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1373. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1374. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1375. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4893', '/a', '/li',
  1376. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1377. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1378. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1379. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1380. ];
  1381. $this->assertHtml($expected, $result);
  1382. $this->Paginator->request->params['paging']['Client']['page'] = 4893;
  1383. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1384. $expected = [
  1385. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1386. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1387. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1388. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1389. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1390. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1391. ['li' => []], ['a' => ['href' => '/index?page=4891']], '4891', '/a', '/li',
  1392. ['li' => []], ['a' => ['href' => '/index?page=4892']], '4892', '/a', '/li',
  1393. ['li' => ['class' => 'active']], '<a href=""', '4893', '/a', '/li',
  1394. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1395. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1396. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1397. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1398. ];
  1399. $this->assertHtml($expected, $result);
  1400. $this->Paginator->request->params['paging']['Client']['page'] = 58;
  1401. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1402. $expected = [
  1403. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1404. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1405. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1406. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1407. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1408. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1409. ['li' => []], ['a' => ['href' => '/index?page=56']], '56', '/a', '/li',
  1410. ['li' => []], ['a' => ['href' => '/index?page=57']], '57', '/a', '/li',
  1411. ['li' => ['class' => 'active']], '<a href=""', '58', '/a', '/li',
  1412. ['li' => []], ['a' => ['href' => '/index?page=59']], '59', '/a', '/li',
  1413. ['li' => []], ['a' => ['href' => '/index?page=60']], '60', '/a', '/li',
  1414. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1415. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4893', '/a', '/li',
  1416. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1417. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1418. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1419. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1420. ];
  1421. $this->assertHtml($expected, $result);
  1422. $this->Paginator->request->params['paging']['Client']['page'] = 5;
  1423. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1424. $expected = [
  1425. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1426. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1427. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1428. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1429. ['li' => ['class' => 'active']], '<a href=""', '5', '/a', '/li',
  1430. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1431. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1432. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1433. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4893', '/a', '/li',
  1434. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1435. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1436. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1437. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1438. ];
  1439. $this->assertHtml($expected, $result);
  1440. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1441. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1442. $expected = [
  1443. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1444. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1445. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1446. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1447. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1448. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1449. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1450. ];
  1451. $this->assertHtml($expected, $result);
  1452. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1453. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 0, 'last' => 2]);
  1454. $expected = [
  1455. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1456. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1457. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1458. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1459. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1460. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1461. ];
  1462. $this->assertHtml($expected, $result);
  1463. }
  1464. /**
  1465. * Tests that disabling modulus displays all page links.
  1466. *
  1467. * @return void
  1468. */
  1469. public function testModulusDisabled()
  1470. {
  1471. $this->Paginator->request->params['paging'] = [
  1472. 'Client' => [
  1473. 'page' => 4,
  1474. 'current' => 2,
  1475. 'count' => 30,
  1476. 'prevPage' => 1,
  1477. 'nextPage' => 1,
  1478. 'pageCount' => 6,
  1479. ]
  1480. ];
  1481. $result = $this->Paginator->numbers(['modulus' => false]);
  1482. $expected = [
  1483. ['li' => []], '<a href="/index"', '1', '/a', '/li',
  1484. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1485. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1486. ['li' => ['class' => 'active']], ['a' => ['href' => '']], '4', '/a', '/li',
  1487. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1488. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1489. ];
  1490. $this->assertHtml($expected, $result);
  1491. }
  1492. /**
  1493. * Test that numbers() with url options.
  1494. *
  1495. * @return void
  1496. */
  1497. public function testNumbersWithUrlOptions()
  1498. {
  1499. $this->Paginator->request->params['paging'] = [
  1500. 'Client' => [
  1501. 'page' => 8,
  1502. 'current' => 3,
  1503. 'count' => 30,
  1504. 'prevPage' => false,
  1505. 'nextPage' => 2,
  1506. 'pageCount' => 15,
  1507. ]
  1508. ];
  1509. $result = $this->Paginator->numbers(['url' => ['#' => 'foo']]);
  1510. $expected = [
  1511. ['li' => []], ['a' => ['href' => '/index?page=4#foo']], '4', '/a', '/li',
  1512. ['li' => []], ['a' => ['href' => '/index?page=5#foo']], '5', '/a', '/li',
  1513. ['li' => []], ['a' => ['href' => '/index?page=6#foo']], '6', '/a', '/li',
  1514. ['li' => []], ['a' => ['href' => '/index?page=7#foo']], '7', '/a', '/li',
  1515. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1516. ['li' => []], ['a' => ['href' => '/index?page=9#foo']], '9', '/a', '/li',
  1517. ['li' => []], ['a' => ['href' => '/index?page=10#foo']], '10', '/a', '/li',
  1518. ['li' => []], ['a' => ['href' => '/index?page=11#foo']], '11', '/a', '/li',
  1519. ['li' => []], ['a' => ['href' => '/index?page=12#foo']], '12', '/a', '/li',
  1520. ];
  1521. $this->assertHtml($expected, $result);
  1522. $this->Paginator->request->params['paging'] = [
  1523. 'Client' => [
  1524. 'page' => 3,
  1525. 'current' => 10,
  1526. 'count' => 48962,
  1527. 'prevPage' => 1,
  1528. 'nextPage' => 1,
  1529. 'pageCount' => 4897,
  1530. ]
  1531. ];
  1532. $result = $this->Paginator->numbers([
  1533. 'first' => 2,
  1534. 'modulus' => 2,
  1535. 'last' => 2,
  1536. 'url' => ['foo' => 'bar']]);
  1537. $expected = [
  1538. ['li' => []], ['a' => ['href' => '/index?foo=bar']], '1', '/a', '/li',
  1539. ['li' => []], ['a' => ['href' => '/index?page=2&amp;foo=bar']], '2', '/a', '/li',
  1540. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1541. ['li' => []], ['a' => ['href' => '/index?page=4&amp;foo=bar']], '4', '/a', '/li',
  1542. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1543. ['li' => []], ['a' => ['href' => '/index?page=4896&amp;foo=bar']], '4896', '/a', '/li',
  1544. ['li' => []], ['a' => ['href' => '/index?page=4897&amp;foo=bar']], '4897', '/a', '/li',
  1545. ];
  1546. $this->assertHtml($expected, $result);
  1547. }
  1548. /**
  1549. * test numbers() with routing parameters.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testNumbersRouting()
  1554. {
  1555. $this->Paginator->request->params['paging'] = [
  1556. 'Client' => [
  1557. 'page' => 2,
  1558. 'current' => 2,
  1559. 'count' => 30,
  1560. 'prevPage' => false,
  1561. 'nextPage' => 3,
  1562. 'pageCount' => 3,
  1563. ]
  1564. ];
  1565. $request = new Request();
  1566. $request->addParams([
  1567. 'controller' => 'clients', 'action' => 'index', 'plugin' => null
  1568. ]);
  1569. $request->base = '';
  1570. $request->here = '/clients/index?page=2';
  1571. $request->webroot = '/';
  1572. Router::setRequestInfo($request);
  1573. $result = $this->Paginator->numbers();
  1574. $expected = [
  1575. ['li' => []], ['a' => ['href' => '/clients/index']], '1', '/a', '/li',
  1576. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1577. ['li' => []], ['a' => ['href' => '/clients/index?page=3']], '3', '/a', '/li',
  1578. ];
  1579. $this->assertHtml($expected, $result);
  1580. }
  1581. /**
  1582. * Test that numbers() works with the non default model.
  1583. *
  1584. * @return void
  1585. */
  1586. public function testNumbersNonDefaultModel()
  1587. {
  1588. $this->Paginator->request->params['paging'] = [
  1589. 'Client' => [
  1590. 'page' => 1,
  1591. 'current' => 3,
  1592. 'count' => 13,
  1593. 'prevPage' => false,
  1594. 'nextPage' => true,
  1595. 'pageCount' => 5,
  1596. ],
  1597. 'Server' => [
  1598. 'page' => 5,
  1599. 'current' => 1,
  1600. 'count' => 5,
  1601. 'prevPage' => true,
  1602. 'nextPage' => false,
  1603. 'pageCount' => 5,
  1604. ]
  1605. ];
  1606. $result = $this->Paginator->numbers(['model' => 'Server']);
  1607. $this->assertContains('<li class="active"><a href="">5</a></li>', $result);
  1608. $this->assertNotContains('<li class="active"><a href="">1</a></li>', $result);
  1609. $result = $this->Paginator->numbers(['model' => 'Client']);
  1610. $this->assertContains('<li class="active"><a href="">1</a></li>', $result);
  1611. $this->assertNotContains('<li class="active"><a href="">5</a></li>', $result);
  1612. }
  1613. /**
  1614. * test first() and last() with tag options
  1615. *
  1616. * @return void
  1617. */
  1618. public function testFirstAndLastTag()
  1619. {
  1620. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1621. $result = $this->Paginator->first('<<');
  1622. $expected = [
  1623. 'li' => ['class' => 'first'],
  1624. 'a' => ['href' => '/index'],
  1625. '&lt;&lt;',
  1626. '/a',
  1627. '/li'
  1628. ];
  1629. $this->assertHtml($expected, $result);
  1630. $result = $this->Paginator->first('5');
  1631. $expected = [
  1632. 'li' => ['class' => 'first'],
  1633. 'a' => ['href' => '/index'],
  1634. '5',
  1635. '/a',
  1636. '/li'
  1637. ];
  1638. $this->assertHtml($expected, $result);
  1639. $result = $this->Paginator->last(2);
  1640. $expected = [
  1641. '<li',
  1642. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1643. '/li',
  1644. '<li',
  1645. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1646. '/li',
  1647. ];
  1648. $this->assertHtml($expected, $result);
  1649. $result = $this->Paginator->last('9');
  1650. $expected = [
  1651. 'li' => ['class' => 'last'],
  1652. 'a' => ['href' => '/index?page=7'],
  1653. '9',
  1654. '/a',
  1655. '/li'
  1656. ];
  1657. $this->assertHtml($expected, $result);
  1658. }
  1659. /**
  1660. * test that on the last page you don't get a link ot the last page.
  1661. *
  1662. * @return void
  1663. */
  1664. public function testLastNoOutput()
  1665. {
  1666. $this->Paginator->request->params['paging']['Article']['page'] = 15;
  1667. $this->Paginator->request->params['paging']['Article']['pageCount'] = 15;
  1668. $result = $this->Paginator->last();
  1669. $expected = '';
  1670. $this->assertEquals($expected, $result);
  1671. }
  1672. /**
  1673. * test first() with a the model parameter.
  1674. *
  1675. * @return void
  1676. */
  1677. public function testFirstNonDefaultModel()
  1678. {
  1679. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1680. $this->Paginator->request->params['paging']['Client'] = [
  1681. 'page' => 3,
  1682. 'current' => 3,
  1683. 'count' => 13,
  1684. 'prevPage' => false,
  1685. 'nextPage' => true,
  1686. 'pageCount' => 5,
  1687. ];
  1688. $result = $this->Paginator->first('first', ['model' => 'Article']);
  1689. $this->assertEquals('', $result);
  1690. $result = $this->Paginator->first('first', ['model' => 'Client']);
  1691. $expected = [
  1692. 'li' => ['class' => 'first'],
  1693. 'a' => ['href' => '/index'],
  1694. 'first',
  1695. '/a',
  1696. '/li'
  1697. ];
  1698. $this->assertHtml($expected, $result);
  1699. }
  1700. /**
  1701. * test first() on the first page.
  1702. *
  1703. * @return void
  1704. */
  1705. public function testFirstEmpty()
  1706. {
  1707. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1708. $result = $this->Paginator->first();
  1709. $expected = '';
  1710. $this->assertEquals($expected, $result);
  1711. }
  1712. /**
  1713. * test first() and options()
  1714. *
  1715. * @return void
  1716. */
  1717. public function testFirstFullBaseUrl()
  1718. {
  1719. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1720. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  1721. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1722. $this->Paginator->options(['url' => ['_full' => true]]);
  1723. $result = $this->Paginator->first();
  1724. $expected = [
  1725. 'li' => ['class' => 'first'],
  1726. ['a' => [
  1727. 'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC'
  1728. ]],
  1729. '&lt;&lt; first',
  1730. '/a',
  1731. '/li',
  1732. ];
  1733. $this->assertHtml($expected, $result);
  1734. }
  1735. /**
  1736. * test first() on the fence-post
  1737. *
  1738. * @return void
  1739. */
  1740. public function testFirstBoundaries()
  1741. {
  1742. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1743. $result = $this->Paginator->first();
  1744. $expected = [
  1745. 'li' => ['class' => 'first'],
  1746. 'a' => ['href' => '/index'],
  1747. '&lt;&lt; first',
  1748. '/a',
  1749. '/li'
  1750. ];
  1751. $this->assertHtml($expected, $result);
  1752. $result = $this->Paginator->first(2);
  1753. $expected = [
  1754. '<li',
  1755. ['a' => ['href' => '/index']], '1', '/a',
  1756. '/li',
  1757. '<li',
  1758. ['a' => ['href' => '/index?page=2']], '2', '/a',
  1759. '/li'
  1760. ];
  1761. $this->assertHtml($expected, $result);
  1762. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1763. $result = $this->Paginator->first(3);
  1764. $this->assertEquals('', $result, 'When inside the first links range, no links should be made');
  1765. }
  1766. /**
  1767. * test params() method
  1768. *
  1769. * @return void
  1770. */
  1771. public function testParams()
  1772. {
  1773. $result = $this->Paginator->params();
  1774. $this->assertArrayHasKey('page', $result);
  1775. $this->assertArrayHasKey('pageCount', $result);
  1776. $result = $this->Paginator->params('Nope');
  1777. $this->assertEquals([], $result);
  1778. }
  1779. /**
  1780. * test param() method
  1781. *
  1782. * @return void
  1783. */
  1784. public function testParam()
  1785. {
  1786. $result = $this->Paginator->param('count');
  1787. $this->assertSame(62, $result);
  1788. $result = $this->Paginator->param('imaginary');
  1789. $this->assertNull($result);
  1790. }
  1791. /**
  1792. * test last() method
  1793. *
  1794. * @return void
  1795. */
  1796. public function testLast()
  1797. {
  1798. $result = $this->Paginator->last();
  1799. $expected = [
  1800. 'li' => ['class' => 'last'],
  1801. 'a' => ['href' => '/index?page=7'],
  1802. 'last &gt;&gt;',
  1803. '/a',
  1804. '/li'
  1805. ];
  1806. $this->assertHtml($expected, $result);
  1807. $result = $this->Paginator->last(1);
  1808. $expected = [
  1809. '<li',
  1810. 'a' => ['href' => '/index?page=7'],
  1811. '7',
  1812. '/a',
  1813. '/li'
  1814. ];
  1815. $this->assertHtml($expected, $result);
  1816. $this->Paginator->request->params['paging']['Article']['page'] = 6;
  1817. $result = $this->Paginator->last(2);
  1818. $expected = [
  1819. '<li',
  1820. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1821. '/li',
  1822. '<li',
  1823. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1824. '/li',
  1825. ];
  1826. $this->assertHtml($expected, $result);
  1827. $result = $this->Paginator->last(3);
  1828. $this->assertEquals('', $result, 'When inside the last links range, no links should be made');
  1829. }
  1830. /**
  1831. * test the options for last()
  1832. *
  1833. * @return void
  1834. */
  1835. public function testLastOptions()
  1836. {
  1837. $this->Paginator->request->params['paging'] = [
  1838. 'Client' => [
  1839. 'page' => 4,
  1840. 'current' => 3,
  1841. 'count' => 30,
  1842. 'prevPage' => false,
  1843. 'nextPage' => 2,
  1844. 'pageCount' => 15,
  1845. 'sort' => 'Client.name',
  1846. 'direction' => 'DESC',
  1847. ]
  1848. ];
  1849. $result = $this->Paginator->last();
  1850. $expected = [
  1851. 'li' => ['class' => 'last'],
  1852. 'a' => [
  1853. 'href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC',
  1854. ],
  1855. 'last &gt;&gt;', '/a',
  1856. '/li',
  1857. ];
  1858. $this->assertHtml($expected, $result);
  1859. $result = $this->Paginator->last(1);
  1860. $expected = [
  1861. '<li',
  1862. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  1863. '/li',
  1864. ];
  1865. $this->assertHtml($expected, $result);
  1866. $result = $this->Paginator->last(2);
  1867. $expected = [
  1868. '<li',
  1869. ['a' => ['href' => '/index?page=14&amp;sort=Client.name&amp;direction=DESC']], '14', '/a',
  1870. '/li',
  1871. '<li',
  1872. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  1873. '/li',
  1874. ];
  1875. $this->assertHtml($expected, $result);
  1876. }
  1877. /**
  1878. * test last() with a the model parameter.
  1879. *
  1880. * @return void
  1881. */
  1882. public function testLastNonDefaultModel()
  1883. {
  1884. $this->Paginator->request->params['paging']['Article']['page'] = 7;
  1885. $this->Paginator->request->params['paging']['Client'] = [
  1886. 'page' => 3,
  1887. 'current' => 3,
  1888. 'count' => 13,
  1889. 'prevPage' => false,
  1890. 'nextPage' => true,
  1891. 'pageCount' => 5,
  1892. ];
  1893. $result = $this->Paginator->last('last', ['model' => 'Article']);
  1894. $this->assertEquals('', $result);
  1895. $result = $this->Paginator->last('last', ['model' => 'Client']);
  1896. $expected = [
  1897. 'li' => ['class' => 'last'],
  1898. 'a' => ['href' => '/index?page=5'],
  1899. 'last',
  1900. '/a',
  1901. '/li'
  1902. ];
  1903. $this->assertHtml($expected, $result);
  1904. }
  1905. /**
  1906. * testCounter method
  1907. *
  1908. * @return void
  1909. */
  1910. public function testCounter()
  1911. {
  1912. $this->Paginator->request->params['paging'] = [
  1913. 'Client' => [
  1914. 'page' => 1,
  1915. 'current' => 3,
  1916. 'count' => 13,
  1917. 'perPage' => 3,
  1918. 'prevPage' => false,
  1919. 'nextPage' => true,
  1920. 'pageCount' => 5,
  1921. 'limit' => 3,
  1922. 'sort' => 'Client.name',
  1923. 'order' => 'DESC',
  1924. ]
  1925. ];
  1926. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  1927. $input .= 'starting on record {{start}}, ending on {{end}}';
  1928. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
  1929. $expected .= 'ending on 3';
  1930. $result = $this->Paginator->counter($input);
  1931. $this->assertEquals($expected, $result);
  1932. $result = $this->Paginator->counter(['format' => 'pages']);
  1933. $expected = '1 of 5';
  1934. $this->assertEquals($expected, $result);
  1935. $result = $this->Paginator->counter(['format' => 'range']);
  1936. $expected = '1 - 3 of 13';
  1937. $this->assertEquals($expected, $result);
  1938. $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}');
  1939. $this->assertEquals('Showing 1 of 5 clients', $result);
  1940. }
  1941. /**
  1942. * Tests that numbers are formatted according to the locale when using counter()
  1943. *
  1944. * @return void
  1945. */
  1946. public function testCounterBigNumbers()
  1947. {
  1948. $this->Paginator->request->params['paging'] = [
  1949. 'Client' => [
  1950. 'page' => 1523,
  1951. 'current' => 1230,
  1952. 'count' => 234567,
  1953. 'perPage' => 3000,
  1954. 'prevPage' => false,
  1955. 'nextPage' => true,
  1956. 'pageCount' => 1000,
  1957. 'limit' => 5000,
  1958. 'sort' => 'Client.name',
  1959. 'order' => 'DESC',
  1960. ]
  1961. ];
  1962. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  1963. $input .= 'starting on record {{start}}, ending on {{end}}';
  1964. $expected = 'Page 1,523 of 1,000, showing 1,230 records out of 234,567 total, ';
  1965. $expected .= 'starting on record 4,566,001, ending on 234,567';
  1966. $result = $this->Paginator->counter($input);
  1967. $this->assertEquals($expected, $result);
  1968. I18n::locale('de-DE');
  1969. $expected = 'Page 1.523 of 1.000, showing 1.230 records out of 234.567 total, ';
  1970. $expected .= 'starting on record 4.566.001, ending on 234.567';
  1971. $result = $this->Paginator->counter($input);
  1972. $this->assertEquals($expected, $result);
  1973. }
  1974. /**
  1975. * testHasPage method
  1976. *
  1977. * @return void
  1978. */
  1979. public function testHasPage()
  1980. {
  1981. $result = $this->Paginator->hasPage('Article', 15);
  1982. $this->assertFalse($result);
  1983. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  1984. $this->assertFalse($result);
  1985. $result = $this->Paginator->hasPage('Article', 2);
  1986. $this->assertTrue($result);
  1987. $result = $this->Paginator->hasPage(2);
  1988. $this->assertTrue($result);
  1989. }
  1990. /**
  1991. * testNextLinkUsingDotNotation method
  1992. *
  1993. * @return void
  1994. */
  1995. public function testNextLinkUsingDotNotation()
  1996. {
  1997. Router::setRequestInfo([
  1998. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  1999. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  2000. ]);
  2001. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  2002. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  2003. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  2004. $test = ['url' => [
  2005. 'page' => '1',
  2006. 'sort' => 'Article.title',
  2007. 'direction' => 'asc',
  2008. ]];
  2009. $this->Paginator->options($test);
  2010. $result = $this->Paginator->next('Next');
  2011. $expected = [
  2012. 'li' => ['class' => 'next'],
  2013. 'a' => [
  2014. 'href' => '/accounts/index?page=2&amp;sort=Article.title&amp;direction=asc',
  2015. 'rel' => 'next'
  2016. ],
  2017. 'Next',
  2018. '/a',
  2019. '/li',
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. }
  2023. /**
  2024. * test the current() method
  2025. *
  2026. * @return void
  2027. */
  2028. public function testCurrent()
  2029. {
  2030. $result = $this->Paginator->current();
  2031. $this->assertEquals($this->Paginator->request->params['paging']['Article']['page'], $result);
  2032. $result = $this->Paginator->current('Incorrect');
  2033. $this->assertEquals(1, $result);
  2034. }
  2035. /**
  2036. * test the defaultModel() method
  2037. *
  2038. * @return void
  2039. */
  2040. public function testNoDefaultModel()
  2041. {
  2042. $this->Paginator->request = new Request();
  2043. $this->assertNull($this->Paginator->defaultModel());
  2044. }
  2045. /**
  2046. * test the numbers() method when there is only one page
  2047. *
  2048. * @return void
  2049. */
  2050. public function testWithOnePage()
  2051. {
  2052. $this->Paginator->request['paging'] = [
  2053. 'Article' => [
  2054. 'page' => 1,
  2055. 'current' => 2,
  2056. 'count' => 2,
  2057. 'prevPage' => false,
  2058. 'nextPage' => true,
  2059. 'pageCount' => 1,
  2060. ]
  2061. ];
  2062. $this->assertFalse($this->Paginator->numbers());
  2063. $this->assertFalse($this->Paginator->first());
  2064. $this->assertFalse($this->Paginator->last());
  2065. }
  2066. /**
  2067. * test the numbers() method when there is only one page
  2068. *
  2069. * @return void
  2070. */
  2071. public function testWithZeroPages()
  2072. {
  2073. $this->Paginator->request['paging'] = [
  2074. 'Article' => [
  2075. 'page' => 0,
  2076. 'current' => 0,
  2077. 'count' => 0,
  2078. 'perPage' => 10,
  2079. 'prevPage' => false,
  2080. 'nextPage' => false,
  2081. 'pageCount' => 0,
  2082. 'limit' => 10,
  2083. ]
  2084. ];
  2085. $result = $this->Paginator->counter(['format' => 'pages']);
  2086. $expected = '0 of 1';
  2087. $this->assertEquals($expected, $result);
  2088. }
  2089. /**
  2090. * Verifies that no next and prev links are created for single page results.
  2091. *
  2092. * @return void
  2093. */
  2094. public function testMetaPage0()
  2095. {
  2096. $this->Paginator->request['paging'] = [
  2097. 'Article' => [
  2098. 'page' => 1,
  2099. 'prevPage' => false,
  2100. 'nextPage' => false,
  2101. 'pageCount' => 1,
  2102. ]
  2103. ];
  2104. $expected = '';
  2105. $result = $this->Paginator->meta();
  2106. $this->assertSame($expected, $result);
  2107. }
  2108. /**
  2109. * Verifies that page 1 only has a next link.
  2110. *
  2111. * @return void
  2112. */
  2113. public function testMetaPage1()
  2114. {
  2115. $this->Paginator->request['paging'] = [
  2116. 'Article' => [
  2117. 'page' => 1,
  2118. 'prevPage' => false,
  2119. 'nextPage' => true,
  2120. 'pageCount' => 2,
  2121. ]
  2122. ];
  2123. $expected = '<link rel="next" href="http://localhost/index?page=2"/>';
  2124. $result = $this->Paginator->meta();
  2125. $this->assertSame($expected, $result);
  2126. }
  2127. /**
  2128. * Verifies that the method will append to a block.
  2129. *
  2130. * @return void
  2131. */
  2132. public function testMetaPage1InlineFalse()
  2133. {
  2134. $this->Paginator->request['paging'] = [
  2135. 'Article' => [
  2136. 'page' => 1,
  2137. 'prevPage' => false,
  2138. 'nextPage' => true,
  2139. 'pageCount' => 2,
  2140. ]
  2141. ];
  2142. $expected = '<link rel="next" href="http://localhost/index?page=2"/>';
  2143. $this->Paginator->meta(['block' => true]);
  2144. $result = $this->View->fetch('meta');
  2145. $this->assertSame($expected, $result);
  2146. }
  2147. /**
  2148. * Verifies that the last page only has a prev link.
  2149. *
  2150. * @return void
  2151. */
  2152. public function testMetaPage1Last()
  2153. {
  2154. $this->Paginator->request['paging'] = [
  2155. 'Article' => [
  2156. 'page' => 2,
  2157. 'prevPage' => true,
  2158. 'nextPage' => false,
  2159. 'pageCount' => 2,
  2160. ]
  2161. ];
  2162. $expected = '<link rel="prev" href="http://localhost/index"/>';
  2163. $result = $this->Paginator->meta();
  2164. $this->assertSame($expected, $result);
  2165. }
  2166. /**
  2167. * Verifies that a page in the middle has both links.
  2168. *
  2169. * @return void
  2170. */
  2171. public function testMetaPage10Last()
  2172. {
  2173. $this->Paginator->request['paging'] = [
  2174. 'Article' => [
  2175. 'page' => 5,
  2176. 'prevPage' => true,
  2177. 'nextPage' => true,
  2178. 'pageCount' => 10,
  2179. ]
  2180. ];
  2181. $expected = '<link rel="prev" href="http://localhost/index?page=4"/>';
  2182. $expected .= '<link rel="next" href="http://localhost/index?page=6"/>';
  2183. $result = $this->Paginator->meta();
  2184. $this->assertSame($expected, $result);
  2185. }
  2186. }