Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Migrating ModelValidationTest to phpunit
  • Loading branch information
lorenzo committed Jun 1, 2010
1 parent 375dde8 commit 7d6ac75
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
1 change: 1 addition & 0 deletions cake/libs/model/model.php
Expand Up @@ -1722,6 +1722,7 @@ function saveAll($data = null, $options = array()) {
break;
}
}
return $return;
}

/**
Expand Down
22 changes: 6 additions & 16 deletions cake/tests/cases/libs/model/model.test.php
Expand Up @@ -69,35 +69,25 @@ abstract class BaseModelTest extends CakeTestCase {
);

/**
* start method
* setUp method
*
* @access public
* @return void
*/
function start() {
parent::start();
function setUp() {
parent::setUp();
$this->debug = Configure::read('debug');
Configure::write('debug', 2);
}

/**
* end method
* tearDown method
*
* @access public
* @return void
*/
function end() {
parent::end();
function tearDown() {
parent::tearDown();
Configure::write('debug', $this->debug);
}

/**
* endTest method
*
* @access public
* @return void
*/
function endTest() {
ClassRegistry::flush();
}
}
80 changes: 47 additions & 33 deletions cake/tests/cases/libs/model/model_validation.test.php
Expand Up @@ -190,7 +190,7 @@ function testValidates() {
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -208,7 +208,7 @@ function testValidates() {
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -218,7 +218,7 @@ function testValidates() {
'body' => 'body'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -231,7 +231,7 @@ function testValidates() {
'modified' => ''
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -242,7 +242,7 @@ function testValidates() {
'modified' => '2007-05-01'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -253,7 +253,7 @@ function testValidates() {
'modified' => 'invalid-date-here'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -264,7 +264,7 @@ function testValidates() {
'modified' => 0
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -275,35 +275,35 @@ function testValidates() {
'modified' => '0'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$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);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

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

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

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

Expand All @@ -316,7 +316,7 @@ function testValidates() {
'slug' => ''
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -327,7 +327,7 @@ function testValidates() {
'slug' => 'slug-right-here'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -338,7 +338,7 @@ function testValidates() {
'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -358,7 +358,7 @@ function testValidates() {
'number' => '0'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -367,7 +367,7 @@ function testValidates() {
'number' => 0
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -376,7 +376,7 @@ function testValidates() {
'number' => '3'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -385,7 +385,7 @@ function testValidates() {
'number' => 3
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -405,7 +405,7 @@ function testValidates() {
'number' => '3'
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -414,7 +414,7 @@ function testValidates() {
'number' => 3
));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

Expand All @@ -426,19 +426,19 @@ function testValidates() {

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

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

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

Expand All @@ -448,7 +448,7 @@ function testValidates() {
));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -460,7 +460,7 @@ function testValidates() {
)));
$data = array('TestValidate' => array('title' => ''));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertTrue($result);

Expand All @@ -470,15 +470,15 @@ function testValidates() {
));
$data = array('TestValidate' => array('title' => 'My Article Title'));
$result = $TestModel->create($data);
$this->assertTrue($result);
$this->assertEquals($result, $data);
$result = $TestModel->validates();
$this->assertFalse($result);

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

Expand Down Expand Up @@ -529,6 +529,7 @@ function testValidates() {
* @return void
*/
function testValidatesWithAssociations() {
$this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
$data = array(
'Something' => array(
'id' => 5,
Expand Down Expand Up @@ -607,7 +608,7 @@ function testValidatesWithModelsAndSaveAll() {

$Something->create();
$result = $Something->saveAll($data, array('validate' => 'first'));
$this->assertFalse($result);
$this->assertEquals($result, array());
$this->assertEqual($JoinThing->validationErrors, $expectedError);

$count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
Expand All @@ -626,7 +627,6 @@ function testValidatesWithModelsAndSaveAll() {
* @return void
*/
function testMissingValidationErrorTriggering() {
$restore = Configure::read('debug');
Configure::write('debug', 2);

$TestModel =& new ValidationTest1();
Expand All @@ -637,13 +637,27 @@ function testMissingValidationErrorTriggering() {
'required' => true
)
);
$this->expectError(new PatternExpectation('/thisOneBringsThePain for title/i'));
$this->expectError();
$TestModel->invalidFields(array('fieldList' => array('title')));
}

/**
* Test that missing validation methods does not trigger errors in production mode.
*
* @return void
*/
function testMissingValidationErrorNoTriggering() {
Configure::write('debug', 0);
$this->assertNoErrors();
$TestModel =& new ValidationTest1();
$TestModel->create(array('title' => 'foo'));
$TestModel->validate = array(
'title' => array(
'rule' => array('thisOneBringsThePain'),
'required' => true
)
);
$TestModel->invalidFields(array('fieldList' => array('title')));
Configure::write('debug', $restore);
$this->assertEquals($TestModel->validationErrors, array());
}

}
2 changes: 1 addition & 1 deletion cake/tests/cases/libs/model/models.php
Expand Up @@ -174,7 +174,7 @@ function validateNumber($value, $options) {
* @return void
*/
function validateTitle($value) {
return (!empty($value) && strpos(low($value['title']), 'title-') === 0);
return (!empty($value) && strpos(strtolower($value['title']), 'title-') === 0);
}
}

Expand Down

0 comments on commit 7d6ac75

Please sign in to comment.