Skip to content
Permalink
Browse files

Fix empty collections being treated as EntityContext.

An empty collection should not fall into the EntityContext as it will
cause errors downstream. Instead of just sniffing against Travserable,
using a temporary Collection to access the first element will give
a better test of what is in an object/array.

Fixes #4698
  • Loading branch information...
markstory committed Sep 24, 2014
1 parent d5828d0 commit e7a85825e3a967cb6aeee0c31268e408616b7390
Showing with 16 additions and 5 deletions.
  1. +11 −5 src/View/Helper/FormHelper.php
  2. +5 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -16,6 +16,7 @@
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Collection\Collection;
use Cake\ORM\Entity;
use Cake\Routing\Router;
use Cake\Utility\Hash;
@@ -216,11 +217,16 @@ protected function _addDefaultContextProviders() {
});
$this->addContextProvider('orm', function ($request, $data) {
if (
$data['entity'] instanceof Entity ||
$data['entity'] instanceof Traversable ||
(is_array($data['entity']) && !isset($data['entity']['schema']))
) {
if (is_array($data['entity']) || $data['entity'] instanceof Traversable) {
$pass = (new Collection($data['entity']))->first() !== null;
if ($pass) {
return new EntityContext($request, $data);
}
}
if ($data['entity'] instanceof Entity) {
return new EntityContext($request, $data);
}
if (is_array($data['entity']) && empty($data['entity']['schema'])) {
return new EntityContext($request, $data);
}
});
@@ -272,6 +272,9 @@ public function testAddContextProviderInvalid() {
public function contextSelectionProvider() {
$entity = new Article();
$collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
$emptyCollection = new Collection([]);
$emptyArray = [];
$arrayObject = new \ArrayObject([]);
$data = [
'schema' => [
'title' => ['type' => 'string']
@@ -281,7 +284,9 @@ public function contextSelectionProvider() {
return [
'entity' => [$entity, 'Cake\View\Form\EntityContext'],
'collection' => [$collection, 'Cake\View\Form\EntityContext'],
'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
'array' => [$data, 'Cake\View\Form\ArrayContext'],
'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
'none' => [null, 'Cake\View\Form\NullContext'],
'false' => [false, 'Cake\View\Form\NullContext'],
];

0 comments on commit e7a8582

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