PaginatorHelperTest.php 116 KB

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