PaginatorHelperTest.php 87 KB

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