Skip to content

Commit

Permalink
Cleaning up commented and unneeded test cases and moving tests to mor…
Browse files Browse the repository at this point in the history
…e appropriate file
  • Loading branch information
ADmad committed Jan 20, 2010
1 parent 6e22261 commit 570c9e5
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 434 deletions.
18 changes: 2 additions & 16 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -3945,20 +3945,6 @@ function testSaveEmpty() {
$expected = $TestModel->save($data);
$this->assertFalse($expected);
}
// function testBasicValidation() {
// $TestModel =& new ValidationTest1();
// $TestModel->testing = true;
// $TestModel->set(array('title' => '', 'published' => 1));
// $this->assertEqual($TestModel->invalidFields(), array('title' => 'This field cannot be left blank'));
//
// $TestModel->create();
// $TestModel->set(array('title' => 'Hello', 'published' => 0));
// $this->assertEqual($TestModel->invalidFields(), array('published' => 'This field cannot be left blank'));
//
// $TestModel->create();
// $TestModel->set(array('title' => 'Hello', 'published' => 1, 'body' => ''));
// $this->assertEqual($TestModel->invalidFields(), array('body' => 'This field cannot be left blank'));
// }

/**
* testFindAllWithConditionInChildQuery
Expand Down Expand Up @@ -7150,8 +7136,8 @@ function testRecursiveFindAllWithLimit() {
}
/**
* Testing availability of $this->findQueryType in Model callbacks
*
* @return void
*
* @return void
*/
function testFindQueryTypeInCallbacks() {
$this->loadFixtures('Comment');
Expand Down
353 changes: 353 additions & 0 deletions cake/tests/cases/libs/model/model_validation.test.php
Expand Up @@ -169,6 +169,359 @@ function testInvalidFieldsWithFieldListParams() {
$this->assertEqual($TestModel->validate, $validate);
}

/**
* testValidates method
*
* @access public
* @return void
*/
function testValidates() {
$TestModel =& new TestValidate();

$TestModel->validate = array(
'user_id' => 'numeric',
'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
'body' => 'notEmpty'
);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => '',
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 'title',
'body' => 'body'
));
$result = $TestModel->create($data) && $TestModel->validates();
$this->assertTrue($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => '0',
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => ''
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => '2007-05-01'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => 'invalid-date-here'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => 0
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'modified' => '0'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');

$data = array('TestValidate' => array('modified' => null));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array('modified' => false));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array('modified' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'modified' => '2007-05-01'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'slug' => ''
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'slug' => 'slug-right-here'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$data = array('TestValidate' => array(
'user_id' => '1',
'title' => 0,
'body' => 'body',
'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$TestModel->validate = array(
'number' => array(
'rule' => 'validateNumber',
'min' => 3,
'max' => 5
),
'title' => array(
'allowEmpty' => false,
'rule' => 'notEmpty'
));

$data = array('TestValidate' => array(
'title' => 'title',
'number' => '0'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'title' => 'title',
'number' => 0
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'title' => 'title',
'number' => '3'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$data = array('TestValidate' => array(
'title' => 'title',
'number' => 3
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate = array(
'number' => array(
'rule' => 'validateNumber',
'min' => 5,
'max' => 10
),
'title' => array(
'allowEmpty' => false,
'rule' => 'notEmpty'
));

$data = array('TestValidate' => array(
'title' => 'title',
'number' => '3'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'title' => 'title',
'number' => 3
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$TestModel->validate = array(
'title' => array(
'allowEmpty' => false,
'rule' => 'validateTitle'
));

$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array('title' => 'new title'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array('title' => 'title-new'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate = array('title' => array(
'allowEmpty' => true,
'rule' => 'validateTitle'
));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate = array(
'title' => array(
'length' => array(
'allowEmpty' => true,
'rule' => array('maxLength', 10)
)));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate = array(
'title' => array(
'rule' => array('userDefined', 'Article', 'titleDuplicate')
));
$data = array('TestValidate' => array('title' => 'My Article Title'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array('TestValidate' => array(
'title' => 'My Article With a Different Title'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$result = $TestModel->validates();
$this->assertTrue($result);

$TestModel->validate = array(
'title' => array(
'tooShort' => array('rule' => array('minLength', 50)),
'onlyLetters' => array('rule' => '/^[a-z]+$/i')
),
);
$data = array('TestValidate' => array(
'title' => 'I am a short string'
));
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'onlyLetters'
);
$this->assertEqual($result, $expected);

$TestModel->validate = array(
'title' => array(
'tooShort' => array(
'rule' => array('minLength', 50),
'last' => true
),
'onlyLetters' => array('rule' => '/^[a-z]+$/i')
),
);
$data = array('TestValidate' => array(
'title' => 'I am a short string'
));
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'tooShort'
);
$this->assertEqual($result, $expected);
}

/**
* test that validates() checks all the 'with' associations as well for validation
* as this can cause partial/wrong data insertion.
Expand Down

0 comments on commit 570c9e5

Please sign in to comment.