PaginatorComponentTest.php 42 KB

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