PaginatorComponentTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. <?php
  2. /**
  3. * PaginatorComponentTest file
  4. *
  5. * Series of tests for paginator component.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  17. * @package Cake.Test.Case.Controller.Component
  18. * @since CakePHP(tm) v 2.0
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Controller', 'Controller');
  22. App::uses('PaginatorComponent', 'Controller/Component');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('CakeResponse', 'Network');
  25. /**
  26. * PaginatorTestController class
  27. *
  28. * @package Cake.Test.Case.Controller.Component
  29. */
  30. class PaginatorTestController extends Controller {
  31. /**
  32. * name property
  33. *
  34. * @var string 'PaginatorTest'
  35. */
  36. public $name = 'PaginatorTest';
  37. /**
  38. * components property
  39. *
  40. * @var array
  41. */
  42. public $components = array('Paginator');
  43. }
  44. /**
  45. * PaginatorControllerPost class
  46. *
  47. * @package Cake.Test.Case.Controller.Component
  48. */
  49. class PaginatorControllerPost extends CakeTestModel {
  50. /**
  51. * name property
  52. *
  53. * @var string 'PaginatorControllerPost'
  54. */
  55. public $name = 'PaginatorControllerPost';
  56. /**
  57. * useTable property
  58. *
  59. * @var string 'posts'
  60. */
  61. public $useTable = 'posts';
  62. /**
  63. * invalidFields property
  64. *
  65. * @var array
  66. */
  67. public $invalidFields = array('name' => 'error_msg');
  68. /**
  69. * lastQueries property
  70. *
  71. * @var array
  72. */
  73. public $lastQueries = array();
  74. /**
  75. * belongsTo property
  76. *
  77. * @var array
  78. */
  79. public $belongsTo = array('PaginatorAuthor' => array('foreignKey' => 'author_id'));
  80. /**
  81. * beforeFind method
  82. *
  83. * @param mixed $query
  84. * @return void
  85. */
  86. public function beforeFind($query) {
  87. array_unshift($this->lastQueries, $query);
  88. }
  89. /**
  90. * find method
  91. *
  92. * @param mixed $type
  93. * @param array $options
  94. * @return void
  95. */
  96. public function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
  97. if ($conditions == 'popular') {
  98. $conditions = array($this->name . '.' . $this->primaryKey . ' > ' => '1');
  99. $options = Hash::merge($fields, compact('conditions'));
  100. return parent::find('all', $options);
  101. }
  102. return parent::find($conditions, $fields);
  103. }
  104. }
  105. /**
  106. * ControllerPaginateModel class
  107. *
  108. * @package Cake.Test.Case.Controller.Component
  109. */
  110. class ControllerPaginateModel extends CakeTestModel {
  111. /**
  112. * name property
  113. *
  114. * @var string 'ControllerPaginateModel'
  115. */
  116. public $name = 'ControllerPaginateModel';
  117. /**
  118. * useTable property
  119. *
  120. * @var string 'comments'
  121. */
  122. public $useTable = 'comments';
  123. /**
  124. * paginate method
  125. *
  126. * @return void
  127. */
  128. public function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
  129. $this->extra = $extra;
  130. }
  131. /**
  132. * paginateCount
  133. *
  134. * @return void
  135. */
  136. public function paginateCount($conditions, $recursive, $extra) {
  137. $this->extraCount = $extra;
  138. }
  139. }
  140. /**
  141. * PaginatorControllerComment class
  142. *
  143. * @package Cake.Test.Case.Controller.Component
  144. */
  145. class PaginatorControllerComment extends CakeTestModel {
  146. /**
  147. * name property
  148. *
  149. * @var string 'Comment'
  150. */
  151. public $name = 'Comment';
  152. /**
  153. * useTable property
  154. *
  155. * @var string 'comments'
  156. */
  157. public $useTable = 'comments';
  158. /**
  159. * alias property
  160. *
  161. * @var string 'PaginatorControllerComment'
  162. */
  163. public $alias = 'PaginatorControllerComment';
  164. }
  165. /**
  166. * PaginatorAuthor class
  167. *
  168. * @package Cake.Test.Case.Controller.Component
  169. */
  170. class PaginatorAuthor extends CakeTestModel {
  171. /**
  172. * name property
  173. *
  174. * @var string 'PaginatorAuthor'
  175. */
  176. public $name = 'PaginatorAuthor';
  177. /**
  178. * useTable property
  179. *
  180. * @var string 'authors'
  181. */
  182. public $useTable = 'authors';
  183. /**
  184. * alias property
  185. *
  186. * @var string 'PaginatorAuthor'
  187. */
  188. public $alias = 'PaginatorAuthor';
  189. /**
  190. * alias property
  191. *
  192. * @var string 'PaginatorAuthor'
  193. */
  194. public $virtualFields = array(
  195. 'joined_offset' => 'PaginatorAuthor.id + 1'
  196. );
  197. }
  198. /**
  199. * PaginatorCustomPost class
  200. *
  201. * @package Cake.Test.Case.Controller.Component
  202. */
  203. class PaginatorCustomPost extends CakeTestModel {
  204. /**
  205. * useTable property
  206. *
  207. * @var string
  208. */
  209. public $useTable = 'posts';
  210. /**
  211. * belongsTo property
  212. *
  213. * @var string
  214. */
  215. public $belongsTo = array('Author');
  216. /**
  217. * findMethods property
  218. *
  219. * @var array
  220. */
  221. public $findMethods = array(
  222. 'published' => true,
  223. 'totals' => true,
  224. 'totalsOperation' => true
  225. );
  226. /**
  227. * _findPublished custom find
  228. *
  229. * @return array
  230. */
  231. protected function _findPublished($state, $query, $results = array()) {
  232. if ($state === 'before') {
  233. $query['conditions']['published'] = 'Y';
  234. return $query;
  235. }
  236. return $results;
  237. }
  238. /**
  239. * _findTotals custom find
  240. *
  241. * @return array
  242. */
  243. protected function _findTotals($state, $query, $results = array()) {
  244. if ($state == 'before') {
  245. $query['fields'] = array('author_id');
  246. $this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
  247. $query['fields'][] = 'total_posts';
  248. $query['group'] = array('author_id');
  249. $query['order'] = array('author_id' => 'ASC');
  250. return $query;
  251. }
  252. $this->virtualFields = array();
  253. return $results;
  254. }
  255. /**
  256. * _findTotalsOperation custom find
  257. *
  258. * @return array
  259. */
  260. protected function _findTotalsOperation($state, $query, $results = array()) {
  261. if ($state == 'before') {
  262. if (!empty($query['operation']) && $query['operation'] === 'count') {
  263. unset($query['limit']);
  264. $query['recursive'] = -1;
  265. $query['fields'] = array('COUNT(DISTINCT author_id) AS count');
  266. return $query;
  267. }
  268. $query['recursive'] = 0;
  269. $query['callbacks'] = 'before';
  270. $query['fields'] = array('author_id', 'Author.user');
  271. $this->virtualFields['total_posts'] = "COUNT({$this->alias}.id)";
  272. $query['fields'][] = 'total_posts';
  273. $query['group'] = array('author_id', 'Author.user');
  274. $query['order'] = array('author_id' => 'ASC');
  275. return $query;
  276. }
  277. $this->virtualFields = array();
  278. return $results;
  279. }
  280. }
  281. class PaginatorComponentTest extends CakeTestCase {
  282. /**
  283. * fixtures property
  284. *
  285. * @var array
  286. */
  287. public $fixtures = array('core.post', 'core.comment', 'core.author');
  288. /**
  289. * setup
  290. *
  291. * @return void
  292. */
  293. public function setUp() {
  294. parent::setUp();
  295. $this->request = new CakeRequest('controller_posts/index');
  296. $this->request->params['pass'] = $this->request->params['named'] = array();
  297. $this->Controller = new Controller($this->request);
  298. $this->Paginator = new PaginatorComponent($this->getMock('ComponentCollection'), array());
  299. $this->Paginator->Controller = $this->Controller;
  300. $this->Controller->Post = $this->getMock('Model');
  301. $this->Controller->Post->alias = 'Post';
  302. }
  303. /**
  304. * testPaginate method
  305. *
  306. * @return void
  307. */
  308. public function testPaginate() {
  309. $Controller = new PaginatorTestController($this->request);
  310. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  311. $Controller->request->params['pass'] = array('1');
  312. $Controller->request->query = array();
  313. $Controller->constructClasses();
  314. $Controller->Paginator->settings = array(
  315. 'order' => array('PaginatorControllerComment.id' => 'ASC')
  316. );
  317. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
  318. $this->assertEquals(array(1, 2, 3, 4, 5, 6), $results);
  319. $Controller->Paginator->settings = array(
  320. 'order' => array('PaginatorControllerPost.id' => 'ASC')
  321. );
  322. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  323. $this->assertEquals(array(1, 2, 3), $results);
  324. $Controller->modelClass = null;
  325. $Controller->uses[0] = 'Plugin.PaginatorControllerPost';
  326. $results = Hash::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
  327. $this->assertEquals(array(1, 2, 3), $results);
  328. $Controller->request->params['named'] = array('page' => '-1');
  329. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  330. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  331. $this->assertEquals(array(1, 2, 3), $results);
  332. $Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
  333. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  334. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  335. $this->assertEquals(array(1, 2, 3), $results);
  336. $Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
  337. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  338. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  339. $this->assertEquals(array(3, 2, 1), $results);
  340. $Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
  341. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  342. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  343. $this->assertEquals(array(3, 2, 1), $results);
  344. $Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
  345. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  346. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page'], 'Invalid field in query %s');
  347. $this->assertEquals(array(1, 2, 3), $results);
  348. $Controller->request->params['named'] = array(
  349. 'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
  350. );
  351. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  352. $this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0]);
  353. $this->assertEquals(array(1, 3, 2), $results);
  354. $Controller->request->params['named'] = array();
  355. $Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10, 'paramType' => 'named');
  356. $Controller->Paginator->paginate('PaginatorControllerPost');
  357. $this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  358. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
  359. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
  360. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
  361. $Controller->request->params['named'] = array();
  362. $Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10, 'paramType' => 'named');
  363. $Controller->Paginator->paginate('PaginatorControllerPost');
  364. $this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  365. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
  366. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
  367. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
  368. $Controller->request->params['named'] = array();
  369. $Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10, 'paramType' => 'named');
  370. $Controller->Paginator->paginate('PaginatorControllerPost');
  371. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['limit'], 1);
  372. $this->assertSame(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  373. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
  374. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['prevPage'], false);
  375. $this->assertSame($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
  376. $Controller->Paginator->settings = array('conditions' => array('PaginatorAuthor.user' => 'mariano'));
  377. $Controller->Paginator->paginate('PaginatorControllerPost');
  378. $this->assertSame(2, $Controller->params['paging']['PaginatorControllerPost']['count']);
  379. }
  380. /**
  381. * Test that non-numeric values are rejected for page, and limit
  382. *
  383. * @return void
  384. */
  385. public function testPageParamCasting() {
  386. $this->Controller->Post->expects($this->at(0))
  387. ->method('hasMethod')
  388. ->with('paginate')
  389. ->will($this->returnValue(false));
  390. $this->Controller->Post->expects($this->at(1))
  391. ->method('find')
  392. ->will($this->returnValue(array('stuff')));
  393. $this->Controller->Post->expects($this->at(2))
  394. ->method('hasMethod')
  395. ->with('paginateCount')
  396. ->will($this->returnValue(false));
  397. $this->Controller->Post->expects($this->at(3))
  398. ->method('find')
  399. ->will($this->returnValue(2));
  400. $this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
  401. $this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
  402. $this->Paginator->paginate('Post');
  403. $this->assertSame(1, $this->request->params['paging']['Post']['page'], 'XSS exploit opened');
  404. }
  405. /**
  406. * testPaginateExtraParams method
  407. *
  408. * @return void
  409. */
  410. public function testPaginateExtraParams() {
  411. $Controller = new PaginatorTestController($this->request);
  412. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  413. $Controller->request->params['pass'] = array('1');
  414. $Controller->params['url'] = array();
  415. $Controller->constructClasses();
  416. $Controller->request->params['named'] = array('page' => '-1', 'contain' => array('PaginatorControllerComment'));
  417. $Controller->Paginator->settings = array(
  418. 'order' => array('PaginatorControllerPost.id' => 'ASC')
  419. );
  420. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  421. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  422. $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  423. $this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
  424. $Controller->request->params['named'] = array('page' => '-1');
  425. $Controller->Paginator->settings = array(
  426. 'PaginatorControllerPost' => array(
  427. 'contain' => array('PaginatorControllerComment'),
  428. 'maxLimit' => 10,
  429. 'paramType' => 'named'
  430. ),
  431. );
  432. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  433. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  434. $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  435. $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
  436. $Controller->Paginator->settings = array(
  437. 'PaginatorControllerPost' => array(
  438. 'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
  439. ),
  440. );
  441. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  442. $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  443. $this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']);
  444. $Controller->request->params['named'] = array('limit' => 12);
  445. $Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
  446. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  447. $paging = $Controller->params['paging']['PaginatorControllerPost'];
  448. $this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[1]['limit']);
  449. $this->assertEquals(12, $paging['options']['limit']);
  450. $Controller = new PaginatorTestController($this->request);
  451. $Controller->uses = array('ControllerPaginateModel');
  452. $Controller->request->query = array();
  453. $Controller->constructClasses();
  454. $Controller->Paginator->settings = array(
  455. 'ControllerPaginateModel' => array(
  456. 'contain' => array('ControllerPaginateModel'),
  457. 'group' => 'Comment.author_id',
  458. 'maxLimit' => 10,
  459. 'paramType' => 'named'
  460. )
  461. );
  462. $result = $Controller->Paginator->paginate('ControllerPaginateModel');
  463. $expected = array(
  464. 'contain' => array('ControllerPaginateModel'),
  465. 'group' => 'Comment.author_id',
  466. 'maxLimit' => 10,
  467. 'paramType' => 'named'
  468. );
  469. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
  470. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
  471. $Controller->Paginator->settings = array(
  472. 'ControllerPaginateModel' => array(
  473. 'foo', 'contain' => array('ControllerPaginateModel'),
  474. 'group' => 'Comment.author_id',
  475. 'maxLimit' => 10,
  476. 'paramType' => 'named'
  477. )
  478. );
  479. $Controller->Paginator->paginate('ControllerPaginateModel');
  480. $expected = array(
  481. 'contain' => array('ControllerPaginateModel'),
  482. 'group' => 'Comment.author_id',
  483. 'type' => 'foo',
  484. 'maxLimit' => 10,
  485. 'paramType' => 'named'
  486. );
  487. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
  488. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
  489. }
  490. /**
  491. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  492. *
  493. * @return void
  494. */
  495. public function testPaginateSpecialType() {
  496. $Controller = new PaginatorTestController($this->request);
  497. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  498. $Controller->passedArgs[] = '1';
  499. $Controller->params['url'] = array();
  500. $Controller->constructClasses();
  501. $Controller->Paginator->settings = array(
  502. 'PaginatorControllerPost' => array(
  503. 'popular',
  504. 'fields' => array('id', 'title'),
  505. 'maxLimit' => 10,
  506. 'paramType' => 'named'
  507. )
  508. );
  509. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  510. $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  511. $this->assertEquals(
  512. $Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
  513. array('PaginatorControllerPost.id > ' => '1')
  514. );
  515. $this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
  516. }
  517. /**
  518. * testDefaultPaginateParams method
  519. *
  520. * @return void
  521. */
  522. public function testDefaultPaginateParams() {
  523. $Controller = new PaginatorTestController($this->request);
  524. $Controller->modelClass = 'PaginatorControllerPost';
  525. $Controller->params['url'] = array();
  526. $Controller->constructClasses();
  527. $Controller->Paginator->settings = array(
  528. 'order' => 'PaginatorControllerPost.id DESC',
  529. 'maxLimit' => 10,
  530. 'paramType' => 'named'
  531. );
  532. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  533. $this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
  534. $this->assertEquals(array(3, 2, 1), $results);
  535. }
  536. /**
  537. * test paginate() and virtualField interactions
  538. *
  539. * @return void
  540. */
  541. public function testPaginateOrderVirtualField() {
  542. $Controller = new PaginatorTestController($this->request);
  543. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  544. $Controller->params['url'] = array();
  545. $Controller->constructClasses();
  546. $Controller->PaginatorControllerPost->virtualFields = array(
  547. 'offset_test' => 'PaginatorControllerPost.id + 1'
  548. );
  549. $Controller->Paginator->settings = array(
  550. 'fields' => array('id', 'title', 'offset_test'),
  551. 'order' => array('offset_test' => 'DESC'),
  552. 'maxLimit' => 10,
  553. 'paramType' => 'named'
  554. );
  555. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  556. $this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
  557. $Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
  558. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  559. $this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
  560. }
  561. /**
  562. * test paginate() and virtualField on joined model
  563. *
  564. * @return void
  565. */
  566. public function testPaginateOrderVirtualFieldJoinedModel() {
  567. $Controller = new PaginatorTestController($this->request);
  568. $Controller->uses = array('PaginatorControllerPost');
  569. $Controller->params['url'] = array();
  570. $Controller->constructClasses();
  571. $Controller->PaginatorControllerPost->recursive = 0;
  572. $Controller->Paginator->settings = array(
  573. 'order' => array('PaginatorAuthor.joined_offset' => 'DESC'),
  574. 'maxLimit' => 10,
  575. 'paramType' => 'named'
  576. );
  577. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  578. $this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
  579. $Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
  580. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  581. $this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
  582. }
  583. /**
  584. * Tests for missing models
  585. *
  586. * @expectedException MissingModelException
  587. */
  588. public function testPaginateMissingModel() {
  589. $Controller = new PaginatorTestController($this->request);
  590. $Controller->constructClasses();
  591. $Controller->Paginator->paginate('MissingModel');
  592. }
  593. /**
  594. * test that option merging prefers specific models
  595. *
  596. * @return void
  597. */
  598. public function testMergeOptionsModelSpecific() {
  599. $this->Paginator->settings = array(
  600. 'page' => 1,
  601. 'limit' => 20,
  602. 'maxLimit' => 100,
  603. 'paramType' => 'named',
  604. 'Post' => array(
  605. 'page' => 1,
  606. 'limit' => 10,
  607. 'maxLimit' => 50,
  608. 'paramType' => 'named',
  609. )
  610. );
  611. $result = $this->Paginator->mergeOptions('Silly');
  612. $this->assertEquals($this->Paginator->settings, $result);
  613. $result = $this->Paginator->mergeOptions('Post');
  614. $expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
  615. $this->assertEquals($expected, $result);
  616. }
  617. /**
  618. * test mergeOptions with named params.
  619. *
  620. * @return void
  621. */
  622. public function testMergeOptionsNamedParams() {
  623. $this->request->params['named'] = array(
  624. 'page' => 10,
  625. 'limit' => 10
  626. );
  627. $this->Paginator->settings = array(
  628. 'page' => 1,
  629. 'limit' => 20,
  630. 'maxLimit' => 100,
  631. 'paramType' => 'named',
  632. );
  633. $result = $this->Paginator->mergeOptions('Post');
  634. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
  635. $this->assertEquals($expected, $result);
  636. }
  637. /**
  638. * test mergeOptions with customFind key
  639. *
  640. * @return void
  641. */
  642. public function testMergeOptionsCustomFindKey() {
  643. $this->request->params['named'] = array(
  644. 'page' => 10,
  645. 'limit' => 10
  646. );
  647. $this->Paginator->settings = array(
  648. 'page' => 1,
  649. 'limit' => 20,
  650. 'maxLimit' => 100,
  651. 'paramType' => 'named',
  652. 'findType' => 'myCustomFind'
  653. );
  654. $result = $this->Paginator->mergeOptions('Post');
  655. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
  656. $this->assertEquals($expected, $result);
  657. }
  658. /**
  659. * test merging options from the querystring.
  660. *
  661. * @return void
  662. */
  663. public function testMergeOptionsQueryString() {
  664. $this->request->params['named'] = array(
  665. 'page' => 10,
  666. 'limit' => 10
  667. );
  668. $this->request->query = array(
  669. 'page' => 99,
  670. 'limit' => 75
  671. );
  672. $this->Paginator->settings = array(
  673. 'page' => 1,
  674. 'limit' => 20,
  675. 'maxLimit' => 100,
  676. 'paramType' => 'querystring',
  677. );
  678. $result = $this->Paginator->mergeOptions('Post');
  679. $expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
  680. $this->assertEquals($expected, $result);
  681. }
  682. /**
  683. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  684. *
  685. * @return void
  686. */
  687. public function testMergeOptionsDefaultWhiteList() {
  688. $this->request->params['named'] = array(
  689. 'page' => 10,
  690. 'limit' => 10,
  691. 'fields' => array('bad.stuff'),
  692. 'recursive' => 1000,
  693. 'conditions' => array('bad.stuff'),
  694. 'contain' => array('bad')
  695. );
  696. $this->Paginator->settings = array(
  697. 'page' => 1,
  698. 'limit' => 20,
  699. 'maxLimit' => 100,
  700. 'paramType' => 'named',
  701. );
  702. $result = $this->Paginator->mergeOptions('Post');
  703. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
  704. $this->assertEquals($expected, $result);
  705. }
  706. /**
  707. * test that modifying the whitelist works.
  708. *
  709. * @return void
  710. */
  711. public function testMergeOptionsExtraWhitelist() {
  712. $this->request->params['named'] = array(
  713. 'page' => 10,
  714. 'limit' => 10,
  715. 'fields' => array('bad.stuff'),
  716. 'recursive' => 1000,
  717. 'conditions' => array('bad.stuff'),
  718. 'contain' => array('bad')
  719. );
  720. $this->Paginator->settings = array(
  721. 'page' => 1,
  722. 'limit' => 20,
  723. 'maxLimit' => 100,
  724. 'paramType' => 'named',
  725. );
  726. $this->Paginator->whitelist[] = 'fields';
  727. $result = $this->Paginator->mergeOptions('Post');
  728. $expected = array(
  729. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
  730. );
  731. $this->assertEquals($expected, $result);
  732. }
  733. /**
  734. * test that invalid directions are ignored.
  735. *
  736. * @return void
  737. */
  738. public function testValidateSortInvalidDirection() {
  739. $model = $this->getMock('Model');
  740. $model->alias = 'model';
  741. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  742. $options = array('sort' => 'something', 'direction' => 'boogers');
  743. $result = $this->Paginator->validateSort($model, $options);
  744. $this->assertEquals('asc', $result['order']['model.something']);
  745. }
  746. /**
  747. * Test that a really large page number gets clamped to the max page size.
  748. */
  749. public function testOutOfRangePageNumberGetsClamped() {
  750. $Controller = new PaginatorTestController($this->request);
  751. $Controller->uses = array('PaginatorControllerPost');
  752. $Controller->params['named'] = array(
  753. 'page' => 3000,
  754. );
  755. $Controller->constructClasses();
  756. $Controller->PaginatorControllerPost->recursive = 0;
  757. $Controller->Paginator->paginate('PaginatorControllerPost');
  758. $this->assertEquals(
  759. 1,
  760. $Controller->request->params['paging']['PaginatorControllerPost']['page'],
  761. 'Super big page number should be capped to max number of pages'
  762. );
  763. $Controller->paginate = array(
  764. 'conditions' => array('PaginatorControllerPost.id >' => 100)
  765. );
  766. $Controller->Paginator->paginate('PaginatorControllerPost');
  767. $this->assertEquals(
  768. 1,
  769. $Controller->request->params['paging']['PaginatorControllerPost']['page'],
  770. 'Page number should not be 0'
  771. );
  772. }
  773. /**
  774. * test that fields not in whitelist won't be part of order conditions.
  775. *
  776. * @return void
  777. */
  778. public function testValidateSortWhitelistFailure() {
  779. $model = $this->getMock('Model');
  780. $model->alias = 'model';
  781. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  782. $options = array('sort' => 'body', 'direction' => 'asc');
  783. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  784. $this->assertNull($result['order']);
  785. }
  786. /**
  787. * test that virtual fields work.
  788. *
  789. * @return void
  790. */
  791. public function testValidateSortVirtualField() {
  792. $model = $this->getMock('Model');
  793. $model->alias = 'model';
  794. $model->expects($this->at(0))
  795. ->method('hasField')
  796. ->with('something')
  797. ->will($this->returnValue(false));
  798. $model->expects($this->at(1))
  799. ->method('hasField')
  800. ->with('something', true)
  801. ->will($this->returnValue(true));
  802. $options = array('sort' => 'something', 'direction' => 'desc');
  803. $result = $this->Paginator->validateSort($model, $options);
  804. $this->assertEquals('desc', $result['order']['something']);
  805. }
  806. /**
  807. * test that multiple sort works.
  808. *
  809. * @return void
  810. */
  811. public function testValidateSortMultiple() {
  812. $model = $this->getMock('Model');
  813. $model->alias = 'model';
  814. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  815. $options = array('order' => array(
  816. 'author_id' => 'asc',
  817. 'title' => 'asc'
  818. ));
  819. $result = $this->Paginator->validateSort($model, $options);
  820. $expected = array(
  821. 'model.author_id' => 'asc',
  822. 'model.title' => 'asc'
  823. );
  824. $this->assertEquals($expected, $result['order']);
  825. }
  826. /**
  827. * Test that no sort doesn't trigger an error.
  828. *
  829. * @return void
  830. */
  831. public function testValidateSortNoSort() {
  832. $model = $this->getMock('Model');
  833. $model->alias = 'model';
  834. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  835. $options = array('direction' => 'asc');
  836. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  837. $this->assertFalse(isset($result['order']));
  838. $options = array('order' => 'invalid desc');
  839. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  840. $this->assertEquals($options['order'], $result['order']);
  841. }
  842. /**
  843. * test that maxLimit is respected
  844. *
  845. * @return void
  846. */
  847. public function testCheckLimit() {
  848. $result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
  849. $this->assertEquals(100, $result['limit']);
  850. $result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
  851. $this->assertEquals(1, $result['limit']);
  852. $result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
  853. $this->assertEquals(1, $result['limit']);
  854. $result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
  855. $this->assertEquals(1, $result['limit']);
  856. $result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
  857. $this->assertEquals(1, $result['limit']);
  858. }
  859. /**
  860. * testPaginateMaxLimit
  861. *
  862. * @return void
  863. */
  864. public function testPaginateMaxLimit() {
  865. $Controller = new Controller($this->request);
  866. $Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
  867. $Controller->passedArgs[] = '1';
  868. $Controller->constructClasses();
  869. $Controller->request->params['named'] = array(
  870. 'contain' => array('ControllerComment'), 'limit' => '1000'
  871. );
  872. $result = $Controller->paginate('PaginatorControllerPost');
  873. $this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  874. $Controller->request->params['named'] = array(
  875. 'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
  876. );
  877. $result = $Controller->paginate('PaginatorControllerPost');
  878. $this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  879. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
  880. $result = $Controller->paginate('PaginatorControllerPost');
  881. $this->assertEquals(10, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  882. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
  883. $Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
  884. $result = $Controller->paginate('PaginatorControllerPost');
  885. $this->assertEquals(1000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  886. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
  887. $result = $Controller->paginate('PaginatorControllerPost');
  888. $this->assertEquals(2000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  889. }
  890. /**
  891. * test paginate() and virtualField overlapping with real fields.
  892. *
  893. * @return void
  894. */
  895. public function testPaginateOrderVirtualFieldSharedWithRealField() {
  896. $Controller = new Controller($this->request);
  897. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  898. $Controller->constructClasses();
  899. $Controller->PaginatorControllerComment->virtualFields = array(
  900. 'title' => 'PaginatorControllerComment.comment'
  901. );
  902. $Controller->PaginatorControllerComment->bindModel(array(
  903. 'belongsTo' => array(
  904. 'PaginatorControllerPost' => array(
  905. 'className' => 'PaginatorControllerPost',
  906. 'foreignKey' => 'article_id'
  907. )
  908. )
  909. ), false);
  910. $Controller->paginate = array(
  911. 'fields' => array('PaginatorControllerComment.id', 'title', 'PaginatorControllerPost.title'),
  912. );
  913. $Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc');
  914. $result = $Controller->paginate('PaginatorControllerComment');
  915. $this->assertEquals(array(1, 2, 3, 4, 5, 6), Hash::extract($result, '{n}.PaginatorControllerComment.id'));
  916. }
  917. /**
  918. * test paginate() and custom find, to make sure the correct count is returned.
  919. *
  920. * @return void
  921. */
  922. public function testPaginateCustomFind() {
  923. $Controller = new Controller($this->request);
  924. $Controller->uses = array('PaginatorCustomPost');
  925. $Controller->constructClasses();
  926. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  927. $Controller->PaginatorCustomPost->create($data);
  928. $result = $Controller->PaginatorCustomPost->save();
  929. $this->assertTrue(!empty($result));
  930. $result = $Controller->paginate();
  931. $this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
  932. $result = $Controller->params['paging']['PaginatorCustomPost'];
  933. $this->assertEquals(4, $result['current']);
  934. $this->assertEquals(4, $result['count']);
  935. $Controller->paginate = array('published');
  936. $result = $Controller->paginate();
  937. $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
  938. $result = $Controller->params['paging']['PaginatorCustomPost'];
  939. $this->assertEquals(3, $result['current']);
  940. $this->assertEquals(3, $result['count']);
  941. $Controller->paginate = array('published', 'limit' => 2);
  942. $result = $Controller->paginate();
  943. $this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
  944. $result = $Controller->params['paging']['PaginatorCustomPost'];
  945. $this->assertEquals(2, $result['current']);
  946. $this->assertEquals(3, $result['count']);
  947. $this->assertEquals(2, $result['pageCount']);
  948. $this->assertTrue($result['nextPage']);
  949. $this->assertFalse($result['prevPage']);
  950. }
  951. /**
  952. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  953. *
  954. * @return void
  955. */
  956. public function testPaginateCustomFindFieldsArray() {
  957. $Controller = new Controller($this->request);
  958. $Controller->uses = array('PaginatorCustomPost');
  959. $Controller->constructClasses();
  960. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  961. $Controller->PaginatorCustomPost->create($data);
  962. $result = $Controller->PaginatorCustomPost->save();
  963. $this->assertTrue(!empty($result));
  964. $Controller->paginate = array(
  965. 'list',
  966. 'conditions' => array('PaginatorCustomPost.published' => 'Y'),
  967. 'limit' => 2
  968. );
  969. $result = $Controller->paginate();
  970. $expected = array(
  971. 1 => 'First Post',
  972. 2 => 'Second Post',
  973. );
  974. $this->assertEquals($expected, $result);
  975. $result = $Controller->params['paging']['PaginatorCustomPost'];
  976. $this->assertEquals(2, $result['current']);
  977. $this->assertEquals(3, $result['count']);
  978. $this->assertEquals(2, $result['pageCount']);
  979. $this->assertTrue($result['nextPage']);
  980. $this->assertFalse($result['prevPage']);
  981. }
  982. /**
  983. * test paginate() and custom find with customFind key, to make sure the correct count is returned.
  984. *
  985. * @return void
  986. */
  987. public function testPaginateCustomFindWithCustomFindKey() {
  988. $Controller = new Controller($this->request);
  989. $Controller->uses = array('PaginatorCustomPost');
  990. $Controller->constructClasses();
  991. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  992. $Controller->PaginatorCustomPost->create($data);
  993. $result = $Controller->PaginatorCustomPost->save();
  994. $this->assertTrue(!empty($result));
  995. $Controller->paginate = array(
  996. 'conditions' => array('PaginatorCustomPost.published' => 'Y'),
  997. 'findType' => 'list',
  998. 'limit' => 2
  999. );
  1000. $result = $Controller->paginate();
  1001. $expected = array(
  1002. 1 => 'First Post',
  1003. 2 => 'Second Post',
  1004. );
  1005. $this->assertEquals($expected, $result);
  1006. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1007. $this->assertEquals(2, $result['current']);
  1008. $this->assertEquals(3, $result['count']);
  1009. $this->assertEquals(2, $result['pageCount']);
  1010. $this->assertTrue($result['nextPage']);
  1011. $this->assertFalse($result['prevPage']);
  1012. }
  1013. /**
  1014. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  1015. *
  1016. * @return void
  1017. */
  1018. public function testPaginateCustomFindGroupBy() {
  1019. $Controller = new Controller($this->request);
  1020. $Controller->uses = array('PaginatorCustomPost');
  1021. $Controller->constructClasses();
  1022. $data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  1023. $Controller->PaginatorCustomPost->create($data);
  1024. $result = $Controller->PaginatorCustomPost->save();
  1025. $this->assertTrue(!empty($result));
  1026. $Controller->paginate = array(
  1027. 'totals',
  1028. 'limit' => 2
  1029. );
  1030. $result = $Controller->paginate();
  1031. $expected = array(
  1032. array(
  1033. 'PaginatorCustomPost' => array(
  1034. 'author_id' => '1',
  1035. 'total_posts' => '2'
  1036. )
  1037. ),
  1038. array(
  1039. 'PaginatorCustomPost' => array(
  1040. 'author_id' => '2',
  1041. 'total_posts' => '1'
  1042. )
  1043. )
  1044. );
  1045. $this->assertEquals($expected, $result);
  1046. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1047. $this->assertEquals(2, $result['current']);
  1048. $this->assertEquals(3, $result['count']);
  1049. $this->assertEquals(2, $result['pageCount']);
  1050. $this->assertTrue($result['nextPage']);
  1051. $this->assertFalse($result['prevPage']);
  1052. $Controller->paginate = array(
  1053. 'totals',
  1054. 'limit' => 2,
  1055. 'page' => 2
  1056. );
  1057. $result = $Controller->paginate();
  1058. $expected = array(
  1059. array(
  1060. 'PaginatorCustomPost' => array(
  1061. 'author_id' => '3',
  1062. 'total_posts' => '1'
  1063. )
  1064. ),
  1065. );
  1066. $this->assertEquals($expected, $result);
  1067. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1068. $this->assertEquals(1, $result['current']);
  1069. $this->assertEquals(3, $result['count']);
  1070. $this->assertEquals(2, $result['pageCount']);
  1071. $this->assertFalse($result['nextPage']);
  1072. $this->assertTrue($result['prevPage']);
  1073. }
  1074. /**
  1075. * test paginate() and custom find with returning other query on count operation,
  1076. * to make sure the correct count is returned.
  1077. *
  1078. * @return void
  1079. */
  1080. public function testPaginateCustomFindCount() {
  1081. $Controller = new Controller($this->request);
  1082. $Controller->uses = array('PaginatorCustomPost');
  1083. $Controller->constructClasses();
  1084. $data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  1085. $Controller->PaginatorCustomPost->create($data);
  1086. $result = $Controller->PaginatorCustomPost->save();
  1087. $this->assertTrue(!empty($result));
  1088. $Controller->paginate = array(
  1089. 'totalsOperation',
  1090. 'limit' => 2
  1091. );
  1092. $result = $Controller->paginate();
  1093. $expected = array(
  1094. array(
  1095. 'PaginatorCustomPost' => array(
  1096. 'author_id' => '1',
  1097. 'total_posts' => '2'
  1098. ),
  1099. 'Author' => array(
  1100. 'user' => 'mariano',
  1101. )
  1102. ),
  1103. array(
  1104. 'PaginatorCustomPost' => array(
  1105. 'author_id' => '2',
  1106. 'total_posts' => '1'
  1107. ),
  1108. 'Author' => array(
  1109. 'user' => 'nate'
  1110. )
  1111. )
  1112. );
  1113. $this->assertEquals($expected, $result);
  1114. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1115. $this->assertEquals(2, $result['current']);
  1116. $this->assertEquals(3, $result['count']);
  1117. $this->assertEquals(2, $result['pageCount']);
  1118. $this->assertTrue($result['nextPage']);
  1119. $this->assertFalse($result['prevPage']);
  1120. }
  1121. }