PaginatorHelperTest.php 66 KB

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