Browse Source

Testing the implementation of find('path')

Jose Lorenzo Rodriguez 12 years ago
parent
commit
cb5e23349a

+ 2 - 2
src/Model/Behavior/TreeBehavior.php

@@ -68,8 +68,8 @@ class TreeBehavior extends Behavior {
 
 		return $this->_scope($query)
 			->where([
-				"$left <=" => $entity->get($left),
-				"$right >=" => $entity->get($right)
+				"$left <=" => $node->get($left),
+				"$right >=" => $node->get($right)
 			]);
 	}
 

+ 1 - 1
tests/Fixture/CategoryFixture.php

@@ -43,7 +43,7 @@ class CategoryFixture extends TestFixture {
  *
  * @var array
  */
-	public $records = array(
+	public $_records = array(
 		array('parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
 		array('parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
 		array('parent_id' => 1, 'name' => 'Category 1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),

+ 79 - 0
tests/Fixture/NumberTreeFixture.php

@@ -41,4 +41,83 @@ class NumberTreeFixture extends TestFixture {
 		'rght' => ['type' => 'integer', 'null' => false],
 		'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
 	);
+
+/**
+ * Records
+ *
+ * @var array
+ */
+	public $records = array(
+		array(
+			'id' => '1',
+			'name' => 'electronics',
+			'parent_id' => null,
+			'lft' => '1',
+			'rght' => '20'
+		),
+		array(
+			'id' => '2',
+			'name' => 'televisions',
+			'parent_id' => '1',
+			'lft' => '2',
+			'rght' => '9'
+		),
+		array(
+			'id' => '3',
+			'name' => 'tube',
+			'parent_id' => '2',
+			'lft' => '3',
+			'rght' => '4'
+		),
+		array(
+			'id' => '4',
+			'name' => 'lcd',
+			'parent_id' => '2',
+			'lft' => '5',
+			'rght' => '6'
+		),
+		array(
+			'id' => '5',
+			'name' => 'plasma',
+			'parent_id' => '2',
+			'lft' => '7',
+			'rght' => '8'
+		),
+		array(
+			'id' => '6',
+			'name' => 'portable',
+			'parent_id' => '1',
+			'lft' => '10',
+			'rght' => '19'
+		),
+		array(
+			'id' => '7',
+			'name' => 'mp3',
+			'parent_id' => '6',
+			'lft' => '11',
+			'rght' => '14'
+		),
+		array(
+			'id' => '8',
+			'name' => 'flash',
+			'parent_id' => '7',
+			'lft' => '12',
+			'rght' => '13'
+		),
+		array(
+			'id' => '9',
+			'name' => 'cd',
+			'parent_id' => '6',
+			'lft' => '15',
+			'rght' => '16'
+		),
+		array(
+			'id' => '10',
+			'name' => 'radios',
+			'parent_id' => '6',
+			'lft' => '17',
+			'rght' => '18'
+		)
+	);
+
 }

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

@@ -0,0 +1,64 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         CakePHP(tm) v 3.0.0
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+namespace Cake\Test\TestCase\Model\Behavior;
+
+use Cake\Collection\Collection;
+use Cake\Event\Event;
+use Cake\Model\Behavior\TranslateBehavior;
+use Cake\ORM\Entity;
+use Cake\ORM\TableRegistry;
+use Cake\TestSuite\TestCase;
+
+/**
+ * Translate behavior test case
+ */
+class TreeBehaviorTest extends TestCase {
+
+/**
+ * fixtures
+ *
+ * @var array
+ */
+	public $fixtures = [
+		'core.number_tree'
+	];
+
+	public function setUp() {
+		$this->table = TableRegistry::get('NumberTrees');
+		$this->table->addBehavior('Tree');
+	}
+
+	public function tearDown() {
+		parent::tearDown();
+		TableRegistry::clear();
+	}
+
+/**
+ * Tests the find('path') method
+ *
+ * @return void
+ */
+	public function testFindPath() {
+		$nodes = $this->table->find('path', ['for' => 9]);
+		$this->assertEquals([1, 6, 9], $nodes->extract('id')->toArray());
+
+		$nodes = $this->table->find('path', ['for' => 10]);
+		$this->assertEquals([1, 6, 10], $nodes->extract('id')->toArray());
+
+		$nodes = $this->table->find('path', ['for' => 5]);
+		$this->assertEquals([1, 2, 5], $nodes->extract('id')->toArray());
+	}
+
+}