Adding paramType to the test cases.

This commit is contained in:
mark_story 2010-12-19 00:38:20 -05:00
parent 54c52d85fb
commit 7585b2941e
2 changed files with 57 additions and 21 deletions

View file

@ -30,11 +30,25 @@
class PaginatorComponent extends Component { class PaginatorComponent extends Component {
/** /**
* Pagination settings * Pagination settings. These settings control pagination at a general level.
* You can also define sub arrays for pagination settings for specific models.
*
* - `maxLimit` The maximum limit users can choose to view. Defaults to 100
* - `limit` The initial number of items per page. Defaults to 20.
* - `page` The starting page, defaults to 1.
* - `paramType` What type of parameters you want pagination to use?
* - `named` Use named parameters.
* - `querystring` Use query string parameters.
* - `route` Use routed parameters, these require you to setup routes that include the pagination params
* *
* @var array * @var array
*/ */
public $settings = array(); public $settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'paramType' => 'named'
);
/** /**
* Constructor * Constructor
@ -43,7 +57,7 @@ class PaginatorComponent extends Component {
* @param array $settings Array of configuration settings. * @param array $settings Array of configuration settings.
*/ */
public function __construct(ComponentCollection $collection, $settings = array()) { public function __construct(ComponentCollection $collection, $settings = array()) {
$settings = array_merge(array('page' => 1, 'limit' => 20, 'maxLimit' => 100), (array)$settings); $settings = array_merge($this->settings, (array)$settings);
$this->Controller = $collection->getController(); $this->Controller = $collection->getController();
parent::__construct($collection, $settings); parent::__construct($collection, $settings);
} }
@ -203,7 +217,8 @@ class PaginatorComponent extends Component {
'nextPage' => ($count > ($page * $limit)), 'nextPage' => ($count > ($page * $limit)),
'pageCount' => $pageCount, 'pageCount' => $pageCount,
'defaults' => array_merge(array('limit' => 20, 'step' => 1), $defaults), 'defaults' => array_merge(array('limit' => 20, 'step' => 1), $defaults),
'options' => $options 'options' => $options,
'paramType' => $options['paramType']
); );
if (!isset($this->Controller->request['paging'])) { if (!isset($this->Controller->request['paging'])) {
$this->Controller->request['paging'] = array(); $this->Controller->request['paging'] = array();

View file

@ -269,13 +269,13 @@ class PaginatorTest extends CakeTestCase {
$this->assertEqual($results, array(1, 3, 2)); $this->assertEqual($results, array(1, 3, 2));
$Controller->passedArgs = array('page' => '1 " onclick="alert(\'xss\');">'); $Controller->passedArgs = array('page' => '1 " onclick="alert(\'xss\');">');
$Controller->Paginator->settings = array('limit' => 1, 'maxLimit' => 10); $Controller->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
$Controller->Paginator->paginate('PaginatorControllerPost'); $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1, 'XSS exploit opened %s'); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1, 'XSS exploit opened %s');
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['options']['page'], 1, 'XSS exploit opened %s'); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['options']['page'], 1, 'XSS exploit opened %s');
$Controller->passedArgs = array(); $Controller->passedArgs = array();
$Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10); $Controller->Paginator->settings = array('limit' => 0, 'maxLimit' => 10, 'paramType' => 'named');
$Controller->Paginator->paginate('PaginatorControllerPost'); $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
@ -283,7 +283,7 @@ class PaginatorTest extends CakeTestCase {
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
$Controller->passedArgs = array(); $Controller->passedArgs = array();
$Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10); $Controller->Paginator->settings = array('limit' => 'garbage!', 'maxLimit' => 10, 'paramType' => 'named');
$Controller->Paginator->paginate('PaginatorControllerPost'); $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
@ -291,7 +291,7 @@ class PaginatorTest extends CakeTestCase {
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['nextPage'], true);
$Controller->passedArgs = array(); $Controller->passedArgs = array();
$Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10); $Controller->Paginator->settings = array('limit' => '-1', 'maxLimit' => 10, 'paramType' => 'named');
$Controller->Paginator->paginate('PaginatorControllerPost'); $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3); $this->assertIdentical($Controller->params['paging']['PaginatorControllerPost']['pageCount'], 3);
@ -324,7 +324,11 @@ class PaginatorTest extends CakeTestCase {
$Controller->passedArgs = array('page' => '-1'); $Controller->passedArgs = array('page' => '-1');
$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
'PaginatorControllerPost' => array('contain' => array('PaginatorControllerComment'), 'maxLimit' => 10), 'PaginatorControllerPost' => array(
'contain' => array('PaginatorControllerComment'),
'maxLimit' => 10,
'paramType' => 'named'
),
); );
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
@ -332,14 +336,16 @@ class PaginatorTest extends CakeTestCase {
$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQuery['contain'])); $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQuery['contain']));
$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
'PaginatorControllerPost' => array('popular', 'fields' => array('id', 'title'), 'maxLimit' => 10), 'PaginatorControllerPost' => array(
'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
),
); );
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3)); $this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['conditions'], array('PaginatorControllerPost.id > ' => '1')); $this->assertEqual($Controller->PaginatorControllerPost->lastQuery['conditions'], array('PaginatorControllerPost.id > ' => '1'));
$Controller->passedArgs = array('limit' => 12); $Controller->passedArgs = array('limit' => 12);
$Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100); $Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$paging = $Controller->params['paging']['PaginatorControllerPost']; $paging = $Controller->params['paging']['PaginatorControllerPost'];
@ -354,11 +360,17 @@ class PaginatorTest extends CakeTestCase {
'ControllerPaginateModel' => array( 'ControllerPaginateModel' => array(
'contain' => array('ControllerPaginateModel'), 'contain' => array('ControllerPaginateModel'),
'group' => 'Comment.author_id', 'group' => 'Comment.author_id',
'maxLimit' => 10 'maxLimit' => 10,
'paramType' => 'named'
) )
); );
$result = $Controller->Paginator->paginate('ControllerPaginateModel'); $result = $Controller->Paginator->paginate('ControllerPaginateModel');
$expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id', 'maxLimit' => 10); $expected = array(
'contain' => array('ControllerPaginateModel'),
'group' => 'Comment.author_id',
'maxLimit' => 10,
'paramType' => 'named'
);
$this->assertEqual($Controller->ControllerPaginateModel->extra, $expected); $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
$this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected); $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
@ -366,7 +378,8 @@ class PaginatorTest extends CakeTestCase {
'ControllerPaginateModel' => array( 'ControllerPaginateModel' => array(
'foo', 'contain' => array('ControllerPaginateModel'), 'foo', 'contain' => array('ControllerPaginateModel'),
'group' => 'Comment.author_id', 'group' => 'Comment.author_id',
'maxLimit' => 10 'maxLimit' => 10,
'paramType' => 'named'
) )
); );
$Controller->Paginator->paginate('ControllerPaginateModel'); $Controller->Paginator->paginate('ControllerPaginateModel');
@ -374,7 +387,8 @@ class PaginatorTest extends CakeTestCase {
'contain' => array('ControllerPaginateModel'), 'contain' => array('ControllerPaginateModel'),
'group' => 'Comment.author_id', 'group' => 'Comment.author_id',
'type' => 'foo', 'type' => 'foo',
'maxLimit' => 10 'maxLimit' => 10,
'paramType' => 'named'
); );
$this->assertEqual($Controller->ControllerPaginateModel->extra, $expected); $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
$this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected); $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
@ -401,7 +415,8 @@ class PaginatorTest extends CakeTestCase {
'limit' => 5, 'limit' => 5,
'page' => 1, 'page' => 1,
'recursive' => -1, 'recursive' => -1,
'maxLimit' => 10 'maxLimit' => 10,
'paramType' => 'named'
); );
$conditions = array(); $conditions = array();
$Controller->Paginator->paginate('PaginatorControllerPost', $conditions); $Controller->Paginator->paginate('PaginatorControllerPost', $conditions);
@ -413,7 +428,8 @@ class PaginatorTest extends CakeTestCase {
'maxLimit' => 10, 'maxLimit' => 10,
'page' => 1, 'page' => 1,
'recursive' => -1, 'recursive' => -1,
'conditions' => array() 'conditions' => array(),
'paramType' => 'named'
); );
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options'],$expected); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options'],$expected);
} }
@ -434,7 +450,9 @@ class PaginatorTest extends CakeTestCase {
$Controller->constructClasses(); $Controller->constructClasses();
$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
'PaginatorControllerPost' => array('popular', 'fields' => array('id', 'title'), 'maxLimit' => 10) 'PaginatorControllerPost' => array(
'popular', 'fields' => array('id', 'title'), 'maxLimit' => 10, 'paramType' => 'named'
)
); );
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
@ -458,7 +476,9 @@ class PaginatorTest extends CakeTestCase {
$Controller->modelClass = 'PaginatorControllerPost'; $Controller->modelClass = 'PaginatorControllerPost';
$Controller->params['url'] = array(); $Controller->params['url'] = array();
$Controller->constructClasses(); $Controller->constructClasses();
$Controller->Paginator->settings = array('order' => 'PaginatorControllerPost.id DESC', 'maxLimit' => 10); $Controller->Paginator->settings = array(
'order' => 'PaginatorControllerPost.id DESC', 'maxLimit' => 10, 'paramType' => 'named'
);
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id'); $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['defaults']['order'], 'PaginatorControllerPost.id DESC'); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['defaults']['order'], 'PaginatorControllerPost.id DESC');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['order'], 'PaginatorControllerPost.id DESC'); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['order'], 'PaginatorControllerPost.id DESC');
@ -485,7 +505,8 @@ class PaginatorTest extends CakeTestCase {
$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
'fields' => array('id', 'title', 'offset_test'), 'fields' => array('id', 'title', 'offset_test'),
'order' => array('offset_test' => 'DESC'), 'order' => array('offset_test' => 'DESC'),
'maxLimit' => 10 'maxLimit' => 10,
'paramType' => 'named'
); );
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.offset_test'), array(4, 3, 2)); $this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.offset_test'), array(4, 3, 2));
@ -539,7 +560,7 @@ class PaginatorTest extends CakeTestCase {
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 10); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 10);
$Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000'); $Controller->passedArgs = array('contain' => array('ControllerComment'), 'limit' => '1000');
$Controller->paginate = array('maxLimit' => 2000); $Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named');
$result = $Controller->paginate('PaginatorControllerPost'); $result = $Controller->paginate('PaginatorControllerPost');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 1000); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 1000);