PaginatorHelperTest.php 79 KB

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