PaginatorHelperTest.php 71 KB

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