From ff01b3a03183aa9e6e883e63b31fe43b528657d2 Mon Sep 17 00:00:00 2001 From: phpnut Date: Fri, 22 Jun 2007 05:01:39 +0000 Subject: [PATCH] Adding enhancement and tests from #2159 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5331 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/behaviors/tree.php | 734 +++++++++++++++--- .../cases/libs/model/behaviors/tree.test.php | 627 +++++++++++++++ cake/tests/fixtures/number_tree_fixture.php | 47 ++ 3 files changed, 1305 insertions(+), 103 deletions(-) create mode 100644 cake/tests/cases/libs/model/behaviors/tree.test.php create mode 100644 cake/tests/fixtures/number_tree_fixture.php diff --git a/cake/libs/model/behaviors/tree.php b/cake/libs/model/behaviors/tree.php index 1b4a0dd6c..49a4effe5 100644 --- a/cake/libs/model/behaviors/tree.php +++ b/cake/libs/model/behaviors/tree.php @@ -1,6 +1,5 @@ 'parent_id', - 'left' => 'lft', - 'right' => 'rght', - 'scope' => '1 = 1', - 'type' => 'nested' + 'parent' => 'parent_id', + 'left' => 'lft', + 'right' => 'rght', + 'scope' => '1 = 1', + 'type' => 'nested' ), $config); /*if (in_array($settings['scope'], $model->getAssociated('belongsTo'))) { $data = $model->getAssociated($settings['scope']); $parent =& $model->{$data['className']}; $settings['scope'] = $model->escapeField($data['foreignKey']) . ' = ' . $parent->escapeField($parent->primaryKey, $settings['scope']); - }*/ + }*/ $this->settings[$model->name] = $settings; } /** - * Sets the parent of the given node + * After save method. Called after all saves * - * @param mixed $parent_id + * Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the + * parameters to be saved. + * + * @param AppModel $model + * @param boolean $created indicates whether the node just saved was created or updated * @return boolean True on success, false on failure */ - function setparent(&$model, $parent_id = null, $created = null) { - extract($this->settings[$model->name]); - if ($created === false && $parent_id == $model->field($parent)) { - return true; - } - list($node) = array_values($model->find(array($model->escapeField() => $model->id), array($model->primaryKey, $parent, $left, $right), null, -1)); - list($edge) = array_values($model->find(null, "MAX({$right}) AS {$right}", null, -1)); - $edge = ife(empty($edge[$right]), 0, $edge[$right]); // Is the tree empty? - - if (!empty($parent_id)) { - @list($parentNode) = array_values($model->find(array($model->escapeField() => $parent_id), array($model->primaryKey, $left, $right), null, -1)); - if (empty($parentNode)) { - trigger_error(__("Null parent in Tree::afterSave()", true), E_USER_WARNING); - return null; - } - $offset = $parentNode[$left]; - - if (!empty($node[$left]) && !empty($node[$right])) { - $shift = ($edge - $node[$left]) + 1; - $diff = $node[$right] - $node[$left] + 1; - $start = $edge + 1; - - // First, move the node (and subnodes) outside the tree - $model->updateAll(array($right => "{$right} + {$shift}"), array($right => "BETWEEN {$node[$left]} AND {$node[$right]}")); - $model->updateAll(array($left => "{$left} + {$shift}"), array($left => "BETWEEN {$node[$left]} AND {$node[$right]}")); - // Close the gap to the right of where the node was - $model->updateAll(array($right => "{$right} - {$diff}"), array($right => "BETWEEN {$node[$right]} AND {$edge}")); - $model->updateAll(array($left => "{$left} - {$diff}"), array($left => "BETWEEN {$node[$right]} AND {$edge}")); - // Open a new gap to insert the node - $model->updateAll(array($right => "{$right} + {$diff}"), array($right => "> {$offset}")); - $model->updateAll(array($left => "{$left} + {$diff}"), array($left => "> {$offset}")); - // Shift the node(s) into position - $model->updateAll(array($right => "{$right} - {$shift}"), array($right => ">= {$start}")); - $model->updateAll(array($left => "{$left} - {$shift}"), array($left => ">= {$start}")); - return; - } - } else { - $offset = $edge; - } - $model->updateAll(array($right => "{$right} + 2"), array($right => "> {$offset}")); - $model->updateAll(array($left => "{$left} + 2"), array($left => "> {$offset}")); - return $model->save(array($left => $offset + 1, $right => $offset + 2, $parent => $parent_id), false); - } - function afterSave(&$model, $created) { extract($this->settings[$model->name]); if ($created) { - if (!isset($model->data[$model->name][$parent])) { - $model->data[$model->name][$parent] = null; + if ((isset($model->data[$model->name][$parent])) && $model->data[$model->name][$parent]) { + return $this->_setParent($model, $model->data[$model->name][$parent], true); + } else { + return true; } - return $this->setparent($model, $model->data[$model->name][$parent], $created); + } elseif (array_key_exists($parent, $model->data[$model->name])) { + return $this->_setParent($model, $model->data[$model->name][$parent], true); } } - +/** + * Before delete method. Called before all deletes + * + * Will delete the current node and all children using the deleteAll method and sync the table + * + * @param AppModel $model + * @return boolean True to continue, false to abort the delete + */ function beforeDelete(&$model) { extract($this->settings[$model->name]); - list($name, $data) = array($model->name, $model->read()); - $data = $data[$name]; - $diff = $data[$right] - $data[$left] + 1; + list($name, $data)= array( + $model->name, + $model->read()); + $data= $data[$name]; - $model->deleteAll(array($scope, $left => '> ' . $data[$left], $right => '< ' . $data[$right])); - $model->updateAll(array($left => "{$left} - {$diff}"), array($scope, $left => '>= ' . $data[$right])); - $model->updateAll(array($right => "{$right} - {$diff}"), array($scope, $right => '>= ' . $data[$right])); + if (!$data[$right] || !$data[$left]) { + return true; + } + $diff = $data[$right] - $data[$left] + 1; + $constraint = $scope . ' AND ' . $left . ' BETWEEN ' . $data[$left] . ' AND ' . $data[$right]; + $model->deleteAll($constraint); + $this->__sync($model, $diff, '-', '> ' . $data[$right], $scope); + } +/** + * Before save method. Called before all saves + * + * Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the + * parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by + * this method bypassing the setParent logic. + * + * @since 1.2 + * @param AppModel $model + * @return boolean True to continue, false to abort the save + */ + function beforeSave(&$model) { + extract($this->settings[$model->name]); + if (isset($model->data[$model->name][$model->primaryKey])) { + if ($model->data[$model->name][$model->primaryKey]) { + if (!$model->id) { + $model->id = $model->data[$model->name][$model->primaryKey]; + } + } + unset ($model->data[$model->name][$model->primaryKey]); + } + + if (!$model->id) { + if ((!isset($model->data[$model->name][$parent])) || (!$model->data[$model->name][$parent])) { + $edge = $this->__getMax($model, $scope, $right); + $model->data[$model->name][$left]= $edge +1; + $model->data[$model->name][$right]= $edge +2; + } else { + $parentNode = $model->find(array($scope, $model->escapeField() => $model->data[$model->name][$parent]), + array($model->primaryKey), null, -1); + if (!$parentNode) { + trigger_error(__('Trying to save a node under a none-existant node in ' . __METHOD__, E_USER_WARNING)); + return false; + } + } + } elseif (isset($model->data[$model->name][$parent])) { + if (!$model->data[$model->name][$parent]) { + $model->data[$model->name][$parent]= null; + } else { + list($node) = array_values($model->find(array($scope,$model->escapeField() => $model->id), + array($model->primaryKey, $parent, $left, $right ), null, -1)); + + $parentNode = $model->find(array($scope, $model->escapeField() => $model->data[$model->name][$parent]), + array($model->primaryKey, $left, $right), null, -1); + if (!$parentNode) { + trigger_error(__('Trying to save a node under a none-existant node in ' . __METHOD__, E_USER_WARNING)); + return false; + } else { + list($parentNode) = array_values($parentNode); + if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) { + trigger_error(__('Trying to save a node under itself in ' . __METHOD__, E_USER_WARNING)); + return false; + } + elseif ($node[$model->primaryKey] == $parentNode[$model->primaryKey]) { + trigger_error(__('Trying to set a node to be the parent of itself in ' . __METHOD__, E_USER_WARNING)); + return false; + } + } + } + } + return true; + } +/** + * Get the number of child nodes + * + * If the direct parameter is set to true, only the direct children are counted (based upon the parent_id field) + * If false is passed for the id parameter, all top level nodes are counted, or all nodes are counted. + * + * @param AppModel $model + * @param mixed $id The ID of the record to read or false to read all top level nodes + * @param boolean $direct whether to count direct, or all, children + * @return int number of child nodes + * @access public + */ + function childcount(&$model, $id = null, $direct = false) { + if ($id === null && $model->id) { + $id = $model->id; + } elseif (!$id) { + $id = null; + } + extract($this->settings[$model->name]); + + if ($direct) { + return $model->findCount(array($scope, $parent => $id)); + } else { + if ($id === null) { + return $model->findCount($scope); + } + elseif (!empty ($model->data)) { + $data = $model->data[$model->name]; + } else { + list($data)= array_values($model->find(array($scope, $model->escapeField() => $id), null, null, -1)); + } + return ($data[$right] - $data[$left] - 1) / 2; + } } /** * Get the child nodes of the current model * - * @return array + * If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field) + * If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted. + * + * @param AppModel $model + * @param mixed $id The ID of the record to read + * @param boolean $direct whether to return only the direct, or all, children + * @param mixed $fields Either a single string of a field name, or an array of field names + * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order + * @param int $limit SQL LIMIT clause, for calculating items per page. + * @param int $page Page number, for accessing paged data + * @param int $recursive The number of levels deep to fetch associated records + * @return array Array of child nodes + * @access public */ - function children(&$model) { + function children(&$model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = -1) { + if ($id === null && $model->id) { + $id = $model->id; + } elseif (!$id) { + $id = null; + } $name = $model->name; extract($this->settings[$name]); - @list($item) = array_values($model->find(array($model->escapeField() => $model->id), array($model->primaryKey, $left, $right), null, -1)); - return $model->findAll(array($right => '< ' . $item[$right], $left => '> ' . $item[$left])); - } -/** - * Get the number of child nodes contained by the current model - * - * @return int - */ - function childcount(&$model) { - extract($this->settings[$model->name]); - if (!empty($model->data)) { - $data = $model->data[$model->name]; - } else { - list($data) = array_values($model->find(array($model->escapeField() => $model->id), null, null, -1)); + + if (!$order) { + $order = $model->name . '.' . $left . ' asc'; + } + if ($direct) { + return $model->findAll(array($scope, $parent => $id), $fields, $order, $limit, $page, $recursive); + } else { + if (!$id) { + $constraint = $scope; + } else { + @list($item) = array_values($model->find(array($scope,$model->escapeField() => $id), array($left, $right ), null, -1)); + $constraint = array($scope, $right => '< ' . $item[$right], $left => '> ' . $item[$left]); + } + return $model->findAll($constraint, $fields, $order, $limit, $page, $recursive); } - return ($data[$right] - $data[$left] - 1) / 2; } /** - * Get the parent node of the given Aro or Aco + * Placeholder. Output multigrouped * - * @param mixed $id - * @return array + * A means of putting mptt data in a select box (using groups?) is needed. Must still be possible to select + * (intermediary) parents. + * + * @param AppModel $model + * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...) + * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") + * @param int $limit SQL LIMIT clause, for calculating items per page + * @param string $keyPath A string path to the key, i.e. "{n}.Post.id" + * @param string $valuePath A string path to the value, i.e. "{n}.Post.title" + * @param string $groupPath A string path to a value to group the elements by, i.e. "{n}.Post.category_id" + * @return array An associative array of records, where the id is the key, and the display field is the value + * @access public */ - function getparentnode(&$model, $id = null) { - if (empty($id)) { + function generateTreeList(&$model, $conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $groupPath = null) { + extract($this->settings[$model->name]); + /* + $model->bindModel( + array('hasOne'=> + array('TreeParent'=> + array( + 'className'=>$model->name, + 'foreignKey'=>'parent_id', + 'conditions'=>'OR 1=1 AND '.$model->escapeField($left).' BETWEEN TreeParent.'.$left.' AND TreeParent.'.$right + ) + ) + ) + ); + $recursive = $model->recursive; + $model->recursive = 0; + .... + $this->recursive = $recursive; + */ + $result = $model->query('SELECT Node.id AS id, CONCAT( REPEAT(\'.....\', COUNT(Parent.name)-1), Node.name) AS name ' . + 'FROM ' . $model->tablePrefix . $model->table . ' As Node, ' . $model->tablePrefix . $model->table . ' As Parent ' . + 'WHERE Node.' . $left . ' BETWEEN Parent.' . $left . ' AND Parent.' . $right . ' ' . + 'GROUP BY Node.' . $model->displayField . ' ' . + 'ORDER BY Node.' . $left); + uses('Set'); + $keys = Set::extract($result, '{n}.Node.id'); + $vals = Set::extract($result, '{n}.0.name'); + if (!empty ($keys) && !empty ($vals)) { + $out = array(); + if ($groupPath != null) { + $group = Set::extract($result, $groupPath); + + if (!empty ($group)) { + $c = count($keys); + for ($i = 0; $i < $c; $i++) { + if (!isset($group[$i])) { + $group[$i] = 0; + } + if (!isset($out[$group[$i]])) { + $out[$group[$i]] = array(); + } + $out[$group[$i]][$keys[$i]] = $vals[$i]; + } + return $out; + } + } + $return = array_combine($keys, $vals); + return $return; + } + return null; + } +/** + * Get the parent node + * + * reads the parent id and returns this node + * + * @param AppModel $model + * @param mixed $id The ID of the record to read + * @param int $recursive The number of levels deep to fetch associated records + * @return array Array of data for the parent node + * @access public + */ + function getparentnode(&$model, $id = null, $fields = null, $recursive = -1) { + if (empty ($id)) { $id = $model->id; } - $path = $this->getPath($model, $id); - if ($path == null || count($path) < 2) { - return null; + extract($this->settings[$model->name]); + $parentId = $model->read($parent, $id); + + if ($parentId) { + $parentId = $parentId[$model->name][$parent]; + $parent = $model->findById($parentId, $fields, null, $recursive); + return $parent; } else { - return $path[count($path) - 2]; + return false; } } /** - * Gets the path to the given Aro or Aco + * Get the path to the given node * - * @param mixed $id - * @return array + * @param AppModel $model + * @param mixed $id The ID of the record to read + * @param mixed $fields Either a single string of a field name, or an array of field names + * @param int $recursive The number of levels deep to fetch associated records + * @return array Array of nodes from top most parent to current node + * @access public */ - function getpath(&$model, $id) { + function getpath(&$model, $id = null, $fields = null, $recursive = -1) { + if (empty ($id)) { + $id = $model->id; + } extract($this->settings[$model->name]); - $rec = $model->recursive; - $model->recursive = -1; - @list($item) = array_values($model->read(null, $id)); + @list($item) = array_values($model->findById($id, array($left, $right))); - if (empty($item)) { + if (empty ($item)) { return null; } - $results = $model->findAll( - array($model->escapeField($left) => '<= ' . $item[$left], $model->escapeField($right) => '>= ' . $item[$right]), - null, array($model->escapeField($left) => 'asc'), null, null, 0 - ); - $model->recursive = $rec; + $results = $model->findAll(array($scope, $model->escapeField($left) => '<= ' . $item[$left], + $model->escapeField($right) => '>= ' . $item[$right]), + $fields, array($model->escapeField($left) => 'asc'), null, null, $recursive); return $results; } -} +/** + * Reorder the node without changing the parent. + * + * If the node is the last child, or is a top level node with no subsequent node this method will return false + * + * @param AppModel $model + * @param mixed $id The ID of the record to move + * @param int $number how many places to move the node + * @return boolean True on success, false on failure + * @access public + */ + function moveDown(&$model, $id = null, $number = 1) { + if (empty ($id)) { + $id = $model->id; + } + extract($this->settings[$model->name]); + list($node) = array_values($model->find(array($scope, $model->escapeField() => $id), + array($model->primaryKey, $left, $right, $parent), null, -1)); + if ($node[$parent]) { + list($parentNode) = array_values($model->find(array($scope, $model->escapeField() => $node[$parent]), + array($model->primaryKey, $left, $right), null, -1)); + if (($node[$right] + 1) == $parentNode[$right]) { + return false; + } + } + $nextNode = $model->find(array($scope, $left => ($node[$right] + 1)), + array($model->primaryKey, $left, $right), null, -1); + if ($nextNode) { + list($nextNode)= array_values($nextNode); + } else { + return false; + } + $edge = $this->__getMax($model, $scope, $right); + $this->__sync($model, $edge - $node[$left] + 1, '+', "BETWEEN {$node[$left]} AND {$node[$right]}", $scope); + $this->__sync($model, $nextNode[$left] - $node[$left], '-', "BETWEEN {$nextNode[$left]} AND {$nextNode[$right]}", $scope); + $this->__sync($model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', "> $edge", $scope); + if ($number > 1) { + return $this->moveDown($model, $number - 1); + } else { + return true; + } + } +/** + * Reorder the node without changing the parent. + * + * If the node is the first child, or is a top level node with no previous node this method will return false + * + * @param AppModel $model + * @param mixed $id The ID of the record to move + * @param int $number how many places to move the node + * @return boolean True on success, false on failure + * @access public + */ + function moveUp(&$model, $id = null, $number = 1) { + if (empty ($id)) { + $id = $model->id; + } + extract($this->settings[$model->name]); + list($node) = array_values($model->find(array($scope, $model->escapeField() => $id), + array($model->primaryKey, $left, $right, $parent ), null, -1)); + if ($node[$parent]) { + list($parentNode) = array_values($model->find(array( $scope, $model->escapeField() => $node[$parent]), + array($model->primaryKey, $left, $right), null, -1)); + if (($node[$left] - 1) == $parentNode[$left]) { + return false; + } + } + $previousNode = $model->find(array($scope, $right => ($node[$left] - 1)), + array($model->primaryKey, $left, $right), null, -1); + if ($previousNode) { + list($previousNode) = array_values($previousNode); + } else { + return false; + } + $edge = $this->__getMax($model, $scope, $right); + $this->__sync($model, $edge - $previousNode[$left] +1, '+', "BETWEEN {$previousNode[$left]} AND {$previousNode[$right]}", $scope); + $this->__sync($model, $node[$left] - $previousNode[$left], '-', "BETWEEN {$node[$left]} AND {$node[$right]}", $scope); + $this->__sync($model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', "> $edge", $scope); + if ($number > 1) { + return $this->moveUp($model, $number -1); + } else { + return true; + } + } +/** + * Recover a corrupted tree + * + * The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data + * will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode + * 'parent' the values of the parent_id field will be used to populate the left and right fields. + * + * @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB. + * @param AppModel $model + * @param string $mode parent or tree + * @return boolean True on success, false on failure + * @access public + */ + function recover(&$model, $mode = 'parent') { + extract($this->settings[$model->name]); + $model->recursive = -1; + if ($mode == 'parent') { + $count = 1; + foreach ($model->findAll($scope, array($model->primaryKey)) as $array) { + $model->{$model->primaryKey} = $array[$model->name][$model->primaryKey]; + $lft = $count++; + $rght = $count++; + $model->save(array($left => $lft,$right => $rght)); + } + foreach ($model->findAll($scope, array($model->primaryKey,$parent)) as $array) { + $model->create(); + $model->id = $array[$model->name][$model->primaryKey]; + $this->_setParent($model, $array[$model->name][$parent], true); + } + } else { + foreach ($model->findAll($scope, array($model->primaryKey, $parent)) as $array) { + $path = $this->getpath($model, $array[$model->name][$model->primaryKey]); + if ($path == null || count($path) < 2) { + $parentId = null; + } else { + $parentId = $path[count($path) - 2][$model->name][$model->primaryKey]; + } + $model->updateAll(array($parent => $parentId), array($model->primaryKey => $array[$model->name][$model->primaryKey])); + } + } + } +/** + * Remove the current node from the tree, and reparent all children up one level. + * + * If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted + * after the children are reparented. + * + * @param AppModel $model + * @param mixed $id The ID of the record to remove + * @param boolean $delete whether to delete the node after reparenting children (if any) + * @return boolean True on success, false on failure + * @access public + */ + function removeFromTree(&$model, $id = null, $delete = false) { + if (empty ($id)) { + $id = $model->id; + } + extract($this->settings[$model->name]); + list($node) = array_values($model->find(array($scope, $model->escapeField() => $id), + array( $model->primaryKey, $left, $right, $parent), null, -1)); + if ($node[$right] == $node[$left] + 1) { + return false; + } elseif ($node[$parent]) { + list($parentNode)= array_values($model->find(array($scope, $model->escapeField() => $node[$parent]), + array($model->primaryKey, $left, $right), null, -1)); + } else { + $parentNode[$right]= $node[$right] + 1; + } + $model->updateAll(array($parent => $node[$parent]), array($parent => $node[$model->primaryKey])); + $this->__sync($model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1), $scope); + $this->__sync($model, 2, '-', '> ' . ($node[$right]), $scope); + $model->id = $id; + if ($delete) { + $model->updateAll(array($left => null, $right => null, $parent => null), array($model->primaryKey => $id)); + return $model->delete($id); + } else { + $edge = $this->__getMax($model, $scope, $right); + if ($node[$right] == $edge) { + $edge = $edge - 2; + } + $model->id = $id; + return $model->save(array($left => $edge + 1, $right => $edge + 2, $parent => null)); + } + } +/** + * Backward compatible method + * + * Returns true if the change is successful. + * + * @param AppModel $model + * @param mixed $parentId The ID to set as the parent of the current node. + * @return true on success + * @access public + */ + function setparent(&$model, $parentId = null , $created = null) { + extract($this->settings[$model->name]); + if ($created ===false && $parentId == $model->field($parent)) { + return true; + } + return $model->saveField($parent, $parentId); + } +/** + * Check if the current tree is valid. + * + * Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message) + * + * @param AppModel $model + * @return mixed true if the tree is valid or empty, otherwise an array of (error type [index, node], + * [incorrect left/right index,node id], message) + * @access public + */ + function verify(&$model) { + extract($this->settings[$model->name]); + if (!$model->findCount($scope)) { + return true; + } + $min = $this->__getMin($model, $scope, $left); + $edge = $this->__getMax($model, $scope, $right); + $errors = array(); + + for ($i = $min; $i <= $edge; $i++) { + $count = $model->findCount(array($scope, 'OR' => array($left => $i, $right => $i))); + if ($count != 1) { + if ($count == 0) { + $errors[] = array('index', $i, 'missing'); + } else { + $errors[] = array('index', $i, 'duplicate'); + } + } + } + $count = $model->findCount(array($scope, $right => '< ' . $model->escapeField($left))); + if ($count != 0) { + $node = $model->find(array($scope, $right => '< ' . $model->escapeField($left))); + $errors[] = array('node', $node[$model->primaryKey], 'left greater than right.'); + } + + $model->bindModel(array('belongsTo' => + array('VerifyParent' => array('className' => $model->name, + 'foreignKey' => $parent, + 'fields' => array($model->primaryKey, $left, $right, $parent))))); + foreach ($model->findAll($scope, null, null, null, null, 1) as $instance) { + if ($instance[$model->name][$parent]) { + if (!$instance['VerifyParent'][$model->primaryKey]) { + $errors[] = array('node', $instance[$model->name][$model->primaryKey], 'The parent node ' . $instance[$model->name][$parent] . ' doesn\'t exist'); + } elseif ($instance[$model->name][$left] < $instance['VerifyParent'][$left]) { + $errors[]= array('node', $instance[$model->name][$model->primaryKey], 'left less than parent (node ' . $instance['VerifyParent'][$model->primaryKey] . ').'); + } elseif ($instance[$model->name][$right] > $instance['VerifyParent'][$right]) { + $errors[]= array('node', $instance[$model->name][$model->primaryKey], 'right greater than parent (node ' . $instance['VerifyParent'][$model->primaryKey] . ').'); + } + } elseif ($model->findCount(array($scope, $left => '< ' . $instance[$model->name][$left], $right => '> ' . $instance[$model->name][$right]))) { + $errors[]= array('node', $instance[$model->name][$model->primaryKey], 'The parent field is blank, but has a parent'); + } + } + if ($errors) { + return $errors; + } else { + return true; + } + } +/** + * Sets the parent of the given node + * + * The force parameter is used to override the "don't change the parent to the current parent" logic in the event + * of recovering a corrupted table, or creating new nodes. Otherwise it should always be false. In reality this + * method could be private, since calling save with parent_id set also calls setParent + * + * @param AppModel $model + * @param mixed $parentId + * @param boolean $force process even if current parent_id is the same as the value to be saved + * @return boolean True on success, false on failure + * @access protected + */ + function _setParent(&$model, $parentId = null, $force = false) { + extract($this->settings[$model->name]); + if (!$force && ($parentId == $model->field($parent))) { + return false; + } + list($node) = array_values($model->find(array($scope, $model->escapeField() => $model->id), + array($model->primaryKey, $parent, $left, $right), null, -1)); + $edge = $this->__getMax($model, $scope, $right); + + if (empty ($parentId)) { + $this->__sync($model, $edge - $node[$left] + 1, '+', "BETWEEN {$node[$left]} AND {$node[$right]}", $scope); + $this->__sync($model, $node[$right] - $node[$left] + 1, '-', "> $node[$left]", $scope); + } else { + list($parentNode)= array_values($model->find(array($scope, $model->escapeField() => $parentId), + array($model->primaryKey, $left, $right), null, -1)); + if (empty ($parentNode)) { + trigger_error(__('Trying to move a node under a none-existant node in ' . __METHOD__, true), E_USER_WARNING); + return false; + } + elseif (($model->id == $parentId)) { + trigger_error(__('Trying to set a node to be the parent of itself in ' . __METHOD__, E_USER_WARNING)); + return false; + } + elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) { + trigger_error(__('Trying to move a node under itself in ' . __METHOD__, E_USER_WARNING)); + return false; + } + if (empty ($node[$left]) && empty ($node[$right])) { + $this->__sync($model, 2, '+', ">= $parentNode[$right]}", $scope); + $model->save(array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId), false); + } else { + $this->__sync($model, $edge - $node[$left] +1, '+', "BETWEEN {$node[$left]} AND {$node[$right]}", $scope); + $diff = $node[$right] - $node[$left] + 1; + + if ($node[$left] > $parentNode[$left]) { + if ($node[$right] < $parentNode[$right]) { + $this->__sync($model, $diff, '-', "BETWEEN {$node[$right]} AND " . ($parentNode[$right] - 1), $scope); + $this->__sync($model, $edge - $parentNode[$right] + $diff + 1, '-', "> $edge", $scope); + } else { + $this->__sync($model, $diff, '+', "BETWEEN {$parentNode[$right]} AND {$node[$right]}", $scope); + $this->__sync($model, $edge - $parentNode[$right] + 1, '-', "> $edge", $scope); + } + } else { + $this->__sync($model, $diff, '-', "BETWEEN {$node[$right]} AND " . ($parentNode[$right] - 1), $scope); + $this->__sync($model, $edge - $parentNode[$right] + $diff + 1, '-', "> $edge", $scope); + } + } + } + return true; + } +/** + * get the maximum index value in the table. + * + * @param AppModel $model + * @param string $scope + * @param string $right + * @return int + * @access private + */ + function __getMax($model, $scope, $right) { + list($edge) = array_values($model->find($scope, "MAX({$right}) AS {$right}", null, -1)); + return ife(empty ($edge[$right]), 0, $edge[$right]); + } +/** + * get the minimum index value in the table. + * + * @param AppModel $model + * @param string $scope + * @param string $right + * @return int + * @access private + */ + function __getMin($model, $scope, $left) { + list($edge) = array_values($model->find($scope, "MIN({$left}) AS {$left}", null, -1)); + return ife(empty ($edge[$left]), 0, $edge[$left]); // Is the tree empty? +} +/** + * Table sync method. + * + * Handles table sync operations, Taking account of the behavior scope. + * + * @param AppModel $model + * @param int $shift + * @param string $direction + * @param array $conditions + * @param mixed $scope + * @param string $field + * @access protected + */ + function __sync(&$model, $shift, $dir = '+', $conditions = array(), $scope = '', $field = 'both') { + $scope = $scope == '1 = 1' ? '' : $scope; + if ($field == 'both') { + $this->__sync($model, $shift, $dir, $conditions, $scope, 'lft'); + $field = 'rght'; + } + if (is_string($conditions)) { + $conditions = array($field => $conditions); + } + if ($scope) { + if (is_string($scope)) { + $conditions[]= $scope; + } else { + $conditions= am($conditions, $scope); + } + } + $model->updateAll(array($field => "{$field} $dir {$shift}"), $conditions); + } +} ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/behaviors/tree.test.php b/cake/tests/cases/libs/model/behaviors/tree.test.php new file mode 100644 index 000000000..4aad4a6d1 --- /dev/null +++ b/cake/tests/cases/libs/model/behaviors/tree.test.php @@ -0,0 +1,627 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.cases.libs.model.behaviors + * @since CakePHP(tm) v 1.2.0.5330 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for file + * + * Long description for file + * + * @package cake + * @subpackage cake.tests.cases.libs.model.behaviors + */ +class NumberTree extends Model { + var $name = 'NumberTree'; + var $useDbConfig = 'test_suite'; + var $cacheQueries = false; + var $actsAs = array('Tree'); + + function __initialize($levelLimit = 3, $childLimit = 3, $currentLevel = null, $parent_id = null, $prefix = '1', $hierachial = true) { + if (!$parent_id) { + $this->deleteAll('1=1'); + $this->save(array('NumberTree' => array('name' => '1. Root'))); + $this->__initialize($levelLimit, $childLimit, 1, $this->id, '1', $hierachial); + $this->create(array()); + } + + if (!$currentLevel) { + return; + } + + if ($currentLevel > $levelLimit) { + return; + } + + for ($i = 1; $i <= $childLimit; $i++) { + $name = $prefix . '.' . $i; + $data = array('NumberTree' => array('name' => $name)); + $this->create($data); + + if ($hierachial) { + $data['NumberTree']['parent_id'] = $parent_id; + } + $this->save($data); + $this->__initialize($levelLimit, $childLimit, $currentLevel + 1, $this->id, $name, $hierachial); + } + } +} +class NumberTreeCase extends CakeTestCase { + var $fixtures = array('number_tree'); + + function testInitialize() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $result = $this->NumberTree->findCount(); + $this->assertEqual($result, 7); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + function testDetectInvalidLeft() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $result = $this->NumberTree->findByName('1.1'); + + $save['NumberTree']['id'] = $result['NumberTree']['id']; + $save['NumberTree']['lft'] = false; + + $this->NumberTree->save($save); + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $this->NumberTree->recover(); + + $result = $this->NumberTree->verify(); + $this->assertIdentical($result, true); + } + + function testDetectInvalidRight() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $result = $this->NumberTree->findByName('1.1'); + + $save['NumberTree']['id'] = $result['NumberTree']['id']; + $save['NumberTree']['rght'] = false; + + $this->NumberTree->save($save); + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $this->NumberTree->recover(); + + $result = $this->NumberTree->verify(); + $this->assertIdentical($result, true); + } + + function testDetectInvalidParent() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $result = $this->NumberTree->findByName('1.1'); + + // Bypass behavior and any other logic + $this->NumberTree->updateAll(array('parent_id' => null), array('id' => $result['NumberTree']['id'])); + + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $this->NumberTree->recover(); + + $result = $this->NumberTree->verify(); + $this->assertIdentical($result, true); + } + + function testDetectNoneExistantParent() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $result = $this->NumberTree->findByName('1.1'); + $this->NumberTree->updateAll(array('parent_id' => 999999), array('id' => $result['NumberTree']['id'])); + + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $this->NumberTree->recover('MPTT'); + + $result = $this->NumberTree->verify(); + $this->assertIdentical($result, true); + } + + function testDetectInvalidParents() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $this->NumberTree->updateAll(array('parent_id' => null)); + + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $this->NumberTree->recover(); + + $result = $this->NumberTree->verify(); + $this->assertIdentical($result, true); + } + + function testDetectInvalidLftsRghts() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $this->NumberTree->updateAll(array('lft' => null, 'rght' => null)); + + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $this->NumberTree->recover(); + + $result = $this->NumberTree->verify(); + $this->assertIdentical($result, true); + } + + function testAddOrphan() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $this->NumberTree->save(array('NumberTree' => array('name' => __METHOD__,'parent_id' => null))); + $result = $this->NumberTree->find(null, array('name', 'parent_id'), 'NumberTree.lft desc'); + $expected = array('NumberTree' => array('name' => __METHOD__, 'parent_id' => null)); + $this->assertEqual($result, $expected); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testAddMiddle() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data= $this->NumberTree->find(array('NumberTree.name' => '1.1'), array('id')); + $initialCount = $this->NumberTree->findCount(); + + $this->NumberTree->create(); + $saveSuccess = $this->NumberTree->save(array('NumberTree' => array('name' => __METHOD__, 'parent_id' => $data['NumberTree']['id']))); + $this->assertIdentical($saveSuccess, true); + + $laterCount = $this->NumberTree->findCount(); + + $laterCount = $this->NumberTree->findCount(); + $this->assertEqual($initialCount + 1, $laterCount); + + $children = $this->NumberTree->children($data['NumberTree']['id'], true, array('name')); + $expects = array(array('NumberTree' => array('name' => '1.1.1')), + array('NumberTree' => array('name' => '1.1.2')), + array('NumberTree' => array('name' => __METHOD__))); + $this->assertIdentical($children, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testAddInvalid() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $initialCount = $this->NumberTree->findCount(); + $this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave'); + + $saveSuccess = $this->NumberTree->save(array('NumberTree' => array('name' => __METHOD__, 'parent_id' => 99999))); + $this->assertIdentical($saveSuccess, false); + + $laterCount = $this->NumberTree->findCount(); + $this->assertIdentical($initialCount, $laterCount); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMovePromote() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $parent = $this->NumberTree->find(array('NumberTree.name' => '1. Root')); + $parent_id = $parent['NumberTree']['id']; + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.1.1'), array('id')); + $this->NumberTree->id= $data['NumberTree']['id']; + $this->NumberTree->saveField('parent_id', $parent_id); + //$this->NumberTree->setparent($parent_id); + $direct = $this->NumberTree->children($parent_id, true); + $expects = array(array('NumberTree' => array('id' => 2, 'name' => '1.1', 'parent_id' => 1, 'lft' => 2, 'rght' => 5)), + array('NumberTree' => array('id' => 5, 'name' => '1.2', 'parent_id' => 1, 'lft' => 6, 'rght' => 11)), + array('NumberTree' => array('id' => 3, 'name' => '1.1.1', 'parent_id' => 1, 'lft' => 12, 'rght' => 13))); + $this->assertEqual($direct, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMoveBefore() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $parent = $this->NumberTree->find(array('NumberTree.name' => '1.1')); + $parent_id = $parent['NumberTree']['id']; + + $data= $this->NumberTree->find(array('NumberTree.name' => '1.2'), array('id')); + $this->NumberTree->id = $data['NumberTree']['id']; + $this->NumberTree->saveField('parent_id', $parent_id); + //$this->NumberTree->setparent($parent_id); + + $result = $this->NumberTree->children($parent_id, true, array('name')); + $expects = array(array('NumberTree' => array('name' => '1.1.1')), + array('NumberTree' => array('name' => '1.1.2')), + array('NumberTree' => array('name' => '1.2'))); + $this->assertEqual($result, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMoveAfter() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $parent = $this->NumberTree->find(array('NumberTree.name' => '1.2')); + $parent_id = $parent['NumberTree']['id']; + + $data= $this->NumberTree->find(array('NumberTree.name' => '1.1'), array('id')); + $this->NumberTree->id = $data['NumberTree']['id']; + $this->NumberTree->saveField('parent_id', $parent_id); + //$this->NumberTree->setparent($parent_id); + + $result = $this->NumberTree->children($parent_id, true, array('name')); + $expects = array(array('NumberTree' => array('name' => '1.2.1')), + array('NumberTree' => array('name' => '1.2.2')), + array('NumberTree' => array('name' => '1.1'))); + $this->assertEqual($result, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMoveDemoteInvalid() { + $this->NumberTree= & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $parent = $this->NumberTree->find(array('NumberTree.name' => '1. Root')); + $parent_id = $parent['NumberTree']['id']; + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.1.1'), array('id')); + + $expects = $this->NumberTree->findAll(); + $before = $this->NumberTree->read(null, $data['NumberTree']['id']); + + $this->NumberTree->id = $parent_id; + $this->expectError('Trying to save a node under itself in TreeBehavior::beforeSave'); + $this->NumberTree->saveField('parent_id', $data['NumberTree']['id']); + //$this->NumberTree->setparent($data['NumberTree']['id']); + + $results = $this->NumberTree->findAll(); + $after = $this->NumberTree->read(null, $data['NumberTree']['id']); + + $this->assertEqual($results, $expects); + $this->assertEqual($before, $after); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMoveToEnd() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.1'), array('id')); + $this->NumberTree->id = $data['NumberTree']['id']; + $this->NumberTree->saveField('parent_id', null); + //$this->NumberTree->setparent(null); + // Find the last parent node in the tree + $result = $this->NumberTree->find(null, array('name','parent_id'), 'NumberTree.rght desc'); + $expected = array('NumberTree' => array('name' => '1.1', 'parent_id' => null)); + $this->assertEqual($result, $expected); + } + + function testMoveInvalid() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $initialCount = $this->NumberTree->findCount(); + $data= $this->NumberTree->findByName('1.1'); + + $this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave'); + $this->NumberTree->id = $data['NumberTree']['id']; + $this->NumberTree->saveField('parent_id', 999999); + //$saveSuccess = $this->NumberTree->setparent(999999); + + //$this->assertIdentical($saveSuccess, false); + $laterCount = $this->NumberTree->findCount(); + $this->assertIdentical($initialCount, $laterCount); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMoveSelfInvalid() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $this->NumberTree->id = null; + + $initialCount = $this->NumberTree->findCount(); + $data= $this->NumberTree->findByName('1.1'); + + $this->expectError('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave'); + $this->NumberTree->id = $data['NumberTree']['id']; + $saveSuccess = $this->NumberTree->saveField('parent_id', $this->NumberTree->id); + //$saveSuccess= $this->NumberTree->setparent($this->NumberTree->id); + + $this->assertIdentical($saveSuccess, false); + $laterCount = $this->NumberTree->findCount(); + $this->assertIdentical($initialCount, $laterCount); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testMoveUpSuccess() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.2'), array('id')); + $this->NumberTree->moveUp($data['NumberTree']['id']); + + $parent = $this->NumberTree->findByName('1. Root', array('id')); + $this->NumberTree->id = $parent['NumberTree']['id']; + $result = $this->NumberTree->children(null, true, array('name')); + $expected = array(array('NumberTree' => array('name' => '1.2',)), + array('NumberTree' => array('name' => '1.1',))); + $this->assertIdentical($result, $expected); + } + + function testMoveUpFail() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.1')); + + $this->NumberTree->moveUp($data['NumberTree']['id']); + + $parent = $this->NumberTree->findByName('1. Root', array('id')); + $this->NumberTree->id = $parent['NumberTree']['id']; + $result = $this->NumberTree->children(null, true, array('name')); + $expected = array(array('NumberTree' => array('name' => '1.1',)), + array('NumberTree' => array('name' => '1.2',))); + $this->assertIdentical($result, $expected); + } + + function testMoveDownSuccess() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.1'), array('id')); + $this->NumberTree->moveDown($data['NumberTree']['id']); + + $parent = $this->NumberTree->findByName('1. Root', array('id')); + $this->NumberTree->id = $parent['NumberTree']['id']; + $result = $this->NumberTree->children(null, true, array('name')); + $expected = array(array('NumberTree' => array('name' => '1.2',)), + array('NumberTree' => array('name' => '1.1',))); + $this->assertIdentical($result, $expected); + } + + function testMoveDownFail() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.2')); + $this->NumberTree->moveDown($data['NumberTree']['id']); + + $parent = $this->NumberTree->findByName('1. Root', array('id')); + $this->NumberTree->id = $parent['NumberTree']['id']; + $result = $this->NumberTree->children(null, true, array('name')); + $expected = array(array('NumberTree' => array('name' => '1.1',)), + array('NumberTree' => array('name' => '1.2',))); + $this->assertIdentical($result, $expected); + } + + function testDelete() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $initialCount = $this->NumberTree->findCount(); + $result = $this->NumberTree->findByName('1.1.1'); + + $this->NumberTree->delete($result['NumberTree']['id']); + + $laterCount = $this->NumberTree->findCount(); + $this->assertEqual($initialCount - 1, $laterCount); + + $validTree= $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + + $initialCount = $this->NumberTree->findCount(); + $result= $this->NumberTree->findByName('1.1'); + + $this->NumberTree->delete($result['NumberTree']['id']); + + $laterCount = $this->NumberTree->findCount(); + $this->assertEqual($initialCount - 2, $laterCount); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testRemove() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + $initialCount = $this->NumberTree->findCount(); + $result = $this->NumberTree->findByName('1.1'); + + $this->NumberTree->removeFromTree($result['NumberTree']['id']); + + $laterCount = $this->NumberTree->findCount(); + $this->assertEqual($initialCount, $laterCount); + + $children = $this->NumberTree->children($result['NumberTree']['parent_id'], true, array('name')); + $expects = array(array('NumberTree' => array('name' => '1.1.1')), + array('NumberTree' => array('name' => '1.1.2')), + array('NumberTree' => array('name' => '1.2'))); + $this->assertEqual($children, $expects); + + $topNodes = $this->NumberTree->children(false,true,array('name')); + $expects = array(array('NumberTree' => array('name' => '1. Root')), + array('NumberTree' => array('name' => '1.1'))); + $this->assertEqual($topNodes, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testRemoveLastTopParent () { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $initialCount = $this->NumberTree->findCount(); + $initialTopNodes = $this->NumberTree->childcount(false); + + $result = $this->NumberTree->findByName('1. Root'); + $this->NumberTree->removeFromTree($result['NumberTree']['id']); + + $laterCount = $this->NumberTree->findCount(); + $laterTopNodes = $this->NumberTree->childcount(false); + + $this->assertEqual($initialCount, $laterCount); + $this->assertEqual($initialTopNodes, $laterTopNodes); + + $topNodes = $this->NumberTree->children(false,true,array('name')); + $expects = array(array('NumberTree' => array('name' => '1.1')), + array('NumberTree' => array('name' => '1.2')), + array('NumberTree' => array('name' => '1. Root'))); + + $this->assertEqual($topNodes, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testRemoveAndDelete() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $initialCount = $this->NumberTree->findCount(); + $result = $this->NumberTree->findByName('1.1'); + + $this->NumberTree->removeFromTree($result['NumberTree']['id'],true); + + $laterCount = $this->NumberTree->findCount(); + $this->assertEqual($initialCount-1, $laterCount); + + $children = $this->NumberTree->children($result['NumberTree']['parent_id'], true, array('name'), 'lft asc'); + $expects= array(array('NumberTree' => array('name' => '1.1.1')), + array('NumberTree' => array('name' => '1.1.2')), + array('NumberTree' => array('name' => '1.2'))); + $this->assertEqual($children, $expects); + + $topNodes = $this->NumberTree->children(false,true,array('name')); + $expects = array(array('NumberTree' => array('name' => '1. Root'))); + $this->assertEqual($topNodes, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + + function testChildren() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1. Root')); + $this->NumberTree->id= $data['NumberTree']['id']; + + $direct = $this->NumberTree->children(null, true); + $expects = array(array('NumberTree' => array('id' => 2, 'name' => '1.1', 'parent_id' => 1, 'lft' => 2, 'rght' => 7)), + array('NumberTree' => array('id' => 5, 'name' => '1.2', 'parent_id' => 1, 'lft' => 8, 'rght' => 13))); + $this->assertEqual($direct, $expects); + + $expects = array(array('NumberTree' => array('id' => 2, 'name' => '1.1', 'parent_id' => 1, 'lft' => 2, 'rght' => 7)), + array('NumberTree' => array('id' => 3, 'name' => '1.1.1', 'parent_id' => 2, 'lft' => 3, 'rght' => 4)), + array('NumberTree' => array('id' => 4, 'name' => '1.1.2', 'parent_id' => 2, 'lft' => 5, 'rght' => 6)), + array('NumberTree' => array('id' => 5, 'name' => '1.2', 'parent_id' => 1, 'lft' => 8, 'rght' => 13)), + array('NumberTree' => array( 'id' => 6, 'name' => '1.2.1', 'parent_id' => 5, 'lft' => 9, 'rght' => 10)), + array('NumberTree' => array('id' => 7, 'name' => '1.2.2', 'parent_id' => 5, 'lft' => 11, 'rght' => 12))); + $total = $this->NumberTree->children(); + $this->assertEqual($total, $expects); + } + + function testCountChildren() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1. Root')); + $this->NumberTree->id = $data['NumberTree']['id']; + + $direct = $this->NumberTree->childcount(null, true); + $this->assertEqual($direct, 2); + + $expects = $this->NumberTree->findCount() - 1; + $total = $this->NumberTree->childcount(); + $this->assertEqual($total, 6); + } + + function testGetParentNode() { + $this->NumberTree= & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.2.2')); + $this->NumberTree->id= $data['NumberTree']['id']; + + $result = $this->NumberTree->getparentNode(null, array('name')); + $expects = array('NumberTree' => array('name' => '1.2')); + $this->assertIdentical($result, $expects); + } + + function testGetPath() { + $this->NumberTree = & new NumberTree(); + $this->NumberTree->__initialize(2, 2); + + $data = $this->NumberTree->find(array('NumberTree.name' => '1.2.2')); + $this->NumberTree->id= $data['NumberTree']['id']; + + $result = $this->NumberTree->getpath(null, array('name')); + $expects = array(array('NumberTree' => array('name' => '1. Root')), + array('NumberTree' => array('name' => '1.2')), + array('NumberTree' => array('name' => '1.2.2'))); + $this->assertIdentical($result, $expects); + } +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/number_tree_fixture.php b/cake/tests/fixtures/number_tree_fixture.php new file mode 100644 index 000000000..943b28de0 --- /dev/null +++ b/cake/tests/fixtures/number_tree_fixture.php @@ -0,0 +1,47 @@ + + * Copyright (c) 2006, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright (c) 2006, Cake Software Foundation, Inc. + * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project + * @package cake + * @subpackage cake.tests.fixtures + * @since CakePHP v 1.2.0.4487 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ +/** + * Number Tree Test Fixture + * + * Generates a tree of data for use testing the tree behavior + * + * @package cake + * @subpackage cake.tests.fixtures + */ +class NumberTreeFixture extends CakeTestFixture { + var $name = 'NumberTree'; + var $fields = array ('id' => array ( + 'type' => 'integer','key' => 'primary'), + 'name' => array ('type' => 'string','null' => false), + 'parent_id' => 'integer', + 'lft' => array ('type' => 'integer','null' => false), + 'rght' => array ('type' => 'integer','null' => false)); +} +?>