Skip to content

Commit

Permalink
Fix incorrect entity being used for missing indexes.
Browse files Browse the repository at this point in the history
When reading non-existing entities after existing ones have been read,
we should not re-use the previous context by accident. Instead a new
blank entity should be used. This properly simulates the state of the
missing entity, as it will be missing errors, and all attributes.

Refs #4769
  • Loading branch information
markstory committed Oct 3, 2014
1 parent 72a892a commit cd0c5c7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/View/Form/EntityContext.php
Expand Up @@ -267,7 +267,7 @@ protected function _getEntity($path) {
$next = $this->_getProp($entity, $prop);

if ($next === null && $prop !== '_ids') {
return false;
return new Entity();
}

$isLast = ($i === $last && isset($next));
Expand Down
67 changes: 67 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -648,6 +648,42 @@ public function testIsRequiredAssociatedHasMany() {
$this->assertFalse($context->isRequired(''));
}

/**
* Test isRequired on associated entities.
*
* @return void
*/
public function testIsRequiredAssociatedHasManyMissingObject() {
$this->_setupTables();

$comments = TableRegistry::get('Comments');
$validator = $comments->validator();
$validator->allowEmpty('comment', function ($context) {
return $context['providers']['entity']->isNew();
});

$row = new Article([
'title' => 'My title',
'comments' => [
new Entity(['comment' => 'First comment'], ['markNew' => false]),
]
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
'validator' => 'default',
]);

$this->assertTrue(
$context->isRequired('comments.0.comment'),
'comment is required as object is not new'
);
$this->assertFalse(
$context->isRequired('comments.1.comment'),
'comment is not required as missing object is "new"'
);
}

/**
* Test isRequired on associated entities with custom validators.
*
Expand Down Expand Up @@ -864,6 +900,37 @@ public function testError() {
$this->assertEquals($expected, $context->error('user.username'));
}

/**
* Test error on associated entities.
*
* @return void
*/
public function testErrorAssociatedHasMany() {
$this->_setupTables();

$comments = TableRegistry::get('Comments');
$row = new Article([
'title' => 'My title',
'comments' => [
new Entity(['comment' => '']),
new Entity(['comment' => 'Second comment']),
]
]);
$row->comments[0]->errors('comment', ['Is required']);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
'validator' => 'default',
]);

$this->assertEquals([], $context->error('title'));
$this->assertEquals([], $context->error('comments.0.user_id'));
$this->assertEquals([], $context->error('comments.0'));
$this->assertEquals(['Is required'], $context->error('comments.0.comment'));
$this->assertEquals([], $context->error('comments.1'));
$this->assertEquals([], $context->error('comments.1.comment'));
}

/**
* Setup tables for tests.
*
Expand Down

0 comments on commit cd0c5c7

Please sign in to comment.