Ensuring list of behavior methods are refreshed between calls of

validates() in the model, fixes #3071
This commit is contained in:
Jose Lorenzo Rodriguez 2012-08-25 14:05:19 +02:00
parent 60eb228ddf
commit f6278488af
2 changed files with 51 additions and 4 deletions

View file

@ -63,6 +63,20 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
*/
protected $_methods = array();
/**
* Holds the available custom callback methods from the model
*
* @var array
*/
protected $_modelMethods = array();
/**
* Holds the list of behavior names that were attached when this object was created
*
* @var array
*/
protected $_behaviors = array();
/**
* Constructor
*
@ -280,15 +294,19 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* @return array List of callables to be used as validation methods
*/
public function getMethods() {
if (!empty($this->_methods)) {
$behaviors = $this->_model->Behaviors->enabled();
if (!empty($this->_methods) && $behaviors === $this->_behaviors) {
return $this->_methods;
}
$this->_behaviors = $behaviors;
$methods = array();
foreach (get_class_methods($this->_model) as $method) {
$methods[strtolower($method)] = array($this->_model, $method);
if (empty($this->_modelMethods)) {
foreach (get_class_methods($this->_model) as $method) {
$this->_modelMethods[strtolower($method)] = array($this->_model, $method);
}
}
$methods = $this->_modelMethods;
foreach (array_keys($this->_model->Behaviors->methods()) as $method) {
$methods += array(strtolower($method) => array($this->_model, $method));
}

View file

@ -1700,6 +1700,35 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals($expected, array_keys($result));
}
/**
* Tests that methods are refreshed when the list of behaviors change
*
* @return void
*/
public function testGetMethodsRefresh() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$Validator = $TestModel->validator();
$result = $Validator->getMethods();
$expected = array_map('strtolower', get_class_methods('Article'));
$this->assertEquals($expected, array_keys($result));
$TestModel->Behaviors->attach('Containable');
$newList = array(
'contain',
'resetbindings',
'containments',
'fielddependencies',
'containmentsmap'
);
$this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
$TestModel->Behaviors->detach('Containable');
$this->assertEquals($expected, array_keys($Validator->getMethods()));
}
/**
* testSetValidationDomain method
*