PaginatorHelperTest.php 93 KB

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