PaginatorHelperTest.php 66 KB

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