Skip to content

Commit

Permalink
Use concrete entities on missing data.
Browse files Browse the repository at this point in the history
When an association is undefined return a concrete entity class. This is
necessary as notEmpty/allowEmpty callbacks could reference the entity
provider.

Refs #5190
  • Loading branch information
markstory committed Nov 19, 2014
1 parent 3f4e79d commit e695fe4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/View/Form/EntityContext.php
Expand Up @@ -268,7 +268,8 @@ protected function _getEntity($path) {
$isLast = ($i === $last);

if (!$isLast && $next === null && $prop !== '_ids') {
return new Entity();
$table = $this->_getTable($path);
return $table->newEntity();
}

$isTraversable = (
Expand Down
41 changes: 41 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -29,6 +29,15 @@
* Test stub.
*/
class Article extends Entity {

/**
* Testing stub method.
*
* @return bool
*/
public function isRequired() {
return true;
}
}

/**
Expand Down Expand Up @@ -647,6 +656,37 @@ public function testIsRequiredAssociatedHasMany() {
$this->assertFalse($context->isRequired(''));
}

/**
* Test isRequired on associated entities with custom validators.
*
* Ensures that missing associations use the correct entity class
* so provider methods work correctly.
*
* @return void
*/
public function testIsRequiredAssociatedCustomValidator() {
$this->_setupTables();
$users = TableRegistry::get('Users');
$articles = TableRegistry::get('Articles');

$validator = $articles->validator();
$validator->notEmpty('title', 'nope', function ($context) {
return $context['providers']['entity']->isRequired();
});
$articles->validator('default', $validator);

$row = new Entity([
'username' => 'mark'
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Users',
'validator' => 'default',
]);

$this->assertTrue($context->isRequired('articles.0.title'));
}

/**
* Test isRequired on associated entities.
*
Expand Down Expand Up @@ -943,6 +983,7 @@ protected function _setupTables() {
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Users');
$articles->hasMany('Comments');
$articles->entityClass(__NAMESPACE__ . '\Article');

$comments = TableRegistry::get('Comments');
$users = TableRegistry::get('Users');
Expand Down

0 comments on commit e695fe4

Please sign in to comment.