Browse Source

Fixing tests and documenting methods

Jose Lorenzo Rodriguez 12 years ago
parent
commit
49a11b2b2e

+ 15 - 1
src/Model/Behavior/TreeBehavior.php

@@ -112,8 +112,9 @@ class TreeBehavior extends Behavior {
  * If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
  *
  * @param array $options Array of options as described above
- * @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
  * @return \Cake\ORM\Query
+ * @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
+ * @throws \InvalidArgumentException When the 'for' key is not passed in $options
  */
 	public function findChildren($query, $options) {
 		extract($this->config());
@@ -269,12 +270,25 @@ class TreeBehavior extends Behavior {
 		return true;
 	}
 
+/**
+ * Recovers the lft and right column values out of the hirearchy defined by the
+ * parent column.
+ *
+ * @return void
+ */
 	public function recover() {
 		$this->_table->connection()->transactional(function() {
 			$this->_recoverTree();
 		});
 	}
 
+/**
+ * Recursive method used to recover a single level of the tree
+ *
+ * @param integer $counter The Last left column value that was assigned
+ * @param mixed $parentId the parent id of the level to be recovered
+ * @return integer Ne next value to use for the left column
+ */
 	protected function _recoverTree($counter = 0, $parentId = null) {
 		$config = $this->config();
 		list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];

+ 5 - 5
tests/Fixture/MenuLinkTreeFixture.php

@@ -81,7 +81,7 @@ class MenuLinkTreeFixture extends TestFixture {
 			'menu' => 'main-menu',
 			'lft' => '1',
 			'rght' => '10',
-			'parent_id' => '0',
+			'parent_id' => null,
 			'url' => '/link1.html',
 			'title' => 'Link 1',
 		),
@@ -126,7 +126,7 @@ class MenuLinkTreeFixture extends TestFixture {
 			'menu' => 'main-menu',
 			'lft' => '11',
 			'rght' => '14',
-			'parent_id' => '0',
+			'parent_id' => null,
 			'url' => '/yeah/another-link.html',
 			'title' => 'Link 6',
 		),
@@ -144,7 +144,7 @@ class MenuLinkTreeFixture extends TestFixture {
 			'menu' => 'main-menu',
 			'lft' => '15',
 			'rght' => '16',
-			'parent_id' => '0',
+			'parent_id' => null,
 			'url' => '/page/who-we-are.html',
 			'title' => 'Link 8',
 		),
@@ -153,7 +153,7 @@ class MenuLinkTreeFixture extends TestFixture {
 			'menu' => 'categories',
 			'lft' => '1',
 			'rght' => '10',
-			'parent_id' => '0',
+			'parent_id' => null,
 			'url' => '/cagetory/electronics.html',
 			'title' => 'electronics',
 		),
@@ -198,7 +198,7 @@ class MenuLinkTreeFixture extends TestFixture {
 			'menu' => 'categories',
 			'lft' => '11',
 			'rght' => '20',
-			'parent_id' => '0',
+			'parent_id' => null,
 			'url' => '/category/portable.html',
 			'title' => 'portable',
 		),

+ 13 - 0
tests/TestCase/Model/Behavior/TreeBehaviorTest.php

@@ -281,6 +281,12 @@ class TreeBehaviorTest extends TestCase {
 			->hydrate(false)
 			->toArray();
 
+		$expected2 = $table->find()
+			->where(['menu' => 'categories'])
+			->order('lft')
+			->hydrate(false)
+			->toArray();
+
 		$table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
 		$table->recover();
 		$result = $table->find()
@@ -289,6 +295,13 @@ class TreeBehaviorTest extends TestCase {
 			->hydrate(false)
 			->toArray();
 		$this->assertEquals($expected, $result);
+
+		$result2 = $table->find()
+			->where(['menu' => 'categories'])
+			->order('lft')
+			->hydrate(false)
+			->toArray();
+		$this->assertEquals($expected2, $result2);
 	}
 
 }