PaginatorHelperTest.php 88 KB

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