PaginatorHelperTest.php 111 KB

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