PaginatorHelperTest.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\Configure;
  17. use Cake\I18n\I18n;
  18. use Cake\Network\Request;
  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. */
  27. class PaginatorHelperTest extends TestCase
  28. {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Configure::write('Config.language', 'eng');
  38. $this->View = new View();
  39. $this->Paginator = new PaginatorHelper($this->View);
  40. $this->Paginator->Js = $this->getMock('Cake\View\Helper\PaginatorHelper', [], [$this->View]);
  41. $this->Paginator->request = new Request();
  42. $this->Paginator->request->addParams([
  43. 'paging' => [
  44. 'Article' => [
  45. 'page' => 1,
  46. 'current' => 9,
  47. 'count' => 62,
  48. 'prevPage' => false,
  49. 'nextPage' => true,
  50. 'pageCount' => 7,
  51. 'sort' => null,
  52. 'direction' => null,
  53. 'limit' => null,
  54. ]
  55. ]
  56. ]);
  57. Configure::write('Routing.prefixes', []);
  58. Router::reload();
  59. Router::connect('/:controller/:action/*');
  60. Router::connect('/:plugin/:controller/:action/*');
  61. $this->locale = I18n::locale();
  62. }
  63. /**
  64. * tearDown method
  65. *
  66. * @return void
  67. */
  68. public function tearDown()
  69. {
  70. parent::tearDown();
  71. unset($this->View, $this->Paginator);
  72. I18n::locale($this->locale);
  73. }
  74. /**
  75. * Test the templates method.
  76. *
  77. * @return void
  78. */
  79. public function testTemplates()
  80. {
  81. $result = $this->Paginator->templates([
  82. 'test' => 'val'
  83. ]);
  84. $this->assertSame(
  85. $this->Paginator,
  86. $result,
  87. 'Setting should return the same object'
  88. );
  89. $result = $this->Paginator->templates();
  90. $this->assertArrayHasKey('test', $result);
  91. $this->assertEquals('val', $result['test']);
  92. $this->assertEquals('val', $this->Paginator->templates('test'));
  93. }
  94. /**
  95. * testHasPrevious method
  96. *
  97. * @return void
  98. */
  99. public function testHasPrevious()
  100. {
  101. $this->assertFalse($this->Paginator->hasPrev());
  102. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  103. $this->assertTrue($this->Paginator->hasPrev());
  104. $this->Paginator->request->params['paging']['Article']['prevPage'] = false;
  105. }
  106. /**
  107. * testHasNext method
  108. *
  109. * @return void
  110. */
  111. public function testHasNext()
  112. {
  113. $this->assertTrue($this->Paginator->hasNext());
  114. $this->Paginator->request->params['paging']['Article']['nextPage'] = false;
  115. $this->assertFalse($this->Paginator->hasNext());
  116. $this->Paginator->request->params['paging']['Article']['nextPage'] = true;
  117. }
  118. /**
  119. * testSortLinks method
  120. *
  121. * @return void
  122. */
  123. public function testSortLinks()
  124. {
  125. Router::setRequestInfo([
  126. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [], 'url' => ['url' => 'accounts/']],
  127. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  128. ]);
  129. $this->Paginator->options(['url' => ['param']]);
  130. $this->Paginator->request['paging'] = [
  131. 'Article' => [
  132. 'current' => 9,
  133. 'count' => 62,
  134. 'prevPage' => false,
  135. 'nextPage' => true,
  136. 'pageCount' => 7,
  137. 'sort' => 'date',
  138. 'direction' => 'asc',
  139. 'page' => 1,
  140. ]
  141. ];
  142. $result = $this->Paginator->sort('title');
  143. $expected = [
  144. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=asc'],
  145. 'Title',
  146. '/a'
  147. ];
  148. $this->assertHtml($expected, $result);
  149. $result = $this->Paginator->sort('title', null, ['model' => 'Nope']);
  150. $this->assertHtml($expected, $result);
  151. $result = $this->Paginator->sort('title', null, ['model' => 'Article']);
  152. $this->assertHtml($expected, $result);
  153. $result = $this->Paginator->sort('date');
  154. $expected = [
  155. 'a' => ['href' => '/accounts/index/param?sort=date&amp;direction=desc', 'class' => 'asc'],
  156. 'Date',
  157. '/a'
  158. ];
  159. $this->assertHtml($expected, $result);
  160. $result = $this->Paginator->sort('title', 'TestTitle');
  161. $expected = [
  162. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=asc'],
  163. 'TestTitle',
  164. '/a'
  165. ];
  166. $this->assertHtml($expected, $result);
  167. $result = $this->Paginator->sort('title', ['asc' => 'ascending', 'desc' => 'descending']);
  168. $expected = [
  169. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=asc'],
  170. 'ascending',
  171. '/a'
  172. ];
  173. $this->assertHtml($expected, $result);
  174. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  175. $result = $this->Paginator->sort('title', ['asc' => 'ascending', 'desc' => 'descending']);
  176. $expected = [
  177. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'],
  178. 'descending',
  179. '/a'
  180. ];
  181. $this->assertHtml($expected, $result);
  182. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  183. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  184. $result = $this->Paginator->sort('title');
  185. $expected = [
  186. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=asc', 'class' => 'desc'],
  187. 'Title',
  188. '/a'
  189. ];
  190. $this->assertHtml($expected, $result);
  191. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  192. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  193. $result = $this->Paginator->sort('title');
  194. $expected = [
  195. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'],
  196. 'Title',
  197. '/a'
  198. ];
  199. $this->assertHtml($expected, $result);
  200. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  201. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  202. $result = $this->Paginator->sort('title', 'Title', ['direction' => 'desc']);
  203. $expected = [
  204. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=asc', 'class' => 'desc'],
  205. 'Title',
  206. '/a'
  207. ];
  208. $this->assertHtml($expected, $result);
  209. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  210. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  211. $result = $this->Paginator->sort('title', 'Title', ['direction' => 'ASC']);
  212. $expected = [
  213. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=asc', 'class' => 'desc'],
  214. 'Title',
  215. '/a'
  216. ];
  217. $this->assertHtml($expected, $result);
  218. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  219. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  220. $result = $this->Paginator->sort('title', 'Title', ['direction' => 'asc']);
  221. $expected = [
  222. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'],
  223. 'Title',
  224. '/a'
  225. ];
  226. $this->assertHtml($expected, $result);
  227. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  228. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  229. $result = $this->Paginator->sort('title', 'Title', ['direction' => 'desc']);
  230. $expected = [
  231. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=desc', 'class' => 'asc'],
  232. 'Title',
  233. '/a'
  234. ];
  235. $this->assertHtml($expected, $result);
  236. }
  237. /**
  238. * test sort() with escape option
  239. */
  240. public function testSortEscape()
  241. {
  242. $result = $this->Paginator->sort('title', 'TestTitle >');
  243. $expected = [
  244. 'a' => ['href' => '/index?sort=title&amp;direction=asc'],
  245. 'TestTitle &gt;',
  246. '/a'
  247. ];
  248. $this->assertHtml($expected, $result);
  249. $result = $this->Paginator->sort('title', 'TestTitle >', ['escape' => true]);
  250. $this->assertHtml($expected, $result);
  251. $result = $this->Paginator->sort('title', 'TestTitle >', ['escape' => false]);
  252. $expected = [
  253. 'a' => ['href' => '/index?sort=title&amp;direction=asc'],
  254. 'TestTitle >',
  255. '/a'
  256. ];
  257. $this->assertHtml($expected, $result);
  258. }
  259. /**
  260. * test that sort() works with virtual field order options.
  261. *
  262. * @return void
  263. */
  264. public function testSortLinkWithVirtualField()
  265. {
  266. Router::setRequestInfo([
  267. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [], 'form' => [], 'url' => ['url' => 'accounts/']],
  268. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  269. ]);
  270. $this->Paginator->request->params['paging']['Article']['sort'] = 'full_name';
  271. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  272. $result = $this->Paginator->sort('Article.full_name');
  273. $expected = [
  274. 'a' => ['href' => '/accounts/index?sort=Article.full_name&amp;direction=desc', 'class' => 'asc'],
  275. 'Article Full Name',
  276. '/a'
  277. ];
  278. $this->assertHtml($expected, $result);
  279. $result = $this->Paginator->sort('full_name');
  280. $expected = [
  281. 'a' => ['href' => '/accounts/index?sort=full_name&amp;direction=desc', 'class' => 'asc'],
  282. 'Full Name',
  283. '/a'
  284. ];
  285. $this->assertHtml($expected, $result);
  286. $this->Paginator->request->params['paging']['Article']['sort'] = 'full_name';
  287. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  288. $result = $this->Paginator->sort('Article.full_name');
  289. $expected = [
  290. 'a' => ['href' => '/accounts/index?sort=Article.full_name&amp;direction=asc', 'class' => 'desc'],
  291. 'Article Full Name',
  292. '/a'
  293. ];
  294. $this->assertHtml($expected, $result);
  295. $result = $this->Paginator->sort('full_name');
  296. $expected = [
  297. 'a' => ['href' => '/accounts/index?sort=full_name&amp;direction=asc', 'class' => 'desc'],
  298. 'Full Name',
  299. '/a'
  300. ];
  301. $this->assertHtml($expected, $result);
  302. }
  303. /**
  304. * testSortLinksUsingDirectionOption method
  305. *
  306. * @return void
  307. */
  308. public function testSortLinksUsingDirectionOption()
  309. {
  310. Router::setRequestInfo([
  311. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index',
  312. 'url' => ['url' => 'accounts/', 'mod_rewrite' => 'true']],
  313. ['base' => '/', 'here' => '/accounts/', 'webroot' => '/']
  314. ]);
  315. $this->Paginator->options(['url' => ['param']]);
  316. $result = $this->Paginator->sort('title', 'TestTitle', ['direction' => 'desc']);
  317. $expected = [
  318. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=desc'],
  319. 'TestTitle',
  320. '/a'
  321. ];
  322. $this->assertHtml($expected, $result);
  323. $result = $this->Paginator->sort('title', ['asc' => 'ascending', 'desc' => 'descending'], ['direction' => 'desc']);
  324. $expected = [
  325. 'a' => ['href' => '/accounts/index/param?sort=title&amp;direction=desc'],
  326. 'descending',
  327. '/a'
  328. ];
  329. $this->assertHtml($expected, $result);
  330. }
  331. /**
  332. * testSortLinksUsingDotNotation method
  333. *
  334. * @return void
  335. */
  336. public function testSortLinksUsingDotNotation()
  337. {
  338. Router::setRequestInfo([
  339. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  340. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  341. ]);
  342. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  343. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  344. $result = $this->Paginator->sort('Article.title');
  345. $expected = [
  346. 'a' => ['href' => '/accounts/index?sort=Article.title&amp;direction=asc', 'class' => 'desc'],
  347. 'Article Title',
  348. '/a'
  349. ];
  350. $this->assertHtml($expected, $result);
  351. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  352. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  353. $result = $this->Paginator->sort('Article.title', 'Title');
  354. $expected = [
  355. 'a' => ['href' => '/accounts/index?sort=Article.title&amp;direction=asc', 'class' => 'desc'],
  356. 'Title',
  357. '/a'
  358. ];
  359. $this->assertHtml($expected, $result);
  360. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  361. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  362. $result = $this->Paginator->sort('Article.title', 'Title');
  363. $expected = [
  364. 'a' => ['href' => '/accounts/index?sort=Article.title&amp;direction=desc', 'class' => 'asc'],
  365. 'Title',
  366. '/a'
  367. ];
  368. $this->assertHtml($expected, $result);
  369. $this->Paginator->request->params['paging']['Article']['sort'] = 'Account.title';
  370. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  371. $result = $this->Paginator->sort('title');
  372. $expected = [
  373. 'a' => ['href' => '/accounts/index?sort=title&amp;direction=asc'],
  374. 'Title',
  375. '/a'
  376. ];
  377. $this->assertHtml($expected, $result);
  378. }
  379. /**
  380. * Test creating paging links for missing models.
  381. *
  382. * @return void
  383. */
  384. public function testPagingLinksMissingModel()
  385. {
  386. $result = $this->Paginator->sort('title', 'Title', ['model' => 'Missing']);
  387. $expected = [
  388. 'a' => ['href' => '/index?sort=title&amp;direction=asc'],
  389. 'Title',
  390. '/a'
  391. ];
  392. $this->assertHtml($expected, $result);
  393. $result = $this->Paginator->next('Next', ['model' => 'Missing']);
  394. $expected = [
  395. 'li' => ['class' => 'next disabled'],
  396. 'a' => ['href' => '', 'onclick' => 'return false;'],
  397. 'Next',
  398. '/a',
  399. '/li'
  400. ];
  401. $this->assertHtml($expected, $result);
  402. $result = $this->Paginator->prev('Prev', ['model' => 'Missing']);
  403. $expected = [
  404. 'li' => ['class' => 'prev disabled'],
  405. 'a' => ['href' => '', 'onclick' => 'return false;'],
  406. 'Prev',
  407. '/a',
  408. '/li'
  409. ];
  410. $this->assertHtml($expected, $result);
  411. }
  412. /**
  413. * testSortKey method
  414. *
  415. * @return void
  416. */
  417. public function testSortKey()
  418. {
  419. $result = $this->Paginator->sortKey('Article', ['sort' => 'Article.title']);
  420. $this->assertEquals('Article.title', $result);
  421. $result = $this->Paginator->sortKey('Article', ['sort' => 'Article']);
  422. $this->assertEquals('Article', $result);
  423. }
  424. /**
  425. * Test that sortKey falls back to the default sorting options set
  426. * in the $params which are the default pagination options.
  427. *
  428. * @return void
  429. */
  430. public function testSortKeyFallbackToParams()
  431. {
  432. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  433. $result = $this->Paginator->sortKey();
  434. $this->assertEquals('Article.body', $result);
  435. $result = $this->Paginator->sortKey('Article');
  436. $this->assertEquals('Article.body', $result);
  437. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  438. $this->Paginator->request->params['paging']['Article']['order'] = 'DESC';
  439. $result = $this->Paginator->sortKey();
  440. $this->assertEquals('Article.body', $result);
  441. $result = $this->Paginator->sortKey('Article');
  442. $this->assertEquals('Article.body', $result);
  443. }
  444. /**
  445. * testSortDir method
  446. *
  447. * @return void
  448. */
  449. public function testSortDir()
  450. {
  451. $result = $this->Paginator->sortDir();
  452. $expected = 'asc';
  453. $this->assertEquals($expected, $result);
  454. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  455. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  456. $result = $this->Paginator->sortDir();
  457. $this->assertEquals('desc', $result);
  458. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  459. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  460. $result = $this->Paginator->sortDir();
  461. $this->assertEquals('asc', $result);
  462. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  463. $this->Paginator->request->params['paging']['Article']['direction'] = 'desc';
  464. $result = $this->Paginator->sortDir();
  465. $this->assertEquals('desc', $result);
  466. $this->Paginator->request->params['paging']['Article']['sort'] = 'title';
  467. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  468. $result = $this->Paginator->sortDir();
  469. $this->assertEquals('asc', $result);
  470. unset($this->Paginator->request->params['paging']['Article']['direction']);
  471. $result = $this->Paginator->sortDir('Article', ['direction' => 'asc']);
  472. $this->assertEquals('asc', $result);
  473. $result = $this->Paginator->sortDir('Article', ['direction' => 'desc']);
  474. $this->assertEquals('desc', $result);
  475. $result = $this->Paginator->sortDir('Article', ['direction' => 'asc']);
  476. $this->assertEquals('asc', $result);
  477. }
  478. /**
  479. * Test that sortDir falls back to the default sorting options set
  480. * in the $params which are the default pagination options.
  481. *
  482. * @return void
  483. */
  484. public function testSortDirFallbackToParams()
  485. {
  486. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  487. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  488. $result = $this->Paginator->sortDir();
  489. $this->assertEquals('asc', $result);
  490. $result = $this->Paginator->sortDir('Article');
  491. $this->assertEquals('asc', $result);
  492. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.body';
  493. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  494. $result = $this->Paginator->sortDir();
  495. $this->assertEquals('desc', $result);
  496. $result = $this->Paginator->sortDir('Article');
  497. $this->assertEquals('desc', $result);
  498. }
  499. /**
  500. * testSortAdminLinks method
  501. *
  502. * @return void
  503. */
  504. public function testSortAdminLinks()
  505. {
  506. Configure::write('Routing.prefixes', ['admin']);
  507. Router::reload();
  508. Router::connect('/admin/:controller/:action/*', ['prefix' => 'admin']);
  509. Router::setRequestInfo([
  510. ['controller' => 'users', 'plugin' => null, 'action' => 'index', 'prefix' => 'admin'],
  511. ['base' => '', 'here' => '/admin/users', 'webroot' => '/']
  512. ]);
  513. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  514. $result = $this->Paginator->next('Next');
  515. $expected = [
  516. 'li' => ['class' => 'next'],
  517. 'a' => ['href' => '/admin/users/index?page=2', 'rel' => 'next'],
  518. 'Next',
  519. '/a',
  520. '/li'
  521. ];
  522. $this->assertHtml($expected, $result);
  523. $this->Paginator->options(['url' => ['param']]);
  524. $result = $this->Paginator->sort('title');
  525. $expected = [
  526. 'a' => ['href' => '/admin/users/index/param?sort=title&amp;direction=asc'],
  527. 'Title',
  528. '/a'
  529. ];
  530. $this->assertHtml($expected, $result);
  531. $this->Paginator->options(['url' => ['param']]);
  532. $result = $this->Paginator->sort('Article.title', 'Title');
  533. $expected = [
  534. 'a' => ['href' => '/admin/users/index/param?sort=Article.title&amp;direction=asc'],
  535. 'Title',
  536. '/a'
  537. ];
  538. $this->assertHtml($expected, $result);
  539. }
  540. /**
  541. * Test that generated URLs work without sort defined within the request
  542. *
  543. * @return void
  544. */
  545. public function testDefaultSortAndNoSort()
  546. {
  547. Router::setRequestInfo([
  548. ['plugin' => null, 'controller' => 'articles', 'action' => 'index'],
  549. ['base' => '/', 'here' => '/articles/', 'webroot' => '/']
  550. ]);
  551. $this->Paginator->request->params['paging'] = [
  552. 'Article' => [
  553. 'page' => 1, 'current' => 3, 'count' => 13,
  554. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  555. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC',
  556. 'sort' => null
  557. ]
  558. ];
  559. $result = $this->Paginator->next('Next');
  560. $expected = [
  561. 'li' => ['class' => 'next'],
  562. 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'],
  563. 'Next',
  564. '/a',
  565. '/li'
  566. ];
  567. $this->assertHtml($expected, $result);
  568. }
  569. /**
  570. * testUrlGeneration method
  571. *
  572. * @return void
  573. */
  574. public function testUrlGeneration()
  575. {
  576. $result = $this->Paginator->sort('controller');
  577. $expected = [
  578. 'a' => ['href' => '/index?sort=controller&amp;direction=asc'],
  579. 'Controller',
  580. '/a'
  581. ];
  582. $this->assertHtml($expected, $result);
  583. $result = $this->Paginator->generateUrl();
  584. $this->assertEquals('/index', $result);
  585. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  586. $result = $this->Paginator->generateUrl();
  587. $this->assertEquals('/index?page=2', $result);
  588. $options = ['sort' => 'Article', 'direction' => 'desc'];
  589. $result = $this->Paginator->generateUrl($options);
  590. $this->assertEquals('/index?page=2&amp;sort=Article&amp;direction=desc', $result);
  591. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  592. $options = ['sort' => 'Article.name', 'direction' => 'desc'];
  593. $result = $this->Paginator->generateUrl($options);
  594. $this->assertEquals('/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
  595. }
  596. /**
  597. * test URL generation with prefix routes
  598. *
  599. * @return void
  600. */
  601. public function testUrlGenerationWithPrefixes()
  602. {
  603. Configure::write('Routing.prefixes', ['members']);
  604. Router::reload();
  605. Router::connect('/members/:controller/:action/*', ['prefix' => 'members']);
  606. Router::connect('/:controller/:action/*');
  607. Router::setRequestInfo([
  608. ['controller' => 'posts', 'action' => 'index', 'plugin' => null],
  609. ['base' => '', 'here' => 'posts/index', 'webroot' => '/']
  610. ]);
  611. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  612. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  613. $options = ['prefix' => 'members'];
  614. $result = $this->Paginator->generateUrl($options);
  615. $expected = '/members/posts/index?page=2';
  616. $this->assertEquals($expected, $result);
  617. $result = $this->Paginator->sort('name', null, ['url' => $options]);
  618. $expected = [
  619. 'a' => ['href' => '/members/posts/index?page=2&amp;sort=name&amp;direction=asc'],
  620. 'Name',
  621. '/a'
  622. ];
  623. $this->assertHtml($expected, $result);
  624. $result = $this->Paginator->next('next', ['url' => $options]);
  625. $expected = [
  626. 'li' => ['class' => 'next'],
  627. 'a' => ['href' => '/members/posts/index?page=3', 'rel' => 'next'],
  628. 'next',
  629. '/a',
  630. '/li'
  631. ];
  632. $this->assertHtml($expected, $result);
  633. $result = $this->Paginator->prev('prev', ['url' => $options]);
  634. $expected = [
  635. 'li' => ['class' => 'prev'],
  636. 'a' => ['href' => '/members/posts/index', 'rel' => 'prev'],
  637. 'prev',
  638. '/a',
  639. '/li'
  640. ];
  641. $this->assertHtml($expected, $result);
  642. $options = ['prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc'];
  643. $result = $this->Paginator->generateUrl($options);
  644. $expected = '/members/posts/index?page=2&amp;sort=name&amp;direction=desc';
  645. $this->assertEquals($expected, $result);
  646. $options = ['controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc'];
  647. $result = $this->Paginator->generateUrl($options);
  648. $expected = '/posts/index?page=2&amp;sort=Article.name&amp;direction=desc';
  649. $this->assertEquals($expected, $result);
  650. }
  651. /**
  652. * testOptions method
  653. *
  654. * @return void
  655. */
  656. public function testOptions()
  657. {
  658. $this->Paginator->options = [];
  659. $this->Paginator->request->params = [];
  660. $options = ['paging' => ['Article' => [
  661. 'direction' => 'desc',
  662. 'sort' => 'title'
  663. ]]];
  664. $this->Paginator->options($options);
  665. $expected = ['Article' => [
  666. 'direction' => 'desc',
  667. 'sort' => 'title'
  668. ]];
  669. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  670. $this->Paginator->options = [];
  671. $this->Paginator->request->params = [];
  672. $options = ['Article' => [
  673. 'direction' => 'desc',
  674. 'sort' => 'title'
  675. ]];
  676. $this->Paginator->options($options);
  677. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  678. $options = ['paging' => ['Article' => [
  679. 'direction' => 'desc',
  680. 'sort' => 'Article.title'
  681. ]]];
  682. $this->Paginator->options($options);
  683. $expected = ['Article' => [
  684. 'direction' => 'desc',
  685. 'sort' => 'Article.title'
  686. ]];
  687. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  688. }
  689. /**
  690. * testPassedArgsMergingWithUrlOptions method
  691. *
  692. * @return void
  693. */
  694. public function testPassedArgsMergingWithUrlOptions()
  695. {
  696. Router::setRequestInfo([
  697. ['plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => ['2']],
  698. ['base' => '/', 'here' => '/articles/', 'webroot' => '/']
  699. ]);
  700. $this->Paginator->request->params['paging'] = [
  701. 'Article' => [
  702. 'page' => 1, 'current' => 3, 'count' => 13,
  703. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  704. 'sort' => null, 'direction' => null,
  705. ]
  706. ];
  707. $this->Paginator->request->params['pass'] = [2];
  708. $this->Paginator->request->query = ['page' => 1, 'foo' => 'bar', 'x' => 'y', 'num' => 0, 'empty' => ''];
  709. $this->View->request = $this->Paginator->request;
  710. $this->Paginator = new PaginatorHelper($this->View);
  711. $result = $this->Paginator->sort('title');
  712. $expected = [
  713. 'a' => ['href' => '/articles/index/2?foo=bar&amp;x=y&amp;num=0&amp;sort=title&amp;direction=asc'],
  714. 'Title',
  715. '/a'
  716. ];
  717. $this->assertHtml($expected, $result);
  718. $result = $this->Paginator->numbers();
  719. $expected = [
  720. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  721. ['li' => []], ['a' => ['href' => '/articles/index/2?page=2&amp;foo=bar&amp;x=y&amp;num=0']], '2', '/a', '/li',
  722. ['li' => []], ['a' => ['href' => '/articles/index/2?page=3&amp;foo=bar&amp;x=y&amp;num=0']], '3', '/a', '/li',
  723. ['li' => []], ['a' => ['href' => '/articles/index/2?page=4&amp;foo=bar&amp;x=y&amp;num=0']], '4', '/a', '/li',
  724. ['li' => []], ['a' => ['href' => '/articles/index/2?page=5&amp;foo=bar&amp;x=y&amp;num=0']], '5', '/a', '/li',
  725. ['li' => []], ['a' => ['href' => '/articles/index/2?page=6&amp;foo=bar&amp;x=y&amp;num=0']], '6', '/a', '/li',
  726. ['li' => []], ['a' => ['href' => '/articles/index/2?page=7&amp;foo=bar&amp;x=y&amp;num=0']], '7', '/a', '/li',
  727. ];
  728. $this->assertHtml($expected, $result);
  729. $result = $this->Paginator->next('Next');
  730. $expected = [
  731. 'li' => ['class' => 'next'],
  732. 'a' => ['href' => '/articles/index/2?page=2&amp;foo=bar&amp;x=y&amp;num=0', 'rel' => 'next'],
  733. 'Next',
  734. '/a',
  735. '/li'
  736. ];
  737. $this->assertHtml($expected, $result);
  738. }
  739. /**
  740. * Test that generated URLs don't include sort and direction parameters
  741. *
  742. * @return void
  743. */
  744. public function testDefaultSortRemovedFromUrl()
  745. {
  746. Router::setRequestInfo([
  747. ['plugin' => null, 'controller' => 'articles', 'action' => 'index'],
  748. ['base' => '/', 'here' => '/articles/', 'webroot' => '/']
  749. ]);
  750. $this->Paginator->request->params['paging'] = [
  751. 'Article' => [
  752. 'page' => 1, 'current' => 3, 'count' => 13,
  753. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  754. 'sort' => 'Article.title', 'direction' => 'ASC',
  755. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
  756. ]
  757. ];
  758. $result = $this->Paginator->next('Next');
  759. $expected = [
  760. 'li' => ['class' => 'next'],
  761. 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'],
  762. 'Next',
  763. '/a',
  764. '/li'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. }
  768. /**
  769. * Test the prev() method.
  770. *
  771. * @return void
  772. */
  773. public function testPrev()
  774. {
  775. $this->Paginator->request->params['paging'] = [
  776. 'Client' => [
  777. 'page' => 1,
  778. 'current' => 3,
  779. 'count' => 13,
  780. 'prevPage' => false,
  781. 'nextPage' => true,
  782. 'pageCount' => 5,
  783. ]
  784. ];
  785. $result = $this->Paginator->prev('<< Previous');
  786. $expected = [
  787. 'li' => ['class' => 'prev disabled'],
  788. 'a' => ['href' => '', 'onclick' => 'return false;'],
  789. '&lt;&lt; Previous',
  790. '/a',
  791. '/li'
  792. ];
  793. $this->assertHtml($expected, $result);
  794. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => 'Prev']);
  795. $expected = [
  796. 'li' => ['class' => 'prev disabled'],
  797. 'a' => ['href' => '', 'onclick' => 'return false;'],
  798. 'Prev',
  799. '/a',
  800. '/li'
  801. ];
  802. $this->assertHtml($expected, $result);
  803. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => false]);
  804. $this->assertEquals('', $result, 'disabled + no text = no link');
  805. $this->Paginator->request->params['paging']['Client']['page'] = 2;
  806. $this->Paginator->request->params['paging']['Client']['prevPage'] = true;
  807. $result = $this->Paginator->prev('<< Previous');
  808. $expected = [
  809. 'li' => ['class' => 'prev'],
  810. 'a' => ['href' => '/index', 'rel' => 'prev'],
  811. '&lt;&lt; Previous',
  812. '/a',
  813. '/li'
  814. ];
  815. $this->assertHtml($expected, $result);
  816. $result = $this->Paginator->prev('Prev', [
  817. 'templates' => [
  818. 'prevActive' => '<a rel="prev" href="{{url}}">{{text}}</a>'
  819. ]
  820. ]);
  821. $expected = [
  822. 'a' => ['href' => '/index', 'rel' => 'prev'],
  823. 'Prev',
  824. '/a',
  825. ];
  826. $this->assertHtml($expected, $result);
  827. }
  828. /**
  829. * Test that prev() and the shared implementation underneath picks up from optins
  830. *
  831. * @return void
  832. */
  833. public function testPrevWithOptions()
  834. {
  835. $this->Paginator->request->params['paging'] = [
  836. 'Client' => [
  837. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
  838. 'nextPage' => false, 'pageCount' => 2,
  839. 'limit' => 10,
  840. ]
  841. ];
  842. $this->Paginator->options(['url' => [12, 'page' => 3]]);
  843. $result = $this->Paginator->prev('Prev', ['url' => ['foo' => 'bar']]);
  844. $expected = [
  845. 'li' => ['class' => 'prev'],
  846. 'a' => ['href' => '/index/12?limit=10&amp;foo=bar', 'rel' => 'prev'],
  847. 'Prev',
  848. '/a',
  849. '/li'
  850. ];
  851. $this->assertHtml($expected, $result);
  852. }
  853. /**
  854. * test the next() method.
  855. *
  856. * @return void
  857. */
  858. public function testNext()
  859. {
  860. $result = $this->Paginator->next('Next >>');
  861. $expected = [
  862. 'li' => ['class' => 'next'],
  863. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  864. 'Next &gt;&gt;',
  865. '/a',
  866. '/li'
  867. ];
  868. $this->assertHtml($expected, $result);
  869. $result = $this->Paginator->next('Next', [
  870. 'templates' => [
  871. 'nextActive' => '<a rel="next" href="{{url}}">{{text}}</a>'
  872. ]
  873. ]);
  874. $expected = [
  875. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  876. 'Next',
  877. '/a',
  878. ];
  879. $this->assertHtml($expected, $result);
  880. $result = $this->Paginator->next('Next >>', ['escape' => false]);
  881. $expected = [
  882. 'li' => ['class' => 'next'],
  883. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  884. 'preg:/Next >>/',
  885. '/a',
  886. '/li'
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * test next() with disabled links
  892. *
  893. * @return void
  894. */
  895. public function testNextDisabled()
  896. {
  897. $this->Paginator->request->params['paging'] = [
  898. 'Client' => [
  899. 'page' => 5,
  900. 'current' => 3,
  901. 'count' => 13,
  902. 'prevPage' => true,
  903. 'nextPage' => false,
  904. 'pageCount' => 5,
  905. ]
  906. ];
  907. $result = $this->Paginator->next('Next >>');
  908. $expected = [
  909. 'li' => ['class' => 'next disabled'],
  910. 'a' => ['href' => '', 'onclick' => 'return false;'],
  911. 'Next &gt;&gt;',
  912. '/a',
  913. '/li'
  914. ];
  915. $this->assertHtml($expected, $result);
  916. $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']);
  917. $expected = [
  918. 'li' => ['class' => 'next disabled'],
  919. 'a' => ['href' => '', 'onclick' => 'return false;'],
  920. 'Next',
  921. '/a',
  922. '/li'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]);
  926. $this->assertEquals('', $result, 'disabled + no text = no link');
  927. }
  928. /**
  929. * Test next() with a model argument.
  930. *
  931. * @return void
  932. */
  933. public function testNextAndPrevNonDefaultModel()
  934. {
  935. $this->Paginator->request->params['paging'] = [
  936. 'Client' => [
  937. 'page' => 1,
  938. 'current' => 3,
  939. 'count' => 13,
  940. 'prevPage' => false,
  941. 'nextPage' => true,
  942. 'pageCount' => 5,
  943. ],
  944. 'Server' => [
  945. 'page' => 5,
  946. 'current' => 1,
  947. 'count' => 5,
  948. 'prevPage' => true,
  949. 'nextPage' => false,
  950. 'pageCount' => 5,
  951. ]
  952. ];
  953. $result = $this->Paginator->next('Next', [
  954. 'model' => 'Client'
  955. ]);
  956. $expected = [
  957. 'li' => ['class' => 'next'],
  958. 'a' => ['href' => '/index?page=2', 'rel' => 'next'],
  959. 'Next',
  960. '/a',
  961. '/li'
  962. ];
  963. $this->assertHtml($expected, $result);
  964. $result = $this->Paginator->prev('Prev', [
  965. 'model' => 'Client'
  966. ]);
  967. $expected = '<li class="prev disabled"><a href="" onclick="return false;">Prev</a></li>';
  968. $this->assertEquals($expected, $result);
  969. $result = $this->Paginator->next('Next', [
  970. 'model' => 'Server'
  971. ]);
  972. $expected = '<li class="next disabled"><a href="" onclick="return false;">Next</a></li>';
  973. $this->assertEquals($expected, $result);
  974. $result = $this->Paginator->prev('Prev', [
  975. 'model' => 'Server'
  976. ]);
  977. $expected = [
  978. 'li' => ['class' => 'prev'],
  979. 'a' => ['href' => '/index?page=4', 'rel' => 'prev'],
  980. 'Prev',
  981. '/a',
  982. '/li'
  983. ];
  984. $this->assertHtml($expected, $result);
  985. }
  986. /**
  987. * testNumbers method
  988. *
  989. * @return void
  990. */
  991. public function testNumbers()
  992. {
  993. $this->Paginator->request->params['paging'] = [
  994. 'Client' => [
  995. 'page' => 8,
  996. 'current' => 3,
  997. 'count' => 30,
  998. 'prevPage' => false,
  999. 'nextPage' => 2,
  1000. 'pageCount' => 15,
  1001. ]
  1002. ];
  1003. $result = $this->Paginator->numbers();
  1004. $expected = [
  1005. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1006. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1007. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1008. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1009. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1010. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1011. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1012. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1013. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1014. ];
  1015. $this->assertHtml($expected, $result);
  1016. $result = $this->Paginator->numbers(['first' => 'first', 'last' => 'last']);
  1017. $expected = [
  1018. ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], 'first', '/a', '/li',
  1019. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1020. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1021. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1022. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1023. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1024. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1025. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1026. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1027. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1028. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1029. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1030. ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], 'last', '/a', '/li',
  1031. ];
  1032. $this->assertHtml($expected, $result);
  1033. $this->Paginator->request->params['paging'] = [
  1034. 'Client' => [
  1035. 'page' => 1,
  1036. 'current' => 3,
  1037. 'count' => 30,
  1038. 'prevPage' => false,
  1039. 'nextPage' => 2,
  1040. 'pageCount' => 15,
  1041. ]
  1042. ];
  1043. $result = $this->Paginator->numbers();
  1044. $expected = [
  1045. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1046. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1047. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1048. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1049. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1050. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1051. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1052. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1053. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1054. ];
  1055. $this->assertHtml($expected, $result);
  1056. $this->Paginator->request->params['paging'] = [
  1057. 'Client' => [
  1058. 'page' => 14,
  1059. 'current' => 3,
  1060. 'count' => 30,
  1061. 'prevPage' => false,
  1062. 'nextPage' => 2,
  1063. 'pageCount' => 15,
  1064. ]
  1065. ];
  1066. $result = $this->Paginator->numbers();
  1067. $expected = [
  1068. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1069. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1070. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1071. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1072. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1073. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1074. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1075. ['li' => ['class' => 'active']], '<a href=""', '14', '/a', '/li',
  1076. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1077. ];
  1078. $this->assertHtml($expected, $result);
  1079. $this->Paginator->request->params['paging'] = [
  1080. 'Client' => [
  1081. 'page' => 2,
  1082. 'current' => 3,
  1083. 'count' => 27,
  1084. 'prevPage' => false,
  1085. 'nextPage' => 2,
  1086. 'pageCount' => 9,
  1087. ]
  1088. ];
  1089. $result = $this->Paginator->numbers(['first' => 1]);
  1090. $expected = [
  1091. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1092. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1093. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1094. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1095. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1096. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1097. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1098. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1099. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1100. ];
  1101. $this->assertHtml($expected, $result);
  1102. $result = $this->Paginator->numbers(['last' => 1]);
  1103. $expected = [
  1104. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1105. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1106. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1107. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1108. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1109. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1110. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1111. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1112. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1113. ];
  1114. $this->assertHtml($expected, $result);
  1115. $this->Paginator->request->params['paging'] = [
  1116. 'Client' => [
  1117. 'page' => 15,
  1118. 'current' => 3,
  1119. 'count' => 30,
  1120. 'prevPage' => false,
  1121. 'nextPage' => 2,
  1122. 'pageCount' => 15,
  1123. ]
  1124. ];
  1125. $result = $this->Paginator->numbers(['first' => 1]);
  1126. $expected = [
  1127. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1128. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1129. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1130. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1131. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1132. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1133. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1134. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1135. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1136. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1137. ['li' => ['class' => 'active']], '<a href=""', '15', '/a', '/li',
  1138. ];
  1139. $this->assertHtml($expected, $result);
  1140. $this->Paginator->request->params['paging'] = [
  1141. 'Client' => [
  1142. 'page' => 10,
  1143. 'current' => 3,
  1144. 'count' => 30,
  1145. 'prevPage' => false,
  1146. 'nextPage' => 2,
  1147. 'pageCount' => 15,
  1148. ]
  1149. ];
  1150. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1151. $expected = [
  1152. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1153. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1154. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1155. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1156. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1157. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1158. ['li' => ['class' => 'active']], '<a href=""', '10', '/a', '/li',
  1159. ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
  1160. ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
  1161. ['li' => []], ['a' => ['href' => '/index?page=13']], '13', '/a', '/li',
  1162. ['li' => []], ['a' => ['href' => '/index?page=14']], '14', '/a', '/li',
  1163. ['li' => []], ['a' => ['href' => '/index?page=15']], '15', '/a', '/li',
  1164. ];
  1165. $this->assertHtml($expected, $result);
  1166. $this->Paginator->request->params['paging'] = [
  1167. 'Client' => [
  1168. 'page' => 6,
  1169. 'current' => 15,
  1170. 'count' => 623,
  1171. 'prevPage' => 1,
  1172. 'nextPage' => 1,
  1173. 'pageCount' => 42,
  1174. ]
  1175. ];
  1176. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1177. $expected = [
  1178. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1179. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1180. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1181. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1182. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1183. ['li' => ['class' => 'active']], '<a href=""', '6', '/a', '/li',
  1184. ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
  1185. ['li' => []], ['a' => ['href' => '/index?page=8']], '8', '/a', '/li',
  1186. ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
  1187. ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
  1188. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1189. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1190. ];
  1191. $this->assertHtml($expected, $result);
  1192. $this->Paginator->request->params['paging'] = [
  1193. 'Client' => [
  1194. 'page' => 37,
  1195. 'current' => 15,
  1196. 'count' => 623,
  1197. 'prevPage' => 1,
  1198. 'nextPage' => 1,
  1199. 'pageCount' => 42,
  1200. ]
  1201. ];
  1202. $result = $this->Paginator->numbers(['first' => 1, 'last' => 1]);
  1203. $expected = [
  1204. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1205. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1206. ['li' => []], ['a' => ['href' => '/index?page=33']], '33', '/a', '/li',
  1207. ['li' => []], ['a' => ['href' => '/index?page=34']], '34', '/a', '/li',
  1208. ['li' => []], ['a' => ['href' => '/index?page=35']], '35', '/a', '/li',
  1209. ['li' => []], ['a' => ['href' => '/index?page=36']], '36', '/a', '/li',
  1210. ['li' => ['class' => 'active']], '<a href=""', '37', '/a', '/li',
  1211. ['li' => []], ['a' => ['href' => '/index?page=38']], '38', '/a', '/li',
  1212. ['li' => []], ['a' => ['href' => '/index?page=39']], '39', '/a', '/li',
  1213. ['li' => []], ['a' => ['href' => '/index?page=40']], '40', '/a', '/li',
  1214. ['li' => []], ['a' => ['href' => '/index?page=41']], '41', '/a', '/li',
  1215. ['li' => []], ['a' => ['href' => '/index?page=42']], '42', '/a', '/li',
  1216. ];
  1217. $this->assertHtml($expected, $result);
  1218. }
  1219. /**
  1220. * Test that numbers() lets you overwrite templates.
  1221. *
  1222. * The templates file has no li elements.
  1223. *
  1224. * @return void
  1225. */
  1226. public function testNumbersTemplates()
  1227. {
  1228. $this->Paginator->request->params['paging'] = [
  1229. 'Client' => [
  1230. 'page' => 8,
  1231. 'current' => 3,
  1232. 'count' => 30,
  1233. 'prevPage' => false,
  1234. 'nextPage' => 2,
  1235. 'pageCount' => 15,
  1236. ]
  1237. ];
  1238. $result = $this->Paginator->numbers(['templates' => 'htmlhelper_tags']);
  1239. $expected = [
  1240. ['a' => ['href' => '/index?page=4']], '4', '/a',
  1241. ['a' => ['href' => '/index?page=5']], '5', '/a',
  1242. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1243. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1244. 'span' => ['class' => 'active'], '8', '/span',
  1245. ['a' => ['href' => '/index?page=9']], '9', '/a',
  1246. ['a' => ['href' => '/index?page=10']], '10', '/a',
  1247. ['a' => ['href' => '/index?page=11']], '11', '/a',
  1248. ['a' => ['href' => '/index?page=12']], '12', '/a',
  1249. ];
  1250. $this->assertHtml($expected, $result);
  1251. $this->assertContains(
  1252. '<li',
  1253. $this->Paginator->templater()->get('current'),
  1254. 'Templates were not restored.'
  1255. );
  1256. }
  1257. /**
  1258. * Test modulus option for numbers()
  1259. *
  1260. * @return void
  1261. */
  1262. public function testNumbersModulus()
  1263. {
  1264. $this->Paginator->request->params['paging'] = [
  1265. 'Client' => [
  1266. 'page' => 1,
  1267. 'current' => 10,
  1268. 'count' => 30,
  1269. 'prevPage' => false,
  1270. 'nextPage' => 2,
  1271. 'pageCount' => 3,
  1272. ]
  1273. ];
  1274. $result = $this->Paginator->numbers(['modulus' => 10]);
  1275. $expected = [
  1276. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1277. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1278. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1279. ];
  1280. $this->assertHtml($expected, $result);
  1281. $result = $this->Paginator->numbers(['modulus' => 3]);
  1282. $expected = [
  1283. ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
  1284. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1285. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1286. ];
  1287. $this->assertHtml($expected, $result);
  1288. $this->Paginator->request->params['paging'] = [
  1289. 'Client' => [
  1290. 'page' => 4895,
  1291. 'current' => 10,
  1292. 'count' => 48962,
  1293. 'prevPage' => 1,
  1294. 'nextPage' => 1,
  1295. 'pageCount' => 4897,
  1296. ]
  1297. ];
  1298. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1299. $expected = [
  1300. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1301. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1302. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1303. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1304. ['li' => ['class' => 'active']], '<a href=""', '4895', '/a', '/li',
  1305. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1306. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1307. ];
  1308. $this->assertHtml($expected, $result);
  1309. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1310. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1311. $expected = [
  1312. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1313. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1314. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1315. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1316. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1317. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1318. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1319. ];
  1320. $this->assertHtml($expected, $result);
  1321. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1322. $expected = [
  1323. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1324. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1325. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1326. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1327. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1328. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1329. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1330. ];
  1331. $this->assertHtml($expected, $result);
  1332. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 5, 'last' => 5]);
  1333. $expected = [
  1334. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1335. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1336. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1337. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1338. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1339. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1340. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1341. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4893', '/a', '/li',
  1342. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1343. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1344. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1345. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1346. ];
  1347. $this->assertHtml($expected, $result);
  1348. $this->Paginator->request->params['paging']['Client']['page'] = 4893;
  1349. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1350. $expected = [
  1351. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1352. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1353. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1354. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1355. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1356. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1357. ['li' => []], ['a' => ['href' => '/index?page=4891']], '4891', '/a', '/li',
  1358. ['li' => []], ['a' => ['href' => '/index?page=4892']], '4892', '/a', '/li',
  1359. ['li' => ['class' => 'active']], '<a href=""', '4893', '/a', '/li',
  1360. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1361. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1362. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1363. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. $this->Paginator->request->params['paging']['Client']['page'] = 58;
  1367. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1368. $expected = [
  1369. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1370. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1371. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1372. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1373. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1374. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1375. ['li' => []], ['a' => ['href' => '/index?page=56']], '56', '/a', '/li',
  1376. ['li' => []], ['a' => ['href' => '/index?page=57']], '57', '/a', '/li',
  1377. ['li' => ['class' => 'active']], '<a href=""', '58', '/a', '/li',
  1378. ['li' => []], ['a' => ['href' => '/index?page=59']], '59', '/a', '/li',
  1379. ['li' => []], ['a' => ['href' => '/index?page=60']], '60', '/a', '/li',
  1380. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1381. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4893', '/a', '/li',
  1382. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1383. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1384. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1385. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $this->Paginator->request->params['paging']['Client']['page'] = 5;
  1389. $result = $this->Paginator->numbers(['first' => 5, 'modulus' => 4, 'last' => 5]);
  1390. $expected = [
  1391. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1392. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1393. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1394. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1395. ['li' => ['class' => 'active']], '<a href=""', '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' => 'ellipsis']], '...', '/li',
  1399. ['li' => []], ['a' => ['href' => '/index?page=4893']], '4893', '/a', '/li',
  1400. ['li' => []], ['a' => ['href' => '/index?page=4894']], '4894', '/a', '/li',
  1401. ['li' => []], ['a' => ['href' => '/index?page=4895']], '4895', '/a', '/li',
  1402. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1403. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1404. ];
  1405. $this->assertHtml($expected, $result);
  1406. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1407. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 2, 'last' => 2]);
  1408. $expected = [
  1409. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1410. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1411. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1412. ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
  1413. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1414. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1415. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1416. ];
  1417. $this->assertHtml($expected, $result);
  1418. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1419. $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 0, 'last' => 2]);
  1420. $expected = [
  1421. ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
  1422. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1423. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1424. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1425. ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
  1426. ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
  1427. ];
  1428. $this->assertHtml($expected, $result);
  1429. }
  1430. /**
  1431. * Tests that disabling modulus displays all page links.
  1432. *
  1433. * @return void
  1434. */
  1435. public function testModulusDisabled()
  1436. {
  1437. $this->Paginator->request->params['paging'] = [
  1438. 'Client' => [
  1439. 'page' => 4,
  1440. 'current' => 2,
  1441. 'count' => 30,
  1442. 'prevPage' => 1,
  1443. 'nextPage' => 1,
  1444. 'pageCount' => 6,
  1445. ]
  1446. ];
  1447. $result = $this->Paginator->numbers(['modulus' => false]);
  1448. $expected = [
  1449. ['li' => []], '<a href="/index"', '1', '/a', '/li',
  1450. ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
  1451. ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
  1452. ['li' => ['class' => 'active']], ['a' => ['href' => '']], '4', '/a', '/li',
  1453. ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
  1454. ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
  1455. ];
  1456. $this->assertHtml($expected, $result);
  1457. }
  1458. /**
  1459. * Test that numbers() with url options.
  1460. *
  1461. * @return void
  1462. */
  1463. public function testNumbersWithUrlOptions()
  1464. {
  1465. $this->Paginator->request->params['paging'] = [
  1466. 'Client' => [
  1467. 'page' => 8,
  1468. 'current' => 3,
  1469. 'count' => 30,
  1470. 'prevPage' => false,
  1471. 'nextPage' => 2,
  1472. 'pageCount' => 15,
  1473. ]
  1474. ];
  1475. $result = $this->Paginator->numbers(['url' => ['#' => 'foo']]);
  1476. $expected = [
  1477. ['li' => []], ['a' => ['href' => '/index?page=4#foo']], '4', '/a', '/li',
  1478. ['li' => []], ['a' => ['href' => '/index?page=5#foo']], '5', '/a', '/li',
  1479. ['li' => []], ['a' => ['href' => '/index?page=6#foo']], '6', '/a', '/li',
  1480. ['li' => []], ['a' => ['href' => '/index?page=7#foo']], '7', '/a', '/li',
  1481. ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
  1482. ['li' => []], ['a' => ['href' => '/index?page=9#foo']], '9', '/a', '/li',
  1483. ['li' => []], ['a' => ['href' => '/index?page=10#foo']], '10', '/a', '/li',
  1484. ['li' => []], ['a' => ['href' => '/index?page=11#foo']], '11', '/a', '/li',
  1485. ['li' => []], ['a' => ['href' => '/index?page=12#foo']], '12', '/a', '/li',
  1486. ];
  1487. $this->assertHtml($expected, $result);
  1488. $this->Paginator->request->params['paging'] = [
  1489. 'Client' => [
  1490. 'page' => 3,
  1491. 'current' => 10,
  1492. 'count' => 48962,
  1493. 'prevPage' => 1,
  1494. 'nextPage' => 1,
  1495. 'pageCount' => 4897,
  1496. ]
  1497. ];
  1498. $result = $this->Paginator->numbers([
  1499. 'first' => 2,
  1500. 'modulus' => 2,
  1501. 'last' => 2,
  1502. 'url' => ['foo' => 'bar']]);
  1503. $expected = [
  1504. ['li' => []], ['a' => ['href' => '/index?foo=bar']], '1', '/a', '/li',
  1505. ['li' => []], ['a' => ['href' => '/index?page=2&amp;foo=bar']], '2', '/a', '/li',
  1506. ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
  1507. ['li' => []], ['a' => ['href' => '/index?page=4&amp;foo=bar']], '4', '/a', '/li',
  1508. ['li' => ['class' => 'ellipsis']], '...', '/li',
  1509. ['li' => []], ['a' => ['href' => '/index?page=4896&amp;foo=bar']], '4896', '/a', '/li',
  1510. ['li' => []], ['a' => ['href' => '/index?page=4897&amp;foo=bar']], '4897', '/a', '/li',
  1511. ];
  1512. $this->assertHtml($expected, $result);
  1513. }
  1514. /**
  1515. * test numbers() with routing parameters.
  1516. *
  1517. * @return void
  1518. */
  1519. public function testNumbersRouting()
  1520. {
  1521. $this->Paginator->request->params['paging'] = [
  1522. 'Client' => [
  1523. 'page' => 2,
  1524. 'current' => 2,
  1525. 'count' => 30,
  1526. 'prevPage' => false,
  1527. 'nextPage' => 3,
  1528. 'pageCount' => 3,
  1529. ]
  1530. ];
  1531. $request = new Request();
  1532. $request->addParams([
  1533. 'controller' => 'clients', 'action' => 'index', 'plugin' => null
  1534. ]);
  1535. $request->base = '';
  1536. $request->here = '/clients/index?page=2';
  1537. $request->webroot = '/';
  1538. Router::setRequestInfo($request);
  1539. $result = $this->Paginator->numbers();
  1540. $expected = [
  1541. ['li' => []], ['a' => ['href' => '/clients/index']], '1', '/a', '/li',
  1542. ['li' => ['class' => 'active']], '<a href=""', '2', '/a', '/li',
  1543. ['li' => []], ['a' => ['href' => '/clients/index?page=3']], '3', '/a', '/li',
  1544. ];
  1545. $this->assertHtml($expected, $result);
  1546. }
  1547. /**
  1548. * Test that numbers() works with the non default model.
  1549. *
  1550. * @return void
  1551. */
  1552. public function testNumbersNonDefaultModel()
  1553. {
  1554. $this->Paginator->request->params['paging'] = [
  1555. 'Client' => [
  1556. 'page' => 1,
  1557. 'current' => 3,
  1558. 'count' => 13,
  1559. 'prevPage' => false,
  1560. 'nextPage' => true,
  1561. 'pageCount' => 5,
  1562. ],
  1563. 'Server' => [
  1564. 'page' => 5,
  1565. 'current' => 1,
  1566. 'count' => 5,
  1567. 'prevPage' => true,
  1568. 'nextPage' => false,
  1569. 'pageCount' => 5,
  1570. ]
  1571. ];
  1572. $result = $this->Paginator->numbers(['model' => 'Server']);
  1573. $this->assertContains('<li class="active"><a href="">5</a></li>', $result);
  1574. $this->assertNotContains('<li class="active"><a href="">1</a></li>', $result);
  1575. $result = $this->Paginator->numbers(['model' => 'Client']);
  1576. $this->assertContains('<li class="active"><a href="">1</a></li>', $result);
  1577. $this->assertNotContains('<li class="active"><a href="">5</a></li>', $result);
  1578. }
  1579. /**
  1580. * test first() and last() with tag options
  1581. *
  1582. * @return void
  1583. */
  1584. public function testFirstAndLastTag()
  1585. {
  1586. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1587. $result = $this->Paginator->first('<<');
  1588. $expected = [
  1589. 'li' => ['class' => 'first'],
  1590. 'a' => ['href' => '/index'],
  1591. '&lt;&lt;',
  1592. '/a',
  1593. '/li'
  1594. ];
  1595. $this->assertHtml($expected, $result);
  1596. $result = $this->Paginator->last(2);
  1597. $expected = [
  1598. '<li',
  1599. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1600. '/li',
  1601. '<li',
  1602. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1603. '/li',
  1604. ];
  1605. $this->assertHtml($expected, $result);
  1606. }
  1607. /**
  1608. * test that on the last page you don't get a link ot the last page.
  1609. *
  1610. * @return void
  1611. */
  1612. public function testLastNoOutput()
  1613. {
  1614. $this->Paginator->request->params['paging']['Article']['page'] = 15;
  1615. $this->Paginator->request->params['paging']['Article']['pageCount'] = 15;
  1616. $result = $this->Paginator->last();
  1617. $expected = '';
  1618. $this->assertEquals($expected, $result);
  1619. }
  1620. /**
  1621. * test first() with a the model parameter.
  1622. *
  1623. * @return void
  1624. */
  1625. public function testFirstNonDefaultModel()
  1626. {
  1627. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1628. $this->Paginator->request->params['paging']['Client'] = [
  1629. 'page' => 3,
  1630. 'current' => 3,
  1631. 'count' => 13,
  1632. 'prevPage' => false,
  1633. 'nextPage' => true,
  1634. 'pageCount' => 5,
  1635. ];
  1636. $result = $this->Paginator->first('first', ['model' => 'Article']);
  1637. $this->assertEquals('', $result);
  1638. $result = $this->Paginator->first('first', ['model' => 'Client']);
  1639. $expected = [
  1640. 'li' => ['class' => 'first'],
  1641. 'a' => ['href' => '/index'],
  1642. 'first',
  1643. '/a',
  1644. '/li'
  1645. ];
  1646. $this->assertHtml($expected, $result);
  1647. }
  1648. /**
  1649. * test first() on the first page.
  1650. *
  1651. * @return void
  1652. */
  1653. public function testFirstEmpty()
  1654. {
  1655. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1656. $result = $this->Paginator->first();
  1657. $expected = '';
  1658. $this->assertEquals($expected, $result);
  1659. }
  1660. /**
  1661. * test first() and options()
  1662. *
  1663. * @return void
  1664. */
  1665. public function testFirstFullBaseUrl()
  1666. {
  1667. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1668. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  1669. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1670. $this->Paginator->options(['url' => ['_full' => true]]);
  1671. $result = $this->Paginator->first();
  1672. $expected = [
  1673. 'li' => ['class' => 'first'],
  1674. ['a' => [
  1675. 'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC'
  1676. ]],
  1677. '&lt;&lt; first',
  1678. '/a',
  1679. '/li',
  1680. ];
  1681. $this->assertHtml($expected, $result);
  1682. }
  1683. /**
  1684. * test first() on the fence-post
  1685. *
  1686. * @return void
  1687. */
  1688. public function testFirstBoundaries()
  1689. {
  1690. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1691. $result = $this->Paginator->first();
  1692. $expected = [
  1693. 'li' => ['class' => 'first'],
  1694. 'a' => ['href' => '/index'],
  1695. '&lt;&lt; first',
  1696. '/a',
  1697. '/li'
  1698. ];
  1699. $this->assertHtml($expected, $result);
  1700. $result = $this->Paginator->first(2);
  1701. $expected = [
  1702. '<li',
  1703. ['a' => ['href' => '/index']], '1', '/a',
  1704. '/li',
  1705. '<li',
  1706. ['a' => ['href' => '/index?page=2']], '2', '/a',
  1707. '/li'
  1708. ];
  1709. $this->assertHtml($expected, $result);
  1710. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1711. $result = $this->Paginator->first(3);
  1712. $this->assertEquals('', $result, 'When inside the first links range, no links should be made');
  1713. }
  1714. /**
  1715. * test params() method
  1716. *
  1717. * @return void
  1718. */
  1719. public function testParams()
  1720. {
  1721. $result = $this->Paginator->params();
  1722. $this->assertArrayHasKey('page', $result);
  1723. $this->assertArrayHasKey('pageCount', $result);
  1724. $result = $this->Paginator->params('Nope');
  1725. $this->assertEquals([], $result);
  1726. }
  1727. /**
  1728. * test param() method
  1729. *
  1730. * @return void
  1731. */
  1732. public function testParam()
  1733. {
  1734. $result = $this->Paginator->param('count');
  1735. $this->assertSame(62, $result);
  1736. $result = $this->Paginator->param('imaginary');
  1737. $this->assertNull($result);
  1738. }
  1739. /**
  1740. * test last() method
  1741. *
  1742. * @return void
  1743. */
  1744. public function testLast()
  1745. {
  1746. $result = $this->Paginator->last();
  1747. $expected = [
  1748. 'li' => ['class' => 'last'],
  1749. 'a' => ['href' => '/index?page=7'],
  1750. 'last &gt;&gt;',
  1751. '/a',
  1752. '/li'
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. $result = $this->Paginator->last(1);
  1756. $expected = [
  1757. '<li',
  1758. 'a' => ['href' => '/index?page=7'],
  1759. '7',
  1760. '/a',
  1761. '/li'
  1762. ];
  1763. $this->assertHtml($expected, $result);
  1764. $this->Paginator->request->params['paging']['Article']['page'] = 6;
  1765. $result = $this->Paginator->last(2);
  1766. $expected = [
  1767. '<li',
  1768. ['a' => ['href' => '/index?page=6']], '6', '/a',
  1769. '/li',
  1770. '<li',
  1771. ['a' => ['href' => '/index?page=7']], '7', '/a',
  1772. '/li',
  1773. ];
  1774. $this->assertHtml($expected, $result);
  1775. $result = $this->Paginator->last(3);
  1776. $this->assertEquals('', $result, 'When inside the last links range, no links should be made');
  1777. }
  1778. /**
  1779. * test the options for last()
  1780. *
  1781. * @return void
  1782. */
  1783. public function testLastOptions()
  1784. {
  1785. $this->Paginator->request->params['paging'] = [
  1786. 'Client' => [
  1787. 'page' => 4,
  1788. 'current' => 3,
  1789. 'count' => 30,
  1790. 'prevPage' => false,
  1791. 'nextPage' => 2,
  1792. 'pageCount' => 15,
  1793. 'sort' => 'Client.name',
  1794. 'direction' => 'DESC',
  1795. ]
  1796. ];
  1797. $result = $this->Paginator->last();
  1798. $expected = [
  1799. 'li' => ['class' => 'last'],
  1800. 'a' => [
  1801. 'href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC',
  1802. ],
  1803. 'last &gt;&gt;', '/a',
  1804. '/li',
  1805. ];
  1806. $this->assertHtml($expected, $result);
  1807. $result = $this->Paginator->last(1);
  1808. $expected = [
  1809. '<li',
  1810. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  1811. '/li',
  1812. ];
  1813. $this->assertHtml($expected, $result);
  1814. $result = $this->Paginator->last(2);
  1815. $expected = [
  1816. '<li',
  1817. ['a' => ['href' => '/index?page=14&amp;sort=Client.name&amp;direction=DESC']], '14', '/a',
  1818. '/li',
  1819. '<li',
  1820. ['a' => ['href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC']], '15', '/a',
  1821. '/li',
  1822. ];
  1823. $this->assertHtml($expected, $result);
  1824. }
  1825. /**
  1826. * test last() with a the model parameter.
  1827. *
  1828. * @return void
  1829. */
  1830. public function testLastNonDefaultModel()
  1831. {
  1832. $this->Paginator->request->params['paging']['Article']['page'] = 7;
  1833. $this->Paginator->request->params['paging']['Client'] = [
  1834. 'page' => 3,
  1835. 'current' => 3,
  1836. 'count' => 13,
  1837. 'prevPage' => false,
  1838. 'nextPage' => true,
  1839. 'pageCount' => 5,
  1840. ];
  1841. $result = $this->Paginator->last('last', ['model' => 'Article']);
  1842. $this->assertEquals('', $result);
  1843. $result = $this->Paginator->last('last', ['model' => 'Client']);
  1844. $expected = [
  1845. 'li' => ['class' => 'last'],
  1846. 'a' => ['href' => '/index?page=5'],
  1847. 'last',
  1848. '/a',
  1849. '/li'
  1850. ];
  1851. $this->assertHtml($expected, $result);
  1852. }
  1853. /**
  1854. * testCounter method
  1855. *
  1856. * @return void
  1857. */
  1858. public function testCounter()
  1859. {
  1860. $this->Paginator->request->params['paging'] = [
  1861. 'Client' => [
  1862. 'page' => 1,
  1863. 'current' => 3,
  1864. 'count' => 13,
  1865. 'perPage' => 3,
  1866. 'prevPage' => false,
  1867. 'nextPage' => true,
  1868. 'pageCount' => 5,
  1869. 'limit' => 3,
  1870. 'sort' => 'Client.name',
  1871. 'order' => 'DESC',
  1872. ]
  1873. ];
  1874. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  1875. $input .= 'starting on record {{start}}, ending on {{end}}';
  1876. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
  1877. $expected .= 'ending on 3';
  1878. $result = $this->Paginator->counter($input);
  1879. $this->assertEquals($expected, $result);
  1880. $result = $this->Paginator->counter(['format' => 'pages']);
  1881. $expected = '1 of 5';
  1882. $this->assertEquals($expected, $result);
  1883. $result = $this->Paginator->counter(['format' => 'range']);
  1884. $expected = '1 - 3 of 13';
  1885. $this->assertEquals($expected, $result);
  1886. $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}');
  1887. $this->assertEquals('Showing 1 of 5 clients', $result);
  1888. }
  1889. /**
  1890. * Tests that numbers are formatted according to the locale when using counter()
  1891. *
  1892. * @return void
  1893. */
  1894. public function testCounterBigNumbers()
  1895. {
  1896. $this->Paginator->request->params['paging'] = [
  1897. 'Client' => [
  1898. 'page' => 1523,
  1899. 'current' => 1230,
  1900. 'count' => 234567,
  1901. 'perPage' => 3000,
  1902. 'prevPage' => false,
  1903. 'nextPage' => true,
  1904. 'pageCount' => 1000,
  1905. 'limit' => 5000,
  1906. 'sort' => 'Client.name',
  1907. 'order' => 'DESC',
  1908. ]
  1909. ];
  1910. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  1911. $input .= 'starting on record {{start}}, ending on {{end}}';
  1912. $expected = 'Page 1,523 of 1,000, showing 1,230 records out of 234,567 total, ';
  1913. $expected .= 'starting on record 4,566,001, ending on 234,567';
  1914. $result = $this->Paginator->counter($input);
  1915. $this->assertEquals($expected, $result);
  1916. I18n::locale('de-DE');
  1917. $expected = 'Page 1.523 of 1.000, showing 1.230 records out of 234.567 total, ';
  1918. $expected .= 'starting on record 4.566.001, ending on 234.567';
  1919. $result = $this->Paginator->counter($input);
  1920. $this->assertEquals($expected, $result);
  1921. }
  1922. /**
  1923. * testHasPage method
  1924. *
  1925. * @return void
  1926. */
  1927. public function testHasPage()
  1928. {
  1929. $result = $this->Paginator->hasPage('Article', 15);
  1930. $this->assertFalse($result);
  1931. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  1932. $this->assertFalse($result);
  1933. $result = $this->Paginator->hasPage('Article', 2);
  1934. $this->assertTrue($result);
  1935. $result = $this->Paginator->hasPage(2);
  1936. $this->assertTrue($result);
  1937. }
  1938. /**
  1939. * testNextLinkUsingDotNotation method
  1940. *
  1941. * @return void
  1942. */
  1943. public function testNextLinkUsingDotNotation()
  1944. {
  1945. Router::setRequestInfo([
  1946. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  1947. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  1948. ]);
  1949. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1950. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  1951. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1952. $test = ['url' => [
  1953. 'page' => '1',
  1954. 'sort' => 'Article.title',
  1955. 'direction' => 'asc',
  1956. ]];
  1957. $this->Paginator->options($test);
  1958. $result = $this->Paginator->next('Next');
  1959. $expected = [
  1960. 'li' => ['class' => 'next'],
  1961. 'a' => [
  1962. 'href' => '/accounts/index?page=2&amp;sort=Article.title&amp;direction=asc',
  1963. 'rel' => 'next'
  1964. ],
  1965. 'Next',
  1966. '/a',
  1967. '/li',
  1968. ];
  1969. $this->assertHtml($expected, $result);
  1970. }
  1971. /**
  1972. * test the current() method
  1973. *
  1974. * @return void
  1975. */
  1976. public function testCurrent()
  1977. {
  1978. $result = $this->Paginator->current();
  1979. $this->assertEquals($this->Paginator->request->params['paging']['Article']['page'], $result);
  1980. $result = $this->Paginator->current('Incorrect');
  1981. $this->assertEquals(1, $result);
  1982. }
  1983. /**
  1984. * test the defaultModel() method
  1985. *
  1986. * @return void
  1987. */
  1988. public function testNoDefaultModel()
  1989. {
  1990. $this->Paginator->request = new Request();
  1991. $this->assertNull($this->Paginator->defaultModel());
  1992. }
  1993. /**
  1994. * test the numbers() method when there is only one page
  1995. *
  1996. * @return void
  1997. */
  1998. public function testWithOnePage()
  1999. {
  2000. $this->Paginator->request['paging'] = [
  2001. 'Article' => [
  2002. 'page' => 1,
  2003. 'current' => 2,
  2004. 'count' => 2,
  2005. 'prevPage' => false,
  2006. 'nextPage' => true,
  2007. 'pageCount' => 1,
  2008. ]
  2009. ];
  2010. $this->assertFalse($this->Paginator->numbers());
  2011. $this->assertFalse($this->Paginator->first());
  2012. $this->assertFalse($this->Paginator->last());
  2013. }
  2014. /**
  2015. * test the numbers() method when there is only one page
  2016. *
  2017. * @return void
  2018. */
  2019. public function testWithZeroPages()
  2020. {
  2021. $this->Paginator->request['paging'] = [
  2022. 'Article' => [
  2023. 'page' => 0,
  2024. 'current' => 0,
  2025. 'count' => 0,
  2026. 'perPage' => 10,
  2027. 'prevPage' => false,
  2028. 'nextPage' => false,
  2029. 'pageCount' => 0,
  2030. 'limit' => 10,
  2031. ]
  2032. ];
  2033. $result = $this->Paginator->counter(['format' => 'pages']);
  2034. $expected = '0 of 1';
  2035. $this->assertEquals($expected, $result);
  2036. }
  2037. /**
  2038. * Verifies that no next and prev links are created for single page results.
  2039. *
  2040. * @return void
  2041. */
  2042. public function testMetaPage0()
  2043. {
  2044. $this->Paginator->request['paging'] = [
  2045. 'Article' => [
  2046. 'page' => 1,
  2047. 'prevPage' => false,
  2048. 'nextPage' => false,
  2049. 'pageCount' => 1,
  2050. ]
  2051. ];
  2052. $expected = '';
  2053. $result = $this->Paginator->meta();
  2054. $this->assertSame($expected, $result);
  2055. }
  2056. /**
  2057. * Verifies that page 1 only has a next link.
  2058. *
  2059. * @return void
  2060. */
  2061. public function testMetaPage1()
  2062. {
  2063. $this->Paginator->request['paging'] = [
  2064. 'Article' => [
  2065. 'page' => 1,
  2066. 'prevPage' => false,
  2067. 'nextPage' => true,
  2068. 'pageCount' => 2,
  2069. ]
  2070. ];
  2071. $expected = '<link rel="next" href="http://localhost/index?page=2"/>';
  2072. $result = $this->Paginator->meta();
  2073. $this->assertSame($expected, $result);
  2074. }
  2075. /**
  2076. * Verifies that the method will append to a block.
  2077. *
  2078. * @return void
  2079. */
  2080. public function testMetaPage1InlineFalse()
  2081. {
  2082. $this->Paginator->request['paging'] = [
  2083. 'Article' => [
  2084. 'page' => 1,
  2085. 'prevPage' => false,
  2086. 'nextPage' => true,
  2087. 'pageCount' => 2,
  2088. ]
  2089. ];
  2090. $expected = '<link rel="next" href="http://localhost/index?page=2"/>';
  2091. $this->Paginator->meta(['block' => true]);
  2092. $result = $this->View->fetch('meta');
  2093. $this->assertSame($expected, $result);
  2094. }
  2095. /**
  2096. * Verifies that the last page only has a prev link.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testMetaPage1Last()
  2101. {
  2102. $this->Paginator->request['paging'] = [
  2103. 'Article' => [
  2104. 'page' => 2,
  2105. 'prevPage' => true,
  2106. 'nextPage' => false,
  2107. 'pageCount' => 2,
  2108. ]
  2109. ];
  2110. $expected = '<link rel="prev" href="http://localhost/index"/>';
  2111. $result = $this->Paginator->meta();
  2112. $this->assertSame($expected, $result);
  2113. }
  2114. /**
  2115. * Verifies that a page in the middle has both links.
  2116. *
  2117. * @return void
  2118. */
  2119. public function testMetaPage10Last()
  2120. {
  2121. $this->Paginator->request['paging'] = [
  2122. 'Article' => [
  2123. 'page' => 5,
  2124. 'prevPage' => true,
  2125. 'nextPage' => true,
  2126. 'pageCount' => 10,
  2127. ]
  2128. ];
  2129. $expected = '<link rel="prev" href="http://localhost/index?page=4"/>';
  2130. $expected .= '<link rel="next" href="http://localhost/index?page=6"/>';
  2131. $result = $this->Paginator->meta();
  2132. $this->assertSame($expected, $result);
  2133. }
  2134. }