PaginatorHelperTest.php 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\Configure;
  17. use Cake\Http\ServerRequest;
  18. use Cake\I18n\I18n;
  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 ServerRequest();
  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::getLocale();
  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::setLocale($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. 'Tags' => [
  413. 'current' => 1,
  414. 'count' => 100,
  415. 'prevPage' => false,
  416. 'nextPage' => true,
  417. 'pageCount' => 5,
  418. 'sort' => 'tag',
  419. 'direction' => 'asc',
  420. 'page' => 1,
  421. 'scope' => 'tags',
  422. ]
  423. ];
  424. $result = $this->Paginator->sort('title', 'Title', ['model' => 'Articles']);
  425. $expected = [
  426. 'a' => ['href' => '/accounts/index?article%5Bsort%5D=title&amp;article%5Bdirection%5D=asc'],
  427. 'Title',
  428. '/a'
  429. ];
  430. $this->assertHtml($expected, $result);
  431. $result = $this->Paginator->sort('tag', 'Tag', ['model' => 'Tags']);
  432. $expected = [
  433. 'a' => ['class' => 'asc', 'href' => '/accounts/index?tags%5Bsort%5D=tag&amp;tags%5Bdirection%5D=desc'],
  434. 'Tag',
  435. '/a'
  436. ];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test creating paging links for missing models.
  441. *
  442. * @return void
  443. */
  444. public function testPagingLinksMissingModel()
  445. {
  446. $result = $this->Paginator->sort('title', 'Title', ['model' => 'Missing']);
  447. $expected = [
  448. 'a' => ['href' => '/index?sort=title&amp;direction=asc'],
  449. 'Title',
  450. '/a'
  451. ];
  452. $this->assertHtml($expected, $result);
  453. $result = $this->Paginator->next('Next', ['model' => 'Missing']);
  454. $expected = [
  455. 'li' => ['class' => 'next disabled'],
  456. 'a' => ['href' => '', 'onclick' => 'return false;'],
  457. 'Next',
  458. '/a',
  459. '/li'
  460. ];
  461. $this->assertHtml($expected, $result);
  462. $result = $this->Paginator->prev('Prev', ['model' => 'Missing']);
  463. $expected = [
  464. 'li' => ['class' => 'prev disabled'],
  465. 'a' => ['href' => '', 'onclick' => 'return false;'],
  466. 'Prev',
  467. '/a',
  468. '/li'
  469. ];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * testSortKey method
  474. *
  475. * @return void
  476. */
  477. public function testSortKey()
  478. {
  479. $result = $this->Paginator->sortKey('Article', ['sort' => 'Article.title']);
  480. $this->assertEquals('Article.title', $result);
  481. $result = $this->Paginator->sortKey('Article', ['sort' => 'Article']);
  482. $this->assertEquals('Article', $result);
  483. }
  484. /**
  485. * Test that sortKey falls back to the default sorting options set
  486. * in the $params which are the default pagination options.
  487. *
  488. * @return void
  489. */
  490. public function testSortKeyFallbackToParams()
  491. {
  492. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  493. $result = $this->Paginator->sortKey();
  494. $this->assertEquals('Article.body', $result);
  495. $result = $this->Paginator->sortKey('Article');
  496. $this->assertEquals('Article.body', $result);
  497. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  498. $this->Paginator->request->params['paging']['Article']['order'] = 'DESC';
  499. $result = $this->Paginator->sortKey();
  500. $this->assertEquals('Article.body', $result);
  501. $result = $this->Paginator->sortKey('Article');
  502. $this->assertEquals('Article.body', $result);
  503. }
  504. /**
  505. * testSortDir method
  506. *
  507. * @return void
  508. */
  509. public function testSortDir()
  510. {
  511. $result = $this->Paginator->sortDir();
  512. $expected = 'asc';
  513. $this->assertEquals($expected, $result);
  514. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  515. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  516. $result = $this->Paginator->sortDir();
  517. $this->assertEquals('desc', $result);
  518. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  519. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  520. $result = $this->Paginator->sortDir();
  521. $this->assertEquals('asc', $result);
  522. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  523. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  524. $result = $this->Paginator->sortDir();
  525. $this->assertEquals('desc', $result);
  526. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  527. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  528. $result = $this->Paginator->sortDir();
  529. $this->assertEquals('asc', $result);
  530. unset($this->Paginator->request->params['paging']['Article']['direction']);
  531. $result = $this->Paginator->sortDir('Article', ['direction' => 'asc']);
  532. $this->assertEquals('asc', $result);
  533. $result = $this->Paginator->sortDir('Article', ['direction' => 'desc']);
  534. $this->assertEquals('desc', $result);
  535. $result = $this->Paginator->sortDir('Article', ['direction' => 'asc']);
  536. $this->assertEquals('asc', $result);
  537. }
  538. /**
  539. * Test that sortDir falls back to the default sorting options set
  540. * in the $params which are the default pagination options.
  541. *
  542. * @return void
  543. */
  544. public function testSortDirFallbackToParams()
  545. {
  546. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  547. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  548. $result = $this->Paginator->sortDir();
  549. $this->assertEquals('asc', $result);
  550. $result = $this->Paginator->sortDir('Article');
  551. $this->assertEquals('asc', $result);
  552. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  553. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  554. $result = $this->Paginator->sortDir();
  555. $this->assertEquals('desc', $result);
  556. $result = $this->Paginator->sortDir('Article');
  557. $this->assertEquals('desc', $result);
  558. }
  559. /**
  560. * testSortAdminLinks method
  561. *
  562. * @return void
  563. */
  564. public function testSortAdminLinks()
  565. {
  566. Router::reload();
  567. Router::connect('/admin/:controller/:action/*', ['prefix' => 'admin']);
  568. Router::setRequestInfo([
  569. ['controller' => 'users', 'plugin' => null, 'action' => 'index', 'prefix' => 'admin'],
  570. ['base' => '', 'here' => '/admin/users', 'webroot' => '/']
  571. ]);
  572. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  573. $result = $this->Paginator->next('Next');
  574. $expected = [
  575. 'li' => ['class' => 'next'],
  576. 'a' => ['href' => '/admin/users/index?page=2', 'rel' => 'next'],
  577. 'Next',
  578. '/a',
  579. '/li'
  580. ];
  581. $this->assertHtml($expected, $result);
  582. $this->Paginator->options(['url' => ['param']]);
  583. $result = $this->Paginator->sort('title');
  584. $expected = [
  585. 'a' => ['href' => '/admin/users/index/param?sort=title&amp;direction=asc'],
  586. 'Title',
  587. '/a'
  588. ];
  589. $this->assertHtml($expected, $result);
  590. $this->Paginator->options(['url' => ['param']]);
  591. $result = $this->Paginator->sort('Article.title', 'Title');
  592. $expected = [
  593. 'a' => ['href' => '/admin/users/index/param?sort=Article.title&amp;direction=asc'],
  594. 'Title',
  595. '/a'
  596. ];
  597. $this->assertHtml($expected, $result);
  598. }
  599. /**
  600. * Test that generated URLs work without sort defined within the request
  601. *
  602. * @return void
  603. */
  604. public function testDefaultSortAndNoSort()
  605. {
  606. Router::setRequestInfo([
  607. ['plugin' => null, 'controller' => 'articles', 'action' => 'index'],
  608. ['base' => '/', 'here' => '/articles/', 'webroot' => '/']
  609. ]);
  610. $this->Paginator->request->params['paging'] = [
  611. 'Article' => [
  612. 'page' => 1, 'current' => 3, 'count' => 13,
  613. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  614. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC',
  615. 'sort' => null
  616. ]
  617. ];
  618. $result = $this->Paginator->next('Next');
  619. $expected = [
  620. 'li' => ['class' => 'next'],
  621. 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'],
  622. 'Next',
  623. '/a',
  624. '/li'
  625. ];
  626. $this->assertHtml($expected, $result);
  627. }
  628. /**
  629. * testUrlGeneration method
  630. *
  631. * @return void
  632. */
  633. public function testUrlGeneration()
  634. {
  635. $result = $this->Paginator->sort('controller');
  636. $expected = [
  637. 'a' => ['href' => '/index?sort=controller&amp;direction=asc'],
  638. 'Controller',
  639. '/a'
  640. ];
  641. $this->assertHtml($expected, $result);
  642. $result = $this->Paginator->generateUrl();
  643. $this->assertEquals('/index', $result);
  644. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  645. $result = $this->Paginator->generateUrl();
  646. $this->assertEquals('/index?page=2', $result);
  647. $options = ['sort' => 'Article', 'direction' => 'desc'];
  648. $result = $this->Paginator->generateUrl($options);
  649. $this->assertEquals('/index?page=2&amp;sort=Article&amp;direction=desc', $result);
  650. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  651. $options = ['sort' => 'Article.name', 'direction' => 'desc'];
  652. $result = $this->Paginator->generateUrl($options);
  653. $this->assertEquals('/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
  654. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  655. $options = ['sort' => 'Article.name', 'direction' => 'desc'];
  656. $result = $this->Paginator->generateUrl($options, null, ['escape' => false]);
  657. $this->assertEquals('/index?page=3&sort=Article.name&direction=desc', $result);
  658. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  659. $options = ['sort' => 'Article.name', 'direction' => 'desc'];
  660. $result = $this->Paginator->generateUrl($options, null, ['fullBase' => true]);
  661. $this->assertEquals('http://localhost/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
  662. // @deprecated 3.3.5 Use fullBase array option instead.
  663. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  664. $options = ['sort' => 'Article.name', 'direction' => 'desc'];
  665. $result = $this->Paginator->generateUrl($options, null, true);
  666. $this->assertEquals('http://localhost/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
  667. }
  668. /**
  669. * test URL generation with prefix routes
  670. *
  671. * @return void
  672. */
  673. public function testGenerateUrlWithPrefixes()
  674. {
  675. Router::reload();
  676. Router::connect('/members/:controller/:action/*', ['prefix' => 'members']);
  677. Router::connect('/:controller/:action/*');
  678. Router::setRequestInfo([
  679. ['controller' => 'posts', 'action' => 'index', 'plugin' => null],
  680. ['base' => '', 'here' => 'posts/index', 'webroot' => '/']
  681. ]);
  682. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  683. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  684. $options = ['prefix' => 'members'];
  685. $result = $this->Paginator->generateUrl($options);
  686. $expected = '/members/posts/index?page=2';
  687. $this->assertEquals($expected, $result);
  688. $result = $this->Paginator->sort('name', null, ['url' => $options]);
  689. $expected = [
  690. 'a' => ['href' => '/members/posts/index?page=2&amp;sort=name&amp;direction=asc'],
  691. 'Name',
  692. '/a'
  693. ];
  694. $this->assertHtml($expected, $result);
  695. $result = $this->Paginator->next('next', ['url' => $options]);
  696. $expected = [
  697. 'li' => ['class' => 'next'],
  698. 'a' => ['href' => '/members/posts/index?page=3', 'rel' => 'next'],
  699. 'next',
  700. '/a',
  701. '/li'
  702. ];
  703. $this->assertHtml($expected, $result);
  704. $result = $this->Paginator->prev('prev', ['url' => $options]);
  705. $expected = [
  706. 'li' => ['class' => 'prev'],
  707. 'a' => ['href' => '/members/posts/index', 'rel' => 'prev'],
  708. 'prev',
  709. '/a',
  710. '/li'
  711. ];
  712. $this->assertHtml($expected, $result);
  713. $options = ['prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc'];
  714. $result = $this->Paginator->generateUrl($options);
  715. $expected = '/members/posts/index?page=2&amp;sort=name&amp;direction=desc';
  716. $this->assertEquals($expected, $result);
  717. $options = ['controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc'];
  718. $result = $this->Paginator->generateUrl($options);
  719. $expected = '/posts/index?page=2&amp;sort=Article.name&amp;direction=desc';
  720. $this->assertEquals($expected, $result);
  721. }
  722. /**
  723. * test URL generation can leave prefix routes
  724. *
  725. * @return void
  726. */
  727. public function testGenerateUrlWithPrefixesLeavePrefix()
  728. {
  729. Router::reload();
  730. Router::connect('/members/:controller/:action/*', ['prefix' => 'members']);
  731. Router::connect('/:controller/:action/*');
  732. $request = new ServerRequest([
  733. 'params' => [
  734. 'prefix' => 'members',
  735. 'controller' => 'posts',
  736. 'action' => 'index',
  737. 'plugin' => null,
  738. 'paging' => [
  739. 'Articles' => ['page' => 2, 'prevPage' => true]
  740. ]
  741. ],
  742. 'webroot' => '/'
  743. ]);
  744. Router::setRequestInfo($request);
  745. $this->Paginator->request = $request;
  746. $result = $this->Paginator->generateUrl();
  747. $expected = '/members/posts/index?page=2';
  748. $this->assertEquals($expected, $result);
  749. $result = $this->Paginator->generateUrl(['prefix' => 'members']);
  750. $expected = '/members/posts/index?page=2';
  751. $this->assertEquals($expected, $result);
  752. $result = $this->Paginator->generateUrl(['prefix' => false]);
  753. $expected = '/posts/index?page=2';
  754. $this->assertEquals($expected, $result);
  755. $this->Paginator->options(['url' => ['prefix' => false]]);
  756. $result = $this->Paginator->generateUrl();
  757. $this->assertEquals($expected, $result, 'Setting prefix in options should work too.');
  758. }
  759. /**
  760. * test generateUrl with multiple pagination
  761. *
  762. * @return void
  763. */
  764. public function testGenerateUrlMultiplePagination()
  765. {
  766. Router::setRequestInfo([
  767. ['controller' => 'posts', 'action' => 'index', 'plugin' => null],
  768. ['base' => '', 'here' => 'posts/index', 'webroot' => '/']
  769. ]);
  770. $this->Paginator->request->params['paging']['Article']['scope'] = 'article';
  771. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  772. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  773. $this->Paginator->options(['model' => 'Article']);
  774. $result = $this->Paginator->generateUrl([]);
  775. $expected = '/posts/index?article%5Bpage%5D=3';
  776. $this->assertEquals($expected, $result);
  777. $result = $this->Paginator->sort('name');
  778. $expected = [
  779. 'a' => ['href' => '/posts/index?article%5Bpage%5D=3&amp;article%5Bsort%5D=name&amp;article%5Bdirection%5D=asc'],
  780. 'Name',
  781. '/a'
  782. ];
  783. $this->assertHtml($expected, $result);
  784. $result = $this->Paginator->next('next');
  785. $expected = [
  786. 'li' => ['class' => 'next'],
  787. 'a' => ['href' => '/posts/index?article%5Bpage%5D=4', 'rel' => 'next'],
  788. 'next',
  789. '/a',
  790. '/li'
  791. ];
  792. $this->assertHtml($expected, $result);
  793. $result = $this->Paginator->prev('prev');
  794. $expected = [
  795. 'li' => ['class' => 'prev'],
  796. 'a' => ['href' => '/posts/index?article%5Bpage%5D=2', 'rel' => 'prev'],
  797. 'prev',
  798. '/a',
  799. '/li'
  800. ];
  801. $this->assertHtml($expected, $result);
  802. $result = $this->Paginator->generateUrl(['sort' => 'name']);
  803. $expected = '/posts/index?article%5Bpage%5D=3&amp;article%5Bsort%5D=name';
  804. $this->assertEquals($expected, $result);
  805. $result = $this->Paginator->generateUrl(['#' => 'foo']);
  806. $expected = '/posts/index?article%5Bpage%5D=3#foo';
  807. $this->assertEquals($expected, $result);
  808. }
  809. /**
  810. * test generateUrl with multiple pagination and query string values
  811. *
  812. * @return void
  813. */
  814. public function testGenerateUrlMultiplePaginationQueryStringData()
  815. {
  816. Router::setRequestInfo([
  817. ['controller' => 'posts', 'action' => 'index', 'plugin' => null],
  818. ['base' => '', 'here' => 'posts/index', 'webroot' => '/']
  819. ]);
  820. $this->View->request->params['paging']['Article']['scope'] = 'article';
  821. $this->View->request->params['paging']['Article']['page'] = 3;
  822. $this->View->request->params['paging']['Article']['prevPage'] = true;
  823. $this->View->request->query = [
  824. 'article' => [
  825. 'puppy' => 'no'
  826. ]
  827. ];
  828. // Need to run __construct to update _config['url']
  829. $paginator = new PaginatorHelper($this->View);
  830. $paginator->options(['model' => 'Article']);
  831. $result = $paginator->generateUrl(['sort' => 'name']);
  832. $expected = '/posts/index?article%5Bpage%5D=3&amp;article%5Bsort%5D=name&amp;article%5Bpuppy%5D=no';
  833. $this->assertEquals($expected, $result);
  834. $result = $paginator->generateUrl([]);
  835. $expected = '/posts/index?article%5Bpage%5D=3&amp;article%5Bpuppy%5D=no';
  836. $this->assertEquals($expected, $result);
  837. }
  838. /**
  839. * testOptions method
  840. *
  841. * @return void
  842. */
  843. public function testOptions()
  844. {
  845. $this->Paginator->options = [];
  846. $this->Paginator->request->params = [];
  847. $options = ['paging' => ['Article' => [
  848. 'direction' => 'desc',
  849. 'sort' => 'title'
  850. ]]];
  851. $this->Paginator->options($options);
  852. $expected = ['Article' => [
  853. 'direction' => 'desc',
  854. 'sort' => 'title'
  855. ]];
  856. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  857. $this->Paginator->options = [];
  858. $this->Paginator->request->params = [];
  859. $options = ['Article' => [
  860. 'direction' => 'desc',
  861. 'sort' => 'title'
  862. ]];
  863. $this->Paginator->options($options);
  864. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  865. $options = ['paging' => ['Article' => [
  866. 'direction' => 'desc',
  867. 'sort' => 'Article.title'
  868. ]]];
  869. $this->Paginator->options($options);
  870. $expected = ['Article' => [
  871. 'direction' => 'desc',
  872. 'sort' => 'Article.title'
  873. ]];
  874. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  875. }
  876. /**
  877. * testPassedArgsMergingWithUrlOptions method
  878. *
  879. * @return void
  880. */
  881. public function testPassedArgsMergingWithUrlOptions()
  882. {
  883. Router::setRequestInfo([
  884. ['plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => ['2']],
  885. ['base' => '/', 'here' => '/articles/', 'webroot' => '/']
  886. ]);
  887. $this->Paginator->request->params['paging'] = [
  888. 'Article' => [
  889. 'page' => 1, 'current' => 3, 'count' => 13,
  890. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  891. 'sort' => null, 'direction' => null,
  892. ]
  893. ];
  894. $this->Paginator->request->params['pass'] = [2];
  895. $this->Paginator->request->query = ['page' => 1, 'foo' => 'bar', 'x' => 'y', 'num' => 0];
  896. $this->View->request = $this->Paginator->request;
  897. $this->Paginator = new PaginatorHelper($this->View);
  898. $result = $this->Paginator->sort('title');
  899. $expected = [
  900. 'a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;sort=title&amp;direction=asc'],
  901. 'Title',
  902. '/a'
  903. ];
  904. $this->assertHtml($expected, $result);
  905. $result = $this->Paginator->numbers();
  906. $expected = [
  907. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  908. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=2']], '2', '/a', '/li',
  909. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=3']], '3', '/a', '/li',
  910. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=4']], '4', '/a', '/li',
  911. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=5']], '5', '/a', '/li',
  912. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=6']], '6', '/a', '/li',
  913. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=7']], '7', '/a', '/li',
  914. ];
  915. $this->assertHtml($expected, $result);
  916. $result = $this->Paginator->next('Next');
  917. $expected = [
  918. 'li' => ['class' => 'next'],
  919. 'a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=2', 'rel' => 'next'],
  920. 'Next',
  921. '/a',
  922. '/li'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * Test that generated URLs don't include sort and direction parameters
  928. *
  929. * @return void
  930. */
  931. public function testDefaultSortRemovedFromUrl()
  932. {
  933. Router::setRequestInfo([
  934. ['plugin' => null, 'controller' => 'articles', 'action' => 'index'],
  935. ['base' => '/', 'here' => '/articles/', 'webroot' => '/']
  936. ]);
  937. $this->Paginator->request->params['paging'] = [
  938. 'Article' => [
  939. 'page' => 1, 'current' => 3, 'count' => 13,
  940. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  941. 'sort' => 'Article.title', 'direction' => 'ASC',
  942. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
  943. ]
  944. ];
  945. $result = $this->Paginator->next('Next');
  946. $expected = [
  947. 'li' => ['class' => 'next'],
  948. 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'],
  949. 'Next',
  950. '/a',
  951. '/li'
  952. ];
  953. $this->assertHtml($expected, $result);
  954. }
  955. /**
  956. * Test the prev() method.
  957. *
  958. * @return void
  959. */
  960. public function testPrev()
  961. {
  962. $this->Paginator->request->params['paging'] = [
  963. 'Client' => [
  964. 'page' => 1,
  965. 'current' => 3,
  966. 'count' => 13,
  967. 'prevPage' => false,
  968. 'nextPage' => true,
  969. 'pageCount' => 5,
  970. ]
  971. ];
  972. $result = $this->Paginator->prev('<< Previous');
  973. $expected = [
  974. 'li' => ['class' => 'prev disabled'],
  975. 'a' => ['href' => '', 'onclick' => 'return false;'],
  976. '&lt;&lt; Previous',
  977. '/a',
  978. '/li'
  979. ];
  980. $this->assertHtml($expected, $result);
  981. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => 'Prev']);
  982. $expected = [
  983. 'li' => ['class' => 'prev disabled'],
  984. 'a' => ['href' => '', 'onclick' => 'return false;'],
  985. 'Prev',
  986. '/a',
  987. '/li'
  988. ];
  989. $this->assertHtml($expected, $result);
  990. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => false]);
  991. $this->assertEquals('', $result, 'disabled + no text = no link');
  992. $this->Paginator->request->params['paging']['Client']['page'] = 2;
  993. $this->Paginator->request->params['paging']['Client']['prevPage'] = true;
  994. $result = $this->Paginator->prev('<< Previous');
  995. $expected = [
  996. 'li' => ['class' => 'prev'],
  997. 'a' => ['href' => '/index', 'rel' => 'prev'],
  998. '&lt;&lt; Previous',
  999. '/a',
  1000. '/li'
  1001. ];
  1002. $this->assertHtml($expected, $result);
  1003. $result = $this->Paginator->prev('Prev', [
  1004. 'templates' => [
  1005. 'prevActive' => '<a rel="prev" href="{{url}}">{{text}}</a>'
  1006. ]
  1007. ]);
  1008. $expected = [
  1009. 'a' => ['href' => '/index', 'rel' => 'prev'],
  1010. 'Prev',
  1011. '/a',
  1012. ];
  1013. $this->assertHtml($expected, $result);
  1014. }
  1015. /**
  1016. * Test that prev() and the shared implementation underneath picks up from options
  1017. *
  1018. * @return void
  1019. */
  1020. public function testPrevWithOptions()
  1021. {
  1022. $this->Paginator->request->params['paging'] = [
  1023. 'Client' => [
  1024. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
  1025. 'nextPage' => false, 'pageCount' => 2,
  1026. 'limit' => 10,
  1027. ]
  1028. ];
  1029. $this->Paginator->options(['url' => [12, 'page' => 3]]);
  1030. $result = $this->Paginator->prev('Prev', ['url' => ['foo' => 'bar']]);
  1031. $expected = [
  1032. 'li' => ['class' => 'prev'],
  1033. 'a' => ['href' => '/index/12?limit=10&amp;foo=bar', 'rel' => 'prev'],
  1034. 'Prev',
  1035. '/a',
  1036. '/li'
  1037. ];
  1038. $this->assertHtml($expected, $result);
  1039. }
  1040. /**
  1041. * test the next() method.
  1042. *
  1043. * @return void
  1044. */
  1045. public function testNext()
  1046. {
  1047. $result = $this->Paginator->next('Next >>');
  1048. $expected = [
  1049. 'li' => ['class' => 'next'],
  1050. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1051. 'Next &gt;&gt;',
  1052. '/a',
  1053. '/li'
  1054. ];
  1055. $this->assertHtml($expected, $result);
  1056. $result = $this->Paginator->next('Next', [
  1057. 'templates' => [
  1058. 'nextActive' => '<a rel="next" href="{{url}}">{{text}}</a>'
  1059. ]
  1060. ]);
  1061. $expected = [
  1062. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1063. 'Next',
  1064. '/a',
  1065. ];
  1066. $this->assertHtml($expected, $result);
  1067. $result = $this->Paginator->next('Next >>', ['escape' => false]);
  1068. $expected = [
  1069. 'li' => ['class' => 'next'],
  1070. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1071. 'preg:/Next >>/',
  1072. '/a',
  1073. '/li'
  1074. ];
  1075. $this->assertHtml($expected, $result);
  1076. }
  1077. /**
  1078. * test next() with disabled links
  1079. *
  1080. * @return void
  1081. */
  1082. public function testNextDisabled()
  1083. {
  1084. $this->Paginator->request->params['paging'] = [
  1085. 'Client' => [
  1086. 'page' => 5,
  1087. 'current' => 3,
  1088. 'count' => 13,
  1089. 'prevPage' => true,
  1090. 'nextPage' => false,
  1091. 'pageCount' => 5,
  1092. ]
  1093. ];
  1094. $result = $this->Paginator->next('Next >>');
  1095. $expected = [
  1096. 'li' => ['class' => 'next disabled'],
  1097. 'a' => ['href' => '', 'onclick' => 'return false;'],
  1098. 'Next &gt;&gt;',
  1099. '/a',
  1100. '/li'
  1101. ];
  1102. $this->assertHtml($expected, $result);
  1103. $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']);
  1104. $expected = [
  1105. 'li' => ['class' => 'next disabled'],
  1106. 'a' => ['href' => '', 'onclick' => 'return false;'],
  1107. 'Next',
  1108. '/a',
  1109. '/li'
  1110. ];
  1111. $this->assertHtml($expected, $result);
  1112. $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]);
  1113. $this->assertEquals('', $result, 'disabled + no text = no link');
  1114. }
  1115. /**
  1116. * Test next() with a model argument.
  1117. *
  1118. * @return void
  1119. */
  1120. public function testNextAndPrevNonDefaultModel()
  1121. {
  1122. $this->Paginator->request->params['paging'] = [
  1123. 'Client' => [
  1124. 'page' => 1,
  1125. 'current' => 3,
  1126. 'count' => 13,
  1127. 'prevPage' => false,
  1128. 'nextPage' => true,
  1129. 'pageCount' => 5,
  1130. ],
  1131. 'Server' => [
  1132. 'page' => 5,
  1133. 'current' => 1,
  1134. 'count' => 5,
  1135. 'prevPage' => true,
  1136. 'nextPage' => false,
  1137. 'pageCount' => 5,
  1138. ]
  1139. ];
  1140. $result = $this->Paginator->next('Next', [
  1141. 'model' => 'Client'
  1142. ]);
  1143. $expected = [
  1144. 'li' => ['class' => 'next'],
  1145. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1146. 'Next',
  1147. '/a',
  1148. '/li'
  1149. ];
  1150. $this->assertHtml($expected, $result);
  1151. $result = $this->Paginator->prev('Prev', [
  1152. 'model' => 'Client'
  1153. ]);
  1154. $expected = '<li class="prev disabled"><a href="" onclick="return false;">Prev</a></li>';
  1155. $this->assertEquals($expected, $result);
  1156. $result = $this->Paginator->next('Next', [
  1157. 'model' => 'Server'
  1158. ]);
  1159. $expected = '<li class="next disabled"><a href="" onclick="return false;">Next</a></li>';
  1160. $this->assertEquals($expected, $result);
  1161. $result = $this->Paginator->prev('Prev', [
  1162. 'model' => 'Server'
  1163. ]);
  1164. $expected = [
  1165. 'li' => ['class' => 'prev'],
  1166. 'a' => ['href' => '/index?page=4', 'rel' => 'prev'],
  1167. 'Prev',
  1168. '/a',
  1169. '/li'
  1170. ];
  1171. $this->assertHtml($expected, $result);
  1172. }
  1173. /**
  1174. * testNumbers method
  1175. *
  1176. * @return void
  1177. */
  1178. public function testNumbers()
  1179. {
  1180. $this->Paginator->request->params['paging'] = [
  1181. 'Client' => [
  1182. 'page' => 8,
  1183. 'current' => 3,
  1184. 'count' => 30,
  1185. 'prevPage' => false,
  1186. 'nextPage' => 2,
  1187. 'pageCount' => 15,
  1188. ]
  1189. ];
  1190. $result = $this->Paginator->numbers();
  1191. $expected = [
  1192. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1193. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1194. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1195. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1196. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1197. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1198. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1199. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1200. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1201. ];
  1202. $this->assertHtml($expected, $result);
  1203. $result = $this->Paginator->numbers(['first' => 'first', 'last' => 'last']);
  1204. $expected = [
  1205. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], 'first', '/a', '/li',
  1206. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1207. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1208. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1209. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1210. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1211. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1212. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1213. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1214. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1215. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1216. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1217. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], 'last', '/a', '/li',
  1218. ];
  1219. $this->assertHtml($expected, $result);
  1220. $result = $this->Paginator->numbers(['first' => '2', 'last' => '8']);
  1221. $expected = [
  1222. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '2', '/a', '/li',
  1223. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1224. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1225. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1226. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1227. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1228. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1229. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1230. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1231. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1232. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1233. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1234. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
  1235. ];
  1236. $this->assertHtml($expected, $result);
  1237. $result = $this->Paginator->numbers(['first' => '8', 'last' => '8']);
  1238. $expected = [
  1239. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '8', '/a', '/li',
  1240. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1241. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1242. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1243. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1244. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1245. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1246. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1247. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1248. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1249. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1250. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1251. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
  1252. ];
  1253. $this->assertHtml($expected, $result);
  1254. $this->Paginator->request->params['paging'] = [
  1255. 'Client' => [
  1256. 'page' => 1,
  1257. 'current' => 3,
  1258. 'count' => 30,
  1259. 'prevPage' => false,
  1260. 'nextPage' => 2,
  1261. 'pageCount' => 15,
  1262. ]
  1263. ];
  1264. $result = $this->Paginator->numbers();
  1265. $expected = [
  1266. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1267. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1268. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1269. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1270. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1271. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1272. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1273. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1274. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1275. ];
  1276. $this->assertHtml($expected, $result);
  1277. $this->Paginator->request->params['paging'] = [
  1278. 'Client' => [
  1279. 'page' => 14,
  1280. 'current' => 3,
  1281. 'count' => 30,
  1282. 'prevPage' => false,
  1283. 'nextPage' => 2,
  1284. 'pageCount' => 15,
  1285. ]
  1286. ];
  1287. $result = $this->Paginator->numbers();
  1288. $expected = [
  1289. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1290. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1291. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1292. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1293. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1294. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1295. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1296. ['li' => ['class' => 'active']], '<a href=""', '14', '/a', '/li',
  1297. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1298. ];
  1299. $this->assertHtml($expected, $result);
  1300. $this->Paginator->request->params['paging'] = [
  1301. 'Client' => [
  1302. 'page' => 2,
  1303. 'current' => 3,
  1304. 'count' => 27,
  1305. 'prevPage' => false,
  1306. 'nextPage' => 2,
  1307. 'pageCount' => 9,
  1308. ]
  1309. ];
  1310. $result = $this->Paginator->numbers(['first' => 1]);
  1311. $expected = [
  1312. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1313. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1314. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1315. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1316. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1317. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1318. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1319. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1320. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1321. ];
  1322. $this->assertHtml($expected, $result);
  1323. $result = $this->Paginator->numbers(['last' => 1]);
  1324. $expected = [
  1325. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1326. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1327. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1328. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1329. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1330. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1331. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1332. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1333. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1334. ];
  1335. $this->assertHtml($expected, $result);
  1336. $this->Paginator->request->params['paging'] = [
  1337. 'Client' => [
  1338. 'page' => 15,
  1339. 'current' => 3,
  1340. 'count' => 30,
  1341. 'prevPage' => false,
  1342. 'nextPage' => 2,
  1343. 'pageCount' => 15,
  1344. ]
  1345. ];
  1346. $result = $this->Paginator->numbers(['first' => 1]);
  1347. $expected = [
  1348. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1349. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1350. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1351. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1352. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1353. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1354. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1355. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1356. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1357. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1358. ['li' => ['class' => 'active']], '<a href=""', '15', '/a', '/li',
  1359. ];
  1360. $this->assertHtml($expected, $result);
  1361. $this->Paginator->request->params['paging'] = [
  1362. 'Client' => [
  1363. 'page' => 10,
  1364. 'current' => 3,
  1365. 'count' => 30,
  1366. 'prevPage' => false,
  1367. 'nextPage' => 2,
  1368. 'pageCount' => 15,
  1369. ]
  1370. ];
  1371. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1372. $expected = [
  1373. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1374. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1375. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1376. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1377. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1378. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1379. ['li' => ['class' => 'active']], '<a href=""', '10', '/a', '/li',
  1380. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1381. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1382. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1383. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1384. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1385. ];
  1386. $this->assertHtml($expected, $result);
  1387. $this->Paginator->request->params['paging'] = [
  1388. 'Client' => [
  1389. 'page' => 6,
  1390. 'current' => 15,
  1391. 'count' => 623,
  1392. 'prevPage' => 1,
  1393. 'nextPage' => 1,
  1394. 'pageCount' => 42,
  1395. ]
  1396. ];
  1397. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1398. $expected = [
  1399. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1400. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1401. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1402. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1403. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1404. ['li' => ['class' => 'active']], '<a href=""', '6', '/a', '/li',
  1405. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1406. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1407. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1408. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1409. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1410. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1411. ];
  1412. $this->assertHtml($expected, $result);
  1413. $this->Paginator->request->params['paging'] = [
  1414. 'Client' => [
  1415. 'page' => 37,
  1416. 'current' => 15,
  1417. 'count' => 623,
  1418. 'prevPage' => 1,
  1419. 'nextPage' => 1,
  1420. 'pageCount' => 42,
  1421. ]
  1422. ];
  1423. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1424. $expected = [
  1425. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1426. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1427. ['li' => []], ['a' => ['href' => '/index?page=33']], '33', '/a', '/li',
  1428. ['li' => []], ['a' => ['href' => '/index?page=34']], '34', '/a', '/li',
  1429. ['li' => []], ['a' => ['href' => '/index?page=35']], '35', '/a', '/li',
  1430. ['li' => []], ['a' => ['href' => '/index?page=36']], '36', '/a', '/li',
  1431. ['li' => ['class' => 'active']], '<a href=""', '37', '/a', '/li',
  1432. ['li' => []], ['a' => ['href' => '/index?page=38']], '38', '/a', '/li',
  1433. ['li' => []], ['a' => ['href' => '/index?page=39']], '39', '/a', '/li',
  1434. ['li' => []], ['a' => ['href' => '/index?page=40']], '40', '/a', '/li',
  1435. ['li' => []], ['a' => ['href' => '/index?page=41']], '41', '/a', '/li',
  1436. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1437. ];
  1438. $this->assertHtml($expected, $result);
  1439. }
  1440. /**
  1441. * testNumbersPages method
  1442. *
  1443. * @return void
  1444. */
  1445. public function testNumbersMulti()
  1446. {
  1447. $expected = [
  1448. 1 => '*1 2 3 4 5 6 7 ',
  1449. 2 => '1 *2 3 4 5 6 7 ',
  1450. 3 => '1 2 *3 4 5 6 7 ',
  1451. 4 => '1 2 3 *4 5 6 7 ',
  1452. 5 => '1 2 3 4 *5 6 7 ',
  1453. 6 => '1 2 3 4 5 *6 7 ',
  1454. 7 => '1 2 3 4 5 6 *7 ',
  1455. ];
  1456. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7);
  1457. $this->assertEquals($expected, $result);
  1458. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7, ['first' => 'F', 'last' => 'L']);
  1459. $this->assertEquals($expected, $result);
  1460. $expected = [
  1461. 1 => '*1 2 3 4 5 6 7 8 9 ',
  1462. 2 => '1 *2 3 4 5 6 7 8 9 ',
  1463. 3 => '1 2 *3 4 5 6 7 8 9 ',
  1464. 4 => '1 2 3 *4 5 6 7 8 9 ',
  1465. 5 => '1 2 3 4 *5 6 7 8 9 ',
  1466. 6 => '2 3 4 5 *6 7 8 9 10 ',
  1467. 7 => '3 4 5 6 *7 8 9 10 11 ',
  1468. 10 => '6 7 8 9 *10 11 12 13 14 ',
  1469. 15 => '11 12 13 14 *15 16 17 18 19 ',
  1470. 16 => '12 13 14 15 *16 17 18 19 20 ',
  1471. 17 => '12 13 14 15 16 *17 18 19 20 ',
  1472. 18 => '12 13 14 15 16 17 *18 19 20 ',
  1473. 19 => '12 13 14 15 16 17 18 *19 20 ',
  1474. 20 => '12 13 14 15 16 17 18 19 *20 ',
  1475. ];
  1476. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20);
  1477. $this->assertEquals($expected, $result);
  1478. $expected = [
  1479. 1 => '*1 2 3 4 5 6 7 8 9 ',
  1480. 2 => '1 *2 3 4 5 6 7 8 9 ',
  1481. 3 => '1 2 *3 4 5 6 7 8 9 ',
  1482. 4 => '1 2 3 *4 5 6 7 8 9 ',
  1483. 5 => '1 2 3 4 *5 6 7 8 9 ',
  1484. 6 => '1 2 3 4 5 *6 7 8 9 10 ',
  1485. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ',
  1486. 8 => '<F ... 4 5 6 7 *8 9 10 11 12 ',
  1487. 9 => '<F ... 5 6 7 8 *9 10 11 12 13 ',
  1488. 10 => '<F ... 6 7 8 9 *10 11 12 13 14 ',
  1489. 15 => '<F ... 11 12 13 14 *15 16 17 18 19 ',
  1490. 16 => '<F ... 12 13 14 15 *16 17 18 19 20 ',
  1491. 17 => '<F ... 12 13 14 15 16 *17 18 19 20 ',
  1492. 18 => '<F ... 12 13 14 15 16 17 *18 19 20 ',
  1493. 19 => '<F ... 12 13 14 15 16 17 18 *19 20 ',
  1494. 20 => '<F ... 12 13 14 15 16 17 18 19 *20 ',
  1495. ];
  1496. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 'F']);
  1497. $this->assertEquals($expected, $result);
  1498. $expected = [
  1499. 1 => '*1 2 3 4 5 6 7 8 9 ',
  1500. 2 => '1 *2 3 4 5 6 7 8 9 ',
  1501. 3 => '1 2 *3 4 5 6 7 8 9 ',
  1502. 4 => '1 2 3 *4 5 6 7 8 9 ',
  1503. 5 => '1 2 3 4 *5 6 7 8 9 ',
  1504. 6 => '1 2 3 4 5 *6 7 8 9 10 ',
  1505. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ',
  1506. 8 => '1 2 3 4 5 6 7 *8 9 10 11 12 ',
  1507. 9 => '1 2 ... 5 6 7 8 *9 10 11 12 13 ',
  1508. 10 => '1 2 ... 6 7 8 9 *10 11 12 13 14 ',
  1509. 15 => '1 2 ... 11 12 13 14 *15 16 17 18 19 ',
  1510. 16 => '1 2 ... 12 13 14 15 *16 17 18 19 20 ',
  1511. 17 => '1 2 ... 12 13 14 15 16 *17 18 19 20 ',
  1512. 18 => '1 2 ... 12 13 14 15 16 17 *18 19 20 ',
  1513. 19 => '1 2 ... 12 13 14 15 16 17 18 *19 20 ',
  1514. 20 => '1 2 ... 12 13 14 15 16 17 18 19 *20 ',
  1515. ];
  1516. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 2]);
  1517. $this->assertEquals($expected, $result);
  1518. $expected = [
  1519. 1 => '*1 2 3 4 5 6 7 8 9 ... L> ',
  1520. 2 => '1 *2 3 4 5 6 7 8 9 ... L> ',
  1521. 3 => '1 2 *3 4 5 6 7 8 9 ... L> ',
  1522. 4 => '1 2 3 *4 5 6 7 8 9 ... L> ',
  1523. 5 => '1 2 3 4 *5 6 7 8 9 ... L> ',
  1524. 6 => '2 3 4 5 *6 7 8 9 10 ... L> ',
  1525. 7 => '3 4 5 6 *7 8 9 10 11 ... L> ',
  1526. 8 => '4 5 6 7 *8 9 10 11 12 ... L> ',
  1527. 9 => '5 6 7 8 *9 10 11 12 13 ... L> ',
  1528. 10 => '6 7 8 9 *10 11 12 13 14 ... L> ',
  1529. 11 => '7 8 9 10 *11 12 13 14 15 ... L> ',
  1530. 12 => '8 9 10 11 *12 13 14 15 16 ... L> ',
  1531. 13 => '9 10 11 12 *13 14 15 16 17 ... L> ',
  1532. 14 => '10 11 12 13 *14 15 16 17 18 19 20 ',
  1533. 15 => '11 12 13 14 *15 16 17 18 19 20 ',
  1534. 16 => '12 13 14 15 *16 17 18 19 20 ',
  1535. 17 => '12 13 14 15 16 *17 18 19 20 ',
  1536. 18 => '12 13 14 15 16 17 *18 19 20 ',
  1537. 19 => '12 13 14 15 16 17 18 *19 20 ',
  1538. 20 => '12 13 14 15 16 17 18 19 *20 ',
  1539. ];
  1540. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['last' => 'L']);
  1541. $this->assertEquals($expected, $result);
  1542. $expected = [
  1543. 1 => '*1 2 3 4 5 6 7 8 9 ... L> ',
  1544. 2 => '1 *2 3 4 5 6 7 8 9 ... L> ',
  1545. 3 => '1 2 *3 4 5 6 7 8 9 ... L> ',
  1546. 4 => '1 2 3 *4 5 6 7 8 9 ... L> ',
  1547. 5 => '1 2 3 4 *5 6 7 8 9 ... L> ',
  1548. 6 => '1 2 3 4 5 *6 7 8 9 10 ... L> ',
  1549. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ... L> ',
  1550. 8 => '<F ... 4 5 6 7 *8 9 10 11 12 ... L> ',
  1551. 9 => '<F ... 5 6 7 8 *9 10 11 12 13 ... L> ',
  1552. 10 => '<F ... 6 7 8 9 *10 11 12 13 14 ... L> ',
  1553. 11 => '<F ... 7 8 9 10 *11 12 13 14 15 ... L> ',
  1554. 12 => '<F ... 8 9 10 11 *12 13 14 15 16 ... L> ',
  1555. 13 => '<F ... 9 10 11 12 *13 14 15 16 17 ... L> ',
  1556. 14 => '<F ... 10 11 12 13 *14 15 16 17 18 19 20 ',
  1557. 15 => '<F ... 11 12 13 14 *15 16 17 18 19 20 ',
  1558. 16 => '<F ... 12 13 14 15 *16 17 18 19 20 ',
  1559. 17 => '<F ... 12 13 14 15 16 *17 18 19 20 ',
  1560. 18 => '<F ... 12 13 14 15 16 17 *18 19 20 ',
  1561. 19 => '<F ... 12 13 14 15 16 17 18 *19 20 ',
  1562. 20 => '<F ... 12 13 14 15 16 17 18 19 *20 ',
  1563. ];
  1564. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 'F', 'last' => 'L']);
  1565. $this->assertEquals($expected, $result);
  1566. $expected = [
  1567. 1 => '*1 2 3 4 5 6 7 8 9 ... 19 20 ',
  1568. 2 => '1 *2 3 4 5 6 7 8 9 ... 19 20 ',
  1569. 3 => '1 2 *3 4 5 6 7 8 9 ... 19 20 ',
  1570. 4 => '1 2 3 *4 5 6 7 8 9 ... 19 20 ',
  1571. 5 => '1 2 3 4 *5 6 7 8 9 ... 19 20 ',
  1572. 6 => '1 2 3 4 5 *6 7 8 9 10 ... 19 20 ',
  1573. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ... 19 20 ',
  1574. 8 => '1 2 3 4 5 6 7 *8 9 10 11 12 ... 19 20 ',
  1575. 9 => '1 2 ... 5 6 7 8 *9 10 11 12 13 ... 19 20 ',
  1576. 10 => '1 2 ... 6 7 8 9 *10 11 12 13 14 ... 19 20 ',
  1577. 11 => '1 2 ... 7 8 9 10 *11 12 13 14 15 ... 19 20 ',
  1578. 12 => '1 2 ... 8 9 10 11 *12 13 14 15 16 ... 19 20 ',
  1579. 13 => '1 2 ... 9 10 11 12 *13 14 15 16 17 18 19 20 ',
  1580. 14 => '1 2 ... 10 11 12 13 *14 15 16 17 18 19 20 ',
  1581. 15 => '1 2 ... 11 12 13 14 *15 16 17 18 19 20 ',
  1582. 16 => '1 2 ... 12 13 14 15 *16 17 18 19 20 ',
  1583. 17 => '1 2 ... 12 13 14 15 16 *17 18 19 20 ',
  1584. 18 => '1 2 ... 12 13 14 15 16 17 *18 19 20 ',
  1585. 19 => '1 2 ... 12 13 14 15 16 17 18 *19 20 ',
  1586. 20 => '1 2 ... 12 13 14 15 16 17 18 19 *20 ',
  1587. ];
  1588. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 2, 'last' => 2]);
  1589. $this->assertEquals($expected, $result);
  1590. $expected = [
  1591. 1 => '*1 2 3 4 5 6 7 8 9 ... 19 20 ',
  1592. 2 => '1 *2 3 4 5 6 7 8 9 ... 19 20 ',
  1593. 3 => '1 2 *3 4 5 6 7 8 9 ... 19 20 ',
  1594. 4 => '1 2 3 *4 5 6 7 8 9 ... 19 20 ',
  1595. 5 => '1 2 3 4 *5 6 7 8 9 ... 19 20 ',
  1596. 6 => '2 3 4 5 *6 7 8 9 10 ... 19 20 ',
  1597. 7 => '3 4 5 6 *7 8 9 10 11 ... 19 20 ',
  1598. 8 => '4 5 6 7 *8 9 10 11 12 ... 19 20 ',
  1599. 9 => '5 6 7 8 *9 10 11 12 13 ... 19 20 ',
  1600. 10 => '6 7 8 9 *10 11 12 13 14 ... 19 20 ',
  1601. 11 => '7 8 9 10 *11 12 13 14 15 ... 19 20 ',
  1602. 12 => '8 9 10 11 *12 13 14 15 16 ... 19 20 ',
  1603. 13 => '9 10 11 12 *13 14 15 16 17 18 19 20 ',
  1604. 14 => '10 11 12 13 *14 15 16 17 18 19 20 ',
  1605. 15 => '11 12 13 14 *15 16 17 18 19 20 ',
  1606. 16 => '12 13 14 15 *16 17 18 19 20 ',
  1607. 17 => '12 13 14 15 16 *17 18 19 20 ',
  1608. 18 => '12 13 14 15 16 17 *18 19 20 ',
  1609. 19 => '12 13 14 15 16 17 18 *19 20 ',
  1610. 20 => '12 13 14 15 16 17 18 19 *20 ',
  1611. ];
  1612. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['last' => 2]);
  1613. $this->assertEquals($expected, $result);
  1614. }
  1615. /**
  1616. * Retrieves result of PaginatorHelper::numbers for multiple pages
  1617. *
  1618. * @param int[] $pagesToCheck Pages to get result for
  1619. * @param int $pageCount Number of total pages
  1620. * @param array $options Options for PaginatorHelper::numbers
  1621. * @return string[]
  1622. */
  1623. protected function getNumbersForMultiplePages($pagesToCheck, $pageCount, $options = [])
  1624. {
  1625. $options['templates'] = [
  1626. 'first' => '<{{text}} ',
  1627. 'last' => '{{text}}> ',
  1628. 'number' => '{{text}} ',
  1629. 'current' => '*{{text}} ',
  1630. 'ellipsis' => '... ',
  1631. ];
  1632. $this->Paginator->request->params['paging'] = [
  1633. 'Client' => [
  1634. 'page' => 1,
  1635. 'pageCount' => $pageCount,
  1636. ]
  1637. ];
  1638. $result = [];
  1639. foreach ($pagesToCheck as $page) {
  1640. $this->Paginator->request->params['paging']['Client']['page'] = $page;
  1641. $result[$page] = $this->Paginator->numbers($options);
  1642. }
  1643. return $result;
  1644. }
  1645. /**
  1646. * Test that numbers() lets you overwrite templates.
  1647. *
  1648. * The templates file has no li elements.
  1649. *
  1650. * @return void
  1651. */
  1652. public function testNumbersTemplates()
  1653. {
  1654. $this->Paginator->request->params['paging'] = [
  1655. 'Client' => [
  1656. 'page' => 8,
  1657. 'current' => 3,
  1658. 'count' => 30,
  1659. 'prevPage' => false,
  1660. 'nextPage' => 2,
  1661. 'pageCount' => 15,
  1662. ]
  1663. ];
  1664. $result = $this->Paginator->numbers(['templates' => 'htmlhelper_tags']);
  1665. $expected = [
  1666. ['a' => ['href' => '/index?page=4']], '4', '/a',
  1667. ['a' => ['href' => '/index?page=5']], '5', '/a',
  1668. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1669. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1670. 'span' => ['class' => 'active'], '8', '/span',
  1671. ['a' => ['href' => '/index?page=9']], '9', '/a',
  1672. ['a' => ['href' => '/index?page=10']], '10', '/a',
  1673. ['a' => ['href' => '/index?page=11']], '11', '/a',
  1674. ['a' => ['href' => '/index?page=12']], '12', '/a',
  1675. ];
  1676. $this->assertHtml($expected, $result);
  1677. $this->assertContains(
  1678. '<li',
  1679. $this->Paginator->templater()->get('current'),
  1680. 'Templates were not restored.'
  1681. );
  1682. }
  1683. /**
  1684. * Test modulus option for numbers()
  1685. *
  1686. * @return void
  1687. */
  1688. public function testNumbersModulus()
  1689. {
  1690. $this->Paginator->request->params['paging'] = [
  1691. 'Client' => [
  1692. 'page' => 1,
  1693. 'current' => 10,
  1694. 'count' => 30,
  1695. 'prevPage' => false,
  1696. 'nextPage' => 2,
  1697. 'pageCount' => 3,
  1698. ]
  1699. ];
  1700. $result = $this->Paginator->numbers(['modulus' => 10]);
  1701. $expected = [
  1702. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1703. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1704. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1705. ];
  1706. $this->assertHtml($expected, $result);
  1707. $result = $this->Paginator->numbers(['modulus' => 3]);
  1708. $expected = [
  1709. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1710. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1711. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1712. ];
  1713. $this->assertHtml($expected, $result);
  1714. $this->Paginator->request->params['paging'] = [
  1715. 'Client' => [
  1716. 'page' => 4895,
  1717. 'current' => 10,
  1718. 'count' => 48962,
  1719. 'prevPage' => 1,
  1720. 'nextPage' => 1,
  1721. 'pageCount' => 4897,
  1722. ]
  1723. ];
  1724. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1725. $expected = [
  1726. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1727. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1728. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1729. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1730. ['li' => ['class' => 'active']], '<a href=""', '4,895', '/a', '/li',
  1731. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1732. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1733. ];
  1734. $this->assertHtml($expected, $result);
  1735. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1736. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1737. $expected = [
  1738. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1739. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1740. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1741. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1742. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1743. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1744. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1745. ];
  1746. $this->assertHtml($expected, $result);
  1747. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 5, 'last' => 5]);
  1748. $expected = [
  1749. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1750. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1751. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1752. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1753. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1754. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1755. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1756. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4,893', '/a', '/li',
  1757. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1758. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1759. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1760. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1761. ];
  1762. $this->assertHtml($expected, $result);
  1763. $this->Paginator->request->params['paging']['Client']['page'] = 4893;
  1764. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1765. $expected = [
  1766. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1767. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1768. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1769. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1770. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1771. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1772. ['li' => []], ['a' => ['href' => '/index?page=4891']], '4,891', '/a', '/li',
  1773. ['li' => []], ['a' => ['href' => '/index?page=4892']], '4,892', '/a', '/li',
  1774. ['li' => ['class' => 'active']], '<a href=""', '4,893', '/a', '/li',
  1775. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1776. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1777. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1778. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1779. ];
  1780. $this->assertHtml($expected, $result);
  1781. $this->Paginator->request->params['paging']['Client']['page'] = 58;
  1782. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1783. $expected = [
  1784. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1785. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1786. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1787. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1788. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1789. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1790. ['li' => []], ['a' => ['href' => '/index?page=56']], '56', '/a', '/li',
  1791. ['li' => []], ['a' => ['href' => '/index?page=57']], '57', '/a', '/li',
  1792. ['li' => ['class' => 'active']], '<a href=""', '58', '/a', '/li',
  1793. ['li' => []], ['a' => ['href' => '/index?page=59']], '59', '/a', '/li',
  1794. ['li' => []], ['a' => ['href' => '/index?page=60']], '60', '/a', '/li',
  1795. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1796. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4,893', '/a', '/li',
  1797. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1798. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1799. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1800. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1801. ];
  1802. $this->assertHtml($expected, $result);
  1803. $this->Paginator->request->params['paging']['Client']['page'] = 5;
  1804. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1805. $expected = [
  1806. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1807. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1808. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1809. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1810. ['li' => ['class' => 'active']], '<a href=""', '5', '/a', '/li',
  1811. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1812. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1813. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1814. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4,893', '/a', '/li',
  1815. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1816. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1817. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1818. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1819. ];
  1820. $this->assertHtml($expected, $result);
  1821. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1822. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1823. $expected = [
  1824. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1825. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1826. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1827. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1828. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1829. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1830. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1831. ];
  1832. $this->assertHtml($expected, $result);
  1833. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1834. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 0, 'last' => 2]);
  1835. $expected = [
  1836. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1837. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1838. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1839. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1840. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1841. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1842. ];
  1843. $this->assertHtml($expected, $result);
  1844. }
  1845. /**
  1846. * Test modulus option for numbers()
  1847. *
  1848. * @return void
  1849. */
  1850. public function testNumbersModulusMulti()
  1851. {
  1852. $expected = [
  1853. 1 => '*1 2 3 4 ',
  1854. 2 => '1 *2 3 4 ',
  1855. 3 => '1 2 *3 4 ',
  1856. 4 => '2 3 *4 5 ',
  1857. 5 => '3 4 *5 6 ',
  1858. 6 => '4 5 *6 7 ',
  1859. 7 => '4 5 6 *7 ',
  1860. ];
  1861. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7, ['modulus' => 3]);
  1862. $this->assertEquals($expected, $result);
  1863. $expected = [
  1864. 1 => '*1 2 3 4 ... L> ',
  1865. 2 => '1 *2 3 4 ... L> ',
  1866. 3 => '1 2 *3 4 ... L> ',
  1867. 4 => '1 2 3 *4 5 6 7 ',
  1868. 5 => '1 2 3 4 *5 6 7 ',
  1869. 6 => '<F ... 4 5 *6 7 ',
  1870. 7 => '<F ... 4 5 6 *7 ',
  1871. ];
  1872. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7, ['modulus' => 3, 'first' => 'F', 'last' => 'L']);
  1873. $this->assertEquals($expected, $result);
  1874. $expected = [
  1875. 1 => '*1 2 3 ... 19 20 ',
  1876. 2 => '1 *2 3 ... 19 20 ',
  1877. 3 => '1 2 *3 4 ... 19 20 ',
  1878. 4 => '1 2 3 *4 5 ... 19 20 ',
  1879. 5 => '1 2 3 4 *5 6 ... 19 20 ',
  1880. 6 => '1 2 ... 5 *6 7 ... 19 20 ',
  1881. 15 => '1 2 ... 14 *15 16 ... 19 20 ',
  1882. 16 => '1 2 ... 15 *16 17 18 19 20 ',
  1883. 17 => '1 2 ... 16 *17 18 19 20 ',
  1884. 18 => '1 2 ... 17 *18 19 20 ',
  1885. 19 => '1 2 ... 18 *19 20 ',
  1886. 20 => '1 2 ... 18 19 *20 ',
  1887. ];
  1888. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 2, 'modulus' => 2, 'last' => 2]);
  1889. $this->assertEquals($expected, $result);
  1890. $expected = [
  1891. 1 => '*1 2 3 4 5 ... 16 17 18 19 20 ',
  1892. 2 => '1 *2 3 4 5 ... 16 17 18 19 20 ',
  1893. 3 => '1 2 *3 4 5 ... 16 17 18 19 20 ',
  1894. 4 => '1 2 3 *4 5 6 ... 16 17 18 19 20 ',
  1895. 5 => '1 2 3 4 *5 6 7 ... 16 17 18 19 20 ',
  1896. 6 => '1 2 3 4 5 *6 7 8 ... 16 17 18 19 20 ',
  1897. 7 => '1 2 3 4 5 6 *7 8 9 ... 16 17 18 19 20 ',
  1898. 8 => '1 2 3 4 5 6 7 *8 9 10 ... 16 17 18 19 20 ',
  1899. 9 => '1 2 3 4 5 6 7 8 *9 10 11 ... 16 17 18 19 20 ',
  1900. 10 => '1 2 3 4 5 ... 8 9 *10 11 12 ... 16 17 18 19 20 ',
  1901. 11 => '1 2 3 4 5 ... 9 10 *11 12 13 ... 16 17 18 19 20 ',
  1902. 12 => '1 2 3 4 5 ... 10 11 *12 13 14 15 16 17 18 19 20 ',
  1903. 13 => '1 2 3 4 5 ... 11 12 *13 14 15 16 17 18 19 20 ',
  1904. 14 => '1 2 3 4 5 ... 12 13 *14 15 16 17 18 19 20 ',
  1905. 15 => '1 2 3 4 5 ... 13 14 *15 16 17 18 19 20 ',
  1906. 16 => '1 2 3 4 5 ... 14 15 *16 17 18 19 20 ',
  1907. 17 => '1 2 3 4 5 ... 15 16 *17 18 19 20 ',
  1908. 18 => '1 2 3 4 5 ... 16 17 *18 19 20 ',
  1909. 19 => '1 2 3 4 5 ... 16 17 18 *19 20 ',
  1910. 20 => '1 2 3 4 5 ... 16 17 18 19 *20 ',
  1911. ];
  1912. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 5, 'modulus' => 4, 'last' => 5]);
  1913. $this->assertEquals($expected, $result);
  1914. }
  1915. /**
  1916. * Tests that disabling modulus displays all page links.
  1917. *
  1918. * @return void
  1919. */
  1920. public function testModulusDisabled()
  1921. {
  1922. $this->Paginator->request->params['paging'] = [
  1923. 'Client' => [
  1924. 'page' => 4,
  1925. 'current' => 2,
  1926. 'count' => 30,
  1927. 'prevPage' => 1,
  1928. 'nextPage' => 1,
  1929. 'pageCount' => 6,
  1930. ]
  1931. ];
  1932. $result = $this->Paginator->numbers(['modulus' => false]);
  1933. $expected = [
  1934. ['li' => []], '<a href="/index"', '1', '/a', '/li',
  1935. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1936. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1937. ['li' => ['class' => 'active']], ['a' => ['href' => '']], '4', '/a', '/li',
  1938. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1939. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1940. ];
  1941. $this->assertHtml($expected, $result);
  1942. }
  1943. /**
  1944. * Test that numbers() with url options.
  1945. *
  1946. * @return void
  1947. */
  1948. public function testNumbersWithUrlOptions()
  1949. {
  1950. $this->Paginator->request->params['paging'] = [
  1951. 'Client' => [
  1952. 'page' => 8,
  1953. 'current' => 3,
  1954. 'count' => 30,
  1955. 'prevPage' => false,
  1956. 'nextPage' => 2,
  1957. 'pageCount' => 15,
  1958. ]
  1959. ];
  1960. $result = $this->Paginator->numbers(['url' => ['#' => 'foo']]);
  1961. $expected = [
  1962. ['li' => []], ['a' => ['href' => '/index?page=4#foo']], '4', '/a', '/li',
  1963. ['li' => []], ['a' => ['href' => '/index?page=5#foo']], '5', '/a', '/li',
  1964. ['li' => []], ['a' => ['href' => '/index?page=6#foo']], '6', '/a', '/li',
  1965. ['li' => []], ['a' => ['href' => '/index?page=7#foo']], '7', '/a', '/li',
  1966. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1967. ['li' => []], ['a' => ['href' => '/index?page=9#foo']], '9', '/a', '/li',
  1968. ['li' => []], ['a' => ['href' => '/index?page=10#foo']], '10', '/a', '/li',
  1969. ['li' => []], ['a' => ['href' => '/index?page=11#foo']], '11', '/a', '/li',
  1970. ['li' => []], ['a' => ['href' => '/index?page=12#foo']], '12', '/a', '/li',
  1971. ];
  1972. $this->assertHtml($expected, $result);
  1973. $this->Paginator->request->params['paging'] = [
  1974. 'Client' => [
  1975. 'page' => 3,
  1976. 'current' => 10,
  1977. 'count' => 48962,
  1978. 'prevPage' => 1,
  1979. 'nextPage' => 1,
  1980. 'pageCount' => 4897,
  1981. ]
  1982. ];
  1983. $result = $this->Paginator->numbers([
  1984. 'first' => 2,
  1985. 'modulus' => 2,
  1986. 'last' => 2,
  1987. 'url' => ['foo' => 'bar']]);
  1988. $expected = [
  1989. ['li' => []], ['a' => ['href' => '/index?foo=bar']], '1', '/a', '/li',
  1990. ['li' => []], ['a' => ['href' => '/index?page=2&amp;foo=bar']], '2', '/a', '/li',
  1991. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1992. ['li' => []], ['a' => ['href' => '/index?page=4&amp;foo=bar']], '4', '/a', '/li',
  1993. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1994. ['li' => []], ['a' => ['href' => '/index?page=4896&amp;foo=bar']], '4,896', '/a', '/li',
  1995. ['li' => []], ['a' => ['href' => '/index?page=4897&amp;foo=bar']], '4,897', '/a', '/li',
  1996. ];
  1997. $this->assertHtml($expected, $result);
  1998. }
  1999. /**
  2000. * test numbers() with routing parameters.
  2001. *
  2002. * @return void
  2003. */
  2004. public function testNumbersRouting()
  2005. {
  2006. $this->Paginator->request->params['paging'] = [
  2007. 'Client' => [
  2008. 'page' => 2,
  2009. 'current' => 2,
  2010. 'count' => 30,
  2011. 'prevPage' => false,
  2012. 'nextPage' => 3,
  2013. 'pageCount' => 3,
  2014. ]
  2015. ];
  2016. $request = new ServerRequest();
  2017. $request->addParams([
  2018. 'controller' => 'clients', 'action' => 'index', 'plugin' => null
  2019. ]);
  2020. $request->base = '';
  2021. $request->here = '/clients/index?page=2';
  2022. $request->webroot = '/';
  2023. Router::setRequestInfo($request);
  2024. $result = $this->Paginator->numbers();
  2025. $expected = [
  2026. ['li' => []], ['a' => ['href' => '/clients/index']], '1', '/a', '/li',
  2027. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  2028. ['li' => []], ['a' => ['href' => '/clients/index?page=3']], '3', '/a', '/li',
  2029. ];
  2030. $this->assertHtml($expected, $result);
  2031. }
  2032. /**
  2033. * Test that numbers() works with the non default model.
  2034. *
  2035. * @return void
  2036. */
  2037. public function testNumbersNonDefaultModel()
  2038. {
  2039. $this->Paginator->request->params['paging'] = [
  2040. 'Client' => [
  2041. 'page' => 1,
  2042. 'current' => 3,
  2043. 'count' => 13,
  2044. 'prevPage' => false,
  2045. 'nextPage' => true,
  2046. 'pageCount' => 5,
  2047. ],
  2048. 'Server' => [
  2049. 'page' => 5,
  2050. 'current' => 1,
  2051. 'count' => 5,
  2052. 'prevPage' => true,
  2053. 'nextPage' => false,
  2054. 'pageCount' => 5,
  2055. ]
  2056. ];
  2057. $result = $this->Paginator->numbers(['model' => 'Server']);
  2058. $this->assertContains('<li class="active"><a href="">5</a></li>', $result);
  2059. $this->assertNotContains('<li class="active"><a href="">1</a></li>', $result);
  2060. $result = $this->Paginator->numbers(['model' => 'Client']);
  2061. $this->assertContains('<li class="active"><a href="">1</a></li>', $result);
  2062. $this->assertNotContains('<li class="active"><a href="">5</a></li>', $result);
  2063. }
  2064. /**
  2065. * test first() and last() with tag options
  2066. *
  2067. * @return void
  2068. */
  2069. public function testFirstAndLastTag()
  2070. {
  2071. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  2072. $result = $this->Paginator->first('<<');
  2073. $expected = [
  2074. 'li' => ['class' => 'first'],
  2075. 'a' => ['href' => '/index'],
  2076. '&lt;&lt;',
  2077. '/a',
  2078. '/li'
  2079. ];
  2080. $this->assertHtml($expected, $result);
  2081. $result = $this->Paginator->first('5');
  2082. $expected = [
  2083. 'li' => ['class' => 'first'],
  2084. 'a' => ['href' => '/index'],
  2085. '5',
  2086. '/a',
  2087. '/li'
  2088. ];
  2089. $this->assertHtml($expected, $result);
  2090. $result = $this->Paginator->last(2);
  2091. $expected = [
  2092. '<li',
  2093. ['a' => ['href' => '/index?page=6']], '6', '/a',
  2094. '/li',
  2095. '<li',
  2096. ['a' => ['href' => '/index?page=7']], '7', '/a',
  2097. '/li',
  2098. ];
  2099. $this->assertHtml($expected, $result);
  2100. $result = $this->Paginator->last('9');
  2101. $expected = [
  2102. 'li' => ['class' => 'last'],
  2103. 'a' => ['href' => '/index?page=7'],
  2104. '9',
  2105. '/a',
  2106. '/li'
  2107. ];
  2108. $this->assertHtml($expected, $result);
  2109. }
  2110. /**
  2111. * test that on the last page you don't get a link ot the last page.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testLastNoOutput()
  2116. {
  2117. $this->Paginator->request->params['paging']['Article']['page'] = 15;
  2118. $this->Paginator->request->params['paging']['Article']['pageCount'] = 15;
  2119. $result = $this->Paginator->last();
  2120. $expected = '';
  2121. $this->assertEquals($expected, $result);
  2122. }
  2123. /**
  2124. * test first() with a the model parameter.
  2125. *
  2126. * @return void
  2127. */
  2128. public function testFirstNonDefaultModel()
  2129. {
  2130. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  2131. $this->Paginator->request->params['paging']['Client'] = [
  2132. 'page' => 3,
  2133. 'current' => 3,
  2134. 'count' => 13,
  2135. 'prevPage' => false,
  2136. 'nextPage' => true,
  2137. 'pageCount' => 5,
  2138. ];
  2139. $result = $this->Paginator->first('first', ['model' => 'Article']);
  2140. $this->assertEquals('', $result);
  2141. $result = $this->Paginator->first('first', ['model' => 'Client']);
  2142. $expected = [
  2143. 'li' => ['class' => 'first'],
  2144. 'a' => ['href' => '/index'],
  2145. 'first',
  2146. '/a',
  2147. '/li'
  2148. ];
  2149. $this->assertHtml($expected, $result);
  2150. }
  2151. /**
  2152. * test first() on the first page.
  2153. *
  2154. * @return void
  2155. */
  2156. public function testFirstEmpty()
  2157. {
  2158. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  2159. $result = $this->Paginator->first();
  2160. $expected = '';
  2161. $this->assertEquals($expected, $result);
  2162. }
  2163. /**
  2164. * test first() and options()
  2165. *
  2166. * @return void
  2167. */
  2168. public function testFirstFullBaseUrl()
  2169. {
  2170. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  2171. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  2172. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  2173. $this->Paginator->options(['url' => ['_full' => true]]);
  2174. $result = $this->Paginator->first();
  2175. $expected = [
  2176. 'li' => ['class' => 'first'],
  2177. ['a' => [
  2178. 'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC'
  2179. ]],
  2180. '&lt;&lt; first',
  2181. '/a',
  2182. '/li',
  2183. ];
  2184. $this->assertHtml($expected, $result);
  2185. }
  2186. /**
  2187. * test first() on the fence-post
  2188. *
  2189. * @return void
  2190. */
  2191. public function testFirstBoundaries()
  2192. {
  2193. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  2194. $result = $this->Paginator->first();
  2195. $expected = [
  2196. 'li' => ['class' => 'first'],
  2197. 'a' => ['href' => '/index'],
  2198. '&lt;&lt; first',
  2199. '/a',
  2200. '/li'
  2201. ];
  2202. $this->assertHtml($expected, $result);
  2203. $result = $this->Paginator->first(2);
  2204. $expected = [
  2205. '<li',
  2206. ['a' => ['href' => '/index']], '1', '/a',
  2207. '/li',
  2208. '<li',
  2209. ['a' => ['href' => '/index?page=2']], '2', '/a',
  2210. '/li'
  2211. ];
  2212. $this->assertHtml($expected, $result);
  2213. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  2214. $result = $this->Paginator->first(3);
  2215. $this->assertEquals('', $result, 'When inside the first links range, no links should be made');
  2216. }
  2217. /**
  2218. * test params() method
  2219. *
  2220. * @return void
  2221. */
  2222. public function testParams()
  2223. {
  2224. $result = $this->Paginator->params();
  2225. $this->assertArrayHasKey('page', $result);
  2226. $this->assertArrayHasKey('pageCount', $result);
  2227. $result = $this->Paginator->params('Nope');
  2228. $this->assertEquals([], $result);
  2229. }
  2230. /**
  2231. * test param() method
  2232. *
  2233. * @return void
  2234. */
  2235. public function testParam()
  2236. {
  2237. $result = $this->Paginator->param('count');
  2238. $this->assertSame(62, $result);
  2239. $result = $this->Paginator->param('imaginary');
  2240. $this->assertNull($result);
  2241. }
  2242. /**
  2243. * test last() method
  2244. *
  2245. * @return void
  2246. */
  2247. public function testLast()
  2248. {
  2249. $result = $this->Paginator->last();
  2250. $expected = [
  2251. 'li' => ['class' => 'last'],
  2252. 'a' => ['href' => '/index?page=7'],
  2253. 'last &gt;&gt;',
  2254. '/a',
  2255. '/li'
  2256. ];
  2257. $this->assertHtml($expected, $result);
  2258. $result = $this->Paginator->last(1);
  2259. $expected = [
  2260. '<li',
  2261. 'a' => ['href' => '/index?page=7'],
  2262. '7',
  2263. '/a',
  2264. '/li'
  2265. ];
  2266. $this->assertHtml($expected, $result);
  2267. $this->Paginator->request->params['paging']['Article']['page'] = 6;
  2268. $result = $this->Paginator->last(2);
  2269. $expected = [
  2270. '<li',
  2271. ['a' => ['href' => '/index?page=6']], '6', '/a',
  2272. '/li',
  2273. '<li',
  2274. ['a' => ['href' => '/index?page=7']], '7', '/a',
  2275. '/li',
  2276. ];
  2277. $this->assertHtml($expected, $result);
  2278. $result = $this->Paginator->last(3);
  2279. $this->assertEquals('', $result, 'When inside the last links range, no links should be made');
  2280. }
  2281. /**
  2282. * test the options for last()
  2283. *
  2284. * @return void
  2285. */
  2286. public function testLastOptions()
  2287. {
  2288. $this->Paginator->request->params['paging'] = [
  2289. 'Client' => [
  2290. 'page' => 4,
  2291. 'current' => 3,
  2292. 'count' => 30,
  2293. 'prevPage' => false,
  2294. 'nextPage' => 2,
  2295. 'pageCount' => 15,
  2296. 'sort' => 'Client.name',
  2297. 'direction' => 'DESC',
  2298. ]
  2299. ];
  2300. $result = $this->Paginator->last();
  2301. $expected = [
  2302. 'li' => ['class' => 'last'],
  2303. 'a' => [
  2304. 'href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC',
  2305. ],
  2306. 'last &gt;&gt;', '/a',
  2307. '/li',
  2308. ];
  2309. $this->assertHtml($expected, $result);
  2310. $result = $this->Paginator->last(1);
  2311. $expected = [
  2312. '<li',
  2313. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  2314. '/li',
  2315. ];
  2316. $this->assertHtml($expected, $result);
  2317. $result = $this->Paginator->last(2);
  2318. $expected = [
  2319. '<li',
  2320. ['a' => ['href' => '/index?page=14&amp;sort=Client.name&amp;direction=DESC']], '14', '/a',
  2321. '/li',
  2322. '<li',
  2323. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  2324. '/li',
  2325. ];
  2326. $this->assertHtml($expected, $result);
  2327. }
  2328. /**
  2329. * test last() with a the model parameter.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testLastNonDefaultModel()
  2334. {
  2335. $this->Paginator->request->params['paging']['Article']['page'] = 7;
  2336. $this->Paginator->request->params['paging']['Client'] = [
  2337. 'page' => 3,
  2338. 'current' => 3,
  2339. 'count' => 13,
  2340. 'prevPage' => false,
  2341. 'nextPage' => true,
  2342. 'pageCount' => 5,
  2343. ];
  2344. $result = $this->Paginator->last('last', ['model' => 'Article']);
  2345. $this->assertEquals('', $result);
  2346. $result = $this->Paginator->last('last', ['model' => 'Client']);
  2347. $expected = [
  2348. 'li' => ['class' => 'last'],
  2349. 'a' => ['href' => '/index?page=5'],
  2350. 'last',
  2351. '/a',
  2352. '/li'
  2353. ];
  2354. $this->assertHtml($expected, $result);
  2355. }
  2356. /**
  2357. * testCounter method
  2358. *
  2359. * @return void
  2360. */
  2361. public function testCounter()
  2362. {
  2363. $this->Paginator->request->params['paging'] = [
  2364. 'Client' => [
  2365. 'page' => 1,
  2366. 'current' => 3,
  2367. 'count' => 13,
  2368. 'perPage' => 3,
  2369. 'prevPage' => false,
  2370. 'nextPage' => true,
  2371. 'pageCount' => 5,
  2372. 'limit' => 3,
  2373. 'sort' => 'Client.name',
  2374. 'order' => 'DESC',
  2375. ]
  2376. ];
  2377. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  2378. $input .= 'starting on record {{start}}, ending on {{end}}';
  2379. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
  2380. $expected .= 'ending on 3';
  2381. $result = $this->Paginator->counter($input);
  2382. $this->assertEquals($expected, $result);
  2383. $result = $this->Paginator->counter(['format' => 'pages']);
  2384. $expected = '1 of 5';
  2385. $this->assertEquals($expected, $result);
  2386. $result = $this->Paginator->counter(['format' => 'range']);
  2387. $expected = '1 - 3 of 13';
  2388. $this->assertEquals($expected, $result);
  2389. $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}');
  2390. $this->assertEquals('Showing 1 of 5 clients', $result);
  2391. }
  2392. /**
  2393. * Tests that numbers are formatted according to the locale when using counter()
  2394. *
  2395. * @return void
  2396. */
  2397. public function testCounterBigNumbers()
  2398. {
  2399. $this->Paginator->request->params['paging'] = [
  2400. 'Client' => [
  2401. 'page' => 1523,
  2402. 'current' => 1230,
  2403. 'count' => 234567,
  2404. 'perPage' => 3000,
  2405. 'prevPage' => false,
  2406. 'nextPage' => true,
  2407. 'pageCount' => 1000,
  2408. 'limit' => 5000,
  2409. 'sort' => 'Client.name',
  2410. 'order' => 'DESC',
  2411. ]
  2412. ];
  2413. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  2414. $input .= 'starting on record {{start}}, ending on {{end}}';
  2415. $expected = 'Page 1,523 of 1,000, showing 1,230 records out of 234,567 total, ';
  2416. $expected .= 'starting on record 4,566,001, ending on 234,567';
  2417. $result = $this->Paginator->counter($input);
  2418. $this->assertEquals($expected, $result);
  2419. I18n::setLocale('de-DE');
  2420. $expected = 'Page 1.523 of 1.000, showing 1.230 records out of 234.567 total, ';
  2421. $expected .= 'starting on record 4.566.001, ending on 234.567';
  2422. $result = $this->Paginator->counter($input);
  2423. $this->assertEquals($expected, $result);
  2424. }
  2425. /**
  2426. * testHasPage method
  2427. *
  2428. * @return void
  2429. */
  2430. public function testHasPage()
  2431. {
  2432. $result = $this->Paginator->hasPage('Article', 15);
  2433. $this->assertFalse($result);
  2434. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  2435. $this->assertFalse($result);
  2436. $result = $this->Paginator->hasPage('Article', 2);
  2437. $this->assertTrue($result);
  2438. $result = $this->Paginator->hasPage(2);
  2439. $this->assertTrue($result);
  2440. }
  2441. /**
  2442. * testNextLinkUsingDotNotation method
  2443. *
  2444. * @return void
  2445. */
  2446. public function testNextLinkUsingDotNotation()
  2447. {
  2448. Router::setRequestInfo([
  2449. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  2450. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  2451. ]);
  2452. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  2453. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  2454. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  2455. $test = ['url' => [
  2456. 'page' => '1',
  2457. 'sort' => 'Article.title',
  2458. 'direction' => 'asc',
  2459. ]];
  2460. $this->Paginator->options($test);
  2461. $result = $this->Paginator->next('Next');
  2462. $expected = [
  2463. 'li' => ['class' => 'next'],
  2464. 'a' => [
  2465. 'href' => '/accounts/index?page=2&amp;sort=Article.title&amp;direction=asc',
  2466. 'rel' => 'next'
  2467. ],
  2468. 'Next',
  2469. '/a',
  2470. '/li',
  2471. ];
  2472. $this->assertHtml($expected, $result);
  2473. }
  2474. /**
  2475. * test the current() method
  2476. *
  2477. * @return void
  2478. */
  2479. public function testCurrent()
  2480. {
  2481. $result = $this->Paginator->current();
  2482. $this->assertEquals($this->Paginator->request->params['paging']['Article']['page'], $result);
  2483. $result = $this->Paginator->current('Incorrect');
  2484. $this->assertEquals(1, $result);
  2485. }
  2486. /**
  2487. * test the total() method
  2488. *
  2489. * @return void
  2490. */
  2491. public function testTotal()
  2492. {
  2493. $result = $this->Paginator->total();
  2494. $this->assertSame($this->Paginator->request->params['paging']['Article']['pageCount'], $result);
  2495. $result = $this->Paginator->total('Incorrect');
  2496. $this->assertSame(0, $result);
  2497. }
  2498. /**
  2499. * test the defaultModel() method
  2500. *
  2501. * @return void
  2502. */
  2503. public function testNoDefaultModel()
  2504. {
  2505. $this->Paginator->request = new ServerRequest();
  2506. $this->assertNull($this->Paginator->defaultModel());
  2507. $this->Paginator->defaultModel('Article');
  2508. $this->assertEquals('Article', $this->Paginator->defaultModel());
  2509. $this->Paginator->options(['model' => 'Client']);
  2510. $this->assertEquals('Client', $this->Paginator->defaultModel());
  2511. }
  2512. /**
  2513. * test the numbers() method when there is only one page
  2514. *
  2515. * @return void
  2516. */
  2517. public function testWithOnePage()
  2518. {
  2519. $this->Paginator->request->params['paging'] = [
  2520. 'Article' => [
  2521. 'page' => 1,
  2522. 'current' => 2,
  2523. 'count' => 2,
  2524. 'prevPage' => false,
  2525. 'nextPage' => true,
  2526. 'pageCount' => 1,
  2527. ]
  2528. ];
  2529. $this->assertFalse($this->Paginator->numbers());
  2530. $this->assertFalse($this->Paginator->first());
  2531. $this->assertFalse($this->Paginator->last());
  2532. }
  2533. /**
  2534. * test the numbers() method when there is only one page
  2535. *
  2536. * @return void
  2537. */
  2538. public function testWithZeroPages()
  2539. {
  2540. $this->Paginator->request->params['paging'] = [
  2541. 'Article' => [
  2542. 'page' => 0,
  2543. 'current' => 0,
  2544. 'count' => 0,
  2545. 'perPage' => 10,
  2546. 'prevPage' => false,
  2547. 'nextPage' => false,
  2548. 'pageCount' => 0,
  2549. 'limit' => 10,
  2550. ]
  2551. ];
  2552. $result = $this->Paginator->counter(['format' => 'pages']);
  2553. $expected = '0 of 1';
  2554. $this->assertEquals($expected, $result);
  2555. }
  2556. /**
  2557. * Test data for meta()
  2558. *
  2559. * @return array
  2560. */
  2561. public function dataMetaProvider()
  2562. {
  2563. return [
  2564. // Verifies that no next and prev links are created for single page results.
  2565. [1, false, false, 1, [], ''],
  2566. // Verifies that first and last pages are created for single page results.
  2567. [1, false, false, 1, ['first' => true, 'last' => true], '<link href="http://localhost/index" rel="first"/>' .
  2568. '<link href="http://localhost/index" rel="last"/>'],
  2569. // Verifies that first page is created for single page results.
  2570. [1, false, false, 1, ['first' => true], '<link href="http://localhost/index" rel="first"/>'],
  2571. // Verifies that last page is created for single page results.
  2572. [1, false, false, 1, ['last' => true], '<link href="http://localhost/index" rel="last"/>'],
  2573. // Verifies that page 1 only has a next link.
  2574. [1, false, true, 2, [], '<link href="http://localhost/index?page=2" rel="next"/>'],
  2575. // Verifies that page 1 only has next, first and last link.
  2576. [1, false, true, 2, ['first' => true, 'last' => true], '<link href="http://localhost/index?page=2" rel="next"/>' .
  2577. '<link href="http://localhost/index" rel="first"/>' .
  2578. '<link href="http://localhost/index?page=2" rel="last"/>'],
  2579. // Verifies that page 1 only has next and first link.
  2580. [1, false, true, 2, ['first' => true], '<link href="http://localhost/index?page=2" rel="next"/>' .
  2581. '<link href="http://localhost/index" rel="first"/>'],
  2582. // Verifies that page 1 only has next and last link.
  2583. [1, false, true, 2, ['last' => true], '<link href="http://localhost/index?page=2" rel="next"/>' .
  2584. '<link href="http://localhost/index?page=2" rel="last"/>'],
  2585. // Verifies that the last page only has a prev link.
  2586. [2, true, false, 2, [], '<link href="http://localhost/index" rel="prev"/>'],
  2587. // Verifies that the last page only has a prev, first and last link.
  2588. [2, true, false, 2, ['first' => true, 'last' => true], '<link href="http://localhost/index" rel="prev"/>' .
  2589. '<link href="http://localhost/index" rel="first"/>' .
  2590. '<link href="http://localhost/index?page=2" rel="last"/>'],
  2591. // Verifies that a page in the middle has both links.
  2592. [5, true, true, 10, [], '<link href="http://localhost/index?page=4" rel="prev"/>' .
  2593. '<link href="http://localhost/index?page=6" rel="next"/>'],
  2594. // Verifies that a page in the middle has both links.
  2595. [5, true, true, 10, ['first' => true, 'last' => true], '<link href="http://localhost/index?page=4" rel="prev"/>' .
  2596. '<link href="http://localhost/index?page=6" rel="next"/>' .
  2597. '<link href="http://localhost/index" rel="first"/>' .
  2598. '<link href="http://localhost/index?page=10" rel="last"/>']
  2599. ];
  2600. }
  2601. /**
  2602. * @param int $page
  2603. * @param int $prevPage
  2604. * @param int $nextPage
  2605. * @param int $pageCount
  2606. * @param array $options
  2607. * @param string $expected
  2608. * @dataProvider dataMetaProvider
  2609. */
  2610. public function testMeta($page, $prevPage, $nextPage, $pageCount, $options, $expected)
  2611. {
  2612. $this->Paginator->request->params['paging'] = [
  2613. 'Article' => [
  2614. 'page' => $page,
  2615. 'prevPage' => $prevPage,
  2616. 'nextPage' => $nextPage,
  2617. 'pageCount' => $pageCount,
  2618. ]
  2619. ];
  2620. $result = $this->Paginator->meta($options);
  2621. $this->assertSame($expected, $result);
  2622. $this->assertEquals('', $this->View->fetch('meta'));
  2623. $result = $this->Paginator->meta($options + ['block' => true]);
  2624. $this->assertNull($result);
  2625. $this->assertSame($expected, $this->View->fetch('meta'));
  2626. }
  2627. /**
  2628. * test the limitControl() method
  2629. *
  2630. * @return void
  2631. */
  2632. public function testLimitControl()
  2633. {
  2634. $out = $this->Paginator->limitControl([1 => 1]);
  2635. $expected = [
  2636. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2637. ['div' => ['class' => 'input select']],
  2638. ['label' => ['for' => 'limit']],
  2639. 'View',
  2640. '/label',
  2641. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
  2642. ['option' => ['value' => '1']],
  2643. '1',
  2644. '/option',
  2645. '/select',
  2646. '/div',
  2647. '/form'
  2648. ];
  2649. $this->assertHtml($expected, $out);
  2650. $out = $this->Paginator->limitControl([1 => 1, 5 => 5], null, ['class' => 'form-control']);
  2651. $expected = [
  2652. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2653. ['div' => ['class' => 'input select']],
  2654. ['label' => ['for' => 'limit']],
  2655. 'View',
  2656. '/label',
  2657. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()', 'class' => 'form-control']],
  2658. ['option' => ['value' => '1']],
  2659. '1',
  2660. '/option',
  2661. ['option' => ['value' => '5']],
  2662. '5',
  2663. '/option',
  2664. '/select',
  2665. '/div',
  2666. '/form'
  2667. ];
  2668. $this->assertHtml($expected, $out);
  2669. $out = $this->Paginator->limitControl([], null, ['class' => 'form-control']);
  2670. $expected = [
  2671. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2672. ['div' => ['class' => 'input select']],
  2673. ['label' => ['for' => 'limit']],
  2674. 'View',
  2675. '/label',
  2676. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()', 'class' => 'form-control']],
  2677. ['option' => ['value' => '20']],
  2678. '20',
  2679. '/option',
  2680. ['option' => ['value' => '50']],
  2681. '50',
  2682. '/option',
  2683. ['option' => ['value' => '100']],
  2684. '100',
  2685. '/option',
  2686. '/select',
  2687. '/div',
  2688. '/form'
  2689. ];
  2690. $this->assertHtml($expected, $out);
  2691. $out = $this->Paginator->limitControl();
  2692. $expected = [
  2693. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2694. ['div' => ['class' => 'input select']],
  2695. ['label' => ['for' => 'limit']],
  2696. 'View',
  2697. '/label',
  2698. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
  2699. ['option' => ['value' => '20']],
  2700. '20',
  2701. '/option',
  2702. ['option' => ['value' => '50']],
  2703. '50',
  2704. '/option',
  2705. ['option' => ['value' => '100']],
  2706. '100',
  2707. '/option',
  2708. '/select',
  2709. '/div',
  2710. '/form'
  2711. ];
  2712. $this->assertHtml($expected, $out);
  2713. }
  2714. }