PaginatorHelperTest.php 116 KB

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