PaginatorHelperTest.php 111 KB

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