PaginatorHelperTest.php 115 KB

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