From 84a83fa802ad39ecda15d7548bc06dc9625200c3 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 12 Jul 2014 18:49:00 +0200 Subject: [PATCH] Using an array primaryKey value in TreeBehaviorTests to confirm that it is not a problem --- src/Model/Behavior/TreeBehavior.php | 5 ++ .../Model/Behavior/TreeBehaviorTest.php | 58 +++++++------------ 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/Model/Behavior/TreeBehavior.php b/src/Model/Behavior/TreeBehavior.php index 7b5a12fe4d2..20676201d28 100644 --- a/src/Model/Behavior/TreeBehavior.php +++ b/src/Model/Behavior/TreeBehavior.php @@ -756,6 +756,11 @@ protected function _ensureFields($entity) { } } +/** + * Returns a single string value representing the primary key of the attached table + * + * @return string + */ protected function _getPrimaryKey() { if (!$this->_primaryKey) { $this->_primaryKey = (array)$this->_table->primaryKey(); diff --git a/tests/TestCase/Model/Behavior/TreeBehaviorTest.php b/tests/TestCase/Model/Behavior/TreeBehaviorTest.php index 89bf7d196d8..84d9140f226 100644 --- a/tests/TestCase/Model/Behavior/TreeBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/TreeBehaviorTest.php @@ -39,6 +39,7 @@ class TreeBehaviorTest extends TestCase { public function setUp() { parent::setUp(); $this->table = TableRegistry::get('NumberTrees'); + $this->table->primaryKey(['id']); $this->table->addBehavior('Tree'); } @@ -385,8 +386,7 @@ public function testMoveDownNoTreeColumns() { * @return void */ public function testRecover() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $expected = $table->find()->order('lft')->hydrate(false)->toArray(); $table->updateAll(['lft' => null, 'rght' => null], []); $table->recover(); @@ -437,8 +437,7 @@ public function testRecoverScoped() { * @return void */ public function testAddOrphan() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = new Entity( ['name' => 'New Orphan', 'parent_id' => null], ['markNew' => true] @@ -459,8 +458,7 @@ public function testAddOrphan() { * @return void */ public function testAddMiddle() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = new Entity( ['name' => 'laptops', 'parent_id' => 1], ['markNew' => true] @@ -481,8 +479,7 @@ public function testAddMiddle() { * @return void */ public function testAddLeaf() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = new Entity( ['name' => 'laptops', 'parent_id' => 2], ['markNew' => true] @@ -503,8 +500,7 @@ public function testAddLeaf() { * @return void */ public function testReParentSubTreeRight() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(2); $entity->parent_id = 6; $this->assertSame($entity, $table->save($entity)); @@ -522,8 +518,7 @@ public function testReParentSubTreeRight() { * @return void */ public function testReParentSubTreeLeft() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(6); $entity->parent_id = 2; $this->assertSame($entity, $table->save($entity)); @@ -542,8 +537,7 @@ public function testReParentSubTreeLeft() { * @return void */ public function testReParentLeafLeft() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(10); $entity->parent_id = 2; $this->assertSame($entity, $table->save($entity)); @@ -562,8 +556,7 @@ public function testReParentLeafLeft() { * @return void */ public function testReParentLeafRight() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(5); $entity->parent_id = 6; $this->assertSame($entity, $table->save($entity)); @@ -581,8 +574,7 @@ public function testReParentLeafRight() { * @return void */ public function testReParentNoTreeColumns() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(6); $entity->unsetProperty('lft'); $entity->unsetProperty('rght'); @@ -603,8 +595,7 @@ public function testReParentNoTreeColumns() { * @return void */ public function testRootingSubTree() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(2); $entity->parent_id = null; $this->assertSame($entity, $table->save($entity)); @@ -622,8 +613,7 @@ public function testRootingSubTree() { * @return void */ public function testRootingNoTreeColumns() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(2); $entity->unsetProperty('lft'); $entity->unsetProperty('rght'); @@ -645,8 +635,7 @@ public function testRootingNoTreeColumns() { * @return void */ public function testReparentCycle() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(2); $entity->parent_id = 5; $table->save($entity); @@ -658,8 +647,7 @@ public function testReparentCycle() { * @return void */ public function testDeleteLeaf() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(4); $this->assertTrue($table->delete($entity)); $result = $table->find()->order('lft')->hydrate(false)->toArray(); @@ -674,8 +662,7 @@ public function testDeleteLeaf() { * @return void */ public function testDeleteSubTree() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(6); $this->assertTrue($table->delete($entity)); $result = $table->find()->order('lft')->hydrate(false)->toArray(); @@ -690,8 +677,7 @@ public function testDeleteSubTree() { * @return void */ public function testDeleteRoot() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(1); $this->assertTrue($table->delete($entity)); $result = $table->find()->order('lft')->hydrate(false)->toArray(); @@ -706,8 +692,7 @@ public function testDeleteRoot() { * @return void */ public function testDeleteRootNoTreeColumns() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(1); $entity->unsetProperty('lft'); $entity->unsetProperty('rght'); @@ -724,8 +709,7 @@ public function testDeleteRootNoTreeColumns() { * @return void */ public function testRemoveFromLeafFromTree() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(10); $this->assertSame($entity, $table->removeFromTree($entity)); $this->assertEquals(21, $entity->lft); @@ -742,8 +726,7 @@ public function testRemoveFromLeafFromTree() { * @return void */ public function testRemoveMiddleNodeFromTree() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(6); $this->assertSame($entity, $table->removeFromTree($entity)); $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray(); @@ -761,8 +744,7 @@ public function testRemoveMiddleNodeFromTree() { * @return void */ public function testRemoveRootFromTree() { - $table = TableRegistry::get('NumberTrees'); - $table->addBehavior('Tree'); + $table = $this->table; $entity = $table->get(1); $this->assertSame($entity, $table->removeFromTree($entity)); $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();