PaginatorHelperTest.php 95 KB

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