PaginatorHelperTest.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  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. * Test that generated URLs work without sort defined within the request
  485. *
  486. * @return void
  487. */
  488. public function testDefaultSortAndNoSort() {
  489. Router::setRequestInfo(array(
  490. array('plugin' => null, 'controller' => 'articles', 'action' => 'index'),
  491. array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
  492. ));
  493. $this->Paginator->request->params['paging'] = array(
  494. 'Article' => array(
  495. 'page' => 1, 'current' => 3, 'count' => 13,
  496. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  497. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC',
  498. 'sort' => null
  499. )
  500. );
  501. $result = $this->Paginator->next('Next');
  502. $expected = array(
  503. 'li' => array('class' => 'next'),
  504. 'a' => array('rel' => 'next', 'href' => '/articles/index?page=2'),
  505. 'Next',
  506. '/a',
  507. '/li'
  508. );
  509. $this->assertTags($result, $expected);
  510. }
  511. /**
  512. * testUrlGeneration method
  513. *
  514. * @return void
  515. */
  516. public function testUrlGeneration() {
  517. $result = $this->Paginator->sort('controller');
  518. $expected = array(
  519. 'a' => array('href' => '/index?sort=controller&amp;direction=asc'),
  520. 'Controller',
  521. '/a'
  522. );
  523. $this->assertTags($result, $expected);
  524. $result = $this->Paginator->url();
  525. $this->assertEquals('/index', $result);
  526. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  527. $result = $this->Paginator->url();
  528. $this->assertEquals('/index?page=2', $result);
  529. $options = array('sort' => 'Article', 'direction' => 'desc');
  530. $result = $this->Paginator->url($options);
  531. $this->assertEquals('/index?page=2&amp;sort=Article&amp;direction=desc', $result);
  532. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  533. $options = array('sort' => 'Article.name', 'direction' => 'desc');
  534. $result = $this->Paginator->url($options);
  535. $this->assertEquals('/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
  536. }
  537. /**
  538. * test URL generation with prefix routes
  539. *
  540. * @return void
  541. */
  542. public function testUrlGenerationWithPrefixes() {
  543. Configure::write('Routing.prefixes', array('members'));
  544. Router::reload();
  545. Router::connect('/members/:controller/:action/*', array('prefix' => 'members'));
  546. Router::connect('/:controller/:action/*');
  547. Router::setRequestInfo(array(
  548. array('controller' => 'posts', 'action' => 'index', 'plugin' => null),
  549. array('base' => '', 'here' => 'posts/index', 'webroot' => '/')
  550. ));
  551. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  552. $this->Paginator->request->params['paging']['Article']['prevPage'] = true;
  553. $options = array('prefix' => 'members');
  554. $result = $this->Paginator->url($options);
  555. $expected = '/members/posts/index?page=2';
  556. $this->assertEquals($expected, $result);
  557. $result = $this->Paginator->sort('name', null, array('url' => $options));
  558. $expected = array(
  559. 'a' => array('href' => '/members/posts/index?page=2&amp;sort=name&amp;direction=asc'),
  560. 'Name',
  561. '/a'
  562. );
  563. $this->assertTags($result, $expected);
  564. $result = $this->Paginator->next('next', array('url' => $options));
  565. $expected = array(
  566. 'li' => array('class' => 'next'),
  567. 'a' => array('href' => '/members/posts/index?page=3', 'rel' => 'next'),
  568. 'next',
  569. '/a',
  570. '/li'
  571. );
  572. $this->assertTags($result, $expected);
  573. $result = $this->Paginator->prev('prev', array('url' => $options));
  574. $expected = array(
  575. 'li' => array('class' => 'prev'),
  576. 'a' => array('href' => '/members/posts/index', 'rel' => 'prev'),
  577. 'prev',
  578. '/a',
  579. '/li'
  580. );
  581. $this->assertTags($result, $expected);
  582. $options = array('prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc');
  583. $result = $this->Paginator->url($options);
  584. $expected = '/members/posts/index?page=2&amp;sort=name&amp;direction=desc';
  585. $this->assertEquals($expected, $result);
  586. $options = array('controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc');
  587. $result = $this->Paginator->url($options);
  588. $expected = '/posts/index?page=2&amp;sort=Article.name&amp;direction=desc';
  589. $this->assertEquals($expected, $result);
  590. }
  591. /**
  592. * testOptions method
  593. *
  594. * @return void
  595. */
  596. public function testOptions() {
  597. $this->Paginator->options = array();
  598. $this->Paginator->request->params = array();
  599. $options = array('paging' => array('Article' => array(
  600. 'direction' => 'desc',
  601. 'sort' => 'title'
  602. )));
  603. $this->Paginator->options($options);
  604. $expected = array('Article' => array(
  605. 'direction' => 'desc',
  606. 'sort' => 'title'
  607. ));
  608. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  609. $this->Paginator->options = array();
  610. $this->Paginator->request->params = array();
  611. $options = array('Article' => array(
  612. 'direction' => 'desc',
  613. 'sort' => 'title'
  614. ));
  615. $this->Paginator->options($options);
  616. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  617. $options = array('paging' => array('Article' => array(
  618. 'direction' => 'desc',
  619. 'sort' => 'Article.title'
  620. )));
  621. $this->Paginator->options($options);
  622. $expected = array('Article' => array(
  623. 'direction' => 'desc',
  624. 'sort' => 'Article.title'
  625. ));
  626. $this->assertEquals($expected, $this->Paginator->request->params['paging']);
  627. }
  628. /**
  629. * testPassedArgsMergingWithUrlOptions method
  630. *
  631. * @return void
  632. */
  633. public function testPassedArgsMergingWithUrlOptions() {
  634. Router::setRequestInfo(array(
  635. array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => array('2')),
  636. array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
  637. ));
  638. $this->Paginator->request->params['paging'] = array(
  639. 'Article' => array(
  640. 'page' => 1, 'current' => 3, 'count' => 13,
  641. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  642. 'sort' => null, 'direction' => null,
  643. )
  644. );
  645. $this->Paginator->request->params['pass'] = array(2);
  646. $this->Paginator->request->query = array('page' => 1, 'foo' => 'bar', 'x' => 'y');
  647. $this->Paginator->beforeRender(null, 'posts/index');
  648. $result = $this->Paginator->sort('title');
  649. $expected = array(
  650. 'a' => array('href' => '/articles/index/2?foo=bar&amp;x=y&amp;sort=title&amp;direction=asc'),
  651. 'Title',
  652. '/a'
  653. );
  654. $this->assertTags($result, $expected);
  655. $result = $this->Paginator->numbers();
  656. $expected = array(
  657. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  658. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=2&amp;foo=bar&amp;x=y')), '2', '/a', '/li',
  659. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=3&amp;foo=bar&amp;x=y')), '3', '/a', '/li',
  660. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=4&amp;foo=bar&amp;x=y')), '4', '/a', '/li',
  661. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=5&amp;foo=bar&amp;x=y')), '5', '/a', '/li',
  662. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=6&amp;foo=bar&amp;x=y')), '6', '/a', '/li',
  663. array('li' => array()), array('a' => array('href' => '/articles/index/2?page=7&amp;foo=bar&amp;x=y')), '7', '/a', '/li',
  664. );
  665. $this->assertTags($result, $expected);
  666. $result = $this->Paginator->next('Next');
  667. $expected = array(
  668. 'li' => array('class' => 'next'),
  669. 'a' => array('href' => '/articles/index/2?page=2&amp;foo=bar&amp;x=y', 'rel' => 'next'),
  670. 'Next',
  671. '/a',
  672. '/li'
  673. );
  674. $this->assertTags($result, $expected);
  675. }
  676. /**
  677. * Test that generated URLs don't include sort and direction parameters
  678. *
  679. * @return void
  680. */
  681. public function testDefaultSortRemovedFromUrl() {
  682. Router::setRequestInfo(array(
  683. array('plugin' => null, 'controller' => 'articles', 'action' => 'index'),
  684. array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
  685. ));
  686. $this->Paginator->request->params['paging'] = array(
  687. 'Article' => array(
  688. 'page' => 1, 'current' => 3, 'count' => 13,
  689. 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
  690. 'sort' => 'Article.title', 'direction' => 'ASC',
  691. 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
  692. )
  693. );
  694. $result = $this->Paginator->next('Next');
  695. $expected = array(
  696. 'li' => array('class' => 'next'),
  697. 'a' => array('rel' => 'next', 'href' => '/articles/index?page=2'),
  698. 'Next',
  699. '/a',
  700. '/li'
  701. );
  702. $this->assertTags($result, $expected);
  703. }
  704. /**
  705. * Test the prev() method.
  706. *
  707. * @return void
  708. */
  709. public function testPrev() {
  710. $this->Paginator->request->params['paging'] = array(
  711. 'Client' => array(
  712. 'page' => 1,
  713. 'current' => 3,
  714. 'count' => 13,
  715. 'prevPage' => false,
  716. 'nextPage' => true,
  717. 'pageCount' => 5,
  718. )
  719. );
  720. $result = $this->Paginator->prev('<< Previous');
  721. $expected = array(
  722. 'li' => array('class' => 'prev disabled'),
  723. 'span' => array(),
  724. '&lt;&lt; Previous',
  725. '/span',
  726. '/li'
  727. );
  728. $this->assertTags($result, $expected);
  729. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => 'Prev']);
  730. $expected = array(
  731. 'li' => array('class' => 'prev disabled'),
  732. 'span' => array(),
  733. 'Prev',
  734. '/span',
  735. '/li'
  736. );
  737. $this->assertTags($result, $expected);
  738. $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => false]);
  739. $this->assertEquals('', $result, 'disabled + no text = no link');
  740. $this->Paginator->request->params['paging']['Client']['page'] = 2;
  741. $this->Paginator->request->params['paging']['Client']['prevPage'] = true;
  742. $result = $this->Paginator->prev('<< Previous');
  743. $expected = array(
  744. 'li' => array('class' => 'prev'),
  745. 'a' => array('href' => '/index', 'rel' => 'prev'),
  746. '&lt;&lt; Previous',
  747. '/a',
  748. '/li'
  749. );
  750. $this->assertTags($result, $expected);
  751. }
  752. /**
  753. * Test that prev() and the shared implementation underneath picks up from optins
  754. *
  755. * @return void
  756. */
  757. public function testPrevWithOptions() {
  758. $this->Paginator->request->params['paging'] = array(
  759. 'Client' => array(
  760. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
  761. 'nextPage' => false, 'pageCount' => 2,
  762. 'limit' => 10,
  763. )
  764. );
  765. $this->Paginator->options(array('url' => array(12, 'page' => 3)));
  766. $result = $this->Paginator->prev('Prev', array('url' => array('foo' => 'bar')));
  767. $expected = array(
  768. 'li' => array('class' => 'prev'),
  769. 'a' => array('href' => '/index/12?limit=10&amp;foo=bar', 'rel' => 'prev'),
  770. 'Prev',
  771. '/a',
  772. '/li'
  773. );
  774. $this->assertTags($result, $expected);
  775. }
  776. /**
  777. * test the next() method.
  778. *
  779. * @return void
  780. */
  781. public function testNext() {
  782. $result = $this->Paginator->next('Next >>');
  783. $expected = array(
  784. 'li' => array('class' => 'next'),
  785. 'a' => array('href' => '/index?page=2', 'rel' => 'next'),
  786. 'Next &gt;&gt;',
  787. '/a',
  788. '/li'
  789. );
  790. $this->assertTags($result, $expected);
  791. $result = $this->Paginator->next('Next >>', ['escape' => false]);
  792. $expected = array(
  793. 'li' => array('class' => 'next'),
  794. 'a' => array('href' => '/index?page=2', 'rel' => 'next'),
  795. 'preg:/Next >>/',
  796. '/a',
  797. '/li'
  798. );
  799. $this->assertTags($result, $expected);
  800. }
  801. /**
  802. * test next() with disabled links
  803. *
  804. * @return void
  805. */
  806. public function testNextDisabled() {
  807. $this->Paginator->request->params['paging'] = array(
  808. 'Client' => array(
  809. 'page' => 5,
  810. 'current' => 3,
  811. 'count' => 13,
  812. 'prevPage' => true,
  813. 'nextPage' => false,
  814. 'pageCount' => 5,
  815. )
  816. );
  817. $result = $this->Paginator->next('Next >>');
  818. $expected = array(
  819. 'li' => array('class' => 'next disabled'),
  820. 'span' => array(),
  821. 'Next &gt;&gt;',
  822. '/span',
  823. '/li'
  824. );
  825. $this->assertTags($result, $expected);
  826. $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']);
  827. $expected = array(
  828. 'li' => array('class' => 'next disabled'),
  829. 'span' => array(),
  830. 'Next',
  831. '/span',
  832. '/li'
  833. );
  834. $this->assertTags($result, $expected);
  835. $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]);
  836. $this->assertEquals('', $result, 'disabled + no text = no link');
  837. }
  838. /**
  839. * Test next() with a model argument.
  840. *
  841. * @return void
  842. */
  843. public function testNextAndPrevNonDefaultModel() {
  844. $this->Paginator->request->params['paging'] = array(
  845. 'Client' => array(
  846. 'page' => 1,
  847. 'current' => 3,
  848. 'count' => 13,
  849. 'prevPage' => false,
  850. 'nextPage' => true,
  851. 'pageCount' => 5,
  852. ),
  853. 'Server' => array(
  854. 'page' => 5,
  855. 'current' => 1,
  856. 'count' => 5,
  857. 'prevPage' => true,
  858. 'nextPage' => false,
  859. 'pageCount' => 5,
  860. )
  861. );
  862. $result = $this->Paginator->next('Next', [
  863. 'model' => 'Client'
  864. ]);
  865. $expected = array(
  866. 'li' => array('class' => 'next'),
  867. 'a' => array('href' => '/index?page=2', 'rel' => 'next'),
  868. 'Next',
  869. '/a',
  870. '/li'
  871. );
  872. $this->assertTags($result, $expected);
  873. $result = $this->Paginator->prev('Prev', [
  874. 'model' => 'Client'
  875. ]);
  876. $expected = '<li class="prev disabled"><span>Prev</span></li>';
  877. $this->assertEquals($expected, $result);
  878. $result = $this->Paginator->next('Next', [
  879. 'model' => 'Server'
  880. ]);
  881. $expected = '<li class="next disabled"><span>Next</span></li>';
  882. $this->assertEquals($expected, $result);
  883. $result = $this->Paginator->prev('Prev', [
  884. 'model' => 'Server'
  885. ]);
  886. $expected = array(
  887. 'li' => array('class' => 'prev'),
  888. 'a' => array('href' => '/index?page=4', 'rel' => 'prev'),
  889. 'Prev',
  890. '/a',
  891. '/li'
  892. );
  893. $this->assertTags($result, $expected);
  894. }
  895. /**
  896. * testNumbers method
  897. *
  898. * @return void
  899. */
  900. public function testNumbers() {
  901. $this->Paginator->request->params['paging'] = array(
  902. 'Client' => array(
  903. 'page' => 8,
  904. 'current' => 3,
  905. 'count' => 30,
  906. 'prevPage' => false,
  907. 'nextPage' => 2,
  908. 'pageCount' => 15,
  909. )
  910. );
  911. $result = $this->Paginator->numbers();
  912. $expected = array(
  913. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  914. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  915. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  916. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  917. array('li' => array('class' => 'active')), '<span', '8', '/span', '/li',
  918. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  919. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  920. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  921. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  922. );
  923. $this->assertTags($result, $expected);
  924. $result = $this->Paginator->numbers(true);
  925. $expected = array(
  926. array('li' => array('class' => 'first')), array('a' => array('href' => '/index')), 'first', '/a', '/li',
  927. array('li' => array('class' => 'ellipsis')), '...', '/li',
  928. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  929. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  930. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  931. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  932. array('li' => array('class' => 'active')), '<span', '8', '/span', '/li',
  933. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  934. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  935. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  936. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  937. array('li' => array('class' => 'ellipsis')), '...', '/li',
  938. array('li' => array('class' => 'last')), array('a' => array('href' => '/index?page=15')), 'last', '/a', '/li',
  939. );
  940. $this->assertTags($result, $expected);
  941. $this->Paginator->request->params['paging'] = array(
  942. 'Client' => array(
  943. 'page' => 1,
  944. 'current' => 3,
  945. 'count' => 30,
  946. 'prevPage' => false,
  947. 'nextPage' => 2,
  948. 'pageCount' => 15,
  949. )
  950. );
  951. $result = $this->Paginator->numbers();
  952. $expected = array(
  953. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  954. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  955. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  956. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  957. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  958. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  959. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  960. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  961. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  962. );
  963. $this->assertTags($result, $expected);
  964. $this->Paginator->request->params['paging'] = array(
  965. 'Client' => array(
  966. 'page' => 14,
  967. 'current' => 3,
  968. 'count' => 30,
  969. 'prevPage' => false,
  970. 'nextPage' => 2,
  971. 'pageCount' => 15,
  972. )
  973. );
  974. $result = $this->Paginator->numbers();
  975. $expected = array(
  976. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  977. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  978. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  979. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  980. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  981. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  982. array('li' => array()), array('a' => array('href' => '/index?page=13')), '13', '/a', '/li',
  983. array('li' => array('class' => 'active')), '<span', '14', '/span', '/li',
  984. array('li' => array()), array('a' => array('href' => '/index?page=15')), '15', '/a', '/li',
  985. );
  986. $this->assertTags($result, $expected);
  987. $this->Paginator->request->params['paging'] = array(
  988. 'Client' => array(
  989. 'page' => 2,
  990. 'current' => 3,
  991. 'count' => 27,
  992. 'prevPage' => false,
  993. 'nextPage' => 2,
  994. 'pageCount' => 9,
  995. )
  996. );
  997. $result = $this->Paginator->numbers(array('first' => 1));
  998. $expected = array(
  999. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1000. array('li' => array('class' => 'active')), '<span', '2', '/span', '/li',
  1001. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1002. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1003. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1004. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1005. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1006. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1007. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1008. );
  1009. $this->assertTags($result, $expected);
  1010. $result = $this->Paginator->numbers(array('last' => 1));
  1011. $expected = array(
  1012. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1013. array('li' => array('class' => 'active')), '<span', '2', '/span', '/li',
  1014. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1015. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1016. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1017. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1018. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1019. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1020. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1021. );
  1022. $this->assertTags($result, $expected);
  1023. $this->Paginator->request->params['paging'] = array(
  1024. 'Client' => array(
  1025. 'page' => 15,
  1026. 'current' => 3,
  1027. 'count' => 30,
  1028. 'prevPage' => false,
  1029. 'nextPage' => 2,
  1030. 'pageCount' => 15,
  1031. )
  1032. );
  1033. $result = $this->Paginator->numbers(array('first' => 1));
  1034. $expected = array(
  1035. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1036. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1037. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1038. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1039. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1040. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  1041. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  1042. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  1043. array('li' => array()), array('a' => array('href' => '/index?page=13')), '13', '/a', '/li',
  1044. array('li' => array()), array('a' => array('href' => '/index?page=14')), '14', '/a', '/li',
  1045. array('li' => array('class' => 'active')), '<span', '15', '/span', '/li',
  1046. );
  1047. $this->assertTags($result, $expected);
  1048. $this->Paginator->request->params['paging'] = array(
  1049. 'Client' => array(
  1050. 'page' => 10,
  1051. 'current' => 3,
  1052. 'count' => 30,
  1053. 'prevPage' => false,
  1054. 'nextPage' => 2,
  1055. 'pageCount' => 15,
  1056. )
  1057. );
  1058. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  1059. $expected = array(
  1060. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1061. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1062. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1063. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1064. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1065. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1066. array('li' => array('class' => 'active')), '<span', '10', '/span', '/li',
  1067. array('li' => array()), array('a' => array('href' => '/index?page=11')), '11', '/a', '/li',
  1068. array('li' => array()), array('a' => array('href' => '/index?page=12')), '12', '/a', '/li',
  1069. array('li' => array()), array('a' => array('href' => '/index?page=13')), '13', '/a', '/li',
  1070. array('li' => array()), array('a' => array('href' => '/index?page=14')), '14', '/a', '/li',
  1071. array('li' => array()), array('a' => array('href' => '/index?page=15')), '15', '/a', '/li',
  1072. );
  1073. $this->assertTags($result, $expected);
  1074. $this->Paginator->request->params['paging'] = array(
  1075. 'Client' => array(
  1076. 'page' => 6,
  1077. 'current' => 15,
  1078. 'count' => 623,
  1079. 'prevPage' => 1,
  1080. 'nextPage' => 1,
  1081. 'pageCount' => 42,
  1082. )
  1083. );
  1084. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  1085. $expected = array(
  1086. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1087. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1088. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1089. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1090. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1091. array('li' => array('class' => 'active')), '<span', '6', '/span', '/li',
  1092. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1093. array('li' => array()), array('a' => array('href' => '/index?page=8')), '8', '/a', '/li',
  1094. array('li' => array()), array('a' => array('href' => '/index?page=9')), '9', '/a', '/li',
  1095. array('li' => array()), array('a' => array('href' => '/index?page=10')), '10', '/a', '/li',
  1096. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1097. array('li' => array()), array('a' => array('href' => '/index?page=42')), '42', '/a', '/li',
  1098. );
  1099. $this->assertTags($result, $expected);
  1100. $this->Paginator->request->params['paging'] = array(
  1101. 'Client' => array(
  1102. 'page' => 37,
  1103. 'current' => 15,
  1104. 'count' => 623,
  1105. 'prevPage' => 1,
  1106. 'nextPage' => 1,
  1107. 'pageCount' => 42,
  1108. )
  1109. );
  1110. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  1111. $expected = array(
  1112. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1113. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1114. array('li' => array()), array('a' => array('href' => '/index?page=33')), '33', '/a', '/li',
  1115. array('li' => array()), array('a' => array('href' => '/index?page=34')), '34', '/a', '/li',
  1116. array('li' => array()), array('a' => array('href' => '/index?page=35')), '35', '/a', '/li',
  1117. array('li' => array()), array('a' => array('href' => '/index?page=36')), '36', '/a', '/li',
  1118. array('li' => array('class' => 'active')), '<span', '37', '/span', '/li',
  1119. array('li' => array()), array('a' => array('href' => '/index?page=38')), '38', '/a', '/li',
  1120. array('li' => array()), array('a' => array('href' => '/index?page=39')), '39', '/a', '/li',
  1121. array('li' => array()), array('a' => array('href' => '/index?page=40')), '40', '/a', '/li',
  1122. array('li' => array()), array('a' => array('href' => '/index?page=41')), '41', '/a', '/li',
  1123. array('li' => array()), array('a' => array('href' => '/index?page=42')), '42', '/a', '/li',
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. }
  1127. /**
  1128. * Test modulus option for numbers()
  1129. *
  1130. * @return void
  1131. */
  1132. public function testNumbersModulus() {
  1133. $this->Paginator->request->params['paging'] = array(
  1134. 'Client' => array(
  1135. 'page' => 1,
  1136. 'current' => 10,
  1137. 'count' => 30,
  1138. 'prevPage' => false,
  1139. 'nextPage' => 2,
  1140. 'pageCount' => 3,
  1141. )
  1142. );
  1143. $options = array('modulus' => 10);
  1144. $result = $this->Paginator->numbers($options);
  1145. $expected = array(
  1146. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  1147. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1148. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1149. );
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Paginator->numbers(array('modulus' => 3));
  1152. $expected = array(
  1153. array('li' => array('class' => 'active')), '<span', '1', '/span', '/li',
  1154. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1155. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1156. );
  1157. $this->assertTags($result, $expected);
  1158. $this->Paginator->request->params['paging'] = array(
  1159. 'Client' => array(
  1160. 'page' => 4895,
  1161. 'current' => 10,
  1162. 'count' => 48962,
  1163. 'prevPage' => 1,
  1164. 'nextPage' => 1,
  1165. 'pageCount' => 4897,
  1166. )
  1167. );
  1168. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1169. $expected = array(
  1170. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1171. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1172. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1173. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1174. array('li' => array('class' => 'active')), '<span', '4895', '/span', '/li',
  1175. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1176. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1177. );
  1178. $this->assertTags($result, $expected);
  1179. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1180. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1181. $expected = array(
  1182. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1183. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1184. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1185. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1186. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1187. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1188. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1189. );
  1190. $this->assertTags($result, $expected);
  1191. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1192. $expected = array(
  1193. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1194. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1195. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1196. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1197. array('li' => array('class' => 'ellipsis')), '...', '/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. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 5, 'last' => 5));
  1203. $expected = array(
  1204. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1205. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1206. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1207. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1208. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1209. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1210. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1211. array('li' => array()), array('a' => array('href' => '/index?page=4893')), '4893', '/a', '/li',
  1212. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1213. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1214. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1215. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1216. );
  1217. $this->assertTags($result, $expected);
  1218. $this->Paginator->request->params['paging']['Client']['page'] = 4893;
  1219. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5));
  1220. $expected = array(
  1221. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1222. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1223. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1224. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1225. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1226. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1227. array('li' => array()), array('a' => array('href' => '/index?page=4891')), '4891', '/a', '/li',
  1228. array('li' => array()), array('a' => array('href' => '/index?page=4892')), '4892', '/a', '/li',
  1229. array('li' => array('class' => 'active')), '<span', '4893', '/span', '/li',
  1230. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1231. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1232. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1233. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1234. );
  1235. $this->assertTags($result, $expected);
  1236. $this->Paginator->request->params['paging']['Client']['page'] = 58;
  1237. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5));
  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()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1242. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1243. array('li' => array()), array('a' => array('href' => '/index?page=5')), '5', '/a', '/li',
  1244. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1245. array('li' => array()), array('a' => array('href' => '/index?page=56')), '56', '/a', '/li',
  1246. array('li' => array()), array('a' => array('href' => '/index?page=57')), '57', '/a', '/li',
  1247. array('li' => array('class' => 'active')), '<span', '58', '/span', '/li',
  1248. array('li' => array()), array('a' => array('href' => '/index?page=59')), '59', '/a', '/li',
  1249. array('li' => array()), array('a' => array('href' => '/index?page=60')), '60', '/a', '/li',
  1250. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1251. array('li' => array()), array('a' => array('href' => '/index?page=4893')), '4893', '/a', '/li',
  1252. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1253. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1254. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1255. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1256. );
  1257. $this->assertTags($result, $expected);
  1258. $this->Paginator->request->params['paging']['Client']['page'] = 5;
  1259. $result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5));
  1260. $expected = array(
  1261. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1262. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1263. array('li' => array()), array('a' => array('href' => '/index?page=3')), '3', '/a', '/li',
  1264. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1265. array('li' => array('class' => 'active')), '<span', '5', '/span', '/li',
  1266. array('li' => array()), array('a' => array('href' => '/index?page=6')), '6', '/a', '/li',
  1267. array('li' => array()), array('a' => array('href' => '/index?page=7')), '7', '/a', '/li',
  1268. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1269. array('li' => array()), array('a' => array('href' => '/index?page=4893')), '4893', '/a', '/li',
  1270. array('li' => array()), array('a' => array('href' => '/index?page=4894')), '4894', '/a', '/li',
  1271. array('li' => array()), array('a' => array('href' => '/index?page=4895')), '4895', '/a', '/li',
  1272. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1273. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1274. );
  1275. $this->assertTags($result, $expected);
  1276. $this->Paginator->request->params['paging']['Client']['page'] = 3;
  1277. $result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
  1278. $expected = array(
  1279. array('li' => array()), array('a' => array('href' => '/index')), '1', '/a', '/li',
  1280. array('li' => array()), array('a' => array('href' => '/index?page=2')), '2', '/a', '/li',
  1281. array('li' => array('class' => 'active')), '<span', '3', '/span', '/li',
  1282. array('li' => array()), array('a' => array('href' => '/index?page=4')), '4', '/a', '/li',
  1283. array('li' => array('class' => 'ellipsis')), '...', '/li',
  1284. array('li' => array()), array('a' => array('href' => '/index?page=4896')), '4896', '/a', '/li',
  1285. array('li' => array()), array('a' => array('href' => '/index?page=4897')), '4897', '/a', '/li',
  1286. );
  1287. $this->assertTags($result, $expected);
  1288. }
  1289. /**
  1290. * test numbers() with routing parameters.
  1291. *
  1292. * @return void
  1293. */
  1294. public function testNumbersRouting() {
  1295. $this->Paginator->request->params['paging'] = array(
  1296. 'Client' => array(
  1297. 'page' => 2,
  1298. 'current' => 2,
  1299. 'count' => 30,
  1300. 'prevPage' => false,
  1301. 'nextPage' => 3,
  1302. 'pageCount' => 3,
  1303. )
  1304. );
  1305. $request = new Request();
  1306. $request->addParams(array(
  1307. 'controller' => 'clients', 'action' => 'index', 'plugin' => null
  1308. ));
  1309. $request->base = '';
  1310. $request->here = '/clients/index?page=2';
  1311. $request->webroot = '/';
  1312. Router::setRequestInfo($request);
  1313. $result = $this->Paginator->numbers();
  1314. $expected = array(
  1315. array('li' => array()), array('a' => array('href' => '/clients/index')), '1', '/a', '/li',
  1316. array('li' => array('class' => 'active')), '<span', '2', '/span', '/li',
  1317. array('li' => array()), array('a' => array('href' => '/clients/index?page=3')), '3', '/a', '/li',
  1318. );
  1319. $this->assertTags($result, $expected);
  1320. }
  1321. /**
  1322. * Test that numbers() works with the non default model.
  1323. *
  1324. * @return void
  1325. */
  1326. public function testNumbersNonDefaultModel() {
  1327. $this->Paginator->request->params['paging'] = array(
  1328. 'Client' => array(
  1329. 'page' => 1,
  1330. 'current' => 3,
  1331. 'count' => 13,
  1332. 'prevPage' => false,
  1333. 'nextPage' => true,
  1334. 'pageCount' => 5,
  1335. ),
  1336. 'Server' => array(
  1337. 'page' => 5,
  1338. 'current' => 1,
  1339. 'count' => 5,
  1340. 'prevPage' => true,
  1341. 'nextPage' => false,
  1342. 'pageCount' => 5,
  1343. )
  1344. );
  1345. $result = $this->Paginator->numbers(['model' => 'Server']);
  1346. $this->assertContains('<li class="active"><span>5</span></li>', $result);
  1347. $this->assertNotContains('<li class="active"><span>1</span></li>', $result);
  1348. $result = $this->Paginator->numbers(['model' => 'Client']);
  1349. $this->assertContains('<li class="active"><span>1</span></li>', $result);
  1350. $this->assertNotContains('<li class="active"><span>5</span></li>', $result);
  1351. }
  1352. /**
  1353. * test first() and last() with tag options
  1354. *
  1355. * @return void
  1356. */
  1357. public function testFirstAndLastTag() {
  1358. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1359. $result = $this->Paginator->first('<<');
  1360. $expected = array(
  1361. 'li' => array('class' => 'first'),
  1362. 'a' => array('href' => '/index'),
  1363. '&lt;&lt;',
  1364. '/a',
  1365. '/li'
  1366. );
  1367. $this->assertTags($result, $expected);
  1368. $result = $this->Paginator->last(2);
  1369. $expected = array(
  1370. '<li',
  1371. array('a' => array('href' => '/index?page=6')), '6', '/a',
  1372. '/li',
  1373. '<li',
  1374. array('a' => array('href' => '/index?page=7')), '7', '/a',
  1375. '/li',
  1376. );
  1377. $this->assertTags($result, $expected);
  1378. }
  1379. /**
  1380. * test that on the last page you don't get a link ot the last page.
  1381. *
  1382. * @return void
  1383. */
  1384. public function testLastNoOutput() {
  1385. $this->Paginator->request->params['paging']['Article']['page'] = 15;
  1386. $this->Paginator->request->params['paging']['Article']['pageCount'] = 15;
  1387. $result = $this->Paginator->last();
  1388. $expected = '';
  1389. $this->assertEquals($expected, $result);
  1390. }
  1391. /**
  1392. * test first() with a the model parameter.
  1393. *
  1394. * @return void
  1395. */
  1396. public function testFirstNonDefaultModel() {
  1397. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1398. $this->Paginator->request->params['paging']['Client'] = array(
  1399. 'page' => 3,
  1400. 'current' => 3,
  1401. 'count' => 13,
  1402. 'prevPage' => false,
  1403. 'nextPage' => true,
  1404. 'pageCount' => 5,
  1405. );
  1406. $result = $this->Paginator->first('first', ['model' => 'Article:']);
  1407. $this->assertEquals('', $result);
  1408. $result = $this->Paginator->first('first', ['model' => 'Client']);
  1409. $expected = array(
  1410. 'li' => array('class' => 'first'),
  1411. 'a' => array('href' => '/index'),
  1412. 'first',
  1413. '/a',
  1414. '/li'
  1415. );
  1416. $this->assertTags($result, $expected);
  1417. }
  1418. /**
  1419. * test first() on the first page.
  1420. *
  1421. * @return void
  1422. */
  1423. public function testFirstEmpty() {
  1424. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1425. $result = $this->Paginator->first();
  1426. $expected = '';
  1427. $this->assertEquals($expected, $result);
  1428. }
  1429. /**
  1430. * test first() and options()
  1431. *
  1432. * @return void
  1433. */
  1434. public function testFirstFullBaseUrl() {
  1435. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1436. $this->Paginator->request->params['paging']['Article']['direction'] = 'DESC';
  1437. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1438. $this->Paginator->options(array('url' => array('_full' => true)));
  1439. $result = $this->Paginator->first();
  1440. $expected = array(
  1441. 'li' => ['class' => 'first'],
  1442. array('a' => array(
  1443. 'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC'
  1444. )),
  1445. '&lt;&lt; first',
  1446. '/a',
  1447. '/li',
  1448. );
  1449. $this->assertTags($result, $expected);
  1450. }
  1451. /**
  1452. * test first() on the fence-post
  1453. *
  1454. * @return void
  1455. */
  1456. public function testFirstBoundaries() {
  1457. $this->Paginator->request->params['paging']['Article']['page'] = 3;
  1458. $result = $this->Paginator->first();
  1459. $expected = array(
  1460. 'li' => ['class' => 'first'],
  1461. 'a' => array('href' => '/index'),
  1462. '&lt;&lt; first',
  1463. '/a',
  1464. '/li'
  1465. );
  1466. $this->assertTags($result, $expected);
  1467. $result = $this->Paginator->first(2);
  1468. $expected = array(
  1469. '<li',
  1470. array('a' => array('href' => '/index')), '1', '/a',
  1471. '/li',
  1472. '<li',
  1473. array('a' => array('href' => '/index?page=2')), '2', '/a',
  1474. '/li'
  1475. );
  1476. $this->assertTags($result, $expected);
  1477. $this->Paginator->request->params['paging']['Article']['page'] = 2;
  1478. $result = $this->Paginator->first(3);
  1479. $this->assertEquals('', $result, 'When inside the first links range, no links should be made');
  1480. }
  1481. /**
  1482. * test params() method
  1483. *
  1484. * @return void
  1485. */
  1486. public function testParams() {
  1487. $result = $this->Paginator->params();
  1488. $this->assertArrayHasKey('page', $result);
  1489. $this->assertArrayHasKey('pageCount', $result);
  1490. }
  1491. /**
  1492. * test param() method
  1493. *
  1494. * @return void
  1495. */
  1496. public function testParam() {
  1497. $result = $this->Paginator->param('count');
  1498. $this->assertSame(62, $result);
  1499. $result = $this->Paginator->param('imaginary');
  1500. $this->assertNull($result);
  1501. }
  1502. /**
  1503. * test last() method
  1504. *
  1505. * @return void
  1506. */
  1507. public function testLast() {
  1508. $result = $this->Paginator->last();
  1509. $expected = array(
  1510. 'li' => ['class' => 'last'],
  1511. 'a' => array('href' => '/index?page=7'),
  1512. 'last &gt;&gt;',
  1513. '/a',
  1514. '/li'
  1515. );
  1516. $this->assertTags($result, $expected);
  1517. $result = $this->Paginator->last(1);
  1518. $expected = array(
  1519. '<li',
  1520. 'a' => array('href' => '/index?page=7'),
  1521. '7',
  1522. '/a',
  1523. '/li'
  1524. );
  1525. $this->assertTags($result, $expected);
  1526. $this->Paginator->request->params['paging']['Article']['page'] = 6;
  1527. $result = $this->Paginator->last(2);
  1528. $expected = array(
  1529. '<li',
  1530. array('a' => array('href' => '/index?page=6')), '6', '/a',
  1531. '/li',
  1532. '<li',
  1533. array('a' => array('href' => '/index?page=7')), '7', '/a',
  1534. '/li',
  1535. );
  1536. $this->assertTags($result, $expected);
  1537. $result = $this->Paginator->last(3);
  1538. $this->assertEquals('', $result, 'When inside the last links range, no links should be made');
  1539. }
  1540. /**
  1541. * test the options for last()
  1542. *
  1543. * @return void
  1544. */
  1545. public function testLastOptions() {
  1546. $this->Paginator->request->params['paging'] = array(
  1547. 'Client' => array(
  1548. 'page' => 4,
  1549. 'current' => 3,
  1550. 'count' => 30,
  1551. 'prevPage' => false,
  1552. 'nextPage' => 2,
  1553. 'pageCount' => 15,
  1554. 'sort' => 'Client.name',
  1555. 'direction' => 'DESC',
  1556. )
  1557. );
  1558. $result = $this->Paginator->last();
  1559. $expected = array(
  1560. 'li' => ['class' => 'last'],
  1561. 'a' => array(
  1562. 'href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC',
  1563. ),
  1564. 'last &gt;&gt;', '/a',
  1565. '/li',
  1566. );
  1567. $this->assertTags($result, $expected);
  1568. $result = $this->Paginator->last(1);
  1569. $expected = array(
  1570. '<li',
  1571. array('a' => array('href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC')), '15', '/a',
  1572. '/li',
  1573. );
  1574. $this->assertTags($result, $expected);
  1575. $result = $this->Paginator->last(2);
  1576. $expected = array(
  1577. '<li',
  1578. array('a' => array('href' => '/index?page=14&amp;sort=Client.name&amp;direction=DESC')), '14', '/a',
  1579. '/li',
  1580. '<li',
  1581. array('a' => array('href' => '/index?page=15&amp;sort=Client.name&amp;direction=DESC')), '15', '/a',
  1582. '/li',
  1583. );
  1584. $this->assertTags($result, $expected);
  1585. }
  1586. /**
  1587. * test last() with a the model parameter.
  1588. *
  1589. * @return void
  1590. */
  1591. public function testLastNonDefaultModel() {
  1592. $this->Paginator->request->params['paging']['Article']['page'] = 7;
  1593. $this->Paginator->request->params['paging']['Client'] = array(
  1594. 'page' => 3,
  1595. 'current' => 3,
  1596. 'count' => 13,
  1597. 'prevPage' => false,
  1598. 'nextPage' => true,
  1599. 'pageCount' => 5,
  1600. );
  1601. $result = $this->Paginator->last('last', ['model' => 'Article:']);
  1602. $this->assertEquals('', $result);
  1603. $result = $this->Paginator->last('last', ['model' => 'Client']);
  1604. $expected = array(
  1605. 'li' => array('class' => 'last'),
  1606. 'a' => array('href' => '/index?page=5'),
  1607. 'last',
  1608. '/a',
  1609. '/li'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. }
  1613. /**
  1614. * testCounter method
  1615. *
  1616. * @return void
  1617. */
  1618. public function testCounter() {
  1619. $this->Paginator->request->params['paging'] = array(
  1620. 'Client' => array(
  1621. 'page' => 1,
  1622. 'current' => 3,
  1623. 'count' => 13,
  1624. 'prevPage' => false,
  1625. 'nextPage' => true,
  1626. 'pageCount' => 5,
  1627. 'limit' => 3,
  1628. 'sort' => 'Client.name',
  1629. 'order' => 'DESC',
  1630. )
  1631. );
  1632. $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, ';
  1633. $input .= 'starting on record {{start}}, ending on {{end}}';
  1634. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
  1635. $expected .= 'ending on 3';
  1636. $result = $this->Paginator->counter($input);
  1637. $this->assertEquals($expected, $result);
  1638. $result = $this->Paginator->counter(array('format' => 'pages'));
  1639. $expected = '1 of 5';
  1640. $this->assertEquals($expected, $result);
  1641. $result = $this->Paginator->counter(array('format' => 'range'));
  1642. $expected = '1 - 3 of 13';
  1643. $this->assertEquals($expected, $result);
  1644. $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}');
  1645. $this->assertEquals('Showing 1 of 5 clients', $result);
  1646. }
  1647. /**
  1648. * testHasPage method
  1649. *
  1650. * @return void
  1651. */
  1652. public function testHasPage() {
  1653. $result = $this->Paginator->hasPage('Article', 15);
  1654. $this->assertFalse($result);
  1655. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  1656. $this->assertFalse($result);
  1657. $result = $this->Paginator->hasPage('Article', 2);
  1658. $this->assertTrue($result);
  1659. $result = $this->Paginator->hasPage(2);
  1660. $this->assertTrue($result);
  1661. }
  1662. /**
  1663. * testNextLinkUsingDotNotation method
  1664. *
  1665. * @return void
  1666. */
  1667. public function testNextLinkUsingDotNotation() {
  1668. Router::setRequestInfo(array(
  1669. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array()),
  1670. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  1671. ));
  1672. $this->Paginator->request->params['paging']['Article']['sort'] = 'Article.title';
  1673. $this->Paginator->request->params['paging']['Article']['direction'] = 'asc';
  1674. $this->Paginator->request->params['paging']['Article']['page'] = 1;
  1675. $test = array('url' => array(
  1676. 'page' => '1',
  1677. 'sort' => 'Article.title',
  1678. 'direction' => 'asc',
  1679. ));
  1680. $this->Paginator->options($test);
  1681. $result = $this->Paginator->next('Next');
  1682. $expected = array(
  1683. 'li' => array('class' => 'next'),
  1684. 'a' => array(
  1685. 'href' => '/accounts/index?page=2&amp;sort=Article.title&amp;direction=asc',
  1686. 'rel' => 'next'
  1687. ),
  1688. 'Next',
  1689. '/a',
  1690. '/li',
  1691. );
  1692. $this->assertTags($result, $expected);
  1693. }
  1694. /**
  1695. * test the current() method
  1696. *
  1697. * @return void
  1698. */
  1699. public function testCurrent() {
  1700. $result = $this->Paginator->current();
  1701. $this->assertEquals($this->Paginator->request->params['paging']['Article']['page'], $result);
  1702. $result = $this->Paginator->current('Incorrect');
  1703. $this->assertEquals(1, $result);
  1704. }
  1705. /**
  1706. * test the defaultModel() method
  1707. *
  1708. * @return void
  1709. */
  1710. public function testNoDefaultModel() {
  1711. $this->Paginator->request = new Request();
  1712. $this->assertNull($this->Paginator->defaultModel());
  1713. }
  1714. /**
  1715. * test the numbers() method when there is only one page
  1716. *
  1717. * @return void
  1718. */
  1719. public function testWithOnePage() {
  1720. $this->Paginator->request['paging'] = array(
  1721. 'Article' => array(
  1722. 'page' => 1,
  1723. 'current' => 2,
  1724. 'count' => 2,
  1725. 'prevPage' => false,
  1726. 'nextPage' => true,
  1727. 'pageCount' => 1,
  1728. )
  1729. );
  1730. $this->assertFalse($this->Paginator->numbers());
  1731. $this->assertFalse($this->Paginator->first());
  1732. $this->assertFalse($this->Paginator->last());
  1733. }
  1734. /**
  1735. * test the numbers() method when there is only one page
  1736. *
  1737. * @return void
  1738. */
  1739. public function testWithZeroPages() {
  1740. $this->Paginator->request['paging'] = array(
  1741. 'Article' => array(
  1742. 'page' => 0,
  1743. 'current' => 0,
  1744. 'count' => 0,
  1745. 'prevPage' => false,
  1746. 'nextPage' => false,
  1747. 'pageCount' => 0,
  1748. 'limit' => 10,
  1749. )
  1750. );
  1751. $result = $this->Paginator->counter(array('format' => 'pages'));
  1752. $expected = '0 of 1';
  1753. $this->assertEquals($expected, $result);
  1754. }
  1755. }