PaginatorHelperTest.php 92 KB

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