PaginatorHelperTest.php 64 KB

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