Added unittest for saveAll() validation with the data no id assigned.

This commit is contained in:
Yosuke Basuke Suzuki 2011-09-23 22:23:26 +09:00
parent 36928d35f9
commit 5612d411f3

View file

@ -639,6 +639,48 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEqual($joinRecords, 0, 'Records were saved on the join table. %s'); $this->assertEqual($joinRecords, 0, 'Records were saved on the join table. %s');
} }
/**
* test that saveAll and with models at initial insert (no id has set yet)
* with validation interact well
*
* @return void
*/
public function testValidatesWithModelsAndSaveAllWithoutId() {
$this->loadFixtures('Post', 'Author');
$data = array(
'Author' => array(
'name' => 'Foo Bar',
),
'Post' => array(
array('title' => 'Hello'),
array('title' => 'World'),
)
);
$Author = new Author();
$Post = $Author->Post;
$Post->validate = array('author_id' => array('rule' => 'numeric'));
$Author->create();
$result = $Author->saveAll($data, array('validate' => 'only'));
$this->assertTrue($result);
$Author->create();
$result = $Author->saveAll($data, array('validate' => 'first'));
$this->assertTrue($result);
$this->assertFalse(is_null($Author->id));
$id = $Author->id;
$count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
$this->assertIdentical($count, 1);
$count = $Post->find('count', array(
'conditions' => array('Post.author_id' => $id)
));
$this->assertEqual($count, count($data['Post']));
}
/** /**
* Test that missing validation methods trigger errors in development mode. * Test that missing validation methods trigger errors in development mode.
* Helps to make developement easier. * Helps to make developement easier.