PaginatorHelperTest.php 110 KB

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