PaginatorHelperTest.php 121 KB

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