/** * testValidateAssociated method * * @return void */ public function testValidateAssociated() { $this->loadFixtures('Attachment', 'Boat', 'Comment'); $TestModel = new Comment(); $TestModel->Attachment->validate = array('attachment' => 'notBlank'); $data = array('Comment' => array('comment' => 'This is the comment'), 'Attachment' => array('attachment' => '')); $result = $TestModel->validateAssociated($data); $this->assertFalse($result); $TestModel = new Boat(); $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); $TestModel->Comment->validate = array('comment' => 'notBlank'); $data = array('Boat' => array('id' => 2), 'Comment' => array(array('id' => 1, 'comment' => '', 'published' => 'Y', 'user_id' => 1), array('id' => 2, 'comment' => 'comment', 'published' => 'Y', 'user_id' => 1))); $result = $TestModel->validateAssociated($data); $this->assertFalse($result); $data = array('Boat' => array('id' => 2), 'Comment' => array(array('id' => 1, 'comment' => '', 'published' => 'Y', 'user_id' => 1), array('id' => 2, 'comment' => 'comment', 'published' => 'Y', 'user_id' => 1), array('id' => 3, 'comment' => '', 'published' => 'Y', 'user_id' => 1))); $result = $TestModel->validateAssociated($data, array('atomic' => false)); $expected = array('Boat' => true, 'Comment' => array(false, true, false)); $this->assertSame($expected, $result); $expected = array('Comment' => array(0 => array('comment' => array('This field cannot be left blank')), 2 => array('comment' => array('This field cannot be left blank')))); $this->assertEquals($expected, $TestModel->validationErrors); $expected = array(0 => array('comment' => array('This field cannot be left blank')), 2 => array('comment' => array('This field cannot be left blank'))); $this->assertEquals($expected, $TestModel->Comment->validationErrors); }
/** * testValidateAssociated method * * @return void */ public function testValidateAssociated() { $this->loadFixtures('Comment', 'Attachment', 'Boat', 'User'); $TestModel = new Comment(); $TestModel->Attachment->validate = array('attachment' => 'notBlank'); $data = array('Comment' => array('comment' => 'This is the comment'), 'Attachment' => array('attachment' => '')); $result = $TestModel->saveAll($data, array('validate' => 'only')); $this->assertFalse($result); $result = $TestModel->validateAssociated($data); $this->assertFalse($result); $fieldList = array('Attachment' => array('comment_id')); $result = $TestModel->saveAll($data, array('fieldList' => $fieldList, 'validate' => 'only')); $this->assertTrue($result); $this->assertEmpty($TestModel->validationErrors); $result = $TestModel->validateAssociated($data, array('fieldList' => $fieldList)); $this->assertTrue($result); $this->assertEmpty($TestModel->validationErrors); $TestModel->validate = array('comment' => 'notBlank'); $record = array('Comment' => array('user_id' => 1, 'article_id' => 1, 'comment' => ''), 'Attachment' => array('attachment' => '')); $result = $TestModel->saveAll($record, array('validate' => 'only')); $this->assertFalse($result); $result = $TestModel->validateAssociated($record); $this->assertFalse($result); $fieldList = array('Comment' => array('id', 'article_id', 'user_id'), 'Attachment' => array('comment_id')); $result = $TestModel->saveAll($record, array('fieldList' => $fieldList, 'validate' => 'only')); $this->assertTrue($result); $this->assertEmpty($TestModel->validationErrors); $result = $TestModel->validateAssociated($record, array('fieldList' => $fieldList)); $this->assertTrue($result); $this->assertEmpty($TestModel->validationErrors); $TestModel = new Boat(); $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); $TestModel->Comment->validate = array('comment' => 'notBlank'); $data = array('Boat' => array('id' => 2), 'Comment' => array(array('id' => 1, 'comment' => '', 'published' => 'Y', 'user_id' => 1), array('id' => 2, 'comment' => 'comment', 'published' => 'Y', 'user_id' => 1), array('id' => 3, 'comment' => '', 'published' => 'Y', 'user_id' => 1))); $result = $TestModel->saveAll($data, array('validate' => 'only')); $this->assertFalse($result); $result = $TestModel->validateAssociated($data); $this->assertFalse($result); $expected = array('Boat' => true, 'Comment' => array(false, true, false)); $result = $TestModel->saveAll($data, array('atomic' => false, 'validate' => 'only')); $this->assertSame($expected, $result); $result = $TestModel->validateAssociated($data, array('atomic' => false)); $this->assertSame($expected, $result); $expected = array('Comment' => array(0 => array('comment' => array('This field cannot be left blank')), 2 => array('comment' => array('This field cannot be left blank')))); $this->assertEquals($expected['Comment'], $TestModel->Comment->validationErrors); $model = new Comment(); $model->deleteAll(true); $model->validate = array('comment' => 'notBlank'); $model->Attachment->validate = array('attachment' => 'notBlank'); $model->Attachment->bindModel(array('belongsTo' => array('Comment'))); $expected = array('comment' => array('This field cannot be left blank'), 'Attachment' => array('attachment' => array('This field cannot be left blank'))); $data = array('Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1), 'Attachment' => array('attachment' => '')); $result = $model->saveAll($data, array('validate' => 'only')); $this->assertFalse($result); $result = $model->validateAssociated($data); $this->assertFalse($result); $this->assertEquals($expected, $model->validationErrors); $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors); }