PaginatorHelperTest.php 67 KB

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