diff --git a/lib/Cake/Model/Behavior/TreeBehavior.php b/lib/Cake/Model/Behavior/TreeBehavior.php index fd8d3f724..dc5c22085 100644 --- a/lib/Cake/Model/Behavior/TreeBehavior.php +++ b/lib/Cake/Model/Behavior/TreeBehavior.php @@ -375,6 +375,12 @@ class TreeBehavior extends ModelBehavior { } else { array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix'); } + + $conditions = (array)$conditions; + if ($scope) { + $conditions[] = $scope; + } + $order = $Model->escapeField($left) . " asc"; $results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive')); $stack = array(); diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php index ce452fbac..8f809b51e 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php @@ -340,4 +340,44 @@ class TreeBehaviorScopedTest extends CakeTestCase { )); $this->assertEquals($expected, $result); } + +/** + * testGenerateTreeListWithScope method + * + * @return void + */ + public function testGenerateTreeListWithScope() { + extract($this->settings); + $this->Tree = new $modelClass(); + $this->Tree->order = null; + $this->Tree->initialize(2, 3); + + $this->Tree->id = 1; + $this->Tree->saveField('flag', 1); + $this->Tree->id = 2; + $this->Tree->saveField('flag', 1); + + $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1))); + + $result = $this->Tree->generateTreeList(); + $expected = array( + 1 => '1. Root', + 2 => '_1.1' + ); + $this->assertEquals($expected, $result); + + // As string. + $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1')); + + $result = $this->Tree->generateTreeList(); + $this->assertEquals($expected, $result); + + // Merging conditions. + $result = $this->Tree->generateTreeList(array('FlagTree.id >' => 1)); + $expected = array( + 2 => '1.1' + ); + $this->assertEquals($expected, $result); + } + }