PaginatorHelperTest.php 117 KB

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