PaginatorHelperTest.php 123 KB

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