PaginatorHelperTest.php 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  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. * Verify that sort links always result in a url that is page 1 (page not
  723. * present in the url)
  724. *
  725. * @param string $field
  726. * @param array $options
  727. * @param string $expected
  728. * @dataProvider urlGenerationResetsToPage1Provider
  729. */
  730. public function testUrlGenerationResetsToPage1($field, $options, $expected)
  731. {
  732. $this->Paginator->request = $this->Paginator->request
  733. ->withParam('paging.Article.page', 2)
  734. ->withParam('paging.Article.sort', 'name')
  735. ->withParam('paging.Article.direction', 'asc');
  736. $result = $this->Paginator->sort($field, null, ['url' => $options]);
  737. $this->assertSame($expected, $result);
  738. }
  739. /**
  740. * Returns data sets of:
  741. * * the name of the field being sorted on
  742. * * url paramters to pass to paginator sort
  743. * * expected result as a string
  744. *
  745. * @return array
  746. */
  747. public function urlGenerationResetsToPage1Provider()
  748. {
  749. return [
  750. 'Sorting the field currently sorted asc, asc' => [
  751. 'name',
  752. ['sort' => 'name', 'direction' => 'asc'],
  753. '<a class="asc" href="/index?sort=name&amp;direction=asc">Name</a>'
  754. ],
  755. 'Sorting the field currently sorted asc, desc' => [
  756. 'name',
  757. ['sort' => 'name', 'direction' => 'desc'],
  758. '<a class="asc" href="/index?sort=name&amp;direction=desc">Name</a>'
  759. ],
  760. 'Sorting other asc' => [
  761. 'other',
  762. ['sort' => 'other', 'direction' => 'asc'],
  763. '<a href="/index?sort=other&amp;direction=asc">Other</a>'
  764. ],
  765. 'Sorting other desc' => [
  766. 'other',
  767. ['sort' => 'other', 'direction' => 'desc'],
  768. '<a href="/index?sort=other&amp;direction=desc">Other</a>'
  769. ]
  770. ];
  771. }
  772. /**
  773. * test URL generation with prefix routes
  774. *
  775. * @return void
  776. */
  777. public function testGenerateUrlWithPrefixes()
  778. {
  779. Router::reload();
  780. Router::connect('/members/:controller/:action/*', ['prefix' => 'members']);
  781. Router::connect('/:controller/:action/*');
  782. $request = new ServerRequest([
  783. 'url' => '/posts/index/',
  784. 'params' => [
  785. 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'pass' => []
  786. ],
  787. 'base' => '',
  788. 'webroot' => '/'
  789. ]);
  790. Router::setRequestInfo($request);
  791. $this->Paginator->request = $this->Paginator->request
  792. ->withParam('paging.Article.page', 2)
  793. ->withParam('paging.Article.prevPage', true);
  794. $options = ['prefix' => 'members'];
  795. $result = $this->Paginator->generateUrl($options);
  796. $expected = '/members/posts/index?page=2';
  797. $this->assertEquals($expected, $result);
  798. $result = $this->Paginator->sort('name', null, ['url' => $options]);
  799. $expected = [
  800. 'a' => ['href' => '/members/posts/index?sort=name&amp;direction=asc'],
  801. 'Name',
  802. '/a'
  803. ];
  804. $this->assertHtml($expected, $result);
  805. $result = $this->Paginator->next('next', ['url' => $options]);
  806. $expected = [
  807. 'li' => ['class' => 'next'],
  808. 'a' => ['href' => '/members/posts/index?page=3', 'rel' => 'next'],
  809. 'next',
  810. '/a',
  811. '/li'
  812. ];
  813. $this->assertHtml($expected, $result);
  814. $result = $this->Paginator->prev('prev', ['url' => $options]);
  815. $expected = [
  816. 'li' => ['class' => 'prev'],
  817. 'a' => ['href' => '/members/posts/index', 'rel' => 'prev'],
  818. 'prev',
  819. '/a',
  820. '/li'
  821. ];
  822. $this->assertHtml($expected, $result);
  823. $options = ['prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc'];
  824. $result = $this->Paginator->generateUrl($options);
  825. $expected = '/members/posts/index?page=2&amp;sort=name&amp;direction=desc';
  826. $this->assertEquals($expected, $result);
  827. $options = ['controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc'];
  828. $result = $this->Paginator->generateUrl($options);
  829. $expected = '/posts/index?page=2&amp;sort=Article.name&amp;direction=desc';
  830. $this->assertEquals($expected, $result);
  831. }
  832. /**
  833. * test URL generation can leave prefix routes
  834. *
  835. * @return void
  836. */
  837. public function testGenerateUrlWithPrefixesLeavePrefix()
  838. {
  839. Router::reload();
  840. Router::connect('/members/:controller/:action/*', ['prefix' => 'members']);
  841. Router::connect('/:controller/:action/*');
  842. $request = new ServerRequest([
  843. 'params' => [
  844. 'prefix' => 'members',
  845. 'controller' => 'posts',
  846. 'action' => 'index',
  847. 'plugin' => null,
  848. 'paging' => [
  849. 'Articles' => ['page' => 2, 'prevPage' => true]
  850. ]
  851. ],
  852. 'webroot' => '/'
  853. ]);
  854. Router::setRequestInfo($request);
  855. $this->Paginator->request = $request;
  856. $result = $this->Paginator->generateUrl();
  857. $expected = '/members/posts/index?page=2';
  858. $this->assertEquals($expected, $result);
  859. $result = $this->Paginator->generateUrl(['prefix' => 'members']);
  860. $expected = '/members/posts/index?page=2';
  861. $this->assertEquals($expected, $result);
  862. $result = $this->Paginator->generateUrl(['prefix' => false]);
  863. $expected = '/posts/index?page=2';
  864. $this->assertEquals($expected, $result);
  865. $this->Paginator->options(['url' => ['prefix' => false]]);
  866. $result = $this->Paginator->generateUrl();
  867. $this->assertEquals($expected, $result, 'Setting prefix in options should work too.');
  868. }
  869. /**
  870. * test generateUrl with multiple pagination
  871. *
  872. * @return void
  873. */
  874. public function testGenerateUrlMultiplePagination()
  875. {
  876. $request = new ServerRequest([
  877. 'url' => '/posts/index/',
  878. 'params' => [
  879. 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'pass' => []
  880. ],
  881. 'base' => '',
  882. 'webroot' => '/'
  883. ]);
  884. Router::setRequestInfo($request);
  885. $this->Paginator->request = $this->Paginator->request
  886. ->withParam('paging.Article.scope', 'article')
  887. ->withParam('paging.Article.page', 3)
  888. ->withParam('paging.Article.prevPage', true);
  889. $this->Paginator->options(['model' => 'Article']);
  890. $result = $this->Paginator->generateUrl([]);
  891. $expected = '/posts/index?article%5Bpage%5D=3';
  892. $this->assertEquals($expected, $result);
  893. $result = $this->Paginator->sort('name');
  894. $expected = [
  895. 'a' => ['href' => '/posts/index?article%5Bsort%5D=name&amp;article%5Bdirection%5D=asc'],
  896. 'Name',
  897. '/a'
  898. ];
  899. $this->assertHtml($expected, $result);
  900. $result = $this->Paginator->next('next');
  901. $expected = [
  902. 'li' => ['class' => 'next'],
  903. 'a' => ['href' => '/posts/index?article%5Bpage%5D=4', 'rel' => 'next'],
  904. 'next',
  905. '/a',
  906. '/li'
  907. ];
  908. $this->assertHtml($expected, $result);
  909. $result = $this->Paginator->prev('prev');
  910. $expected = [
  911. 'li' => ['class' => 'prev'],
  912. 'a' => ['href' => '/posts/index?article%5Bpage%5D=2', 'rel' => 'prev'],
  913. 'prev',
  914. '/a',
  915. '/li'
  916. ];
  917. $this->assertHtml($expected, $result);
  918. $result = $this->Paginator->generateUrl(['sort' => 'name']);
  919. $expected = '/posts/index?article%5Bpage%5D=3&amp;article%5Bsort%5D=name';
  920. $this->assertEquals($expected, $result);
  921. $result = $this->Paginator->generateUrl(['#' => 'foo']);
  922. $expected = '/posts/index?article%5Bpage%5D=3#foo';
  923. $this->assertEquals($expected, $result);
  924. }
  925. /**
  926. * test generateUrl with multiple pagination and query string values
  927. *
  928. * @return void
  929. */
  930. public function testGenerateUrlMultiplePaginationQueryStringData()
  931. {
  932. $request = new ServerRequest([
  933. 'url' => '/posts/index/',
  934. 'params' => [
  935. 'plugin' => null, 'controller' => 'posts', 'action' => 'index'
  936. ]
  937. ]);
  938. Router::setRequestInfo($request);
  939. $this->View->setRequest($this->Paginator->request
  940. ->withParam('paging.Article.scope', 'article')
  941. ->withParam('paging.Article.page', 3)
  942. ->withParam('paging.Article.prevPage', true)
  943. ->withQueryParams([
  944. 'article' => [
  945. 'puppy' => 'no'
  946. ]
  947. ])
  948. );
  949. // Need to run __construct to update _config['url']
  950. $paginator = new PaginatorHelper($this->View);
  951. $paginator->options(['model' => 'Article']);
  952. $result = $paginator->generateUrl(['sort' => 'name']);
  953. $expected = '/posts/index?article%5Bpage%5D=3&amp;article%5Bsort%5D=name&amp;article%5Bpuppy%5D=no';
  954. $this->assertEquals($expected, $result);
  955. $result = $paginator->generateUrl([]);
  956. $expected = '/posts/index?article%5Bpage%5D=3&amp;article%5Bpuppy%5D=no';
  957. $this->assertEquals($expected, $result);
  958. }
  959. /**
  960. * testOptions method
  961. *
  962. * @return void
  963. */
  964. public function testOptions()
  965. {
  966. $this->Paginator->options = [];
  967. $this->Paginator->request = $this->Paginator->request->withAttribute('params', []);
  968. $options = ['paging' => ['Article' => [
  969. 'direction' => 'desc',
  970. 'sort' => 'title'
  971. ]]];
  972. $this->Paginator->options($options);
  973. $expected = ['Article' => [
  974. 'direction' => 'desc',
  975. 'sort' => 'title'
  976. ]];
  977. $this->assertEquals($expected, $this->Paginator->request->getParam('paging'));
  978. $this->Paginator->options = [];
  979. $this->Paginator->request = $this->Paginator->request->withAttribute('params', []);
  980. $options = ['Article' => [
  981. 'direction' => 'desc',
  982. 'sort' => 'title'
  983. ]];
  984. $this->Paginator->options($options);
  985. $this->assertEquals($expected, $this->Paginator->request->getParam('paging'));
  986. $options = ['paging' => ['Article' => [
  987. 'direction' => 'desc',
  988. 'sort' => 'Article.title'
  989. ]]];
  990. $this->Paginator->options($options);
  991. $expected = ['Article' => [
  992. 'direction' => 'desc',
  993. 'sort' => 'Article.title'
  994. ]];
  995. $this->assertEquals($expected, $this->Paginator->request->getParam('paging'));
  996. }
  997. /**
  998. * testPassedArgsMergingWithUrlOptions method
  999. *
  1000. * @return void
  1001. */
  1002. public function testPassedArgsMergingWithUrlOptions()
  1003. {
  1004. $request = new ServerRequest([
  1005. 'url' => '/articles/',
  1006. 'params' => [
  1007. 'plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => []
  1008. ],
  1009. ]);
  1010. Router::setRequestInfo($request);
  1011. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1012. 'Article' => [
  1013. 'page' => 1, 'current' => 3, 'count' => 13,
  1014. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  1015. 'sort' => null, 'direction' => null,
  1016. ]
  1017. ]);
  1018. $this->Paginator->request = $this->Paginator->request
  1019. ->withParam('pass', [2])
  1020. ->withQueryParams(['page' => 1, 'foo' => 'bar', 'x' => 'y', 'num' => 0]);
  1021. $this->View->setRequest($this->Paginator->request);
  1022. $this->Paginator = new PaginatorHelper($this->View);
  1023. $result = $this->Paginator->sort('title');
  1024. $expected = [
  1025. 'a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;sort=title&amp;direction=asc'],
  1026. 'Title',
  1027. '/a'
  1028. ];
  1029. $this->assertHtml($expected, $result);
  1030. $result = $this->Paginator->numbers();
  1031. $expected = [
  1032. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1033. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=2']], '2', '/a', '/li',
  1034. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=3']], '3', '/a', '/li',
  1035. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=4']], '4', '/a', '/li',
  1036. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=5']], '5', '/a', '/li',
  1037. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=6']], '6', '/a', '/li',
  1038. ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=7']], '7', '/a', '/li',
  1039. ];
  1040. $this->assertHtml($expected, $result);
  1041. $result = $this->Paginator->next('Next');
  1042. $expected = [
  1043. 'li' => ['class' => 'next'],
  1044. 'a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;page=2', 'rel' => 'next'],
  1045. 'Next',
  1046. '/a',
  1047. '/li'
  1048. ];
  1049. $this->assertHtml($expected, $result);
  1050. }
  1051. /**
  1052. * Test that generated URLs don't include sort and direction parameters
  1053. *
  1054. * @return void
  1055. */
  1056. public function testDefaultSortRemovedFromUrl()
  1057. {
  1058. $request = new ServerRequest([
  1059. 'url' => '/articles/',
  1060. 'params' => [
  1061. 'plugin' => null, 'controller' => 'articles', 'action' => 'index'
  1062. ]
  1063. ]);
  1064. Router::setRequestInfo($request);
  1065. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1066. 'Article' => [
  1067. 'page' => 1, 'current' => 3, 'count' => 13,
  1068. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  1069. 'sort' => 'Article.title', 'direction' => 'ASC',
  1070. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
  1071. ]
  1072. ]);
  1073. $result = $this->Paginator->next('Next');
  1074. $expected = [
  1075. 'li' => ['class' => 'next'],
  1076. 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'],
  1077. 'Next',
  1078. '/a',
  1079. '/li'
  1080. ];
  1081. $this->assertHtml($expected, $result);
  1082. }
  1083. /**
  1084. * Test the prev() method.
  1085. *
  1086. * @return void
  1087. */
  1088. public function testPrev()
  1089. {
  1090. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1091. 'Client' => [
  1092. 'page' => 1,
  1093. 'current' => 3,
  1094. 'count' => 13,
  1095. 'prevPage' => false,
  1096. 'nextPage' => true,
  1097. 'pageCount' => 5,
  1098. ]
  1099. ]);
  1100. $result = $this->Paginator->prev('<< Previous');
  1101. $expected = [
  1102. 'li' => ['class' => 'prev disabled'],
  1103. 'a' => ['href' => '', 'onclick' => 'return false;'],
  1104. '&lt;&lt; Previous',
  1105. '/a',
  1106. '/li'
  1107. ];
  1108. $this->assertHtml($expected, $result);
  1109. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => 'Prev']);
  1110. $expected = [
  1111. 'li' => ['class' => 'prev disabled'],
  1112. 'a' => ['href' => '', 'onclick' => 'return false;'],
  1113. 'Prev',
  1114. '/a',
  1115. '/li'
  1116. ];
  1117. $this->assertHtml($expected, $result);
  1118. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => false]);
  1119. $this->assertEquals('', $result, 'disabled + no text = no link');
  1120. $this->Paginator->request = $this->Paginator->request
  1121. ->withParam('paging.Client.page', 2)
  1122. ->withParam('paging.Client.prevPage', true);
  1123. $result = $this->Paginator->prev('<< Previous');
  1124. $expected = [
  1125. 'li' => ['class' => 'prev'],
  1126. 'a' => ['href' => '/index', 'rel' => 'prev'],
  1127. '&lt;&lt; Previous',
  1128. '/a',
  1129. '/li'
  1130. ];
  1131. $this->assertHtml($expected, $result);
  1132. $result = $this->Paginator->prev('Prev', [
  1133. 'templates' => [
  1134. 'prevActive' => '<a rel="prev" href="{{url}}">{{text}}</a>'
  1135. ]
  1136. ]);
  1137. $expected = [
  1138. 'a' => ['href' => '/index', 'rel' => 'prev'],
  1139. 'Prev',
  1140. '/a',
  1141. ];
  1142. $this->assertHtml($expected, $result);
  1143. }
  1144. /**
  1145. * Test that prev() and the shared implementation underneath picks up from options
  1146. *
  1147. * @return void
  1148. */
  1149. public function testPrevWithOptions()
  1150. {
  1151. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1152. 'Client' => [
  1153. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
  1154. 'nextPage' => false, 'pageCount' => 2,
  1155. 'limit' => 10,
  1156. ]
  1157. ]);
  1158. $this->Paginator->options(['url' => [12, 'page' => 3]]);
  1159. $result = $this->Paginator->prev('Prev', ['url' => ['foo' => 'bar']]);
  1160. $expected = [
  1161. 'li' => ['class' => 'prev'],
  1162. 'a' => ['href' => '/index/12?limit=10&amp;foo=bar', 'rel' => 'prev'],
  1163. 'Prev',
  1164. '/a',
  1165. '/li'
  1166. ];
  1167. $this->assertHtml($expected, $result);
  1168. }
  1169. /**
  1170. * test the next() method.
  1171. *
  1172. * @return void
  1173. */
  1174. public function testNext()
  1175. {
  1176. $result = $this->Paginator->next('Next >>');
  1177. $expected = [
  1178. 'li' => ['class' => 'next'],
  1179. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1180. 'Next &gt;&gt;',
  1181. '/a',
  1182. '/li'
  1183. ];
  1184. $this->assertHtml($expected, $result);
  1185. $result = $this->Paginator->next('Next', [
  1186. 'templates' => [
  1187. 'nextActive' => '<a rel="next" href="{{url}}">{{text}}</a>'
  1188. ]
  1189. ]);
  1190. $expected = [
  1191. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1192. 'Next',
  1193. '/a',
  1194. ];
  1195. $this->assertHtml($expected, $result);
  1196. $result = $this->Paginator->next('Next >>', ['escape' => false]);
  1197. $expected = [
  1198. 'li' => ['class' => 'next'],
  1199. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1200. 'preg:/Next >>/',
  1201. '/a',
  1202. '/li'
  1203. ];
  1204. $this->assertHtml($expected, $result);
  1205. }
  1206. /**
  1207. * test next() with disabled links
  1208. *
  1209. * @return void
  1210. */
  1211. public function testNextDisabled()
  1212. {
  1213. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1214. 'Client' => [
  1215. 'page' => 5,
  1216. 'current' => 3,
  1217. 'count' => 13,
  1218. 'prevPage' => true,
  1219. 'nextPage' => false,
  1220. 'pageCount' => 5,
  1221. ]
  1222. ]);
  1223. $result = $this->Paginator->next('Next >>');
  1224. $expected = [
  1225. 'li' => ['class' => 'next disabled'],
  1226. 'a' => ['href' => '', 'onclick' => 'return false;'],
  1227. 'Next &gt;&gt;',
  1228. '/a',
  1229. '/li'
  1230. ];
  1231. $this->assertHtml($expected, $result);
  1232. $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']);
  1233. $expected = [
  1234. 'li' => ['class' => 'next disabled'],
  1235. 'a' => ['href' => '', 'onclick' => 'return false;'],
  1236. 'Next',
  1237. '/a',
  1238. '/li'
  1239. ];
  1240. $this->assertHtml($expected, $result);
  1241. $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]);
  1242. $this->assertEquals('', $result, 'disabled + no text = no link');
  1243. }
  1244. /**
  1245. * Test next() with a model argument.
  1246. *
  1247. * @return void
  1248. */
  1249. public function testNextAndPrevNonDefaultModel()
  1250. {
  1251. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1252. 'Client' => [
  1253. 'page' => 1,
  1254. 'current' => 3,
  1255. 'count' => 13,
  1256. 'prevPage' => false,
  1257. 'nextPage' => true,
  1258. 'pageCount' => 5,
  1259. ],
  1260. 'Server' => [
  1261. 'page' => 5,
  1262. 'current' => 1,
  1263. 'count' => 5,
  1264. 'prevPage' => true,
  1265. 'nextPage' => false,
  1266. 'pageCount' => 5,
  1267. ]
  1268. ]);
  1269. $result = $this->Paginator->next('Next', [
  1270. 'model' => 'Client'
  1271. ]);
  1272. $expected = [
  1273. 'li' => ['class' => 'next'],
  1274. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  1275. 'Next',
  1276. '/a',
  1277. '/li'
  1278. ];
  1279. $this->assertHtml($expected, $result);
  1280. $result = $this->Paginator->prev('Prev', [
  1281. 'model' => 'Client'
  1282. ]);
  1283. $expected = '<li class="prev disabled"><a href="" onclick="return false;">Prev</a></li>';
  1284. $this->assertEquals($expected, $result);
  1285. $result = $this->Paginator->next('Next', [
  1286. 'model' => 'Server'
  1287. ]);
  1288. $expected = '<li class="next disabled"><a href="" onclick="return false;">Next</a></li>';
  1289. $this->assertEquals($expected, $result);
  1290. $result = $this->Paginator->prev('Prev', [
  1291. 'model' => 'Server'
  1292. ]);
  1293. $expected = [
  1294. 'li' => ['class' => 'prev'],
  1295. 'a' => ['href' => '/index?page=4', 'rel' => 'prev'],
  1296. 'Prev',
  1297. '/a',
  1298. '/li'
  1299. ];
  1300. $this->assertHtml($expected, $result);
  1301. }
  1302. /**
  1303. * testNumbers method
  1304. *
  1305. * @return void
  1306. */
  1307. public function testNumbers()
  1308. {
  1309. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1310. 'Client' => [
  1311. 'page' => 8,
  1312. 'current' => 3,
  1313. 'count' => 30,
  1314. 'prevPage' => false,
  1315. 'nextPage' => 2,
  1316. 'pageCount' => 15,
  1317. ]
  1318. ]);
  1319. $result = $this->Paginator->numbers();
  1320. $expected = [
  1321. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1322. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1323. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1324. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1325. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1326. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1327. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1328. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1329. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1330. ];
  1331. $this->assertHtml($expected, $result);
  1332. $result = $this->Paginator->numbers(['first' => 'first', 'last' => 'last']);
  1333. $expected = [
  1334. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], 'first', '/a', '/li',
  1335. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1336. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1337. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1338. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1339. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1340. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1341. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1342. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1343. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1344. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1345. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1346. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], 'last', '/a', '/li',
  1347. ];
  1348. $this->assertHtml($expected, $result);
  1349. $result = $this->Paginator->numbers(['first' => '2', 'last' => '8']);
  1350. $expected = [
  1351. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '2', '/a', '/li',
  1352. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1353. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1354. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1355. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1356. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1357. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1358. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1359. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1360. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1361. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1362. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1363. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. $result = $this->Paginator->numbers(['first' => '8', 'last' => '8']);
  1367. $expected = [
  1368. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '8', '/a', '/li',
  1369. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1370. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1371. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1372. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1373. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1374. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1375. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1376. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1377. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1378. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1379. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1380. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
  1381. ];
  1382. $this->assertHtml($expected, $result);
  1383. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1384. 'Client' => [
  1385. 'page' => 1,
  1386. 'current' => 3,
  1387. 'count' => 30,
  1388. 'prevPage' => false,
  1389. 'nextPage' => 2,
  1390. 'pageCount' => 15,
  1391. ]
  1392. ]);
  1393. $result = $this->Paginator->numbers();
  1394. $expected = [
  1395. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1396. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1397. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1398. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1399. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1400. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1401. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1402. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1403. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1404. ];
  1405. $this->assertHtml($expected, $result);
  1406. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1407. 'Client' => [
  1408. 'page' => 14,
  1409. 'current' => 3,
  1410. 'count' => 30,
  1411. 'prevPage' => false,
  1412. 'nextPage' => 2,
  1413. 'pageCount' => 15,
  1414. ]
  1415. ]);
  1416. $result = $this->Paginator->numbers();
  1417. $expected = [
  1418. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1419. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1420. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1421. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1422. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1423. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1424. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1425. ['li' => ['class' => 'active']], '<a href=""', '14', '/a', '/li',
  1426. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1427. ];
  1428. $this->assertHtml($expected, $result);
  1429. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1430. 'Client' => [
  1431. 'page' => 2,
  1432. 'current' => 3,
  1433. 'count' => 27,
  1434. 'prevPage' => false,
  1435. 'nextPage' => 2,
  1436. 'pageCount' => 9,
  1437. ]
  1438. ]);
  1439. $result = $this->Paginator->numbers(['first' => 1]);
  1440. $expected = [
  1441. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1442. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1443. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1444. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1445. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1446. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1447. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1448. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1449. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1450. ];
  1451. $this->assertHtml($expected, $result);
  1452. $result = $this->Paginator->numbers(['last' => 1]);
  1453. $expected = [
  1454. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1455. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1456. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1457. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1458. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1459. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1460. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1461. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1462. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1463. ];
  1464. $this->assertHtml($expected, $result);
  1465. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1466. 'Client' => [
  1467. 'page' => 15,
  1468. 'current' => 3,
  1469. 'count' => 30,
  1470. 'prevPage' => false,
  1471. 'nextPage' => 2,
  1472. 'pageCount' => 15,
  1473. ]
  1474. ]);
  1475. $result = $this->Paginator->numbers(['first' => 1]);
  1476. $expected = [
  1477. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1478. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1479. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1480. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1481. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1482. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1483. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1484. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1485. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1486. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1487. ['li' => ['class' => 'active']], '<a href=""', '15', '/a', '/li',
  1488. ];
  1489. $this->assertHtml($expected, $result);
  1490. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1491. 'Client' => [
  1492. 'page' => 10,
  1493. 'current' => 3,
  1494. 'count' => 30,
  1495. 'prevPage' => false,
  1496. 'nextPage' => 2,
  1497. 'pageCount' => 15,
  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=6']], '6', '/a', '/li',
  1505. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1506. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1507. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1508. ['li' => ['class' => 'active']], '<a href=""', '10', '/a', '/li',
  1509. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1510. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1511. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1512. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1513. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1514. ];
  1515. $this->assertHtml($expected, $result);
  1516. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1517. 'Client' => [
  1518. 'page' => 6,
  1519. 'current' => 15,
  1520. 'count' => 623,
  1521. 'prevPage' => 1,
  1522. 'nextPage' => 1,
  1523. 'pageCount' => 42,
  1524. ]
  1525. ]);
  1526. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1527. $expected = [
  1528. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1529. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1530. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1531. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1532. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1533. ['li' => ['class' => 'active']], '<a href=""', '6', '/a', '/li',
  1534. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1535. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1536. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1537. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1538. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1539. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1540. ];
  1541. $this->assertHtml($expected, $result);
  1542. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1543. 'Client' => [
  1544. 'page' => 37,
  1545. 'current' => 15,
  1546. 'count' => 623,
  1547. 'prevPage' => 1,
  1548. 'nextPage' => 1,
  1549. 'pageCount' => 42,
  1550. ]
  1551. ]);
  1552. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1553. $expected = [
  1554. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1555. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1556. ['li' => []], ['a' => ['href' => '/index?page=33']], '33', '/a', '/li',
  1557. ['li' => []], ['a' => ['href' => '/index?page=34']], '34', '/a', '/li',
  1558. ['li' => []], ['a' => ['href' => '/index?page=35']], '35', '/a', '/li',
  1559. ['li' => []], ['a' => ['href' => '/index?page=36']], '36', '/a', '/li',
  1560. ['li' => ['class' => 'active']], '<a href=""', '37', '/a', '/li',
  1561. ['li' => []], ['a' => ['href' => '/index?page=38']], '38', '/a', '/li',
  1562. ['li' => []], ['a' => ['href' => '/index?page=39']], '39', '/a', '/li',
  1563. ['li' => []], ['a' => ['href' => '/index?page=40']], '40', '/a', '/li',
  1564. ['li' => []], ['a' => ['href' => '/index?page=41']], '41', '/a', '/li',
  1565. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1566. ];
  1567. $this->assertHtml($expected, $result);
  1568. }
  1569. /**
  1570. * testNumbersPages method
  1571. *
  1572. * @return void
  1573. */
  1574. public function testNumbersMulti()
  1575. {
  1576. $expected = [
  1577. 1 => '*1 2 3 4 5 6 7 ',
  1578. 2 => '1 *2 3 4 5 6 7 ',
  1579. 3 => '1 2 *3 4 5 6 7 ',
  1580. 4 => '1 2 3 *4 5 6 7 ',
  1581. 5 => '1 2 3 4 *5 6 7 ',
  1582. 6 => '1 2 3 4 5 *6 7 ',
  1583. 7 => '1 2 3 4 5 6 *7 ',
  1584. ];
  1585. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7);
  1586. $this->assertEquals($expected, $result);
  1587. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7, ['first' => 'F', 'last' => 'L']);
  1588. $this->assertEquals($expected, $result);
  1589. $expected = [
  1590. 1 => '*1 2 3 4 5 6 7 8 9 ',
  1591. 2 => '1 *2 3 4 5 6 7 8 9 ',
  1592. 3 => '1 2 *3 4 5 6 7 8 9 ',
  1593. 4 => '1 2 3 *4 5 6 7 8 9 ',
  1594. 5 => '1 2 3 4 *5 6 7 8 9 ',
  1595. 6 => '2 3 4 5 *6 7 8 9 10 ',
  1596. 7 => '3 4 5 6 *7 8 9 10 11 ',
  1597. 10 => '6 7 8 9 *10 11 12 13 14 ',
  1598. 15 => '11 12 13 14 *15 16 17 18 19 ',
  1599. 16 => '12 13 14 15 *16 17 18 19 20 ',
  1600. 17 => '12 13 14 15 16 *17 18 19 20 ',
  1601. 18 => '12 13 14 15 16 17 *18 19 20 ',
  1602. 19 => '12 13 14 15 16 17 18 *19 20 ',
  1603. 20 => '12 13 14 15 16 17 18 19 *20 ',
  1604. ];
  1605. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20);
  1606. $this->assertEquals($expected, $result);
  1607. $expected = [
  1608. 1 => '*1 2 3 4 5 6 7 8 9 ',
  1609. 2 => '1 *2 3 4 5 6 7 8 9 ',
  1610. 3 => '1 2 *3 4 5 6 7 8 9 ',
  1611. 4 => '1 2 3 *4 5 6 7 8 9 ',
  1612. 5 => '1 2 3 4 *5 6 7 8 9 ',
  1613. 6 => '1 2 3 4 5 *6 7 8 9 10 ',
  1614. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ',
  1615. 8 => '<F ... 4 5 6 7 *8 9 10 11 12 ',
  1616. 9 => '<F ... 5 6 7 8 *9 10 11 12 13 ',
  1617. 10 => '<F ... 6 7 8 9 *10 11 12 13 14 ',
  1618. 15 => '<F ... 11 12 13 14 *15 16 17 18 19 ',
  1619. 16 => '<F ... 12 13 14 15 *16 17 18 19 20 ',
  1620. 17 => '<F ... 12 13 14 15 16 *17 18 19 20 ',
  1621. 18 => '<F ... 12 13 14 15 16 17 *18 19 20 ',
  1622. 19 => '<F ... 12 13 14 15 16 17 18 *19 20 ',
  1623. 20 => '<F ... 12 13 14 15 16 17 18 19 *20 ',
  1624. ];
  1625. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 'F']);
  1626. $this->assertEquals($expected, $result);
  1627. $expected = [
  1628. 1 => '*1 2 3 4 5 6 7 8 9 ',
  1629. 2 => '1 *2 3 4 5 6 7 8 9 ',
  1630. 3 => '1 2 *3 4 5 6 7 8 9 ',
  1631. 4 => '1 2 3 *4 5 6 7 8 9 ',
  1632. 5 => '1 2 3 4 *5 6 7 8 9 ',
  1633. 6 => '1 2 3 4 5 *6 7 8 9 10 ',
  1634. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ',
  1635. 8 => '1 2 3 4 5 6 7 *8 9 10 11 12 ',
  1636. 9 => '1 2 ... 5 6 7 8 *9 10 11 12 13 ',
  1637. 10 => '1 2 ... 6 7 8 9 *10 11 12 13 14 ',
  1638. 15 => '1 2 ... 11 12 13 14 *15 16 17 18 19 ',
  1639. 16 => '1 2 ... 12 13 14 15 *16 17 18 19 20 ',
  1640. 17 => '1 2 ... 12 13 14 15 16 *17 18 19 20 ',
  1641. 18 => '1 2 ... 12 13 14 15 16 17 *18 19 20 ',
  1642. 19 => '1 2 ... 12 13 14 15 16 17 18 *19 20 ',
  1643. 20 => '1 2 ... 12 13 14 15 16 17 18 19 *20 ',
  1644. ];
  1645. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 2]);
  1646. $this->assertEquals($expected, $result);
  1647. $expected = [
  1648. 1 => '*1 2 3 4 5 6 7 8 9 ... L> ',
  1649. 2 => '1 *2 3 4 5 6 7 8 9 ... L> ',
  1650. 3 => '1 2 *3 4 5 6 7 8 9 ... L> ',
  1651. 4 => '1 2 3 *4 5 6 7 8 9 ... L> ',
  1652. 5 => '1 2 3 4 *5 6 7 8 9 ... L> ',
  1653. 6 => '2 3 4 5 *6 7 8 9 10 ... L> ',
  1654. 7 => '3 4 5 6 *7 8 9 10 11 ... L> ',
  1655. 8 => '4 5 6 7 *8 9 10 11 12 ... L> ',
  1656. 9 => '5 6 7 8 *9 10 11 12 13 ... L> ',
  1657. 10 => '6 7 8 9 *10 11 12 13 14 ... L> ',
  1658. 11 => '7 8 9 10 *11 12 13 14 15 ... L> ',
  1659. 12 => '8 9 10 11 *12 13 14 15 16 ... L> ',
  1660. 13 => '9 10 11 12 *13 14 15 16 17 ... L> ',
  1661. 14 => '10 11 12 13 *14 15 16 17 18 19 20 ',
  1662. 15 => '11 12 13 14 *15 16 17 18 19 20 ',
  1663. 16 => '12 13 14 15 *16 17 18 19 20 ',
  1664. 17 => '12 13 14 15 16 *17 18 19 20 ',
  1665. 18 => '12 13 14 15 16 17 *18 19 20 ',
  1666. 19 => '12 13 14 15 16 17 18 *19 20 ',
  1667. 20 => '12 13 14 15 16 17 18 19 *20 ',
  1668. ];
  1669. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['last' => 'L']);
  1670. $this->assertEquals($expected, $result);
  1671. $expected = [
  1672. 1 => '*1 2 3 4 5 6 7 8 9 ... L> ',
  1673. 2 => '1 *2 3 4 5 6 7 8 9 ... L> ',
  1674. 3 => '1 2 *3 4 5 6 7 8 9 ... L> ',
  1675. 4 => '1 2 3 *4 5 6 7 8 9 ... L> ',
  1676. 5 => '1 2 3 4 *5 6 7 8 9 ... L> ',
  1677. 6 => '1 2 3 4 5 *6 7 8 9 10 ... L> ',
  1678. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ... L> ',
  1679. 8 => '<F ... 4 5 6 7 *8 9 10 11 12 ... L> ',
  1680. 9 => '<F ... 5 6 7 8 *9 10 11 12 13 ... L> ',
  1681. 10 => '<F ... 6 7 8 9 *10 11 12 13 14 ... L> ',
  1682. 11 => '<F ... 7 8 9 10 *11 12 13 14 15 ... L> ',
  1683. 12 => '<F ... 8 9 10 11 *12 13 14 15 16 ... L> ',
  1684. 13 => '<F ... 9 10 11 12 *13 14 15 16 17 ... L> ',
  1685. 14 => '<F ... 10 11 12 13 *14 15 16 17 18 19 20 ',
  1686. 15 => '<F ... 11 12 13 14 *15 16 17 18 19 20 ',
  1687. 16 => '<F ... 12 13 14 15 *16 17 18 19 20 ',
  1688. 17 => '<F ... 12 13 14 15 16 *17 18 19 20 ',
  1689. 18 => '<F ... 12 13 14 15 16 17 *18 19 20 ',
  1690. 19 => '<F ... 12 13 14 15 16 17 18 *19 20 ',
  1691. 20 => '<F ... 12 13 14 15 16 17 18 19 *20 ',
  1692. ];
  1693. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 'F', 'last' => 'L']);
  1694. $this->assertEquals($expected, $result);
  1695. $expected = [
  1696. 1 => '*1 2 3 4 5 6 7 8 9 ... 19 20 ',
  1697. 2 => '1 *2 3 4 5 6 7 8 9 ... 19 20 ',
  1698. 3 => '1 2 *3 4 5 6 7 8 9 ... 19 20 ',
  1699. 4 => '1 2 3 *4 5 6 7 8 9 ... 19 20 ',
  1700. 5 => '1 2 3 4 *5 6 7 8 9 ... 19 20 ',
  1701. 6 => '1 2 3 4 5 *6 7 8 9 10 ... 19 20 ',
  1702. 7 => '1 2 3 4 5 6 *7 8 9 10 11 ... 19 20 ',
  1703. 8 => '1 2 3 4 5 6 7 *8 9 10 11 12 ... 19 20 ',
  1704. 9 => '1 2 ... 5 6 7 8 *9 10 11 12 13 ... 19 20 ',
  1705. 10 => '1 2 ... 6 7 8 9 *10 11 12 13 14 ... 19 20 ',
  1706. 11 => '1 2 ... 7 8 9 10 *11 12 13 14 15 ... 19 20 ',
  1707. 12 => '1 2 ... 8 9 10 11 *12 13 14 15 16 ... 19 20 ',
  1708. 13 => '1 2 ... 9 10 11 12 *13 14 15 16 17 18 19 20 ',
  1709. 14 => '1 2 ... 10 11 12 13 *14 15 16 17 18 19 20 ',
  1710. 15 => '1 2 ... 11 12 13 14 *15 16 17 18 19 20 ',
  1711. 16 => '1 2 ... 12 13 14 15 *16 17 18 19 20 ',
  1712. 17 => '1 2 ... 12 13 14 15 16 *17 18 19 20 ',
  1713. 18 => '1 2 ... 12 13 14 15 16 17 *18 19 20 ',
  1714. 19 => '1 2 ... 12 13 14 15 16 17 18 *19 20 ',
  1715. 20 => '1 2 ... 12 13 14 15 16 17 18 19 *20 ',
  1716. ];
  1717. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 2, 'last' => 2]);
  1718. $this->assertEquals($expected, $result);
  1719. $expected = [
  1720. 1 => '*1 2 3 4 5 6 7 8 9 ... 19 20 ',
  1721. 2 => '1 *2 3 4 5 6 7 8 9 ... 19 20 ',
  1722. 3 => '1 2 *3 4 5 6 7 8 9 ... 19 20 ',
  1723. 4 => '1 2 3 *4 5 6 7 8 9 ... 19 20 ',
  1724. 5 => '1 2 3 4 *5 6 7 8 9 ... 19 20 ',
  1725. 6 => '2 3 4 5 *6 7 8 9 10 ... 19 20 ',
  1726. 7 => '3 4 5 6 *7 8 9 10 11 ... 19 20 ',
  1727. 8 => '4 5 6 7 *8 9 10 11 12 ... 19 20 ',
  1728. 9 => '5 6 7 8 *9 10 11 12 13 ... 19 20 ',
  1729. 10 => '6 7 8 9 *10 11 12 13 14 ... 19 20 ',
  1730. 11 => '7 8 9 10 *11 12 13 14 15 ... 19 20 ',
  1731. 12 => '8 9 10 11 *12 13 14 15 16 ... 19 20 ',
  1732. 13 => '9 10 11 12 *13 14 15 16 17 18 19 20 ',
  1733. 14 => '10 11 12 13 *14 15 16 17 18 19 20 ',
  1734. 15 => '11 12 13 14 *15 16 17 18 19 20 ',
  1735. 16 => '12 13 14 15 *16 17 18 19 20 ',
  1736. 17 => '12 13 14 15 16 *17 18 19 20 ',
  1737. 18 => '12 13 14 15 16 17 *18 19 20 ',
  1738. 19 => '12 13 14 15 16 17 18 *19 20 ',
  1739. 20 => '12 13 14 15 16 17 18 19 *20 ',
  1740. ];
  1741. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['last' => 2]);
  1742. $this->assertEquals($expected, $result);
  1743. }
  1744. /**
  1745. * Retrieves result of PaginatorHelper::numbers for multiple pages
  1746. *
  1747. * @param int[] $pagesToCheck Pages to get result for
  1748. * @param int $pageCount Number of total pages
  1749. * @param array $options Options for PaginatorHelper::numbers
  1750. * @return string[]
  1751. */
  1752. protected function getNumbersForMultiplePages($pagesToCheck, $pageCount, $options = [])
  1753. {
  1754. $options['templates'] = [
  1755. 'first' => '<{{text}} ',
  1756. 'last' => '{{text}}> ',
  1757. 'number' => '{{text}} ',
  1758. 'current' => '*{{text}} ',
  1759. 'ellipsis' => '... ',
  1760. ];
  1761. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1762. 'Client' => [
  1763. 'page' => 1,
  1764. 'pageCount' => $pageCount,
  1765. ]
  1766. ]);
  1767. $result = [];
  1768. foreach ($pagesToCheck as $page) {
  1769. $this->Paginator->request = $this->Paginator->request->withParam('paging.Client.page', $page);
  1770. $result[$page] = $this->Paginator->numbers($options);
  1771. }
  1772. return $result;
  1773. }
  1774. /**
  1775. * Test that numbers() lets you overwrite templates.
  1776. *
  1777. * The templates file has no li elements.
  1778. *
  1779. * @return void
  1780. */
  1781. public function testNumbersTemplates()
  1782. {
  1783. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1784. 'Client' => [
  1785. 'page' => 8,
  1786. 'current' => 3,
  1787. 'count' => 30,
  1788. 'prevPage' => false,
  1789. 'nextPage' => 2,
  1790. 'pageCount' => 15,
  1791. ]
  1792. ]);
  1793. $result = $this->Paginator->numbers(['templates' => 'htmlhelper_tags']);
  1794. $expected = [
  1795. ['a' => ['href' => '/index?page=4']], '4', '/a',
  1796. ['a' => ['href' => '/index?page=5']], '5', '/a',
  1797. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1798. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1799. 'span' => ['class' => 'active'], '8', '/span',
  1800. ['a' => ['href' => '/index?page=9']], '9', '/a',
  1801. ['a' => ['href' => '/index?page=10']], '10', '/a',
  1802. ['a' => ['href' => '/index?page=11']], '11', '/a',
  1803. ['a' => ['href' => '/index?page=12']], '12', '/a',
  1804. ];
  1805. $this->assertHtml($expected, $result);
  1806. $this->assertContains(
  1807. '<li',
  1808. $this->Paginator->templater()->get('current'),
  1809. 'Templates were not restored.'
  1810. );
  1811. }
  1812. /**
  1813. * Test modulus option for numbers()
  1814. *
  1815. * @return void
  1816. */
  1817. public function testNumbersModulus()
  1818. {
  1819. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1820. 'Client' => [
  1821. 'page' => 1,
  1822. 'current' => 10,
  1823. 'count' => 30,
  1824. 'prevPage' => false,
  1825. 'nextPage' => 2,
  1826. 'pageCount' => 3,
  1827. ]
  1828. ]);
  1829. $result = $this->Paginator->numbers(['modulus' => 10]);
  1830. $expected = [
  1831. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1832. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1833. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1834. ];
  1835. $this->assertHtml($expected, $result);
  1836. $result = $this->Paginator->numbers(['modulus' => 3]);
  1837. $expected = [
  1838. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1839. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1840. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1841. ];
  1842. $this->assertHtml($expected, $result);
  1843. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  1844. 'Client' => [
  1845. 'page' => 4895,
  1846. 'current' => 10,
  1847. 'count' => 48962,
  1848. 'prevPage' => 1,
  1849. 'nextPage' => 1,
  1850. 'pageCount' => 4897,
  1851. ]
  1852. ]);
  1853. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1854. $expected = [
  1855. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1856. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1857. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1858. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1859. ['li' => ['class' => 'active']], '<a href=""', '4,895', '/a', '/li',
  1860. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1861. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1862. ];
  1863. $this->assertHtml($expected, $result);
  1864. $this->Paginator->request = $this->Paginator->request->withParam('paging.Client.page', 3);
  1865. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1866. $expected = [
  1867. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1868. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1869. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1870. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1871. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1872. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1873. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1874. ];
  1875. $this->assertHtml($expected, $result);
  1876. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 5, 'last' => 5]);
  1877. $expected = [
  1878. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1879. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1880. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1881. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1882. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1883. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1884. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1885. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4,893', '/a', '/li',
  1886. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1887. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1888. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1889. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. $this->Paginator->request = $this->Paginator->request->withParam('paging.Client.page', 4893);
  1893. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1894. $expected = [
  1895. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1896. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1897. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1898. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1899. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1900. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1901. ['li' => []], ['a' => ['href' => '/index?page=4891']], '4,891', '/a', '/li',
  1902. ['li' => []], ['a' => ['href' => '/index?page=4892']], '4,892', '/a', '/li',
  1903. ['li' => ['class' => 'active']], '<a href=""', '4,893', '/a', '/li',
  1904. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1905. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/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', 58);
  1911. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1912. $expected = [
  1913. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1914. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1915. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1916. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1917. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1918. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1919. ['li' => []], ['a' => ['href' => '/index?page=56']], '56', '/a', '/li',
  1920. ['li' => []], ['a' => ['href' => '/index?page=57']], '57', '/a', '/li',
  1921. ['li' => ['class' => 'active']], '<a href=""', '58', '/a', '/li',
  1922. ['li' => []], ['a' => ['href' => '/index?page=59']], '59', '/a', '/li',
  1923. ['li' => []], ['a' => ['href' => '/index?page=60']], '60', '/a', '/li',
  1924. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1925. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4,893', '/a', '/li',
  1926. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1927. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1928. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1929. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1930. ];
  1931. $this->assertHtml($expected, $result);
  1932. $this->Paginator->request = $this->Paginator->request->withParam('paging.Client.page', 5);
  1933. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1934. $expected = [
  1935. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1936. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1937. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1938. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1939. ['li' => ['class' => 'active']], '<a href=""', '5', '/a', '/li',
  1940. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1941. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1942. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1943. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4,893', '/a', '/li',
  1944. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4,894', '/a', '/li',
  1945. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4,895', '/a', '/li',
  1946. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1947. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1948. ];
  1949. $this->assertHtml($expected, $result);
  1950. $this->Paginator->request = $this->Paginator->request->withParam('paging.Client.page', 3);
  1951. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1952. $expected = [
  1953. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1954. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1955. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1956. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1957. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1958. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1959. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1960. ];
  1961. $this->assertHtml($expected, $result);
  1962. $this->Paginator->request = $this->Paginator->request->withParam('paging.Client.page', 3);
  1963. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 0, 'last' => 2]);
  1964. $expected = [
  1965. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1966. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1967. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1968. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  1969. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4,896', '/a', '/li',
  1970. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4,897', '/a', '/li',
  1971. ];
  1972. $this->assertHtml($expected, $result);
  1973. }
  1974. /**
  1975. * Test modulus option for numbers()
  1976. *
  1977. * @return void
  1978. */
  1979. public function testNumbersModulusMulti()
  1980. {
  1981. $expected = [
  1982. 1 => '*1 2 3 4 ',
  1983. 2 => '1 *2 3 4 ',
  1984. 3 => '1 2 *3 4 ',
  1985. 4 => '2 3 *4 5 ',
  1986. 5 => '3 4 *5 6 ',
  1987. 6 => '4 5 *6 7 ',
  1988. 7 => '4 5 6 *7 ',
  1989. ];
  1990. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7, ['modulus' => 3]);
  1991. $this->assertEquals($expected, $result);
  1992. $expected = [
  1993. 1 => '*1 2 3 4 ... L> ',
  1994. 2 => '1 *2 3 4 ... L> ',
  1995. 3 => '1 2 *3 4 ... L> ',
  1996. 4 => '1 2 3 *4 5 6 7 ',
  1997. 5 => '1 2 3 4 *5 6 7 ',
  1998. 6 => '<F ... 4 5 *6 7 ',
  1999. 7 => '<F ... 4 5 6 *7 ',
  2000. ];
  2001. $result = $this->getNumbersForMultiplePages(array_keys($expected), 7, ['modulus' => 3, 'first' => 'F', 'last' => 'L']);
  2002. $this->assertEquals($expected, $result);
  2003. $expected = [
  2004. 1 => '*1 2 3 ... 19 20 ',
  2005. 2 => '1 *2 3 ... 19 20 ',
  2006. 3 => '1 2 *3 4 ... 19 20 ',
  2007. 4 => '1 2 3 *4 5 ... 19 20 ',
  2008. 5 => '1 2 3 4 *5 6 ... 19 20 ',
  2009. 6 => '1 2 ... 5 *6 7 ... 19 20 ',
  2010. 15 => '1 2 ... 14 *15 16 ... 19 20 ',
  2011. 16 => '1 2 ... 15 *16 17 18 19 20 ',
  2012. 17 => '1 2 ... 16 *17 18 19 20 ',
  2013. 18 => '1 2 ... 17 *18 19 20 ',
  2014. 19 => '1 2 ... 18 *19 20 ',
  2015. 20 => '1 2 ... 18 19 *20 ',
  2016. ];
  2017. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 2, 'modulus' => 2, 'last' => 2]);
  2018. $this->assertEquals($expected, $result);
  2019. $expected = [
  2020. 1 => '*1 2 3 4 5 ... 16 17 18 19 20 ',
  2021. 2 => '1 *2 3 4 5 ... 16 17 18 19 20 ',
  2022. 3 => '1 2 *3 4 5 ... 16 17 18 19 20 ',
  2023. 4 => '1 2 3 *4 5 6 ... 16 17 18 19 20 ',
  2024. 5 => '1 2 3 4 *5 6 7 ... 16 17 18 19 20 ',
  2025. 6 => '1 2 3 4 5 *6 7 8 ... 16 17 18 19 20 ',
  2026. 7 => '1 2 3 4 5 6 *7 8 9 ... 16 17 18 19 20 ',
  2027. 8 => '1 2 3 4 5 6 7 *8 9 10 ... 16 17 18 19 20 ',
  2028. 9 => '1 2 3 4 5 6 7 8 *9 10 11 ... 16 17 18 19 20 ',
  2029. 10 => '1 2 3 4 5 ... 8 9 *10 11 12 ... 16 17 18 19 20 ',
  2030. 11 => '1 2 3 4 5 ... 9 10 *11 12 13 ... 16 17 18 19 20 ',
  2031. 12 => '1 2 3 4 5 ... 10 11 *12 13 14 15 16 17 18 19 20 ',
  2032. 13 => '1 2 3 4 5 ... 11 12 *13 14 15 16 17 18 19 20 ',
  2033. 14 => '1 2 3 4 5 ... 12 13 *14 15 16 17 18 19 20 ',
  2034. 15 => '1 2 3 4 5 ... 13 14 *15 16 17 18 19 20 ',
  2035. 16 => '1 2 3 4 5 ... 14 15 *16 17 18 19 20 ',
  2036. 17 => '1 2 3 4 5 ... 15 16 *17 18 19 20 ',
  2037. 18 => '1 2 3 4 5 ... 16 17 *18 19 20 ',
  2038. 19 => '1 2 3 4 5 ... 16 17 18 *19 20 ',
  2039. 20 => '1 2 3 4 5 ... 16 17 18 19 *20 ',
  2040. ];
  2041. $result = $this->getNumbersForMultiplePages(array_keys($expected), 20, ['first' => 5, 'modulus' => 4, 'last' => 5]);
  2042. $this->assertEquals($expected, $result);
  2043. }
  2044. /**
  2045. * Tests that disabling modulus displays all page links.
  2046. *
  2047. * @return void
  2048. */
  2049. public function testModulusDisabled()
  2050. {
  2051. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2052. 'Client' => [
  2053. 'page' => 4,
  2054. 'current' => 2,
  2055. 'count' => 30,
  2056. 'prevPage' => 1,
  2057. 'nextPage' => 1,
  2058. 'pageCount' => 6,
  2059. ]
  2060. ]);
  2061. $result = $this->Paginator->numbers(['modulus' => false]);
  2062. $expected = [
  2063. ['li' => []], '<a href="/index"', '1', '/a', '/li',
  2064. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  2065. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  2066. ['li' => ['class' => 'active']], ['a' => ['href' => '']], '4', '/a', '/li',
  2067. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  2068. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  2069. ];
  2070. $this->assertHtml($expected, $result);
  2071. }
  2072. /**
  2073. * Test that numbers() with url options.
  2074. *
  2075. * @return void
  2076. */
  2077. public function testNumbersWithUrlOptions()
  2078. {
  2079. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2080. 'Client' => [
  2081. 'page' => 8,
  2082. 'current' => 3,
  2083. 'count' => 30,
  2084. 'prevPage' => false,
  2085. 'nextPage' => 2,
  2086. 'pageCount' => 15,
  2087. ]
  2088. ]);
  2089. $result = $this->Paginator->numbers(['url' => ['#' => 'foo']]);
  2090. $expected = [
  2091. ['li' => []], ['a' => ['href' => '/index?page=4#foo']], '4', '/a', '/li',
  2092. ['li' => []], ['a' => ['href' => '/index?page=5#foo']], '5', '/a', '/li',
  2093. ['li' => []], ['a' => ['href' => '/index?page=6#foo']], '6', '/a', '/li',
  2094. ['li' => []], ['a' => ['href' => '/index?page=7#foo']], '7', '/a', '/li',
  2095. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  2096. ['li' => []], ['a' => ['href' => '/index?page=9#foo']], '9', '/a', '/li',
  2097. ['li' => []], ['a' => ['href' => '/index?page=10#foo']], '10', '/a', '/li',
  2098. ['li' => []], ['a' => ['href' => '/index?page=11#foo']], '11', '/a', '/li',
  2099. ['li' => []], ['a' => ['href' => '/index?page=12#foo']], '12', '/a', '/li',
  2100. ];
  2101. $this->assertHtml($expected, $result);
  2102. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2103. 'Client' => [
  2104. 'page' => 3,
  2105. 'current' => 10,
  2106. 'count' => 48962,
  2107. 'prevPage' => 1,
  2108. 'nextPage' => 1,
  2109. 'pageCount' => 4897,
  2110. ]
  2111. ]);
  2112. $result = $this->Paginator->numbers([
  2113. 'first' => 2,
  2114. 'modulus' => 2,
  2115. 'last' => 2,
  2116. 'url' => ['foo' => 'bar']]);
  2117. $expected = [
  2118. ['li' => []], ['a' => ['href' => '/index?foo=bar']], '1', '/a', '/li',
  2119. ['li' => []], ['a' => ['href' => '/index?page=2&amp;foo=bar']], '2', '/a', '/li',
  2120. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  2121. ['li' => []], ['a' => ['href' => '/index?page=4&amp;foo=bar']], '4', '/a', '/li',
  2122. ['li' => ['class' => 'ellipsis']], '&hellip;', '/li',
  2123. ['li' => []], ['a' => ['href' => '/index?page=4896&amp;foo=bar']], '4,896', '/a', '/li',
  2124. ['li' => []], ['a' => ['href' => '/index?page=4897&amp;foo=bar']], '4,897', '/a', '/li',
  2125. ];
  2126. $this->assertHtml($expected, $result);
  2127. }
  2128. /**
  2129. * test numbers() with routing parameters.
  2130. *
  2131. * @return void
  2132. */
  2133. public function testNumbersRouting()
  2134. {
  2135. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2136. 'Client' => [
  2137. 'page' => 2,
  2138. 'current' => 2,
  2139. 'count' => 30,
  2140. 'prevPage' => false,
  2141. 'nextPage' => 3,
  2142. 'pageCount' => 3,
  2143. ]
  2144. ]);
  2145. $request = new ServerRequest([
  2146. 'params' => ['controller' => 'clients', 'action' => 'index', 'plugin' => null],
  2147. 'url' => '/clients/index?page=2'
  2148. ]);
  2149. Router::setRequestInfo($request);
  2150. $result = $this->Paginator->numbers();
  2151. $expected = [
  2152. ['li' => []], ['a' => ['href' => '/clients/index']], '1', '/a', '/li',
  2153. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  2154. ['li' => []], ['a' => ['href' => '/clients/index?page=3']], '3', '/a', '/li',
  2155. ];
  2156. $this->assertHtml($expected, $result);
  2157. }
  2158. /**
  2159. * Test that numbers() works with the non default model.
  2160. *
  2161. * @return void
  2162. */
  2163. public function testNumbersNonDefaultModel()
  2164. {
  2165. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2166. 'Client' => [
  2167. 'page' => 1,
  2168. 'current' => 3,
  2169. 'count' => 13,
  2170. 'prevPage' => false,
  2171. 'nextPage' => true,
  2172. 'pageCount' => 5,
  2173. ],
  2174. 'Server' => [
  2175. 'page' => 5,
  2176. 'current' => 1,
  2177. 'count' => 5,
  2178. 'prevPage' => true,
  2179. 'nextPage' => false,
  2180. 'pageCount' => 5,
  2181. ]
  2182. ]);
  2183. $result = $this->Paginator->numbers(['model' => 'Server']);
  2184. $this->assertContains('<li class="active"><a href="">5</a></li>', $result);
  2185. $this->assertNotContains('<li class="active"><a href="">1</a></li>', $result);
  2186. $result = $this->Paginator->numbers(['model' => 'Client']);
  2187. $this->assertContains('<li class="active"><a href="">1</a></li>', $result);
  2188. $this->assertNotContains('<li class="active"><a href="">5</a></li>', $result);
  2189. }
  2190. /**
  2191. * test first() and last() with tag options
  2192. *
  2193. * @return void
  2194. */
  2195. public function testFirstAndLastTag()
  2196. {
  2197. $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 2);
  2198. $result = $this->Paginator->first('<<');
  2199. $expected = [
  2200. 'li' => ['class' => 'first'],
  2201. 'a' => ['href' => '/index'],
  2202. '&lt;&lt;',
  2203. '/a',
  2204. '/li'
  2205. ];
  2206. $this->assertHtml($expected, $result);
  2207. $result = $this->Paginator->first('5');
  2208. $expected = [
  2209. 'li' => ['class' => 'first'],
  2210. 'a' => ['href' => '/index'],
  2211. '5',
  2212. '/a',
  2213. '/li'
  2214. ];
  2215. $this->assertHtml($expected, $result);
  2216. $result = $this->Paginator->last(2);
  2217. $expected = [
  2218. '<li',
  2219. ['a' => ['href' => '/index?page=6']], '6', '/a',
  2220. '/li',
  2221. '<li',
  2222. ['a' => ['href' => '/index?page=7']], '7', '/a',
  2223. '/li',
  2224. ];
  2225. $this->assertHtml($expected, $result);
  2226. $result = $this->Paginator->last('9');
  2227. $expected = [
  2228. 'li' => ['class' => 'last'],
  2229. 'a' => ['href' => '/index?page=7'],
  2230. '9',
  2231. '/a',
  2232. '/li'
  2233. ];
  2234. $this->assertHtml($expected, $result);
  2235. }
  2236. /**
  2237. * test that on the last page you don't get a link ot the last page.
  2238. *
  2239. * @return void
  2240. */
  2241. public function testLastNoOutput()
  2242. {
  2243. $this->Paginator->request = $this->Paginator->request
  2244. ->withParam('paging.Article.page', 15)
  2245. ->withParam('paging.Article.pageCount', 15);
  2246. $result = $this->Paginator->last();
  2247. $expected = '';
  2248. $this->assertEquals($expected, $result);
  2249. }
  2250. /**
  2251. * test first() with a the model parameter.
  2252. *
  2253. * @return void
  2254. */
  2255. public function testFirstNonDefaultModel()
  2256. {
  2257. $this->Paginator->request = $this->Paginator->request
  2258. ->withParam('paging.Article.page', 1)
  2259. ->withParam('paging.Client', [
  2260. 'page' => 3,
  2261. 'current' => 3,
  2262. 'count' => 13,
  2263. 'prevPage' => false,
  2264. 'nextPage' => true,
  2265. 'pageCount' => 5,
  2266. ]);
  2267. $result = $this->Paginator->first('first', ['model' => 'Article']);
  2268. $this->assertEquals('', $result);
  2269. $result = $this->Paginator->first('first', ['model' => 'Client']);
  2270. $expected = [
  2271. 'li' => ['class' => 'first'],
  2272. 'a' => ['href' => '/index'],
  2273. 'first',
  2274. '/a',
  2275. '/li'
  2276. ];
  2277. $this->assertHtml($expected, $result);
  2278. }
  2279. /**
  2280. * test first() on the first page.
  2281. *
  2282. * @return void
  2283. */
  2284. public function testFirstEmpty()
  2285. {
  2286. $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 1);
  2287. $result = $this->Paginator->first();
  2288. $expected = '';
  2289. $this->assertEquals($expected, $result);
  2290. }
  2291. /**
  2292. * test first() and options()
  2293. *
  2294. * @return void
  2295. */
  2296. public function testFirstFullBaseUrl()
  2297. {
  2298. $this->Paginator->request = $this->Paginator->request
  2299. ->withParam('paging.Article.page', 3)
  2300. ->withParam('paging.Article.direction', 'DESC')
  2301. ->withParam('paging.Article.sort', 'Article.title');
  2302. $this->Paginator->options(['url' => ['_full' => true]]);
  2303. $result = $this->Paginator->first();
  2304. $expected = [
  2305. 'li' => ['class' => 'first'],
  2306. ['a' => [
  2307. 'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC'
  2308. ]],
  2309. '&lt;&lt; first',
  2310. '/a',
  2311. '/li',
  2312. ];
  2313. $this->assertHtml($expected, $result);
  2314. }
  2315. /**
  2316. * test first() on the fence-post
  2317. *
  2318. * @return void
  2319. */
  2320. public function testFirstBoundaries()
  2321. {
  2322. $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 3);
  2323. $result = $this->Paginator->first();
  2324. $expected = [
  2325. 'li' => ['class' => 'first'],
  2326. 'a' => ['href' => '/index'],
  2327. '&lt;&lt; first',
  2328. '/a',
  2329. '/li'
  2330. ];
  2331. $this->assertHtml($expected, $result);
  2332. $result = $this->Paginator->first(2);
  2333. $expected = [
  2334. '<li',
  2335. ['a' => ['href' => '/index']], '1', '/a',
  2336. '/li',
  2337. '<li',
  2338. ['a' => ['href' => '/index?page=2']], '2', '/a',
  2339. '/li'
  2340. ];
  2341. $this->assertHtml($expected, $result);
  2342. $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 2);
  2343. $result = $this->Paginator->first(3);
  2344. $this->assertEquals('', $result, 'When inside the first links range, no links should be made');
  2345. }
  2346. /**
  2347. * test params() method
  2348. *
  2349. * @return void
  2350. */
  2351. public function testParams()
  2352. {
  2353. $result = $this->Paginator->params();
  2354. $this->assertArrayHasKey('page', $result);
  2355. $this->assertArrayHasKey('pageCount', $result);
  2356. $result = $this->Paginator->params('Nope');
  2357. $this->assertEquals([], $result);
  2358. }
  2359. /**
  2360. * test param() method
  2361. *
  2362. * @return void
  2363. */
  2364. public function testParam()
  2365. {
  2366. $result = $this->Paginator->param('count');
  2367. $this->assertSame(62, $result);
  2368. $result = $this->Paginator->param('imaginary');
  2369. $this->assertNull($result);
  2370. }
  2371. /**
  2372. * test last() method
  2373. *
  2374. * @return void
  2375. */
  2376. public function testLast()
  2377. {
  2378. $result = $this->Paginator->last();
  2379. $expected = [
  2380. 'li' => ['class' => 'last'],
  2381. 'a' => ['href' => '/index?page=7'],
  2382. 'last &gt;&gt;',
  2383. '/a',
  2384. '/li'
  2385. ];
  2386. $this->assertHtml($expected, $result);
  2387. $result = $this->Paginator->last(1);
  2388. $expected = [
  2389. '<li',
  2390. 'a' => ['href' => '/index?page=7'],
  2391. '7',
  2392. '/a',
  2393. '/li'
  2394. ];
  2395. $this->assertHtml($expected, $result);
  2396. $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 6);
  2397. $result = $this->Paginator->last(2);
  2398. $expected = [
  2399. '<li',
  2400. ['a' => ['href' => '/index?page=6']], '6', '/a',
  2401. '/li',
  2402. '<li',
  2403. ['a' => ['href' => '/index?page=7']], '7', '/a',
  2404. '/li',
  2405. ];
  2406. $this->assertHtml($expected, $result);
  2407. $result = $this->Paginator->last(3);
  2408. $this->assertEquals('', $result, 'When inside the last links range, no links should be made');
  2409. }
  2410. /**
  2411. * test the options for last()
  2412. *
  2413. * @return void
  2414. */
  2415. public function testLastOptions()
  2416. {
  2417. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2418. 'Client' => [
  2419. 'page' => 4,
  2420. 'current' => 3,
  2421. 'count' => 30,
  2422. 'prevPage' => false,
  2423. 'nextPage' => 2,
  2424. 'pageCount' => 15,
  2425. 'sort' => 'Client.name',
  2426. 'direction' => 'DESC',
  2427. ]
  2428. ]);
  2429. $result = $this->Paginator->last();
  2430. $expected = [
  2431. 'li' => ['class' => 'last'],
  2432. 'a' => [
  2433. 'href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC',
  2434. ],
  2435. 'last &gt;&gt;', '/a',
  2436. '/li',
  2437. ];
  2438. $this->assertHtml($expected, $result);
  2439. $result = $this->Paginator->last(1);
  2440. $expected = [
  2441. '<li',
  2442. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  2443. '/li',
  2444. ];
  2445. $this->assertHtml($expected, $result);
  2446. $result = $this->Paginator->last(2);
  2447. $expected = [
  2448. '<li',
  2449. ['a' => ['href' => '/index?page=14&amp;sort=Client.name&amp;direction=DESC']], '14', '/a',
  2450. '/li',
  2451. '<li',
  2452. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  2453. '/li',
  2454. ];
  2455. $this->assertHtml($expected, $result);
  2456. }
  2457. /**
  2458. * test last() with a the model parameter.
  2459. *
  2460. * @return void
  2461. */
  2462. public function testLastNonDefaultModel()
  2463. {
  2464. $this->Paginator->request = $this->Paginator->request
  2465. ->withParam('paging.Article.page', 7)
  2466. ->withParam('paging.Client', [
  2467. 'page' => 3,
  2468. 'current' => 3,
  2469. 'count' => 13,
  2470. 'prevPage' => false,
  2471. 'nextPage' => true,
  2472. 'pageCount' => 5,
  2473. ]);
  2474. $result = $this->Paginator->last('last', ['model' => 'Article']);
  2475. $this->assertEquals('', $result);
  2476. $result = $this->Paginator->last('last', ['model' => 'Client']);
  2477. $expected = [
  2478. 'li' => ['class' => 'last'],
  2479. 'a' => ['href' => '/index?page=5'],
  2480. 'last',
  2481. '/a',
  2482. '/li'
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. }
  2486. /**
  2487. * testCounter method
  2488. *
  2489. * @return void
  2490. */
  2491. public function testCounter()
  2492. {
  2493. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2494. 'Client' => [
  2495. 'page' => 1,
  2496. 'current' => 3,
  2497. 'count' => 13,
  2498. 'perPage' => 3,
  2499. 'prevPage' => false,
  2500. 'nextPage' => true,
  2501. 'pageCount' => 5,
  2502. 'limit' => 3,
  2503. 'sort' => 'Client.name',
  2504. 'order' => 'DESC',
  2505. ]
  2506. ]);
  2507. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  2508. $input .= 'starting on record {{start}}, ending on {{end}}';
  2509. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
  2510. $expected .= 'ending on 3';
  2511. $result = $this->Paginator->counter($input);
  2512. $this->assertEquals($expected, $result);
  2513. $result = $this->Paginator->counter(['format' => 'pages']);
  2514. $expected = '1 of 5';
  2515. $this->assertEquals($expected, $result);
  2516. $result = $this->Paginator->counter(['format' => 'range']);
  2517. $expected = '1 - 3 of 13';
  2518. $this->assertEquals($expected, $result);
  2519. $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}');
  2520. $this->assertEquals('Showing 1 of 5 clients', $result);
  2521. }
  2522. /**
  2523. * Tests that numbers are formatted according to the locale when using counter()
  2524. *
  2525. * @return void
  2526. */
  2527. public function testCounterBigNumbers()
  2528. {
  2529. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2530. 'Client' => [
  2531. 'page' => 1523,
  2532. 'current' => 1230,
  2533. 'count' => 234567,
  2534. 'perPage' => 3000,
  2535. 'prevPage' => false,
  2536. 'nextPage' => true,
  2537. 'pageCount' => 1000,
  2538. 'limit' => 5000,
  2539. 'sort' => 'Client.name',
  2540. 'order' => 'DESC',
  2541. ]
  2542. ]);
  2543. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  2544. $input .= 'starting on record {{start}}, ending on {{end}}';
  2545. $expected = 'Page 1,523 of 1,000, showing 1,230 records out of 234,567 total, ';
  2546. $expected .= 'starting on record 4,566,001, ending on 234,567';
  2547. $result = $this->Paginator->counter($input);
  2548. $this->assertEquals($expected, $result);
  2549. I18n::setLocale('de-DE');
  2550. $expected = 'Page 1.523 of 1.000, showing 1.230 records out of 234.567 total, ';
  2551. $expected .= 'starting on record 4.566.001, ending on 234.567';
  2552. $result = $this->Paginator->counter($input);
  2553. $this->assertEquals($expected, $result);
  2554. }
  2555. /**
  2556. * testHasPage method
  2557. *
  2558. * @return void
  2559. */
  2560. public function testHasPage()
  2561. {
  2562. $result = $this->Paginator->hasPage('Article', 15);
  2563. $this->assertFalse($result);
  2564. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  2565. $this->assertFalse($result);
  2566. $result = $this->Paginator->hasPage('Article', 2);
  2567. $this->assertTrue($result);
  2568. $result = $this->Paginator->hasPage(2);
  2569. $this->assertTrue($result);
  2570. }
  2571. /**
  2572. * testNextLinkUsingDotNotation method
  2573. *
  2574. * @return void
  2575. */
  2576. public function testNextLinkUsingDotNotation()
  2577. {
  2578. $request = new ServerRequest([
  2579. 'url' => '/accounts/',
  2580. 'params' => [
  2581. 'plugin' => null, 'controller' => 'accounts', 'action' => 'index'
  2582. ]
  2583. ]);
  2584. Router::setRequestInfo($request);
  2585. $this->Paginator->request = $this->Paginator->request
  2586. ->withParam('paging.Article.sort', 'Article.title')
  2587. ->withParam('paging.Article.direction', 'asc')
  2588. ->withParam('paging.Article.page', 1);
  2589. $test = ['url' => [
  2590. 'page' => '1',
  2591. 'sort' => 'Article.title',
  2592. 'direction' => 'asc',
  2593. ]];
  2594. $this->Paginator->options($test);
  2595. $result = $this->Paginator->next('Next');
  2596. $expected = [
  2597. 'li' => ['class' => 'next'],
  2598. 'a' => [
  2599. 'href' => '/accounts/index?page=2&amp;sort=Article.title&amp;direction=asc',
  2600. 'rel' => 'next'
  2601. ],
  2602. 'Next',
  2603. '/a',
  2604. '/li',
  2605. ];
  2606. $this->assertHtml($expected, $result);
  2607. }
  2608. /**
  2609. * test the current() method
  2610. *
  2611. * @return void
  2612. */
  2613. public function testCurrent()
  2614. {
  2615. $result = $this->Paginator->current();
  2616. $this->assertEquals($this->Paginator->request->getParam('paging.Article.page'), $result);
  2617. $result = $this->Paginator->current('Incorrect');
  2618. $this->assertEquals(1, $result);
  2619. }
  2620. /**
  2621. * test the total() method
  2622. *
  2623. * @return void
  2624. */
  2625. public function testTotal()
  2626. {
  2627. $result = $this->Paginator->total();
  2628. $this->assertSame($this->Paginator->request->getParam('paging.Article.pageCount'), $result);
  2629. $result = $this->Paginator->total('Incorrect');
  2630. $this->assertSame(0, $result);
  2631. }
  2632. /**
  2633. * test the defaultModel() method
  2634. *
  2635. * @return void
  2636. */
  2637. public function testNoDefaultModel()
  2638. {
  2639. $this->Paginator->request = new ServerRequest();
  2640. $this->assertNull($this->Paginator->defaultModel());
  2641. $this->Paginator->defaultModel('Article');
  2642. $this->assertEquals('Article', $this->Paginator->defaultModel());
  2643. $this->Paginator->options(['model' => 'Client']);
  2644. $this->assertEquals('Client', $this->Paginator->defaultModel());
  2645. }
  2646. /**
  2647. * test the numbers() method when there is only one page
  2648. *
  2649. * @return void
  2650. */
  2651. public function testWithOnePage()
  2652. {
  2653. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2654. 'Article' => [
  2655. 'page' => 1,
  2656. 'current' => 2,
  2657. 'count' => 2,
  2658. 'prevPage' => false,
  2659. 'nextPage' => true,
  2660. 'pageCount' => 1,
  2661. ]
  2662. ]);
  2663. $this->assertFalse($this->Paginator->numbers());
  2664. $this->assertFalse($this->Paginator->first());
  2665. $this->assertFalse($this->Paginator->last());
  2666. }
  2667. /**
  2668. * test the numbers() method when there is only one page
  2669. *
  2670. * @return void
  2671. */
  2672. public function testWithZeroPages()
  2673. {
  2674. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2675. 'Article' => [
  2676. 'page' => 0,
  2677. 'current' => 0,
  2678. 'count' => 0,
  2679. 'perPage' => 10,
  2680. 'prevPage' => false,
  2681. 'nextPage' => false,
  2682. 'pageCount' => 0,
  2683. 'limit' => 10,
  2684. ]
  2685. ]);
  2686. $result = $this->Paginator->counter(['format' => 'pages']);
  2687. $expected = '0 of 1';
  2688. $this->assertEquals($expected, $result);
  2689. }
  2690. /**
  2691. * Test data for meta()
  2692. *
  2693. * @return array
  2694. */
  2695. public function dataMetaProvider()
  2696. {
  2697. return [
  2698. // Verifies that no next and prev links are created for single page results.
  2699. [1, false, false, 1, [], ''],
  2700. // Verifies that first and last pages are created for single page results.
  2701. [1, false, false, 1, ['first' => true, 'last' => true], '<link href="http://localhost/index" rel="first"/>' .
  2702. '<link href="http://localhost/index" rel="last"/>'],
  2703. // Verifies that first page is created for single page results.
  2704. [1, false, false, 1, ['first' => true], '<link href="http://localhost/index" rel="first"/>'],
  2705. // Verifies that last page is created for single page results.
  2706. [1, false, false, 1, ['last' => true], '<link href="http://localhost/index" rel="last"/>'],
  2707. // Verifies that page 1 only has a next link.
  2708. [1, false, true, 2, [], '<link href="http://localhost/index?page=2" rel="next"/>'],
  2709. // Verifies that page 1 only has next, first and last link.
  2710. [1, false, true, 2, ['first' => true, 'last' => true], '<link href="http://localhost/index?page=2" rel="next"/>' .
  2711. '<link href="http://localhost/index" rel="first"/>' .
  2712. '<link href="http://localhost/index?page=2" rel="last"/>'],
  2713. // Verifies that page 1 only has next and first link.
  2714. [1, false, true, 2, ['first' => true], '<link href="http://localhost/index?page=2" rel="next"/>' .
  2715. '<link href="http://localhost/index" rel="first"/>'],
  2716. // Verifies that page 1 only has next and last link.
  2717. [1, false, true, 2, ['last' => true], '<link href="http://localhost/index?page=2" rel="next"/>' .
  2718. '<link href="http://localhost/index?page=2" rel="last"/>'],
  2719. // Verifies that the last page only has a prev link.
  2720. [2, true, false, 2, [], '<link href="http://localhost/index" rel="prev"/>'],
  2721. // Verifies that the last page only has a prev, first and last link.
  2722. [2, true, false, 2, ['first' => true, 'last' => true], '<link href="http://localhost/index" rel="prev"/>' .
  2723. '<link href="http://localhost/index" rel="first"/>' .
  2724. '<link href="http://localhost/index?page=2" rel="last"/>'],
  2725. // Verifies that a page in the middle has both links.
  2726. [5, true, true, 10, [], '<link href="http://localhost/index?page=4" rel="prev"/>' .
  2727. '<link href="http://localhost/index?page=6" rel="next"/>'],
  2728. // Verifies that a page in the middle has both links.
  2729. [5, true, true, 10, ['first' => true, 'last' => true], '<link href="http://localhost/index?page=4" rel="prev"/>' .
  2730. '<link href="http://localhost/index?page=6" rel="next"/>' .
  2731. '<link href="http://localhost/index" rel="first"/>' .
  2732. '<link href="http://localhost/index?page=10" rel="last"/>']
  2733. ];
  2734. }
  2735. /**
  2736. * @param int $page
  2737. * @param int $prevPage
  2738. * @param int $nextPage
  2739. * @param int $pageCount
  2740. * @param array $options
  2741. * @param string $expected
  2742. * @dataProvider dataMetaProvider
  2743. */
  2744. public function testMeta($page, $prevPage, $nextPage, $pageCount, $options, $expected)
  2745. {
  2746. $this->Paginator->request = $this->Paginator->request->withParam('paging', [
  2747. 'Article' => [
  2748. 'page' => $page,
  2749. 'prevPage' => $prevPage,
  2750. 'nextPage' => $nextPage,
  2751. 'pageCount' => $pageCount,
  2752. ]
  2753. ]);
  2754. $result = $this->Paginator->meta($options);
  2755. $this->assertSame($expected, $result);
  2756. $this->assertEquals('', $this->View->fetch('meta'));
  2757. $result = $this->Paginator->meta($options + ['block' => true]);
  2758. $this->assertNull($result);
  2759. $this->assertSame($expected, $this->View->fetch('meta'));
  2760. }
  2761. /**
  2762. * test the limitControl() method
  2763. *
  2764. * @return void
  2765. */
  2766. public function testLimitControl()
  2767. {
  2768. $out = $this->Paginator->limitControl([1 => 1]);
  2769. $expected = [
  2770. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2771. ['div' => ['class' => 'input select']],
  2772. ['label' => ['for' => 'limit']],
  2773. 'View',
  2774. '/label',
  2775. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
  2776. ['option' => ['value' => '1']],
  2777. '1',
  2778. '/option',
  2779. '/select',
  2780. '/div',
  2781. '/form'
  2782. ];
  2783. $this->assertHtml($expected, $out);
  2784. $out = $this->Paginator->limitControl([1 => 1, 5 => 5], null, ['class' => 'form-control']);
  2785. $expected = [
  2786. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2787. ['div' => ['class' => 'input select']],
  2788. ['label' => ['for' => 'limit']],
  2789. 'View',
  2790. '/label',
  2791. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()', 'class' => 'form-control']],
  2792. ['option' => ['value' => '1']],
  2793. '1',
  2794. '/option',
  2795. ['option' => ['value' => '5']],
  2796. '5',
  2797. '/option',
  2798. '/select',
  2799. '/div',
  2800. '/form'
  2801. ];
  2802. $this->assertHtml($expected, $out);
  2803. $out = $this->Paginator->limitControl([], null, ['class' => 'form-control']);
  2804. $expected = [
  2805. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2806. ['div' => ['class' => 'input select']],
  2807. ['label' => ['for' => 'limit']],
  2808. 'View',
  2809. '/label',
  2810. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()', 'class' => 'form-control']],
  2811. ['option' => ['value' => '20']],
  2812. '20',
  2813. '/option',
  2814. ['option' => ['value' => '50']],
  2815. '50',
  2816. '/option',
  2817. ['option' => ['value' => '100']],
  2818. '100',
  2819. '/option',
  2820. '/select',
  2821. '/div',
  2822. '/form'
  2823. ];
  2824. $this->assertHtml($expected, $out);
  2825. $out = $this->Paginator->limitControl();
  2826. $expected = [
  2827. ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
  2828. ['div' => ['class' => 'input select']],
  2829. ['label' => ['for' => 'limit']],
  2830. 'View',
  2831. '/label',
  2832. ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
  2833. ['option' => ['value' => '20']],
  2834. '20',
  2835. '/option',
  2836. ['option' => ['value' => '50']],
  2837. '50',
  2838. '/option',
  2839. ['option' => ['value' => '100']],
  2840. '100',
  2841. '/option',
  2842. '/select',
  2843. '/div',
  2844. '/form'
  2845. ];
  2846. $this->assertHtml($expected, $out);
  2847. }
  2848. }