Skip to content

Commit

Permalink
Fix empty collections being treated as EntityContext.
Browse files Browse the repository at this point in the history
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 25, 2014
1 parent d5828d0 commit e7a8582
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/View/Helper/FormHelper.php
Expand Up @@ -16,6 +16,7 @@


use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Core\Exception\Exception; use Cake\Core\Exception\Exception;
use Cake\Collection\Collection;
use Cake\ORM\Entity; use Cake\ORM\Entity;
use Cake\Routing\Router; use Cake\Routing\Router;
use Cake\Utility\Hash; use Cake\Utility\Hash;
Expand Down Expand Up @@ -216,11 +217,16 @@ protected function _addDefaultContextProviders() {
}); });


$this->addContextProvider('orm', function ($request, $data) { $this->addContextProvider('orm', function ($request, $data) {
if ( if (is_array($data['entity']) || $data['entity'] instanceof Traversable) {
$data['entity'] instanceof Entity || $pass = (new Collection($data['entity']))->first() !== null;
$data['entity'] instanceof Traversable || if ($pass) {
(is_array($data['entity']) && !isset($data['entity']['schema'])) 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); return new EntityContext($request, $data);
} }
}); });
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -272,6 +272,9 @@ public function testAddContextProviderInvalid() {
public function contextSelectionProvider() { public function contextSelectionProvider() {
$entity = new Article(); $entity = new Article();
$collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]); $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
$emptyCollection = new Collection([]);
$emptyArray = [];
$arrayObject = new \ArrayObject([]);
$data = [ $data = [
'schema' => [ 'schema' => [
'title' => ['type' => 'string'] 'title' => ['type' => 'string']
Expand All @@ -281,7 +284,9 @@ public function contextSelectionProvider() {
return [ return [
'entity' => [$entity, 'Cake\View\Form\EntityContext'], 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
'collection' => [$collection, 'Cake\View\Form\EntityContext'], 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
'array' => [$data, 'Cake\View\Form\ArrayContext'], 'array' => [$data, 'Cake\View\Form\ArrayContext'],
'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
'none' => [null, 'Cake\View\Form\NullContext'], 'none' => [null, 'Cake\View\Form\NullContext'],
'false' => [false, 'Cake\View\Form\NullContext'], 'false' => [false, 'Cake\View\Form\NullContext'],
]; ];
Expand Down

0 comments on commit e7a8582

Please sign in to comment.