Skip to content

Commit

Permalink
added fixture and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
quickapps authored and lorenzo committed Mar 29, 2014
1 parent fe05b8d commit 026b4fe
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 4 deletions.
243 changes: 243 additions & 0 deletions tests/Fixture/MenuLinkTreeFixture.php
@@ -0,0 +1,243 @@
<?php
/**
* Tree behavior class.
*
* Enables a model object to act as a node-based tree.
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 1.2.0.5331
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* Class NumberTreeFixture
*
* Generates a tree of data for use testing the tree behavior
*
*/
class MenuLinkTreeFixture extends TestFixture {

/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => ['type' => 'integer'],
'menu' => ['type' => 'string', 'null' => false],
'lft' => ['type' => 'integer', 'null' => false],
'rght' => ['type' => 'integer', 'null' => false],
'parent_id' => 'integer',
'url' => ['type' => 'string', 'null' => false],
'title' => ['type' => 'string', 'null' => false],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
);

/**
* Records
*
* # main-menu:
*
* - Link 1:1
* - Link 2:2
* - Link 3:3
* - Link 4:4
* - Link 5:5
* - Link 6:6
* - Link 7:7
* - Link 8:8
*
* ***
*
* # categories:
*
* - electronics:9
* - televisions:10
* - tube:11
* - lcd:12
* - plasma:13
* - portable:14
* - mp3:15
* - flash:16
* - cd:17
* - radios:18
*
* **Note:** title:id
*/
public $records = array(
array(
'id' => '1',
'menu' => 'main-menu',
'lft' => '1',
'rght' => '10',
'parent_id' => '0',
'url' => '/link1.html',
'title' => 'Link 1',
),
array(
'id' => '2',
'menu' => 'main-menu',
'lft' => '2',
'rght' => '3',
'parent_id' => '1',
'url' => 'http://example.com',
'title' => 'Link 2',
),
array(
'id' => '3',
'menu' => 'main-menu',
'lft' => '4',
'rght' => '9',
'parent_id' => '1',
'url' => '/what/even-more-links.html',
'title' => 'Link 3',
),
array(
'id' => '4',
'menu' => 'main-menu',
'lft' => '5',
'rght' => '8',
'parent_id' => '3',
'url' => '/lorem/ipsum.html',
'title' => 'Link 4',
),
array(
'id' => '5',
'menu' => 'main-menu',
'lft' => '6',
'rght' => '7',
'parent_id' => '4',
'url' => '/what/the.html',
'title' => 'Link 5',
),
array(
'id' => '6',
'menu' => 'main-menu',
'lft' => '11',
'rght' => '14',
'parent_id' => '0',
'url' => '/yeah/another-link.html',
'title' => 'Link 6',
),
array(
'id' => '7',
'menu' => 'main-menu',
'lft' => '12',
'rght' => '13',
'parent_id' => '6',
'url' => 'http://cakephp.org',
'title' => 'Link 7',
),
array(
'id' => '8',
'menu' => 'main-menu',
'lft' => '15',
'rght' => '16',
'parent_id' => '0',
'url' => '/page/who-we-are.html',
'title' => 'Link 8',
),
array(
'id' => '9',
'menu' => 'categories',
'lft' => '1',
'rght' => '10',
'parent_id' => '0',
'url' => '/cagetory/electronics.html',
'title' => 'electronics',
),
array(
'id' => '10',
'menu' => 'categories',
'lft' => '2',
'rght' => '9',
'parent_id' => '9',
'url' => '/category/televisions.html',
'title' => 'televisions',
),
array(
'id' => '11',
'menu' => 'categories',
'lft' => '3',
'rght' => '4',
'parent_id' => '10',
'url' => '/category/tube.html',
'title' => 'tube',
),
array(
'id' => '12',
'menu' => 'categories',
'lft' => '5',
'rght' => '8',
'parent_id' => '10',
'url' => '/category/lcd.html',
'title' => 'lcd',
),
array(
'id' => '13',
'menu' => 'categories',
'lft' => '6',
'rght' => '7',
'parent_id' => '12',
'url' => '/category/plasma.html',
'title' => 'plasma',
),
array(
'id' => '14',
'menu' => 'categories',
'lft' => '11',
'rght' => '20',
'parent_id' => '0',
'url' => '/category/portable.html',
'title' => 'portable',
),
array(
'id' => '15',
'menu' => 'categories',
'lft' => '12',
'rght' => '15',
'parent_id' => '14',
'url' => '/category/mp3.html',
'title' => 'mp3',
),
array(
'id' => '16',
'menu' => 'categories',
'lft' => '13',
'rght' => '14',
'parent_id' => '15',
'url' => '/category/flash.html',
'title' => 'flash',
),
array(
'id' => '17',
'menu' => 'categories',
'lft' => '16',
'rght' => '17',
'parent_id' => '14',
'url' => '/category/cd.html',
'title' => 'cd',
),
array(
'id' => '18',
'menu' => 'categories',
'lft' => '18',
'rght' => '19',
'parent_id' => '14',
'url' => '/category/radios.html',
'title' => 'radios',
),
);

}
34 changes: 30 additions & 4 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -32,7 +32,8 @@ class TreeBehaviorTest extends TestCase {
* @var array
*/
public $fixtures = [
'core.number_tree'
'core.number_tree',
'core.menu_link_tree'
];

public function setUp() {
Expand Down Expand Up @@ -62,9 +63,12 @@ public function testFindPath() {

$nodes = $this->table->find('path', ['for' => 1]);
$this->assertEquals([1], $nodes->extract('id')->toArray());

$nodes = $this->table->find('path', ['for' => 11]);
$this->assertEquals([11], $nodes->extract('id')->toArray());

// find path with scope
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$nodes = $table->find('path', ['for' => 5]);
$this->assertEquals([1, 3, 4, 5], $nodes->extract('id')->toArray());
}

/**
Expand Down Expand Up @@ -92,5 +96,27 @@ public function testChildCount() {
// count leaf children
$count = $this->table->childCount(10, false);
$this->assertEquals($count, 0);

// test scoping
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$count = $table->childCount(3, false);
$this->assertEquals($count, 2);
}

/**
* Tests the childCount() plus callable scoping
*
* @return void
*/
public function testCallableScoping() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', [
'scope' => function ($query) {
return $query->where(['url LIKE' => '/what%']);
}
]);
$count = $table->childCount(2, false);
$this->assertEquals($count, 2);
}
}

0 comments on commit 026b4fe

Please sign in to comment.