Adding test cases for TreeBehavior scoping

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6591 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-03-19 00:54:32 +00:00
parent 26489efe19
commit c286a7293f
5 changed files with 187 additions and 83 deletions

View file

@ -313,8 +313,10 @@ class BehaviorCollection extends Object {
if (!in_array($name, $this->_attached)) {
$this->_attached[] = $name;
}
if (in_array($name, $this->_disabled)) {
if (in_array($name, $this->_disabled) && !(isset($config['enabled']) && $config['enabled'] === false)) {
$this->enable($name);
} elseif (isset($config['enabled']) && $config['enabled'] === false) {
$this->disable($name);
}
return true;
}

View file

@ -38,32 +38,30 @@ class TreeBehavior extends ModelBehavior {
var $errors = array();
function setup(&$model, $config = array()) {
$settings = array_merge(array(
'parent' => 'parent_id',
'left' => 'lft',
'right' => 'rght',
'scope' => '1 = 1',
'enabled' => true,
'type' => 'nested',
'__parentChange' => false
), (array)$config);
var $_defaults = array(
'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false
);
/*if (in_array($settings['scope'], $model->getAssociated('belongsTo'))) {
function setup(&$model, $config = array()) {
if (!is_array($config)) {
$config = array('type' => $config);
}
$settings = array_merge($this->_defaults, $config);
if (in_array($settings['scope'], $model->getAssociated('belongsTo'))) {
$data = $model->getAssociated($settings['scope']);
$parent =& $model->{$data['className']};
$settings['scope'] = $model->alias . '.' . $data['foreignKey']) . ' = ' . $parent->alias . '.' . $parent->primaryKey, $settings['scope']);
}*/
$parent =& $model->{$settings['scope']};
$settings['scope'] = $model->alias . '.' . $data['foreignKey'] . ' = ' . $parent->alias . '.' . $parent->primaryKey;
}
$this->settings[$model->alias] = $settings;
}
/**
* Change the Tree behavior on the fly
*
* @param object $model
* @param mixed $scope
* @deprecated
*/
function setScope(&$model, $scope) {
$this->settings[$model->name]['scope'] = $scope;
trigger_error(__('(TreeBehavior::setScope) Deprecated - Use BehaviorCollection::attach() to re-attach with new settings', true), E_USER_WARNING);
$this->settings[$model->name]['scope'] = $scope;
}
/**
* After save method. Called after all saves
@ -77,9 +75,6 @@ class TreeBehavior extends ModelBehavior {
*/
function afterSave(&$model, $created) {
extract($this->settings[$model->alias]);
if (!$enabled) {
return true;
}
if ($created) {
if ((isset($model->data[$model->alias][$parent])) && $model->data[$model->alias][$parent]) {
return $this->_setParent($model, $model->data[$model->alias][$parent]);
@ -99,10 +94,6 @@ class TreeBehavior extends ModelBehavior {
*/
function beforeDelete(&$model) {
extract($this->settings[$model->alias]);
if (!$enabled) {
return true;
}
list($name, $data) = array($model->alias, $model->read());
$data = $data[$name];
@ -115,7 +106,7 @@ class TreeBehavior extends ModelBehavior {
if (is_string($scope)) {
$scope = array($scope);
}
$scope[][$model->escapeField($left)] = 'BETWEEN ' . ($data[$left] + 1) . ' AND ' . ($data[$right] - 1);
$scope[][$model->alias . '.' . $left] = 'BETWEEN ' . ($data[$left] + 1) . ' AND ' . ($data[$right] - 1);
$model->deleteAll($scope);
}
$this->__sync($model, $diff, '-', '> ' . $data[$right]);
@ -135,9 +126,6 @@ class TreeBehavior extends ModelBehavior {
function beforeSave(&$model) {
extract($this->settings[$model->alias]);
if (!$enabled) {
return true;
}
if (isset($model->data[$model->alias][$model->primaryKey])) {
if ($model->data[$model->alias][$model->primaryKey]) {
if (!$model->id) {
@ -182,13 +170,13 @@ class TreeBehavior extends ModelBehavior {
));
if (!$parentNode) {
return false;
} else {
list($parentNode) = array_values($parentNode);
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
return false;
} elseif ($node[$model->primaryKey] == $parentNode[$model->primaryKey]) {
return false;
}
}
list($parentNode) = array_values($parentNode);
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
return false;
} elseif ($node[$model->primaryKey] == $parentNode[$model->primaryKey]) {
return false;
}
}
}
@ -216,16 +204,16 @@ class TreeBehavior extends ModelBehavior {
if ($direct) {
return $model->find('count', array('conditions' => array($scope, $model->escapeField($parent) => $id)));
} else {
if ($id === null) {
return $model->find('count', array('conditions' => $scope));
} elseif (!empty ($model->data)) {
$data = $model->data[$model->alias];
} else {
list($data) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id), 'recursive' => -1)));
}
return ($data[$right] - $data[$left] - 1) / 2;
}
if ($id === null) {
return $model->find('count', array('conditions' => $scope));
} elseif (!empty ($model->data)) {
$data = $model->data[$model->alias];
} else {
list($data) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id), 'recursive' => -1)));
}
return ($data[$right] - $data[$left] - 1) / 2;
}
/**
* Get the child nodes of the current model
@ -259,15 +247,14 @@ class TreeBehavior extends ModelBehavior {
if ($direct) {
$conditions = array($scope, $model->escapeField($parent) => $id);
return $model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
} else {
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)));
$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));
}
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)));
$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));
}
/**
* A convenience method for returning a hierarchical array used for HTML select boxes

View file

@ -69,9 +69,13 @@ class NumberTree extends CakeTestModel {
}
}
class FlagTree extends NumberTree {
var $name = 'FlagTree';
}
class NumberTreeCase extends CakeTestCase {
var $fixtures = array('core.number_tree');
var $fixtures = array('core.number_tree', 'core.flag_tree');
var $debug = false;
function tearDown() {
@ -92,7 +96,69 @@ class NumberTreeCase extends CakeTestCase {
$this->assertIdentical($validTree, true);
}
function testDetectInvalidLeft() {
function testStringScope() {
$this->FlagTree =& new FlagTree();
$this->FlagTree->__initialize(2, 3);
$this->FlagTree->id = 1;
$this->FlagTree->saveField('flag', true);
$this->FlagTree->id = 2;
$this->FlagTree->saveField('flag', true);
$result = $this->FlagTree->children();
$expected = array(
array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
);
$this->assertEqual($result, $expected);
$this->FlagTree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
$this->assertEqual($this->FlagTree->children(), array());
$this->FlagTree->id = 1;
$this->FlagTree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
$result = $this->FlagTree->children();
$expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
$this->assertEqual($result, $expected);
$this->assertTrue($this->FlagTree->delete());
$this->assertEqual($this->FlagTree->find('count'), 11);
}
function testArrayScope() {
$this->FlagTree =& new FlagTree();
$this->FlagTree->__initialize(2, 3);
$this->FlagTree->id = 1;
$this->FlagTree->saveField('flag', true);
$this->FlagTree->id = 2;
$this->FlagTree->saveField('flag', true);
$result = $this->FlagTree->children();
$expected = array(
array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
);
$this->assertEqual($result, $expected);
$this->FlagTree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => true)));
$this->assertEqual($this->FlagTree->children(), array());
$this->FlagTree->id = 1;
$this->FlagTree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => true)));
$result = $this->FlagTree->children();
$expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
$this->assertEqual($result, $expected);
$this->assertTrue($this->FlagTree->delete());
$this->assertEqual($this->FlagTree->find('count'), 11);
}
function testDetectInvalidLeft() {
$this->NumberTree =& new NumberTree();
$this->NumberTree->__initialize(2, 2);
@ -226,16 +292,16 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree->__initialize(2, 2);
$data= $this->NumberTree->find(array('NumberTree.name' => '1.1'), array('id'));
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$this->NumberTree->create();
$result = $this->NumberTree->save(array('NumberTree' => array('name' => 'testAddMiddle', 'parent_id' => $data['NumberTree']['id'])));
$expected = array('NumberTree' => array('name' => 'testAddMiddle', 'parent_id' => '2'));
$this->assertIdentical($result, $expected);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertEqual($initialCount + 1, $laterCount);
$children = $this->NumberTree->children($data['NumberTree']['id'], true, array('name'));
@ -253,13 +319,13 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree->__initialize(2, 2);
$this->NumberTree->id = null;
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
//$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
$saveSuccess = $this->NumberTree->save(array('NumberTree' => array('name' => 'testAddInvalid', 'parent_id' => 99999)));
$this->assertIdentical($saveSuccess, false);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertIdentical($initialCount, $laterCount);
$validTree = $this->NumberTree->verify();
@ -377,7 +443,7 @@ class NumberTreeCase extends CakeTestCase {
$data = $this->NumberTree->find(array('NumberTree.name' => '1.1.1'), array('id'));
$expects = $this->NumberTree->findAll();
$expects = $this->NumberTree->find('all');
$before = $this->NumberTree->read(null, $data['NumberTree']['id']);
$this->NumberTree->id = $parent_id;
@ -385,7 +451,7 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree->saveField('parent_id', $data['NumberTree']['id']);
//$this->NumberTree->setparent($data['NumberTree']['id']);
$results = $this->NumberTree->findAll();
$results = $this->NumberTree->find('all');
$after = $this->NumberTree->read(null, $data['NumberTree']['id']);
$this->assertEqual($results, $expects);
@ -400,7 +466,7 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree->__initialize(2, 2);
$this->NumberTree->id = null;
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$data= $this->NumberTree->findByName('1.1');
//$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
@ -409,7 +475,7 @@ class NumberTreeCase extends CakeTestCase {
//$saveSuccess = $this->NumberTree->setparent(999999);
//$this->assertIdentical($saveSuccess, false);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertIdentical($initialCount, $laterCount);
$validTree = $this->NumberTree->verify();
@ -421,7 +487,7 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree->__initialize(2, 2);
$this->NumberTree->id = null;
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$data= $this->NumberTree->findByName('1.1');
//$this->expectError('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave');
@ -430,7 +496,7 @@ class NumberTreeCase extends CakeTestCase {
//$saveSuccess= $this->NumberTree->setparent($this->NumberTree->id);
$this->assertIdentical($saveSuccess, false);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertIdentical($initialCount, $laterCount);
$validTree = $this->NumberTree->verify();
@ -629,8 +695,8 @@ class NumberTreeCase extends CakeTestCase {
$this->assertIdentical($result, true);
$this->NumberTree->moveup();
$result = $this->NumberTree->findAll(null, array('name'), 'NumberTree.lft ASC');
$result = $this->NumberTree->find('all', array('fields' => 'name', 'order' => 'NumberTree.lft ASC'));
$expected = array(array('NumberTree' => array('name' => '1.1')),
array('NumberTree' => array('name' => '1. Root')));
$this->assertIdentical($result, $expected);
@ -640,25 +706,25 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree =& new NumberTree();
$this->NumberTree->__initialize(2, 2);
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$result = $this->NumberTree->findByName('1.1.1');
$return = $this->NumberTree->delete($result['NumberTree']['id']);
$this->assertEqual($return, true);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertEqual($initialCount - 1, $laterCount);
$validTree= $this->NumberTree->verify();
$this->assertIdentical($validTree, true);
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$result= $this->NumberTree->findByName('1.1');
$return = $this->NumberTree->delete($result['NumberTree']['id']);
$this->assertEqual($return, true);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertEqual($initialCount - 2, $laterCount);
$validTree = $this->NumberTree->verify();
@ -668,12 +734,12 @@ class NumberTreeCase extends CakeTestCase {
function testRemove() {
$this->NumberTree =& new NumberTree();
$this->NumberTree->__initialize(2, 2);
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$result = $this->NumberTree->findByName('1.1');
$this->NumberTree->removeFromTree($result['NumberTree']['id']);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertEqual($initialCount, $laterCount);
$children = $this->NumberTree->children($result['NumberTree']['parent_id'], true, array('name'));
@ -695,13 +761,13 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree =& new NumberTree();
$this->NumberTree->__initialize(2, 2);
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$initialTopNodes = $this->NumberTree->childCount(false);
$result = $this->NumberTree->findByName('1. Root');
$this->NumberTree->removeFromTree($result['NumberTree']['id']);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$laterTopNodes = $this->NumberTree->childCount(false);
$this->assertEqual($initialCount, $laterCount);
@ -722,12 +788,12 @@ class NumberTreeCase extends CakeTestCase {
$this->NumberTree =& new NumberTree();
$this->NumberTree->__initialize(2, 2);
$initialCount = $this->NumberTree->findCount();
$initialCount = $this->NumberTree->find('count');
$result = $this->NumberTree->findByName('1.1');
$this->NumberTree->removeFromTree($result['NumberTree']['id'],true);
$laterCount = $this->NumberTree->findCount();
$laterCount = $this->NumberTree->find('count');
$this->assertEqual($initialCount-1, $laterCount);
$children = $this->NumberTree->children($result['NumberTree']['parent_id'], true, array('name'), 'lft asc');
@ -776,7 +842,7 @@ class NumberTreeCase extends CakeTestCase {
$direct = $this->NumberTree->childCount(null, true);
$this->assertEqual($direct, 2);
$expects = $this->NumberTree->findCount() - 1;
$expects = $this->NumberTree->find('count') - 1;
$total = $this->NumberTree->childCount();
$this->assertEqual($total, 6);
}
@ -833,7 +899,7 @@ class NumberTreeCase extends CakeTestCase {
$this->assertEqual($total, $expects);
}
function testReorderTree () {
function testReorderTree() {
$this->NumberTree =& new NumberTree();
$this->NumberTree->__initialize(3, 3);
$nodes = $this->NumberTree->find('list', array('order' => 'lft'));

View file

@ -3259,7 +3259,7 @@ class ModelTest extends CakeTestCase {
$this->assertTrue(is_object($this->model->Behaviors->Tree));
$this->assertEqual($this->model->Behaviors->attached(), array('Tree'));
$expected = array('parent' => 'parent_id', 'left' => 'left_field', 'right' => 'right_field', 'scope' => '1 = 1', 'enabled' => true, 'type' => 'nested', '__parentChange' => false);
$expected = array('parent' => 'parent_id', 'left' => 'left_field', 'right' => 'right_field', 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false);
$this->assertEqual($this->model->Behaviors->Tree->settings['Apple'], $expected);
$expected['enabled'] = false;

View file

@ -0,0 +1,49 @@
<?php
/* SVN FILE: $Id: flag_tree_fixture.php 6354 2008-01-10 07:02:33Z nate $ */
/**
* Tree behavior class test fixture.
*
* Enables a model object to act as a node-based tree.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, 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-2008, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.fixtures
* @since CakePHP(tm) v 1.2.0.5331
* @version $Revision: 6354 $
* @modifiedby $LastChangedBy: nate $
* @lastmodified $Date: 2008-01-10 02:02:33 -0500 (Thu, 10 Jan 2008) $
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/**
* Flag Tree Test Fixture
*
* Like Number Tree, but uses a flag for testing scope parameters
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class FlagTreeFixture extends CakeTestFixture {
var $name = 'FlagTree';
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),
'flag' => array('type' => 'boolean','null' => false, 'default' => false)
);
}
?>