PaginatorHelperTest.php 123 KB

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