PaginatorHelperTest.php 120 KB

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