PaginatorHelperTest.php 125 KB

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