From e5d7f5279ed4409f14387e370c0d79d13e650d2f Mon Sep 17 00:00:00 2001 From: the_undefined Date: Mon, 19 May 2008 03:03:44 +0000 Subject: [PATCH] Merged relevant test cases from 'alkemann' into tree test and made them pass, fixes #4493 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6939 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/behaviors/tree.php | 34 ++++++++++++------- .../cases/libs/model/behaviors/tree.test.php | 34 ++++++++++++++++++- cake/tests/fixtures/ad_fixture.php | 25 ++++++++++++++ cake/tests/fixtures/campaign_fixture.php | 16 +++++++++ 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 cake/tests/fixtures/ad_fixture.php create mode 100644 cake/tests/fixtures/campaign_fixture.php diff --git a/cake/libs/model/behaviors/tree.php b/cake/libs/model/behaviors/tree.php index d0443c763..ef11808a4 100644 --- a/cake/libs/model/behaviors/tree.php +++ b/cake/libs/model/behaviors/tree.php @@ -240,7 +240,9 @@ class TreeBehavior extends ModelBehavior { } $name = $model->alias; extract($this->settings[$name]); - + if ($scope != '1 = 1') { + $recursive = 0; + } if (!$order) { $order = $model->alias . '.' . $left . ' asc'; } @@ -251,7 +253,7 @@ class TreeBehavior extends ModelBehavior { if (!$id) { $constraint = $scope; } else { - @list($item) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => -1))); + @list($item) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => $recursive))); $constraint = array($scope, $model->escapeField($right) . '< ' . $item[$right], $model->escapeField($left) => '> ' . $item[$left]); } return $model->find('all', array('conditions' => $constraint, 'fields' => $fields, 'order' => $order, 'limit' => $limit, 'page' => $page, 'recursive' => $recursive)); @@ -380,27 +382,31 @@ class TreeBehavior extends ModelBehavior { $id = $model->id; } extract($this->settings[$model->alias]); + $recursive = -1; + if ($scope != '1 = 1') { + $recursive = 0; + } list($node) = array_values($model->find('first', array( 'conditions' => array($scope, $model->escapeField() => $id), - 'fields' => array($model->primaryKey, $left, $right, $parent), 'recursive' => -1 + 'fields' => array($model->primaryKey, $left, $right, $parent), 'recursive' => $recursive ))); if ($node[$parent]) { list($parentNode) = array_values($model->find('first', array( 'conditions' => array($scope, $model->escapeField() => $node[$parent]), - 'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1 + 'fields' => array($model->primaryKey, $left, $right), 'recursive' => $recursive ))); if (($node[$right] + 1) == $parentNode[$right]) { return false; } } $nextNode = $model->find('first', array('conditions' => array($scope, $model->escapeField($left) => ($node[$right] + 1)), - 'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1)); + 'fields' => array($model->primaryKey, $left, $right), 'recursive' => $recursive)); if ($nextNode) { list($nextNode)= array_values($nextNode); } else { return false; } - $edge = $this->__getMax($model, $scope, $right); + $edge = $this->__getMax($model, $scope, $right, $recursive); $this->__sync($model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]); $this->__sync($model, $nextNode[$left] - $node[$left], '-', 'BETWEEN ' . $nextNode[$left] . ' AND ' . $nextNode[$right]); $this->__sync($model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', '> ' . $edge); @@ -431,27 +437,31 @@ class TreeBehavior extends ModelBehavior { $id = $model->id; } extract($this->settings[$model->alias]); + $recursive = -1; + if ($scope != '1 = 1') { + $recursive = 0; + } list($node) = array_values($model->find('first', array( 'conditions' => array($scope, $model->escapeField() => $id), - 'fields' => array($model->primaryKey, $left, $right, $parent ), 'recursive' => -1 + 'fields' => array($model->primaryKey, $left, $right, $parent ), 'recursive' => $recursive ))); if ($node[$parent]) { list($parentNode) = array_values($model->find('first', array( 'conditions' => array($scope, $model->escapeField() => $node[$parent]), - 'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1 + 'fields' => array($model->primaryKey, $left, $right), 'recursive' => $recursive ))); if (($node[$left] - 1) == $parentNode[$left]) { return false; } } $previousNode = $model->find('first', array('conditions' => array($scope, $model->escapeField($right) => ($node[$left] - 1)), - 'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1)); + 'fields' => array($model->primaryKey, $left, $right), 'recursive' => $recursive)); if ($previousNode) { list($previousNode) = array_values($previousNode); } else { return false; } - $edge = $this->__getMax($model, $scope, $right); + $edge = $this->__getMax($model, $scope, $right, $recursive); $this->__sync($model, $edge - $previousNode[$left] +1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]); $this->__sync($model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' .$node[$left] . ' AND ' . $node[$right]); $this->__sync($model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge); @@ -768,9 +778,9 @@ class TreeBehavior extends ModelBehavior { * @return int * @access private */ - function __getMax($model, $scope, $right) { + function __getMax($model, $scope, $right, $recursive = -1) { $db =& ConnectionManager::getDataSource($model->useDbConfig); - list($edge) = array_values($model->find('first', array('conditions' => $scope, 'fields' => $db->calculate($model, 'max', array($right)), 'recursive' => -1))); + list($edge) = array_values($model->find('first', array('conditions' => $scope, 'fields' => $db->calculate($model, 'max', array($right)), 'recursive' => $recursive))); return ife(empty ($edge[$right]), 0, $edge[$right]); } /** diff --git a/cake/tests/cases/libs/model/behaviors/tree.test.php b/cake/tests/cases/libs/model/behaviors/tree.test.php index 575beefa5..14fced2a7 100644 --- a/cake/tests/cases/libs/model/behaviors/tree.test.php +++ b/cake/tests/cases/libs/model/behaviors/tree.test.php @@ -73,9 +73,19 @@ class FlagTree extends NumberTree { var $name = 'FlagTree'; } +class Campaign extends CakeTestModel { + var $name = 'Campaign'; + var $hasMany = array('Ad' => array('fields'=>array('id','campaign_id','name') )); +} +class Ad extends CakeTestModel { + var $name = 'Ad'; + var $actsAs = array('Tree'); + var $belongsTo = array('Campaign' ); +} + class NumberTreeCase extends CakeTestCase { - var $fixtures = array('core.number_tree', 'core.flag_tree'); + var $fixtures = array('core.number_tree', 'core.flag_tree', 'core.campaign','core.ad'); var $debug = false; function tearDown() { @@ -931,6 +941,28 @@ class NumberTreeCase extends CakeTestCase { $expected = array(1 => '1. Root', 2 => '_1.1', 3 => '__1.1.1', 4 => '__1.1.2', 5 => '_1.2', 6 => '__1.2.1', 7 => '__1.2.2'); $this->assertIdentical($result, $expected); } + + function testMoveUpWithScope() { + $this->Ad =& new Ad(); + $this->Ad->Behaviors->attach('Tree', array('scope'=>'Campaign')); + $this->Ad->moveUp(6); + + $this->Ad->id = 4; + $result = $this->Ad->children(); + $this->assertEqual(Set::extract('/Ad/id', $result), array(6, 5)); + $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2)); + } + + function testMoveDownWithScope() { + $this->Ad =& new Ad(); + $this->Ad->Behaviors->attach('Tree', array('scope'=>'Campaign')); + $this->Ad->moveDown(6); + + $this->Ad->id = 4; + $result = $this->Ad->children(); + $this->assertEqual(Set::extract('/Ad/id', $result), array(5, 6)); + $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2)); + } } ?> \ No newline at end of file diff --git a/cake/tests/fixtures/ad_fixture.php b/cake/tests/fixtures/ad_fixture.php new file mode 100644 index 000000000..bc5e05bf2 --- /dev/null +++ b/cake/tests/fixtures/ad_fixture.php @@ -0,0 +1,25 @@ + array('type' => 'integer', 'key' => 'primary'), + 'campaign_id' => array('type' => 'integer'), + 'parent_id' => array('type' => 'integer'), + 'lft' => array('type' => 'integer'), + 'rght' => array('type' => 'integer'), + 'name' => array('type' => 'string', 'length' => 255, 'null' => false), + ); + + var $records = array( + array( 'id' => 1, 'parent_id' => NULL, 'lft' => 1, 'rght' => 2, 'campaign_id' => 1, 'name' => 'Nordover' ), + array( 'id' => 2, 'parent_id' => NULL, 'lft' => 3, 'rght' => 4, 'campaign_id' => 1, 'name' => 'Statbergen' ), + array( 'id' => 3, 'parent_id' => NULL, 'lft' => 5, 'rght' => 6, 'campaign_id' => 1, 'name' => 'Feroy' ), + array( 'id' => 4, 'parent_id' => NULL, 'lft' => 7, 'rght' => 12, 'campaign_id' => 2, 'name' => 'Newcastle' ), + array( 'id' => 5, 'parent_id' => NULL, 'lft' => 8, 'rght' => 9, 'campaign_id' => 2, 'name' => 'Dublin' ), + array( 'id' => 6, 'parent_id' => NULL, 'lft' => 10, 'rght' => 11, 'campaign_id' => 2, 'name' => 'Alborg' ), + array( 'id' => 7, 'parent_id' => NULL, 'lft' => 13, 'rght' => 14, 'campaign_id' => 3, 'name' => 'New York' ), + ); + +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/campaign_fixture.php b/cake/tests/fixtures/campaign_fixture.php new file mode 100644 index 000000000..e3180113b --- /dev/null +++ b/cake/tests/fixtures/campaign_fixture.php @@ -0,0 +1,16 @@ + array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'length' => 255, 'null' => false), + ); + + var $records = array( + array( 'id' => 1 , 'name' => 'Hurtigruten' ), + array( 'id' => 2 , 'name' => 'Colorline' ), + array( 'id' => 3 , 'name' => 'Queen of Scandinavia' ) + ); +} +?> \ No newline at end of file