PaginatorHelperTest.php 117 KB

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