PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/view/helpers/paginator.test.php

https://github.com/hardsshah/bookmarks
PHP | 848 lines | 587 code | 101 blank | 160 comment | 0 complexity | ea456ecd63f65a2c6ac2d158e54dde33 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * PaginatorHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.view.helpers
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Helper', array('Html', 'Paginator', 'Form', 'Ajax', 'Javascript'));
  28. /**
  29. * PaginatorHelperTest class
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs.view.helpers
  33. */
  34. class PaginatorHelperTest extends CakeTestCase {
  35. /**
  36. * setUp method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function setUp() {
  42. $this->Paginator = new PaginatorHelper();
  43. $this->Paginator->params['paging'] = array(
  44. 'Article' => array(
  45. 'current' => 9,
  46. 'count' => 62,
  47. 'prevPage' => false,
  48. 'nextPage' => true,
  49. 'pageCount' => 7,
  50. 'defaults' => array(
  51. 'order' => 'Article.date ASC',
  52. 'limit' => 9,
  53. 'conditions' => array()
  54. ),
  55. 'options' => array(
  56. 'order' => 'Article.date ASC',
  57. 'limit' => 9,
  58. 'page' => 1,
  59. 'conditions' => array()
  60. )
  61. )
  62. );
  63. $this->Paginator->Html =& new HtmlHelper();
  64. $this->Paginator->Ajax =& new AjaxHelper();
  65. $this->Paginator->Ajax->Html =& new HtmlHelper();
  66. $this->Paginator->Ajax->Javascript =& new JavascriptHelper();
  67. $this->Paginator->Ajax->Form =& new FormHelper();
  68. Configure::write('Routing.admin', '');
  69. Router::reload();
  70. }
  71. /**
  72. * tearDown method
  73. *
  74. * @access public
  75. * @return void
  76. */
  77. function tearDown() {
  78. unset($this->Paginator);
  79. }
  80. /**
  81. * testHasPrevious method
  82. *
  83. * @access public
  84. * @return void
  85. */
  86. function testHasPrevious() {
  87. $this->assertIdentical($this->Paginator->hasPrev(), false);
  88. $this->Paginator->params['paging']['Article']['prevPage'] = true;
  89. $this->assertIdentical($this->Paginator->hasPrev(), true);
  90. $this->Paginator->params['paging']['Article']['prevPage'] = false;
  91. }
  92. /**
  93. * testHasNext method
  94. *
  95. * @access public
  96. * @return void
  97. */
  98. function testHasNext() {
  99. $this->assertIdentical($this->Paginator->hasNext(), true);
  100. $this->Paginator->params['paging']['Article']['nextPage'] = false;
  101. $this->assertIdentical($this->Paginator->hasNext(), false);
  102. $this->Paginator->params['paging']['Article']['nextPage'] = true;
  103. }
  104. /**
  105. * testDisabledLink method
  106. *
  107. * @access public
  108. * @return void
  109. */
  110. function testDisabledLink() {
  111. $this->Paginator->params['paging']['Article']['nextPage'] = false;
  112. $this->Paginator->params['paging']['Article']['page'] = 1;
  113. $result = $this->Paginator->next('Next', array(), true);
  114. $expected = '<div>Next</div>';
  115. $this->assertEqual($result, $expected);
  116. $this->Paginator->params['paging']['Article']['prevPage'] = false;
  117. $result = $this->Paginator->prev('prev', array('update'=> 'theList', 'indicator'=> 'loading', 'url'=> array('controller' => 'posts')), null, array('class' => 'disabled', 'tag' => 'span'));
  118. $expected = '<span class="disabled">prev</span>';
  119. $this->assertEqual($result, $expected);
  120. }
  121. /**
  122. * testSortLinks method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function testSortLinks() {
  128. Router::reload();
  129. Router::parse('/');
  130. Router::setRequestInfo(array(
  131. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/', 'mod_rewrite' => 'true'), 'bare' => 0),
  132. array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/', 'passedArgs' => array())
  133. ));
  134. $this->Paginator->options(array('url' => array('param')));
  135. $result = $this->Paginator->sort('title');
  136. $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:asc">Title<\/a>$/', $result);
  137. $result = $this->Paginator->sort('date');
  138. $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:date\/direction:desc">Date<\/a>$/', $result);
  139. $result = $this->Paginator->numbers(array('modulus'=> '2', 'url'=> array('controller'=>'projects', 'action'=>'sort'),'update'=>'list'));
  140. $this->assertPattern('/\/projects\/sort\/page:2/', $result);
  141. $this->assertPattern('/<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*Event.observe/', $result);
  142. $result = $this->Paginator->sort('TestTitle', 'title');
  143. $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:asc">TestTitle<\/a>$/', $result);
  144. $result = $this->Paginator->sort(array('asc' => 'ascending', 'desc' => 'descending'), 'title');
  145. $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:asc">ascending<\/a>$/', $result);
  146. $this->Paginator->params['paging']['Article']['options']['sort'] = 'title';
  147. $result = $this->Paginator->sort(array('asc' => 'ascending', 'desc' => 'descending'), 'title');
  148. $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:desc">descending<\/a>$/', $result);
  149. }
  150. /**
  151. * testSortLinksUsingDotNotation method
  152. *
  153. * @access public
  154. * @return void
  155. */
  156. function testSortLinksUsingDotNotation() {
  157. Router::reload();
  158. Router::parse('/');
  159. Router::setRequestInfo(array(
  160. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/', 'mod_rewrite' => 'true'), 'bare' => 0),
  161. array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/', 'passedArgs' => array())
  162. ));
  163. $this->Paginator->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
  164. $result = $this->Paginator->sort('Title','Article.title');
  165. $this->assertPattern('/\/accounts\/index\/page:1\/sort:Article.title\/direction:asc">Title<\/a>$/', $result);
  166. $this->Paginator->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
  167. $result = $this->Paginator->sort('Title','Article.title');
  168. $this->assertPattern('/\/accounts\/index\/page:1\/sort:Article.title\/direction:desc">Title<\/a>$/', $result);
  169. $this->Paginator->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
  170. $result = $this->Paginator->sort('title');
  171. $this->assertPattern('/\/accounts\/index\/page:1\/sort:title\/direction:asc">Title<\/a>$/', $result);
  172. }
  173. /**
  174. * testSortKey method
  175. *
  176. * @access public
  177. * @return void
  178. */
  179. function testSortKey() {
  180. $result = $this->Paginator->sortKey(null, array(
  181. 'order' => array('Article.title' => 'desc'
  182. )));
  183. $this->assertEqual('Article.title', $result);
  184. }
  185. /**
  186. * testSortAdminLinks method
  187. *
  188. * @access public
  189. * @return void
  190. */
  191. function testSortAdminLinks() {
  192. Configure::write('Routing.admin', 'admin');
  193. Router::reload();
  194. Router::setRequestInfo(array(
  195. array('pass' => array(), 'named' => array(), 'controller' => 'users', 'plugin' => null, 'action' => 'admin_index', 'prefix' => 'admin', 'admin' => true, 'url' => array('ext' => 'html', 'url' => 'admin/users'), 'form' => array()),
  196. array('base' => '', 'here' => '/admin/users', 'webroot' => '/')
  197. ));
  198. Router::parse('/admin/users');
  199. $this->Paginator->params['paging']['Article']['page'] = 1;
  200. $result = $this->Paginator->next('Next');
  201. $this->assertPattern('/^<a[^<>]+>Next<\/a>$/', $result);
  202. $this->assertPattern('/href="\/admin\/users\/index\/page:2"/', $result);
  203. Router::reload();
  204. Router::setRequestInfo(array(
  205. array('plugin' => null, 'controller' => 'test', 'action' => 'admin_index', 'pass' => array(), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/test')),
  206. array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/admin/test', 'webroot' => '/')
  207. ));
  208. Router::parse('/');
  209. $this->Paginator->options(array('url' => array('param')));
  210. $result = $this->Paginator->sort('title');
  211. $this->assertPattern('/\/admin\/test\/index\/param\/page:1\/sort:title\/direction:asc"\s*>Title<\/a>$/', $result);
  212. $this->Paginator->options(array('url' => array('param')));
  213. $result = $this->Paginator->sort('Title', 'Article.title');
  214. $this->assertPattern('/\/admin\/test\/index\/param\/page:1\/sort:Article.title\/direction:asc"\s*>Title<\/a>$/', $result);
  215. }
  216. /**
  217. * testUrlGeneration method
  218. *
  219. * @access public
  220. * @return void
  221. */
  222. function testUrlGeneration() {
  223. $result = $this->Paginator->sort('controller');
  224. $this->assertPattern('/\/page:1\//', $result);
  225. $this->assertPattern('/\/sort:controller\//', $result);
  226. $result = $this->Paginator->url();
  227. $this->assertEqual($result, '/index/page:1');
  228. $this->Paginator->params['paging']['Article']['options']['page'] = 2;
  229. $result = $this->Paginator->url();
  230. $this->assertEqual($result, '/index/page:2');
  231. $options = array('order' => array('Article' => 'desc'));
  232. $result = $this->Paginator->url($options);
  233. $this->assertEqual($result, '/index/page:2/sort:Article/direction:desc');
  234. $this->Paginator->params['paging']['Article']['options']['page'] = 3;
  235. $options = array('order' => array('Article.name' => 'desc'));
  236. $result = $this->Paginator->url($options);
  237. $this->assertEqual($result, '/index/page:3/sort:Article.name/direction:desc');
  238. }
  239. /**
  240. * test URL generation with prefix routes
  241. *
  242. * @access public
  243. * @return void
  244. */
  245. function testUrlGenerationWithPrefixes() {
  246. $memberPrefixes = array('prefix' => 'members', 'members' => true);
  247. Router::connect('/members/:controller/:action/*', $memberPrefixes);
  248. Router::parse('/');
  249. Router::setRequestInfo( array(
  250. array('controller' => 'posts', 'action' => 'index', 'form' => array(), 'url' => array(), 'plugin' => null),
  251. array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => 'posts/index', 'webroot' => '/')
  252. ));
  253. $this->Paginator->params['paging']['Article']['options']['page'] = 2;
  254. $this->Paginator->params['paging']['Article']['page'] = 2;
  255. $this->Paginator->params['paging']['Article']['prevPage'] = true;
  256. $options = array('members' => true);
  257. $result = $this->Paginator->url($options);
  258. $expected = '/members/posts/index/page:2';
  259. $this->assertEqual($result, $expected);
  260. $result = $this->Paginator->sort('name', null, array('url' => $options));
  261. $expected = '<a href="/members/posts/index/page:2/sort:name/direction:asc">Name</a>';
  262. $this->assertEqual($result, $expected);
  263. $result = $this->Paginator->next('next', array('url' => $options));
  264. $expected = '<a href="/members/posts/index/page:3">next</a>';
  265. $this->assertEqual($result, $expected);
  266. $result = $this->Paginator->prev('prev', array('url' => $options));
  267. $expected = '<a href="/members/posts/index/page:1">prev</a>';
  268. $this->assertEqual($result, $expected);
  269. $options = array('members' => true, 'controller' => 'posts', 'order' => array('name' => 'desc'));
  270. $result = $this->Paginator->url($options);
  271. $expected = '/members/posts/index/page:2/sort:name/direction:desc';
  272. $this->assertEqual($result, $expected);
  273. $options = array('controller' => 'posts', 'order' => array('Article.name' => 'desc'));
  274. $result = $this->Paginator->url($options);
  275. $expected = '/posts/index/page:2/sort:Article.name/direction:desc';
  276. $this->assertEqual($result, $expected);
  277. }
  278. /**
  279. * testOptions method
  280. *
  281. * @access public
  282. * @return void
  283. */
  284. function testOptions() {
  285. $this->Paginator->options('myDiv');
  286. $this->assertEqual('myDiv', $this->Paginator->options['update']);
  287. $this->Paginator->options = array();
  288. $this->Paginator->params = array();
  289. $options = array('paging' => array('Article' => array(
  290. 'order' => 'desc',
  291. 'sort' => 'title'
  292. )));
  293. $this->Paginator->options($options);
  294. $expected = array('Article' => array(
  295. 'order' => 'desc',
  296. 'sort' => 'title'
  297. ));
  298. $this->assertEqual($expected, $this->Paginator->params['paging']);
  299. $this->Paginator->options = array();
  300. $this->Paginator->params = array();
  301. $options = array('Article' => array(
  302. 'order' => 'desc',
  303. 'sort' => 'title'
  304. ));
  305. $this->Paginator->options($options);
  306. $this->assertEqual($expected, $this->Paginator->params['paging']);
  307. $options = array('paging' => array('Article' => array(
  308. 'order' => 'desc',
  309. 'sort' => 'Article.title'
  310. )));
  311. $this->Paginator->options($options);
  312. $expected = array('Article' => array(
  313. 'order' => 'desc',
  314. 'sort' => 'Article.title'
  315. ));
  316. $this->assertEqual($expected, $this->Paginator->params['paging']);
  317. }
  318. /**
  319. * testPagingLinks method
  320. *
  321. * @access public
  322. * @return void
  323. */
  324. function testPagingLinks() {
  325. $this->Paginator->params['paging'] = array('Client' => array(
  326. 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5,
  327. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  328. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  329. );
  330. $result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
  331. $expected = '<div class="disabled">&lt;&lt; Previous</div>';
  332. $this->assertEqual($result, $expected);
  333. $result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled', 'tag' => 'span'));
  334. $expected = '<span class="disabled">&lt;&lt; Previous</span>';
  335. $this->assertEqual($result, $expected);
  336. $this->Paginator->params['paging']['Client']['page'] = 2;
  337. $this->Paginator->params['paging']['Client']['prevPage'] = true;
  338. $result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
  339. $this->assertPattern('/^<a[^<>]+>&lt;&lt; Previous<\/a>$/', $result);
  340. $this->assertPattern('/href="\/index\/page:1"/', $result);
  341. $result = $this->Paginator->next('Next');
  342. $this->assertPattern('/^<a[^<>]+>Next<\/a>$/', $result);
  343. $this->assertPattern('/href="\/index\/page:3"/', $result);
  344. $result = $this->Paginator->prev('<< Previous', array('escape' => true));
  345. $this->assertPattern('/^<a[^<>]+>&lt;&lt; Previous<\/a>$/', $result);
  346. $result = $this->Paginator->prev('<< Previous', array('escape' => false));
  347. $this->assertPattern('/^<a[^<>]+><< Previous<\/a>$/', $result);
  348. $this->Paginator->params['paging'] = array('Client' => array(
  349. 'page' => 1, 'current' => 1, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5,
  350. 'defaults' => array(),
  351. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  352. );
  353. $result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>');
  354. $this->assertPattern('/^<div>&lt;strong&gt;Disabled&lt;\/strong&gt;<\/div>$/', $result);
  355. $result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>', array('escape' => true));
  356. $this->assertPattern('/^<div>&lt;strong&gt;Disabled&lt;\/strong&gt;<\/div>$/', $result);
  357. $result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>', array('escape' => false));
  358. $this->assertPattern('/^<div><strong>Disabled<\/strong><\/div>$/', $result);
  359. $this->Paginator->params['paging'] = array('Client' => array(
  360. 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5,
  361. 'defaults' => array(),
  362. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  363. );
  364. $this->Paginator->params['paging']['Client']['page'] = 2;
  365. $this->Paginator->params['paging']['Client']['prevPage'] = true;
  366. $result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
  367. $this->assertPattern('/\/sort:Client.name\/direction:DESC"/', $result);
  368. $result = $this->Paginator->next('Next');
  369. $this->assertPattern('/\/sort:Client.name\/direction:DESC"/', $result);
  370. $this->Paginator->params['paging'] = array('Client' => array(
  371. 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true, 'nextPage' => false, 'pageCount' => 2,
  372. 'defaults' => array(),
  373. 'options' => array('page' => 2, 'limit' => 10, 'order' => array(), 'conditions' => array())
  374. ));
  375. $result = $this->Paginator->prev('Prev');
  376. $expected = array(
  377. 'a' => array('href' => '/index/page:1/limit:10'),
  378. 'Prev',
  379. '/a',
  380. );
  381. $this->assertTags($result, $expected);
  382. }
  383. /**
  384. * testPagingLinksNotDefaultModel
  385. *
  386. * Test the creation of paging links when the non default model is used.
  387. *
  388. * @access public
  389. * @return void
  390. */
  391. function testPagingLinksNotDefaultModel() {
  392. // Multiple Model Paginate
  393. $this->Paginator->params['paging'] = array(
  394. 'Client' => array(
  395. 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5,
  396. 'defaults' => array( 'limit'=>3, 'order' => array('Client.name' => 'DESC')),
  397. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array())
  398. ),
  399. 'Server' => array(
  400. 'page' => 1, 'current' => 1, 'count' => 5, 'prevPage' => false, 'nextPage' => false, 'pageCount' => 5,
  401. 'defaults' => array(),
  402. 'options' => array('page' => 1, 'limit' => 5, 'order' => array('Server.name' => 'ASC'), 'conditions' => array())
  403. )
  404. );
  405. $result = $this->Paginator->next('Next', array('model' => 'Client'));
  406. $this->assertPattern('/^<a[^<>]+>Next<\/a>$/', $result);
  407. $this->assertPattern('/href="\/index\/page:2"/', $result); // These is passed.
  408. $result = $this->Paginator->next('Next', array('model' => 'Server'), 'No Next', array('model' => 'Server'));
  409. $this->assertPattern('/^<div>No Next<\/div>$/', $result);
  410. }
  411. /**
  412. * testGenericLinks method
  413. *
  414. * @access public
  415. * @return void
  416. */
  417. function testGenericLinks() {
  418. $result = $this->Paginator->link('Sort by title on page 5', array('sort' => 'title', 'page' => 5, 'direction' => 'desc'));
  419. $this->assertPattern('/^<a href=".+"[^<>]*>Sort by title on page 5<\/a>$/', $result);
  420. $this->assertPattern('/\/page:5/', $result);
  421. $this->assertPattern('/\/sort:title/', $result);
  422. $this->assertPattern('/\/direction:desc/', $result);
  423. $this->Paginator->params['paging']['Article']['options']['page'] = 2;
  424. $result = $this->Paginator->link('Sort by title', array('sort' => 'title', 'direction' => 'desc'));
  425. $this->assertPattern('/^<a href=".+"[^<>]*>Sort by title<\/a>$/', $result);
  426. $this->assertPattern('/\/page:2/', $result);
  427. $this->assertPattern('/\/sort:title/', $result);
  428. $this->assertPattern('/\/direction:desc/', $result);
  429. $this->Paginator->params['paging']['Article']['options']['page'] = 4;
  430. $result = $this->Paginator->link('Sort by title on page 4', array('sort' => 'Article.title', 'direction' => 'desc'));
  431. $this->assertPattern('/^<a href=".+"[^<>]*>Sort by title on page 4<\/a>$/', $result);
  432. $this->assertPattern('/\/page:4/', $result);
  433. $this->assertPattern('/\/sort:Article.title/', $result);
  434. $this->assertPattern('/\/direction:desc/', $result);
  435. }
  436. /**
  437. * Tests generation of generic links with preset options
  438. *
  439. * @access public
  440. * @return void
  441. */
  442. function testGenericLinksWithPresetOptions() {
  443. $result = $this->Paginator->link('Foo!', array('page' => 1));
  444. $this->assertTags($result, array('a' => array('href' => '/index/page:1'), 'Foo!', '/a'));
  445. $this->Paginator->options(array('sort' => 'title', 'direction' => 'desc'));
  446. $result = $this->Paginator->link('Foo!', array('page' => 1));
  447. $this->assertTags($result, array(
  448. 'a' => array(
  449. 'href' => '/index/page:1',
  450. 'sort' => 'title',
  451. 'direction' => 'desc'
  452. ),
  453. 'Foo!',
  454. '/a'
  455. ));
  456. $this->Paginator->options(array('sort' => null, 'direction' => null));
  457. $result = $this->Paginator->link('Foo!', array('page' => 1));
  458. $this->assertTags($result, array('a' => array('href' => '/index/page:1'), 'Foo!', '/a'));
  459. $this->Paginator->options(array('url' => array(
  460. 'sort' => 'title',
  461. 'direction' => 'desc'
  462. )));
  463. $result = $this->Paginator->link('Foo!', array('page' => 1));
  464. $this->assertTags($result, array(
  465. 'a' => array('href' => '/index/page:1/sort:title/direction:desc'),
  466. 'Foo!',
  467. '/a'
  468. ));
  469. }
  470. /**
  471. * testNumbers method
  472. *
  473. * @access public
  474. * @return void
  475. */
  476. function testNumbers() {
  477. $this->Paginator->params['paging'] = array('Client' => array(
  478. 'page' => 8, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  479. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  480. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  481. );
  482. $result = $this->Paginator->numbers();
  483. $expected = '<span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span class="current">8</span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span>';
  484. $this->assertEqual($result, $expected);
  485. $result = $this->Paginator->numbers(array('tag' => 'li'));
  486. $expected = '<li><a href="/index/page:4">4</a></li> | <li><a href="/index/page:5">5</a></li> | <li><a href="/index/page:6">6</a></li> | <li><a href="/index/page:7">7</a></li> | <li class="current">8</li> | <li><a href="/index/page:9">9</a></li> | <li><a href="/index/page:10">10</a></li> | <li><a href="/index/page:11">11</a></li> | <li><a href="/index/page:12">12</a></li>';
  487. $this->assertEqual($result, $expected);
  488. $result = $this->Paginator->numbers(array('tag' => 'li', 'separator' => false));
  489. $expected = '<li><a href="/index/page:4">4</a></li><li><a href="/index/page:5">5</a></li><li><a href="/index/page:6">6</a></li><li><a href="/index/page:7">7</a></li><li class="current">8</li><li><a href="/index/page:9">9</a></li><li><a href="/index/page:10">10</a></li><li><a href="/index/page:11">11</a></li><li><a href="/index/page:12">12</a></li>';
  490. $this->assertEqual($result, $expected);
  491. $result = $this->Paginator->numbers(true);
  492. $expected = '<span><a href="/index/page:1">first</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span class="current">8</span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span> | <span><a href="/index/page:15">last</a></span>';
  493. $this->assertEqual($result, $expected);
  494. $this->Paginator->params['paging'] = array('Client' => array(
  495. 'page' => 1, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  496. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  497. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  498. );
  499. $result = $this->Paginator->numbers();
  500. $expected = '<span class="current">1</span> | <span><a href="/index/page:2">2</a></span> | <span><a href="/index/page:3">3</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span>';
  501. $this->assertEqual($result, $expected);
  502. $this->Paginator->params['paging'] = array('Client' => array(
  503. 'page' => 14, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  504. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  505. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  506. );
  507. $result = $this->Paginator->numbers();
  508. $expected = '<span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span> | <span><a href="/index/page:13">13</a></span> | <span class="current">14</span> | <span><a href="/index/page:15">15</a></span>';
  509. $this->assertEqual($result, $expected);
  510. $this->Paginator->params['paging'] = array('Client' => array(
  511. 'page' => 2, 'current' => 3, 'count' => 27, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 9,
  512. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  513. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  514. );
  515. $result = $this->Paginator->numbers(array('first' => 1));
  516. $expected = '<span><a href="/index/page:1">1</a></span> | <span class="current">2</span> | <span><a href="/index/page:3">3</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span>';
  517. $this->assertEqual($result, $expected);
  518. $result = $this->Paginator->numbers(array('last' => 1));
  519. $expected = '<span><a href="/index/page:1">1</a></span> | <span class="current">2</span> | <span><a href="/index/page:3">3</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span>';
  520. $this->assertEqual($result, $expected);
  521. $this->Paginator->params['paging'] = array('Client' => array(
  522. 'page' => 15, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  523. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  524. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  525. );
  526. $result = $this->Paginator->numbers(array('first' => 1));
  527. $expected = '<span><a href="/index/page:1">1</a></span>...<span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span> | <span><a href="/index/page:13">13</a></span> | <span><a href="/index/page:14">14</a></span> | <span class="current">15</span>';
  528. $this->assertEqual($result, $expected);
  529. $this->Paginator->params['paging'] = array('Client' => array(
  530. 'page' => 10, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  531. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  532. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  533. );
  534. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  535. $expected = '<span><a href="/index/page:1">1</a></span>...<span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span> | <span class="current">10</span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span> | <span><a href="/index/page:13">13</a></span> | <span><a href="/index/page:14">14</a></span> | <span><a href="/index/page:15">15</a></span>';
  536. $this->assertEqual($result, $expected);
  537. $this->Paginator->params['paging'] = array('Client' => array(
  538. 'page' => 6, 'current' => 15, 'count' => 623, 'prevPage' => 1, 'nextPage' => 1, 'pageCount' => 42,
  539. 'defaults' => array('limit' => 15, 'step' => 1, 'page' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  540. 'options' => array('page' => 6, 'limit' => 15, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  541. );
  542. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  543. $expected = '<span><a href="/index/page:1">1</a></span> | <span><a href="/index/page:2">2</a></span> | <span><a href="/index/page:3">3</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span class="current">6</span> | <span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span>...<span><a href="/index/page:42">42</a></span>';
  544. $this->assertEqual($result, $expected);
  545. $this->Paginator->params['paging'] = array('Client' => array(
  546. 'page' => 37, 'current' => 15, 'count' => 623, 'prevPage' => 1, 'nextPage' => 1, 'pageCount' => 42,
  547. 'defaults' => array('limit' => 15, 'step' => 1, 'page' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  548. 'options' => array('page' => 37, 'limit' => 15, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  549. );
  550. $result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
  551. $expected = '<span><a href="/index/page:1">1</a></span>...<span><a href="/index/page:33">33</a></span> | <span><a href="/index/page:34">34</a></span> | <span><a href="/index/page:35">35</a></span> | <span><a href="/index/page:36">36</a></span> | <span class="current">37</span> | <span><a href="/index/page:38">38</a></span> | <span><a href="/index/page:39">39</a></span> | <span><a href="/index/page:40">40</a></span> | <span><a href="/index/page:41">41</a></span> | <span><a href="/index/page:42">42</a></span>';
  552. $this->assertEqual($result, $expected);
  553. $this->Paginator->params['paging'] = array(
  554. 'Client' => array(
  555. 'page' => 1,
  556. 'current' => 10,
  557. 'count' => 30,
  558. 'prevPage' => false,
  559. 'nextPage' => 2,
  560. 'pageCount' => 3,
  561. 'defaults' => array(
  562. 'limit' => 3,
  563. 'step' => 1,
  564. 'order' => array('Client.name' => 'DESC'),
  565. 'conditions' => array()
  566. ),
  567. 'options' => array(
  568. 'page' => 1,
  569. 'limit' => 3,
  570. 'order' => array('Client.name' => 'DESC'),
  571. 'conditions' => array()
  572. )
  573. )
  574. );
  575. $options = array('modulus' => 10);
  576. $result = $this->Paginator->numbers($options);
  577. $expected = '<span class="current">1</span> | <span><a href="/index/page:2">2</a></span> | <span><a href="/index/page:3">3</a></span>';
  578. $this->assertEqual($result, $expected);
  579. $this->Paginator->params['paging'] = array('Client' => array(
  580. 'page' => 2, 'current' => 10, 'count' => 31, 'prevPage' => true, 'nextPage' => true, 'pageCount' => 4,
  581. 'defaults' => array('limit' => 10),
  582. 'options' => array('page' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  583. );
  584. $result = $this->Paginator->numbers();
  585. $expected = '<span><a href="/index/page:1/sort:Client.name/direction:DESC">1</a></span> | <span class="current">2</span> | <span><a href="/index/page:3/sort:Client.name/direction:DESC">3</a></span> | <span><a href="/index/page:4/sort:Client.name/direction:DESC">4</a></span>';
  586. $this->assertEqual($result, $expected);
  587. }
  588. /**
  589. * testFirstAndLast method
  590. *
  591. * @access public
  592. * @return void
  593. */
  594. function testFirstAndLast() {
  595. $this->Paginator->params['paging'] = array('Client' => array(
  596. 'page' => 1, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  597. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  598. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  599. );
  600. $result = $this->Paginator->first();
  601. $expected = '';
  602. $this->assertEqual($result, $expected);
  603. $this->Paginator->params['paging'] = array('Client' => array(
  604. 'page' => 4, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  605. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  606. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  607. );
  608. $result = $this->Paginator->first();
  609. $expected = '<span><a href="/index/page:1">&lt;&lt; first</a></span>';
  610. $this->assertEqual($result, $expected);
  611. $result = $this->Paginator->first('<<', array('tag' => 'li'));
  612. $expected = '<li><a href="/index/page:1">&lt;&lt;</a></li>';
  613. $this->assertEqual($result, $expected);
  614. $result = $this->Paginator->last();
  615. $expected = '<span><a href="/index/page:15">last &gt;&gt;</a></span>';
  616. $this->assertEqual($result, $expected);
  617. $result = $this->Paginator->last(1);
  618. $expected = '...<span><a href="/index/page:15">15</a></span>';
  619. $this->assertEqual($result, $expected);
  620. $result = $this->Paginator->last(2);
  621. $expected = '...<span><a href="/index/page:14">14</a></span> | <span><a href="/index/page:15">15</a></span>';
  622. $this->assertEqual($result, $expected);
  623. $result = $this->Paginator->last(2, array('tag' => 'li'));
  624. $expected = '...<li><a href="/index/page:14">14</a></li> | <li><a href="/index/page:15">15</a></li>';
  625. $this->assertEqual($result, $expected);
  626. $this->Paginator->params['paging'] = array('Client' => array(
  627. 'page' => 15, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  628. 'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
  629. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  630. );
  631. $result = $this->Paginator->last();
  632. $expected = '';
  633. $this->assertEqual($result, $expected);
  634. $this->Paginator->params['paging'] = array('Client' => array(
  635. 'page' => 4, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
  636. 'defaults' => array('limit' => 3),
  637. 'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
  638. );
  639. $result = $this->Paginator->first();
  640. $expected = '<span><a href="/index/page:1/sort:Client.name/direction:DESC">&lt;&lt; first</a></span>';
  641. $this->assertEqual($result, $expected);
  642. $result = $this->Paginator->last();
  643. $expected = '<span><a href="/index/page:15/sort:Client.name/direction:DESC">last &gt;&gt;</a></span>';
  644. $this->assertEqual($result, $expected);
  645. $result = $this->Paginator->last(1);
  646. $expected = '...<span><a href="/index/page:15/sort:Client.name/direction:DESC">15</a></span>';
  647. $this->assertEqual($result, $expected);
  648. $result = $this->Paginator->last(2);
  649. $expected = '...<span><a href="/index/page:14/sort:Client.name/direction:DESC">14</a></span> | <span><a href="/index/page:15/sort:Client.name/direction:DESC">15</a></span>';
  650. $this->assertEqual($result, $expected);
  651. }
  652. /**
  653. * testCounter method
  654. *
  655. * @access public
  656. * @return void
  657. */
  658. function testCounter() {
  659. $this->Paginator->params['paging'] = array(
  660. 'Client' => array(
  661. 'page' => 1,
  662. 'current' => 3,
  663. 'count' => 13,
  664. 'prevPage' => false,
  665. 'nextPage' => true,
  666. 'pageCount' => 5,
  667. 'defaults' => array(
  668. 'limit' => 3,
  669. 'step' => 1,
  670. 'order' => array('Client.name' => 'DESC'),
  671. 'conditions' => array()
  672. ),
  673. 'options' => array(
  674. 'page' => 1,
  675. 'limit' => 3,
  676. 'order' => array('Client.name' => 'DESC'),
  677. 'conditions' => array(),
  678. 'separator' => 'of'
  679. ),
  680. )
  681. );
  682. $input = 'Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%';
  683. $result = $this->Paginator->counter($input);
  684. $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ending on 3';
  685. $this->assertEqual($result, $expected);
  686. $input = 'Page %page% of %pages%';
  687. $result = $this->Paginator->counter($input);
  688. $expected = 'Page 1 of 5';
  689. $this->assertEqual($result, $expected);
  690. $result = $this->Paginator->counter(array('format' => $input));
  691. $expected = 'Page 1 of 5';
  692. $this->assertEqual($result, $expected);
  693. $result = $this->Paginator->counter(array('format' => 'pages'));
  694. $expected = '1 of 5';
  695. $this->assertEqual($result, $expected);
  696. $result = $this->Paginator->counter(array('format' => 'range'));
  697. $expected = '1 - 3 of 13';
  698. $this->assertEqual($result, $expected);
  699. }
  700. /**
  701. * testHasPage method
  702. *
  703. * @access public
  704. * @return void
  705. */
  706. function testHasPage() {
  707. $result = $this->Paginator->hasPage('Article', 15);
  708. $this->assertFalse($result);
  709. $result = $this->Paginator->hasPage('UndefinedModel', 2);
  710. $this->assertFalse($result);
  711. $result = $this->Paginator->hasPage('Article', 2);
  712. $this->assertTrue($result);
  713. $result = $this->Paginator->hasPage(2);
  714. $this->assertTrue($result);
  715. }
  716. /**
  717. * testWithPlugin method
  718. *
  719. * @access public
  720. * @return void
  721. */
  722. function testWithPlugin() {
  723. Router::reload();
  724. Router::setRequestInfo(array(
  725. array(
  726. 'pass' => array(), 'named' => array(), 'prefix' => null, 'form' => array(),
  727. 'controller' => 'magazines', 'plugin' => 'my_plugin', 'action' => 'index',
  728. 'url' => array('ext' => 'html', 'url' => 'my_plugin/magazines')),
  729. array('base' => '', 'here' => '/my_plugin/magazines', 'webroot' => '/')
  730. ));
  731. $result = $this->Paginator->link('Page 3', array('page' => 3));
  732. $this->assertPattern('/["\']\/my_plugin\/magazines\/index\/page:3["\']/', $result);
  733. $this->Paginator->options(array('url' => array('action' => 'another_index')));
  734. $result = $this->Paginator->link('Page 3', array('page' => 3));
  735. $this->assertPattern('/["\']\/my_plugin\/magazines\/another_index\/page:3["\']/', $result);
  736. $this->Paginator->options(array('url' => array('controller' => 'issues')));
  737. $result = $this->Paginator->link('Page 3', array('page' => 3));
  738. $this->assertPattern('/["\']\/my_plugin\/issues\/index\/page:3["\']/', $result);
  739. $this->Paginator->options(array('url' => array('plugin' => null)));
  740. $result = $this->Paginator->link('Page 3', array('page' => 3));
  741. $this->assertPattern('/["\']\/magazines\/index\/page:3["\']/', $result);
  742. $this->Paginator->options(array('url' => array('plugin' => null, 'controller' => 'issues')));
  743. $result = $this->Paginator->link('Page 3', array('page' => 3));
  744. $this->assertPattern('/["\']\/issues\/index\/page:3["\']/', $result);
  745. }
  746. }
  747. ?>