From c48afae2dc8a3db3cdf5e600222e9ee39254ef61 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Fri, 27 Dec 2013 12:35:28 +0700 Subject: [PATCH 1/2] Add test to prove issue when saving invalid tree data --- .../Model/Behavior/TreeBehaviorScopedTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php index c7ea1fba2..5cf3e4cbf 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php @@ -130,6 +130,29 @@ class TreeBehaviorScopedTest extends CakeTestCase { $this->assertEquals(11, $this->Tree->find('count')); } +/** + * testSaveWithParentAndInvalidScope method + * + * Attempting to save an invalid data should not trigger an `Undefined offset` + * error + * + * @return void + */ + public function testSaveWithParentAndInvalidScope() { + $this->Tree = new FlagTree(); + $this->Tree->order = null; + $data = $this->Tree->create(array( + 'name' => 'Flag', + )); + $tree = $this->Tree->save($data); + $this->Tree->Behaviors->load('Tree', array( + 'scope' => array('FlagTree.flag' => 100) + )); + $tree['FlagTree']['parent_id'] = 1; + $result = $this->Tree->save($tree); + $this->assertFalse($result); + } + /** * testMoveUpWithScope method * From c4f8289b539143b3d112596eb26a8ac6f0086324 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Fri, 27 Dec 2013 12:37:39 +0700 Subject: [PATCH 2/2] Fix test by checking against an empty array instead of `false` --- lib/Cake/Model/Behavior/TreeBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Model/Behavior/TreeBehavior.php b/lib/Cake/Model/Behavior/TreeBehavior.php index 9c09e0f91..ce64d6c0c 100644 --- a/lib/Cake/Model/Behavior/TreeBehavior.php +++ b/lib/Cake/Model/Behavior/TreeBehavior.php @@ -212,7 +212,7 @@ class TreeBehavior extends ModelBehavior { 'fields' => array($Model->primaryKey, $parent, $left, $right), 'recursive' => $recursive) ); - if ($values === false) { + if (empty($values)) { return false; } list($node) = array_values($values);