PaginatorHelperTest.php 107 KB

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