PaginatorHelperTest.php 123 KB

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