Skip to content
Permalink
Browse files

Documenting function and addng another test case

  • Loading branch information...
lorenzo committed Nov 17, 2013
1 parent 0e88f15 commit edd3796db79e6ca946a3962e065599173f3080d8
Showing with 39 additions and 2 deletions.
  1. +17 −2 Cake/ORM/Entity.php
  2. +22 −0 Cake/Test/TestCase/ORM/EntityTest.php
@@ -443,7 +443,22 @@ public function isNew($new = null) {
return $this->_persisted = (bool)$new;
}
public function validate(Validator $validator, array $fieldList) {
/**
* Validates the internal properties using a validator object. The resulting
* errors will be copied inside this entity and can be retrieved using the
* `errors` method.
*
* The second argument can be used to restrict the fields that need to be passed
* to the validator object.
*
* This function returns true if there were no validation errors or false
* otherwise.
*
* @param \Cake\Validation\Validator $validator
* @param array $fieldList The fields to be passed to the validator
* @return boolean
*/
public function validate(Validator $validator, array $fieldList = []) {
if (empty($fieldList)) {
$fieldList = array_keys($this->_properties);
}
@@ -460,7 +475,7 @@ public function validate(Validator $validator, array $fieldList) {
}
$new = $this->isNew();
$this->errors($validator->errors($data, $new === null ? true : false));
$this->errors($validator->errors($data, $new === null ? true : $new));
return empty($this->_errors);
}
@@ -690,4 +690,26 @@ public function testValidateOnlyFieldsInList() {
$this->assertEquals(['something' => ['not valid']], $entity->errors());
}
/**
* Tests validate when called with no fieldList, it also tests the case when
* the validator returns no errors
*
* @return void
*/
public function testValidateNoFieldList() {
$validator = $this->getMock('\Cake\Validation\Validator');
$data = [
'a' => 'b',
'cool' => false,
'something' => true
];
$entity = new Entity($data);
$entity->isNew(true);
$validator->expects($this->once())->method('errors')
->with($data, true)
->will($this->returnValue([]));
$this->assertTrue($entity->validate($validator));
$this->assertEquals([], $entity->errors());
}
}

0 comments on commit edd3796

Please sign in to comment.
You can’t perform that action at this time.