PaginatorComponentTest.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. 'order' => array('PaginatorControllerPost.id' => 'ASC')
  431. ),
  432. );
  433. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  434. $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
  435. $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  436. $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
  437. $Controller->Paginator->settings = array(
  438. 'PaginatorControllerPost' => array(
  439. 'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
  440. ),
  441. );
  442. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  443. $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  444. $this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']);
  445. $Controller->request->params['named'] = array('limit' => 12);
  446. $Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
  447. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  448. $paging = $Controller->params['paging']['PaginatorControllerPost'];
  449. $this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[1]['limit']);
  450. $this->assertEquals(12, $paging['options']['limit']);
  451. $Controller = new PaginatorTestController($this->request);
  452. $Controller->uses = array('ControllerPaginateModel');
  453. $Controller->request->query = array();
  454. $Controller->constructClasses();
  455. $Controller->Paginator->settings = array(
  456. 'ControllerPaginateModel' => array(
  457. 'contain' => array('ControllerPaginateModel'),
  458. 'group' => 'Comment.author_id',
  459. 'maxLimit' => 10,
  460. 'paramType' => 'named'
  461. )
  462. );
  463. $result = $Controller->Paginator->paginate('ControllerPaginateModel');
  464. $expected = array(
  465. 'contain' => array('ControllerPaginateModel'),
  466. 'group' => 'Comment.author_id',
  467. 'maxLimit' => 10,
  468. 'paramType' => 'named'
  469. );
  470. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
  471. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
  472. $Controller->Paginator->settings = array(
  473. 'ControllerPaginateModel' => array(
  474. 'foo', 'contain' => array('ControllerPaginateModel'),
  475. 'group' => 'Comment.author_id',
  476. 'maxLimit' => 10,
  477. 'paramType' => 'named'
  478. )
  479. );
  480. $Controller->Paginator->paginate('ControllerPaginateModel');
  481. $expected = array(
  482. 'contain' => array('ControllerPaginateModel'),
  483. 'group' => 'Comment.author_id',
  484. 'type' => 'foo',
  485. 'maxLimit' => 10,
  486. 'paramType' => 'named'
  487. );
  488. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
  489. $this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
  490. }
  491. /**
  492. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  493. *
  494. * @return void
  495. */
  496. public function testPaginateSpecialType() {
  497. $Controller = new PaginatorTestController($this->request);
  498. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  499. $Controller->passedArgs[] = '1';
  500. $Controller->params['url'] = array();
  501. $Controller->constructClasses();
  502. $Controller->Paginator->settings = array(
  503. 'PaginatorControllerPost' => array(
  504. 'popular',
  505. 'fields' => array('id', 'title'),
  506. 'maxLimit' => 10,
  507. 'paramType' => 'named'
  508. )
  509. );
  510. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  511. $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
  512. $this->assertEquals(
  513. $Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
  514. array('PaginatorControllerPost.id > ' => '1')
  515. );
  516. $this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
  517. }
  518. /**
  519. * testDefaultPaginateParams method
  520. *
  521. * @return void
  522. */
  523. public function testDefaultPaginateParams() {
  524. $Controller = new PaginatorTestController($this->request);
  525. $Controller->modelClass = 'PaginatorControllerPost';
  526. $Controller->params['url'] = array();
  527. $Controller->constructClasses();
  528. $Controller->Paginator->settings = array(
  529. 'order' => 'PaginatorControllerPost.id DESC',
  530. 'maxLimit' => 10,
  531. 'paramType' => 'named'
  532. );
  533. $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
  534. $this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
  535. $this->assertEquals(array(3, 2, 1), $results);
  536. }
  537. /**
  538. * test paginate() and virtualField interactions
  539. *
  540. * @return void
  541. */
  542. public function testPaginateOrderVirtualField() {
  543. $Controller = new PaginatorTestController($this->request);
  544. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  545. $Controller->params['url'] = array();
  546. $Controller->constructClasses();
  547. $Controller->PaginatorControllerPost->virtualFields = array(
  548. 'offset_test' => 'PaginatorControllerPost.id + 1'
  549. );
  550. $Controller->Paginator->settings = array(
  551. 'fields' => array('id', 'title', 'offset_test'),
  552. 'order' => array('offset_test' => 'DESC'),
  553. 'maxLimit' => 10,
  554. 'paramType' => 'named'
  555. );
  556. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  557. $this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
  558. $Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
  559. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  560. $this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
  561. }
  562. /**
  563. * test paginate() and virtualField on joined model
  564. *
  565. * @return void
  566. */
  567. public function testPaginateOrderVirtualFieldJoinedModel() {
  568. $Controller = new PaginatorTestController($this->request);
  569. $Controller->uses = array('PaginatorControllerPost');
  570. $Controller->params['url'] = array();
  571. $Controller->constructClasses();
  572. $Controller->PaginatorControllerPost->recursive = 0;
  573. $Controller->Paginator->settings = array(
  574. 'order' => array('PaginatorAuthor.joined_offset' => 'DESC'),
  575. 'maxLimit' => 10,
  576. 'paramType' => 'named'
  577. );
  578. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  579. $this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
  580. $Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
  581. $result = $Controller->Paginator->paginate('PaginatorControllerPost');
  582. $this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
  583. }
  584. /**
  585. * Tests for missing models
  586. *
  587. * @expectedException MissingModelException
  588. */
  589. public function testPaginateMissingModel() {
  590. $Controller = new PaginatorTestController($this->request);
  591. $Controller->constructClasses();
  592. $Controller->Paginator->paginate('MissingModel');
  593. }
  594. /**
  595. * test that option merging prefers specific models
  596. *
  597. * @return void
  598. */
  599. public function testMergeOptionsModelSpecific() {
  600. $this->Paginator->settings = array(
  601. 'page' => 1,
  602. 'limit' => 20,
  603. 'maxLimit' => 100,
  604. 'paramType' => 'named',
  605. 'Post' => array(
  606. 'page' => 1,
  607. 'limit' => 10,
  608. 'maxLimit' => 50,
  609. 'paramType' => 'named',
  610. )
  611. );
  612. $result = $this->Paginator->mergeOptions('Silly');
  613. $this->assertEquals($this->Paginator->settings, $result);
  614. $result = $this->Paginator->mergeOptions('Post');
  615. $expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
  616. $this->assertEquals($expected, $result);
  617. }
  618. /**
  619. * test mergeOptions with named params.
  620. *
  621. * @return void
  622. */
  623. public function testMergeOptionsNamedParams() {
  624. $this->request->params['named'] = array(
  625. 'page' => 10,
  626. 'limit' => 10
  627. );
  628. $this->Paginator->settings = array(
  629. 'page' => 1,
  630. 'limit' => 20,
  631. 'maxLimit' => 100,
  632. 'paramType' => 'named',
  633. );
  634. $result = $this->Paginator->mergeOptions('Post');
  635. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
  636. $this->assertEquals($expected, $result);
  637. }
  638. /**
  639. * test mergeOptions with customFind key
  640. *
  641. * @return void
  642. */
  643. public function testMergeOptionsCustomFindKey() {
  644. $this->request->params['named'] = array(
  645. 'page' => 10,
  646. 'limit' => 10
  647. );
  648. $this->Paginator->settings = array(
  649. 'page' => 1,
  650. 'limit' => 20,
  651. 'maxLimit' => 100,
  652. 'paramType' => 'named',
  653. 'findType' => 'myCustomFind'
  654. );
  655. $result = $this->Paginator->mergeOptions('Post');
  656. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
  657. $this->assertEquals($expected, $result);
  658. }
  659. /**
  660. * test merging options from the querystring.
  661. *
  662. * @return void
  663. */
  664. public function testMergeOptionsQueryString() {
  665. $this->request->params['named'] = array(
  666. 'page' => 10,
  667. 'limit' => 10
  668. );
  669. $this->request->query = array(
  670. 'page' => 99,
  671. 'limit' => 75
  672. );
  673. $this->Paginator->settings = array(
  674. 'page' => 1,
  675. 'limit' => 20,
  676. 'maxLimit' => 100,
  677. 'paramType' => 'querystring',
  678. );
  679. $result = $this->Paginator->mergeOptions('Post');
  680. $expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
  681. $this->assertEquals($expected, $result);
  682. }
  683. /**
  684. * test that the default whitelist doesn't let people screw with things they should not be allowed to.
  685. *
  686. * @return void
  687. */
  688. public function testMergeOptionsDefaultWhiteList() {
  689. $this->request->params['named'] = array(
  690. 'page' => 10,
  691. 'limit' => 10,
  692. 'fields' => array('bad.stuff'),
  693. 'recursive' => 1000,
  694. 'conditions' => array('bad.stuff'),
  695. 'contain' => array('bad')
  696. );
  697. $this->Paginator->settings = array(
  698. 'page' => 1,
  699. 'limit' => 20,
  700. 'maxLimit' => 100,
  701. 'paramType' => 'named',
  702. );
  703. $result = $this->Paginator->mergeOptions('Post');
  704. $expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
  705. $this->assertEquals($expected, $result);
  706. }
  707. /**
  708. * test that modifying the whitelist works.
  709. *
  710. * @return void
  711. */
  712. public function testMergeOptionsExtraWhitelist() {
  713. $this->request->params['named'] = array(
  714. 'page' => 10,
  715. 'limit' => 10,
  716. 'fields' => array('bad.stuff'),
  717. 'recursive' => 1000,
  718. 'conditions' => array('bad.stuff'),
  719. 'contain' => array('bad')
  720. );
  721. $this->Paginator->settings = array(
  722. 'page' => 1,
  723. 'limit' => 20,
  724. 'maxLimit' => 100,
  725. 'paramType' => 'named',
  726. );
  727. $this->Paginator->whitelist[] = 'fields';
  728. $result = $this->Paginator->mergeOptions('Post');
  729. $expected = array(
  730. 'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
  731. );
  732. $this->assertEquals($expected, $result);
  733. }
  734. /**
  735. * test mergeOptions with limit > maxLimit in code.
  736. *
  737. * @return void
  738. */
  739. public function testMergeOptionsMaxLimit() {
  740. $this->Paginator->settings = array(
  741. 'limit' => 200,
  742. 'paramType' => 'named',
  743. );
  744. $result = $this->Paginator->mergeOptions('Post');
  745. $expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 200, 'paramType' => 'named');
  746. $this->assertEquals($expected, $result);
  747. $this->Paginator->settings = array(
  748. 'maxLimit' => 10,
  749. 'paramType' => 'named',
  750. );
  751. $result = $this->Paginator->mergeOptions('Post');
  752. $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
  753. $this->assertEquals($expected, $result);
  754. $this->request->params['named'] = array(
  755. 'limit' => 500
  756. );
  757. $this->Paginator->settings = array(
  758. 'limit' => 150,
  759. 'paramType' => 'named',
  760. );
  761. $result = $this->Paginator->mergeOptions('Post');
  762. $expected = array('page' => 1, 'limit' => 500, 'maxLimit' => 150, 'paramType' => 'named');
  763. $this->assertEquals($expected, $result);
  764. }
  765. /**
  766. * test that invalid directions are ignored.
  767. *
  768. * @return void
  769. */
  770. public function testValidateSortInvalidDirection() {
  771. $model = $this->getMock('Model');
  772. $model->alias = 'model';
  773. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  774. $options = array('sort' => 'something', 'direction' => 'boogers');
  775. $result = $this->Paginator->validateSort($model, $options);
  776. $this->assertEquals('asc', $result['order']['model.something']);
  777. }
  778. /**
  779. * Test that a really large page number gets clamped to the max page size.
  780. */
  781. public function testOutOfRangePageNumberGetsClamped() {
  782. $Controller = new PaginatorTestController($this->request);
  783. $Controller->uses = array('PaginatorControllerPost');
  784. $Controller->params['named'] = array(
  785. 'page' => 3000,
  786. );
  787. $Controller->constructClasses();
  788. $Controller->PaginatorControllerPost->recursive = 0;
  789. $Controller->Paginator->paginate('PaginatorControllerPost');
  790. $this->assertEquals(
  791. 1,
  792. $Controller->request->params['paging']['PaginatorControllerPost']['page'],
  793. 'Super big page number should be capped to max number of pages'
  794. );
  795. $Controller->paginate = array(
  796. 'conditions' => array('PaginatorControllerPost.id >' => 100)
  797. );
  798. $Controller->Paginator->paginate('PaginatorControllerPost');
  799. $this->assertEquals(
  800. 1,
  801. $Controller->request->params['paging']['PaginatorControllerPost']['page'],
  802. 'Page number should not be 0'
  803. );
  804. }
  805. /**
  806. * test that fields not in whitelist won't be part of order conditions.
  807. *
  808. * @return void
  809. */
  810. public function testValidateSortWhitelistFailure() {
  811. $model = $this->getMock('Model');
  812. $model->alias = 'model';
  813. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  814. $options = array('sort' => 'body', 'direction' => 'asc');
  815. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  816. $this->assertNull($result['order']);
  817. }
  818. /**
  819. * test that virtual fields work.
  820. *
  821. * @return void
  822. */
  823. public function testValidateSortVirtualField() {
  824. $model = $this->getMock('Model');
  825. $model->alias = 'model';
  826. $model->expects($this->at(0))
  827. ->method('hasField')
  828. ->with('something')
  829. ->will($this->returnValue(false));
  830. $model->expects($this->at(1))
  831. ->method('hasField')
  832. ->with('something', true)
  833. ->will($this->returnValue(true));
  834. $options = array('sort' => 'something', 'direction' => 'desc');
  835. $result = $this->Paginator->validateSort($model, $options);
  836. $this->assertEquals('desc', $result['order']['something']);
  837. }
  838. /**
  839. * test that multiple sort works.
  840. *
  841. * @return void
  842. */
  843. public function testValidateSortMultiple() {
  844. $model = $this->getMock('Model');
  845. $model->alias = 'model';
  846. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  847. $options = array('order' => array(
  848. 'author_id' => 'asc',
  849. 'title' => 'asc'
  850. ));
  851. $result = $this->Paginator->validateSort($model, $options);
  852. $expected = array(
  853. 'model.author_id' => 'asc',
  854. 'model.title' => 'asc'
  855. );
  856. $this->assertEquals($expected, $result['order']);
  857. }
  858. /**
  859. * Test that no sort doesn't trigger an error.
  860. *
  861. * @return void
  862. */
  863. public function testValidateSortNoSort() {
  864. $model = $this->getMock('Model');
  865. $model->alias = 'model';
  866. $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
  867. $options = array('direction' => 'asc');
  868. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  869. $this->assertFalse(isset($result['order']));
  870. $options = array('order' => 'invalid desc');
  871. $result = $this->Paginator->validateSort($model, $options, array('title', 'id'));
  872. $this->assertEquals($options['order'], $result['order']);
  873. }
  874. /**
  875. * test that maxLimit is respected
  876. *
  877. * @return void
  878. */
  879. public function testCheckLimit() {
  880. $result = $this->Paginator->checkLimit(array('limit' => 1000000, 'maxLimit' => 100));
  881. $this->assertEquals(100, $result['limit']);
  882. $result = $this->Paginator->checkLimit(array('limit' => 'sheep!', 'maxLimit' => 100));
  883. $this->assertEquals(1, $result['limit']);
  884. $result = $this->Paginator->checkLimit(array('limit' => '-1', 'maxLimit' => 100));
  885. $this->assertEquals(1, $result['limit']);
  886. $result = $this->Paginator->checkLimit(array('limit' => null, 'maxLimit' => 100));
  887. $this->assertEquals(1, $result['limit']);
  888. $result = $this->Paginator->checkLimit(array('limit' => 0, 'maxLimit' => 100));
  889. $this->assertEquals(1, $result['limit']);
  890. }
  891. /**
  892. * testPaginateMaxLimit
  893. *
  894. * @return void
  895. */
  896. public function testPaginateMaxLimit() {
  897. $Controller = new Controller($this->request);
  898. $Controller->uses = array('PaginatorControllerPost', 'ControllerComment');
  899. $Controller->passedArgs[] = '1';
  900. $Controller->constructClasses();
  901. $Controller->request->params['named'] = array(
  902. 'contain' => array('ControllerComment'), 'limit' => '1000'
  903. );
  904. $result = $Controller->paginate('PaginatorControllerPost');
  905. $this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  906. $Controller->request->params['named'] = array(
  907. 'contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000
  908. );
  909. $result = $Controller->paginate('PaginatorControllerPost');
  910. $this->assertEquals(100, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  911. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10');
  912. $result = $Controller->paginate('PaginatorControllerPost');
  913. $this->assertEquals(10, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  914. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000');
  915. $Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
  916. $result = $Controller->paginate('PaginatorControllerPost');
  917. $this->assertEquals(1000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  918. $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000');
  919. $result = $Controller->paginate('PaginatorControllerPost');
  920. $this->assertEquals(2000, $Controller->params['paging']['PaginatorControllerPost']['options']['limit']);
  921. }
  922. /**
  923. * test paginate() and virtualField overlapping with real fields.
  924. *
  925. * @return void
  926. */
  927. public function testPaginateOrderVirtualFieldSharedWithRealField() {
  928. $Controller = new Controller($this->request);
  929. $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment');
  930. $Controller->constructClasses();
  931. $Controller->PaginatorControllerComment->virtualFields = array(
  932. 'title' => 'PaginatorControllerComment.comment'
  933. );
  934. $Controller->PaginatorControllerComment->bindModel(array(
  935. 'belongsTo' => array(
  936. 'PaginatorControllerPost' => array(
  937. 'className' => 'PaginatorControllerPost',
  938. 'foreignKey' => 'article_id'
  939. )
  940. )
  941. ), false);
  942. $Controller->paginate = array(
  943. 'fields' => array('PaginatorControllerComment.id', 'title', 'PaginatorControllerPost.title'),
  944. );
  945. $Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc');
  946. $result = $Controller->paginate('PaginatorControllerComment');
  947. $this->assertEquals(array(1, 2, 3, 4, 5, 6), Hash::extract($result, '{n}.PaginatorControllerComment.id'));
  948. }
  949. /**
  950. * test paginate() and custom find, to make sure the correct count is returned.
  951. *
  952. * @return void
  953. */
  954. public function testPaginateCustomFind() {
  955. $Controller = new Controller($this->request);
  956. $Controller->uses = array('PaginatorCustomPost');
  957. $Controller->constructClasses();
  958. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  959. $Controller->PaginatorCustomPost->create($data);
  960. $result = $Controller->PaginatorCustomPost->save();
  961. $this->assertTrue(!empty($result));
  962. $result = $Controller->paginate();
  963. $this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
  964. $result = $Controller->params['paging']['PaginatorCustomPost'];
  965. $this->assertEquals(4, $result['current']);
  966. $this->assertEquals(4, $result['count']);
  967. $Controller->paginate = array('published');
  968. $result = $Controller->paginate();
  969. $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
  970. $result = $Controller->params['paging']['PaginatorCustomPost'];
  971. $this->assertEquals(3, $result['current']);
  972. $this->assertEquals(3, $result['count']);
  973. $Controller->paginate = array('published', 'limit' => 2);
  974. $result = $Controller->paginate();
  975. $this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
  976. $result = $Controller->params['paging']['PaginatorCustomPost'];
  977. $this->assertEquals(2, $result['current']);
  978. $this->assertEquals(3, $result['count']);
  979. $this->assertEquals(2, $result['pageCount']);
  980. $this->assertTrue($result['nextPage']);
  981. $this->assertFalse($result['prevPage']);
  982. }
  983. /**
  984. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  985. *
  986. * @return void
  987. */
  988. public function testPaginateCustomFindFieldsArray() {
  989. $Controller = new Controller($this->request);
  990. $Controller->uses = array('PaginatorCustomPost');
  991. $Controller->constructClasses();
  992. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  993. $Controller->PaginatorCustomPost->create($data);
  994. $result = $Controller->PaginatorCustomPost->save();
  995. $this->assertTrue(!empty($result));
  996. $Controller->paginate = array(
  997. 'list',
  998. 'conditions' => array('PaginatorCustomPost.published' => 'Y'),
  999. 'limit' => 2
  1000. );
  1001. $result = $Controller->paginate();
  1002. $expected = array(
  1003. 1 => 'First Post',
  1004. 2 => 'Second Post',
  1005. );
  1006. $this->assertEquals($expected, $result);
  1007. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1008. $this->assertEquals(2, $result['current']);
  1009. $this->assertEquals(3, $result['count']);
  1010. $this->assertEquals(2, $result['pageCount']);
  1011. $this->assertTrue($result['nextPage']);
  1012. $this->assertFalse($result['prevPage']);
  1013. }
  1014. /**
  1015. * test paginate() and custom find with customFind key, to make sure the correct count is returned.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testPaginateCustomFindWithCustomFindKey() {
  1020. $Controller = new Controller($this->request);
  1021. $Controller->uses = array('PaginatorCustomPost');
  1022. $Controller->constructClasses();
  1023. $data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  1024. $Controller->PaginatorCustomPost->create($data);
  1025. $result = $Controller->PaginatorCustomPost->save();
  1026. $this->assertTrue(!empty($result));
  1027. $Controller->paginate = array(
  1028. 'conditions' => array('PaginatorCustomPost.published' => 'Y'),
  1029. 'findType' => 'list',
  1030. 'limit' => 2
  1031. );
  1032. $result = $Controller->paginate();
  1033. $expected = array(
  1034. 1 => 'First Post',
  1035. 2 => 'Second Post',
  1036. );
  1037. $this->assertEquals($expected, $result);
  1038. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1039. $this->assertEquals(2, $result['current']);
  1040. $this->assertEquals(3, $result['count']);
  1041. $this->assertEquals(2, $result['pageCount']);
  1042. $this->assertTrue($result['nextPage']);
  1043. $this->assertFalse($result['prevPage']);
  1044. }
  1045. /**
  1046. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  1047. *
  1048. * @return void
  1049. */
  1050. public function testPaginateCustomFindGroupBy() {
  1051. $Controller = new Controller($this->request);
  1052. $Controller->uses = array('PaginatorCustomPost');
  1053. $Controller->constructClasses();
  1054. $data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  1055. $Controller->PaginatorCustomPost->create($data);
  1056. $result = $Controller->PaginatorCustomPost->save();
  1057. $this->assertTrue(!empty($result));
  1058. $Controller->paginate = array(
  1059. 'totals',
  1060. 'limit' => 2
  1061. );
  1062. $result = $Controller->paginate();
  1063. $expected = array(
  1064. array(
  1065. 'PaginatorCustomPost' => array(
  1066. 'author_id' => '1',
  1067. 'total_posts' => '2'
  1068. )
  1069. ),
  1070. array(
  1071. 'PaginatorCustomPost' => array(
  1072. 'author_id' => '2',
  1073. 'total_posts' => '1'
  1074. )
  1075. )
  1076. );
  1077. $this->assertEquals($expected, $result);
  1078. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1079. $this->assertEquals(2, $result['current']);
  1080. $this->assertEquals(3, $result['count']);
  1081. $this->assertEquals(2, $result['pageCount']);
  1082. $this->assertTrue($result['nextPage']);
  1083. $this->assertFalse($result['prevPage']);
  1084. $Controller->paginate = array(
  1085. 'totals',
  1086. 'limit' => 2,
  1087. 'page' => 2
  1088. );
  1089. $result = $Controller->paginate();
  1090. $expected = array(
  1091. array(
  1092. 'PaginatorCustomPost' => array(
  1093. 'author_id' => '3',
  1094. 'total_posts' => '1'
  1095. )
  1096. ),
  1097. );
  1098. $this->assertEquals($expected, $result);
  1099. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1100. $this->assertEquals(1, $result['current']);
  1101. $this->assertEquals(3, $result['count']);
  1102. $this->assertEquals(2, $result['pageCount']);
  1103. $this->assertFalse($result['nextPage']);
  1104. $this->assertTrue($result['prevPage']);
  1105. }
  1106. /**
  1107. * test paginate() and custom find with returning other query on count operation,
  1108. * to make sure the correct count is returned.
  1109. *
  1110. * @return void
  1111. */
  1112. public function testPaginateCustomFindCount() {
  1113. $Controller = new Controller($this->request);
  1114. $Controller->uses = array('PaginatorCustomPost');
  1115. $Controller->constructClasses();
  1116. $data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  1117. $Controller->PaginatorCustomPost->create($data);
  1118. $result = $Controller->PaginatorCustomPost->save();
  1119. $this->assertTrue(!empty($result));
  1120. $Controller->paginate = array(
  1121. 'totalsOperation',
  1122. 'limit' => 2
  1123. );
  1124. $result = $Controller->paginate();
  1125. $expected = array(
  1126. array(
  1127. 'PaginatorCustomPost' => array(
  1128. 'author_id' => '1',
  1129. 'total_posts' => '2'
  1130. ),
  1131. 'Author' => array(
  1132. 'user' => 'mariano',
  1133. )
  1134. ),
  1135. array(
  1136. 'PaginatorCustomPost' => array(
  1137. 'author_id' => '2',
  1138. 'total_posts' => '1'
  1139. ),
  1140. 'Author' => array(
  1141. 'user' => 'nate'
  1142. )
  1143. )
  1144. );
  1145. $this->assertEquals($expected, $result);
  1146. $result = $Controller->params['paging']['PaginatorCustomPost'];
  1147. $this->assertEquals(2, $result['current']);
  1148. $this->assertEquals(3, $result['count']);
  1149. $this->assertEquals(2, $result['pageCount']);
  1150. $this->assertTrue($result['nextPage']);
  1151. $this->assertFalse($result['prevPage']);
  1152. }
  1153. }