PaginatorHelperTest.php 70 KB

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