| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866 |
- <?php
- /**
- * TreeBehaviorTest file
- *
- * Holds several Test Cases
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
- * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
- * @package cake.tests.cases.libs.model.behaviors
- * @since CakePHP(tm) v 1.2.0.5330
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('Model', 'Model');
- App::uses('AppModel', 'Model');
- require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
- /**
- * NumberTreeTest class
- *
- * @package cake.tests.cases.libs.model.behaviors
- */
- class NumberTreeTest extends CakeTestCase {
- /**
- * Whether backup global state for each test method or not
- *
- * @var bool false
- * @access public
- */
- public $backupGlobals = false;
- /**
- * settings property
- *
- * @var array
- * @access protected
- */
- protected $settings = array(
- 'modelClass' => 'NumberTree',
- 'leftField' => 'lft',
- 'rightField' => 'rght',
- 'parentField' => 'parent_id'
- );
- /**
- * fixtures property
- *
- * @var array
- * @access public
- */
- public $fixtures = array('core.number_tree');
- /**
- * testInitialize method
- *
- * @access public
- * @return void
- */
- function testInitialize() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->find('count');
- $this->assertEqual($result, 7);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testDetectInvalidLeft method
- *
- * @access public
- * @return void
- */
- function testDetectInvalidLeft() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->findByName('1.1');
- $save[$modelClass]['id'] = $result[$modelClass]['id'];
- $save[$modelClass][$leftField] = 0;
- $this->Tree->save($save);
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover();
- $this->assertIdentical($result, true);
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * testDetectInvalidRight method
- *
- * @access public
- * @return void
- */
- function testDetectInvalidRight() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->findByName('1.1');
- $save[$modelClass]['id'] = $result[$modelClass]['id'];
- $save[$modelClass][$rightField] = 0;
- $this->Tree->save($save);
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover();
- $this->assertIdentical($result, true);
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * testDetectInvalidParent method
- *
- * @access public
- * @return void
- */
- function testDetectInvalidParent() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->findByName('1.1');
- // Bypass behavior and any other logic
- $this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover();
- $this->assertIdentical($result, true);
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * testDetectNoneExistantParent method
- *
- * @access public
- * @return void
- */
- function testDetectNoneExistantParent() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->findByName('1.1');
- $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover('MPTT');
- $this->assertIdentical($result, true);
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * testRecoverFromMissingParent method
- *
- * @access public
- * @return void
- */
- function testRecoverFromMissingParent() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->findByName('1.1');
- $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover();
- $this->assertIdentical($result, true);
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * testDetectInvalidParents method
- *
- * @access public
- * @return void
- */
- function testDetectInvalidParents() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->updateAll(array($parentField => null));
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover();
- $this->assertIdentical($result, true);
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * testDetectInvalidLftsRghts method
- *
- * @access public
- * @return void
- */
- function testDetectInvalidLftsRghts() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $this->Tree->recover();
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- }
- /**
- * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
- *
- * @access public
- * @return void
- */
- function testDetectEqualLftsRghts() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 3);
- $result = $this->Tree->findByName('1.1');
- $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
- $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
- array($leftField . ' >' => $result[$modelClass][$leftField]));
- $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
- array($rightField . ' >' => $result[$modelClass][$leftField]));
- $result = $this->Tree->verify();
- $this->assertNotIdentical($result, true);
- $result = $this->Tree->recover();
- $this->assertTrue($result);
- $result = $this->Tree->verify();
- $this->assertTrue($result);
- }
- /**
- * testAddOrphan method
- *
- * @access public
- * @return void
- */
- function testAddOrphan() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
- $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
- $expected = array($modelClass => array('name' => 'testAddOrphan', $parentField => null));
- $this->assertEqual($result, $expected);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testAddMiddle method
- *
- * @access public
- * @return void
- */
- function testAddMiddle() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
- $initialCount = $this->Tree->find('count');
- $this->Tree->create();
- $result = $this->Tree->save(array($modelClass => array('name' => 'testAddMiddle', $parentField => $data[$modelClass]['id'])));
- $expected = array_merge(array($modelClass => array('name' => 'testAddMiddle', $parentField => '2')), $result);
- $this->assertIdentical($result, $expected);
- $laterCount = $this->Tree->find('count');
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount + 1, $laterCount);
- $children = $this->Tree->children($data[$modelClass]['id'], true, array('name'));
- $expects = array(array($modelClass => array('name' => '1.1.1')),
- array($modelClass => array('name' => '1.1.2')),
- array($modelClass => array('name' => 'testAddMiddle')));
- $this->assertIdentical($children, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testAddInvalid method
- *
- * @access public
- * @return void
- */
- function testAddInvalid() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $initialCount = $this->Tree->find('count');
- //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
- $saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
- $this->assertIdentical($saveSuccess, false);
- $laterCount = $this->Tree->find('count');
- $this->assertIdentical($initialCount, $laterCount);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testAddNotIndexedByModel method
- *
- * @access public
- * @return void
- */
- function testAddNotIndexedByModel() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
- $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
- $expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
- $this->assertEqual($result, $expected);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMovePromote method
- *
- * @access public
- * @return void
- */
- function testMovePromote() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $parent_id = $parent[$modelClass]['id'];
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
- $this->Tree->id= $data[$modelClass]['id'];
- $this->Tree->saveField($parentField, $parent_id);
- $direct = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
- $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
- array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
- array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
- $this->assertEqual($direct, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveWithWhitelist method
- *
- * @access public
- * @return void
- */
- function testMoveWithWhitelist() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $parent_id = $parent[$modelClass]['id'];
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->whitelist = array($parentField, 'name', 'description');
- $this->Tree->saveField($parentField, $parent_id);
- $result = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
- $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
- array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
- array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
- $this->assertEqual($result, $expected);
- $this->assertTrue($this->Tree->verify());
- }
- /**
- * testInsertWithWhitelist method
- *
- * @access public
- * @return void
- */
- function testInsertWithWhitelist() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->whitelist = array('name', $parentField);
- $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
- $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
- $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
- $this->assertEqual($result[$modelClass], $expected);
- $this->assertIdentical($this->Tree->verify(), true);
- }
- /**
- * testMoveBefore method
- *
- * @access public
- * @return void
- */
- function testMoveBefore() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
- $parent_id = $parent[$modelClass]['id'];
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->saveField($parentField, $parent_id);
- $result = $this->Tree->children($parent_id, true, array('name'));
- $expects = array(array($modelClass => array('name' => '1.1.1')),
- array($modelClass => array('name' => '1.1.2')),
- array($modelClass => array('name' => '1.2')));
- $this->assertEqual($result, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveAfter method
- *
- * @access public
- * @return void
- */
- function testMoveAfter() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
- $parent_id = $parent[$modelClass]['id'];
- $data= $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->saveField($parentField, $parent_id);
- $result = $this->Tree->children($parent_id, true, array('name'));
- $expects = array(array($modelClass => array('name' => '1.2.1')),
- array($modelClass => array('name' => '1.2.2')),
- array($modelClass => array('name' => '1.1')));
- $this->assertEqual($result, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveDemoteInvalid method
- *
- * @access public
- * @return void
- */
- function testMoveDemoteInvalid() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $parent_id = $parent[$modelClass]['id'];
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
- $expects = $this->Tree->find('all');
- $before = $this->Tree->read(null, $data[$modelClass]['id']);
- $this->Tree->id = $parent_id;
- //$this->expectError('Trying to save a node under itself in TreeBehavior::beforeSave');
- $this->Tree->saveField($parentField, $data[$modelClass]['id']);
- $results = $this->Tree->find('all');
- $after = $this->Tree->read(null, $data[$modelClass]['id']);
- $this->assertEqual($results, $expects);
- $this->assertEqual($before, $after);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveInvalid method
- *
- * @access public
- * @return void
- */
- function testMoveInvalid() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $initialCount = $this->Tree->find('count');
- $data= $this->Tree->findByName('1.1');
- //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->saveField($parentField, 999999);
- //$this->assertIdentical($saveSuccess, false);
- $laterCount = $this->Tree->find('count');
- $this->assertIdentical($initialCount, $laterCount);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveSelfInvalid method
- *
- * @access public
- * @return void
- */
- function testMoveSelfInvalid() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $initialCount = $this->Tree->find('count');
- $data= $this->Tree->findByName('1.1');
- //$this->expectError('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave');
- $this->Tree->id = $data[$modelClass]['id'];
- $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
- $this->assertIdentical($saveSuccess, false);
- $laterCount = $this->Tree->find('count');
- $this->assertIdentical($initialCount, $laterCount);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveUpSuccess method
- *
- * @access public
- * @return void
- */
- function testMoveUpSuccess() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
- $this->Tree->moveUp($data[$modelClass]['id']);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.1', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveUpFail method
- *
- * @access public
- * @return void
- */
- function testMoveUpFail() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
- $this->Tree->moveUp($data[$modelClass]['id']);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveUp2 method
- *
- * @access public
- * @return void
- */
- function testMoveUp2() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 10);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
- $this->Tree->moveUp($data[$modelClass]['id'], 2);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(
- array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.5', )),
- array($modelClass => array('name' => '1.3', )),
- array($modelClass => array('name' => '1.4', )),
- array($modelClass => array('name' => '1.6', )),
- array($modelClass => array('name' => '1.7', )),
- array($modelClass => array('name' => '1.8', )),
- array($modelClass => array('name' => '1.9', )),
- array($modelClass => array('name' => '1.10', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveUpFirst method
- *
- * @access public
- * @return void
- */
- function testMoveUpFirst() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 10);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
- $this->Tree->moveUp($data[$modelClass]['id'], true);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(
- array($modelClass => array('name' => '1.5', )),
- array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.3', )),
- array($modelClass => array('name' => '1.4', )),
- array($modelClass => array('name' => '1.6', )),
- array($modelClass => array('name' => '1.7', )),
- array($modelClass => array('name' => '1.8', )),
- array($modelClass => array('name' => '1.9', )),
- array($modelClass => array('name' => '1.10', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveDownSuccess method
- *
- * @access public
- * @return void
- */
- function testMoveDownSuccess() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
- $this->Tree->moveDown($data[$modelClass]['id']);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.1', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveDownFail method
- *
- * @access public
- * @return void
- */
- function testMoveDownFail() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
- $this->Tree->moveDown($data[$modelClass]['id']);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveDownLast method
- *
- * @access public
- * @return void
- */
- function testMoveDownLast() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 10);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
- $this->Tree->moveDown($data[$modelClass]['id'], true);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(
- array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.3', )),
- array($modelClass => array('name' => '1.4', )),
- array($modelClass => array('name' => '1.6', )),
- array($modelClass => array('name' => '1.7', )),
- array($modelClass => array('name' => '1.8', )),
- array($modelClass => array('name' => '1.9', )),
- array($modelClass => array('name' => '1.10', )),
- array($modelClass => array('name' => '1.5', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveDown2 method
- *
- * @access public
- * @return void
- */
- function testMoveDown2() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 10);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
- $this->Tree->moveDown($data[$modelClass]['id'], 2);
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(
- array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.3', )),
- array($modelClass => array('name' => '1.4', )),
- array($modelClass => array('name' => '1.6', )),
- array($modelClass => array('name' => '1.7', )),
- array($modelClass => array('name' => '1.5', )),
- array($modelClass => array('name' => '1.8', )),
- array($modelClass => array('name' => '1.9', )),
- array($modelClass => array('name' => '1.10', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testSaveNoMove method
- *
- * @access public
- * @return void
- */
- function testSaveNoMove() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 10);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->saveField('name', 'renamed');
- $parent = $this->Tree->findByName('1. Root', array('id'));
- $this->Tree->id = $parent[$modelClass]['id'];
- $result = $this->Tree->children(null, true, array('name'));
- $expected = array(
- array($modelClass => array('name' => '1.1', )),
- array($modelClass => array('name' => '1.2', )),
- array($modelClass => array('name' => '1.3', )),
- array($modelClass => array('name' => '1.4', )),
- array($modelClass => array('name' => 'renamed', )),
- array($modelClass => array('name' => '1.6', )),
- array($modelClass => array('name' => '1.7', )),
- array($modelClass => array('name' => '1.8', )),
- array($modelClass => array('name' => '1.9', )),
- array($modelClass => array('name' => '1.10', )));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testMoveToRootAndMoveUp method
- *
- * @access public
- * @return void
- */
- function testMoveToRootAndMoveUp() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(1, 1);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->save(array($parentField => null));
- $result = $this->Tree->verify();
- $this->assertIdentical($result, true);
- $this->Tree->moveUp();
- $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
- $expected = array(array($modelClass => array('name' => '1.1')),
- array($modelClass => array('name' => '1. Root')));
- $this->assertIdentical($result, $expected);
- }
- /**
- * testDelete method
- *
- * @access public
- * @return void
- */
- function testDelete() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1.1');
- $return = $this->Tree->delete($result[$modelClass]['id']);
- $this->assertEqual($return, true);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount - 1, $laterCount);
- $validTree= $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- $initialCount = $this->Tree->find('count');
- $result= $this->Tree->findByName('1.1');
- $return = $this->Tree->delete($result[$modelClass]['id']);
- $this->assertEqual($return, true);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount - 2, $laterCount);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testRemove method
- *
- * @access public
- * @return void
- */
- function testRemove() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1');
- $this->Tree->removeFromTree($result[$modelClass]['id']);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount, $laterCount);
- $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
- $expects = array(array($modelClass => array('name' => '1.1.1')),
- array($modelClass => array('name' => '1.1.2')),
- array($modelClass => array('name' => '1.2')));
- $this->assertEqual($children, $expects);
- $topNodes = $this->Tree->children(false, true,array('name'));
- $expects = array(array($modelClass => array('name' => '1. Root')),
- array($modelClass => array('name' => '1.1')));
- $this->assertEqual($topNodes, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testRemoveLastTopParent method
- *
- * @access public
- * @return void
- */
- function testRemoveLastTopParent() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $initialTopNodes = $this->Tree->childCount(false);
- $result = $this->Tree->findByName('1. Root');
- $this->Tree->removeFromTree($result[$modelClass]['id']);
- $laterCount = $this->Tree->find('count');
- $laterTopNodes = $this->Tree->childCount(false);
- $this->assertEqual($initialCount, $laterCount);
- $this->assertEqual($initialTopNodes, $laterTopNodes);
- $topNodes = $this->Tree->children(false, true,array('name'));
- $expects = array(array($modelClass => array('name' => '1.1')),
- array($modelClass => array('name' => '1.2')),
- array($modelClass => array('name' => '1. Root')));
- $this->assertEqual($topNodes, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testRemoveNoChildren method
- *
- * @return void
- */
- public function testRemoveNoChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1.1');
- $this->Tree->removeFromTree($result[$modelClass]['id']);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount, $laterCount);
- $nodes = $this->Tree->find('list', array('order' => $leftField));
- $expects = array(
- 1 => '1. Root',
- 2 => '1.1',
- 4 => '1.1.2',
- 5 => '1.2',
- 6 => '1.2.1',
- 7 => '1.2.2',
- 3 => '1.1.1',
- );
- $this->assertEqual($nodes, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testRemoveAndDelete method
- *
- * @access public
- * @return void
- */
- function testRemoveAndDelete() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1');
- $this->Tree->removeFromTree($result[$modelClass]['id'], true);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount-1, $laterCount);
- $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
- $expects= array(array($modelClass => array('name' => '1.1.1')),
- array($modelClass => array('name' => '1.1.2')),
- array($modelClass => array('name' => '1.2')));
- $this->assertEqual($children, $expects);
- $topNodes = $this->Tree->children(false, true,array('name'));
- $expects = array(array($modelClass => array('name' => '1. Root')));
- $this->assertEqual($topNodes, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testRemoveAndDeleteNoChildren method
- *
- * @return void
- */
- public function testRemoveAndDeleteNoChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1.1');
- $this->Tree->removeFromTree($result[$modelClass]['id'], true);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount - 1, $laterCount);
- $nodes = $this->Tree->find('list', array('order' => $leftField));
- $expects = array(
- 1 => '1. Root',
- 2 => '1.1',
- 4 => '1.1.2',
- 5 => '1.2',
- 6 => '1.2.1',
- 7 => '1.2.2',
- );
- $this->assertEqual($nodes, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testChildren method
- *
- * @access public
- * @return void
- */
- function testChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $this->Tree->id= $data[$modelClass]['id'];
- $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
- $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
- array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
- $this->assertEqual($direct, $expects);
- $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
- $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
- array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
- array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
- array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
- array($modelClass => array( 'id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
- array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
- $this->assertEqual($total, $expects);
- $this->assertEqual(array(), $this->Tree->children(10000));
- }
- /**
- * testCountChildren method
- *
- * @access public
- * @return void
- */
- function testCountChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $this->Tree->id = $data[$modelClass]['id'];
- $direct = $this->Tree->childCount(null, true);
- $this->assertEqual($direct, 2);
- $total = $this->Tree->childCount();
- $this->assertEqual($total, 6);
- }
- /**
- * testGetParentNode method
- *
- * @access public
- * @return void
- */
- function testGetParentNode() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
- $this->Tree->id= $data[$modelClass]['id'];
- $result = $this->Tree->getParentNode(null, array('name'));
- $expects = array($modelClass => array('name' => '1.2'));
- $this->assertIdentical($result, $expects);
- }
- /**
- * testGetPath method
- *
- * @access public
- * @return void
- */
- function testGetPath() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
- $this->Tree->id= $data[$modelClass]['id'];
- $result = $this->Tree->getPath(null, array('name'));
- $expects = array(array($modelClass => array('name' => '1. Root')),
- array($modelClass => array('name' => '1.2')),
- array($modelClass => array('name' => '1.2.2')));
- $this->assertIdentical($result, $expects);
- }
- /**
- * testNoAmbiguousColumn method
- *
- * @access public
- * @return void
- */
- function testNoAmbiguousColumn() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
- array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $this->Tree->id= $data[$modelClass]['id'];
- $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
- $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
- array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
- $this->assertEqual($direct, $expects);
- $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
- $expects = array(
- array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
- array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
- array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
- array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
- array($modelClass => array( 'id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
- array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
- );
- $this->assertEqual($total, $expects);
- }
- /**
- * testReorderTree method
- *
- * @access public
- * @return void
- */
- function testReorderTree() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(3, 3);
- $nodes = $this->Tree->find('list', array('order' => $leftField));
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
- $this->Tree->moveDown($data[$modelClass]['id']);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
- $this->Tree->moveDown($data[$modelClass]['id']);
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
- $this->Tree->moveDown($data[$modelClass]['id']);
- $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
- $this->assertEquals($nodes, $unsortedNodes);
- $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
- $this->Tree->reorder();
- $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
- $this->assertIdentical($nodes, $sortedNodes);
- }
- /**
- * test reordering large-ish trees with cacheQueries = true.
- * This caused infinite loops when moving down elements as stale data is returned
- * from the memory cache
- *
- * @access public
- * @return void
- */
- function testReorderBigTreeWithQueryCaching() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 10);
- $original = $this->Tree->cacheQueries;
- $this->Tree->cacheQueries = true;
- $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
- $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
- $this->Tree->cacheQueries = $original;
- }
- /**
- * testGenerateTreeListWithSelfJoin method
- *
- * @access public
- * @return void
- */
- function testGenerateTreeListWithSelfJoin() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
- array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->generateTreeList();
- $expected = array(1 => '1. Root', 2 => '_1.1', 3 => '__1.1.1', 4 => '__1.1.2', 5 => '_1.2', 6 => '__1.2.1', 7 => '__1.2.2');
- $this->assertIdentical($result, $expected);
- }
- /**
- * testArraySyntax method
- *
- * @access public
- * @return void
- */
- function testArraySyntax() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(3, 3);
- $this->assertIdentical($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
- $this->assertIdentical($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
- $this->assertIdentical($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
- }
- }
- /**
- * ScopedTreeTest class
- *
- * @package cake.tests.cases.libs.model.behaviors
- */
- class ScopedTreeTest extends CakeTestCase {
- /**
- * Whether backup global state for each test method or not
- *
- * @var bool false
- * @access public
- */
- public $backupGlobals = false;
- /**
- * settings property
- *
- * @var array
- * @access public
- */
- public $settings = array(
- 'modelClass' => 'FlagTree',
- 'leftField' => 'lft',
- 'rightField' => 'rght',
- 'parentField' => 'parent_id'
- );
- /**
- * fixtures property
- *
- * @var array
- * @access public
- */
- public $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
- /**
- * testStringScope method
- *
- * @access public
- * @return void
- */
- function testStringScope() {
- $this->Tree = new FlagTree();
- $this->Tree->initialize(2, 3);
- $this->Tree->id = 1;
- $this->Tree->saveField('flag', 1);
- $this->Tree->id = 2;
- $this->Tree->saveField('flag', 1);
- $result = $this->Tree->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->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
- $this->assertEqual($this->Tree->children(), array());
- $this->Tree->id = 1;
- $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
- $result = $this->Tree->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->Tree->delete());
- $this->assertEqual($this->Tree->find('count'), 11);
- }
- /**
- * testArrayScope method
- *
- * @access public
- * @return void
- */
- function testArrayScope() {
- $this->Tree = new FlagTree();
- $this->Tree->initialize(2, 3);
- $this->Tree->id = 1;
- $this->Tree->saveField('flag', 1);
- $this->Tree->id = 2;
- $this->Tree->saveField('flag', 1);
- $result = $this->Tree->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->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
- $this->assertEqual($this->Tree->children(), array());
- $this->Tree->id = 1;
- $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
- $result = $this->Tree->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->Tree->delete());
- $this->assertEqual($this->Tree->find('count'), 11);
- }
- /**
- * testMoveUpWithScope method
- *
- * @access public
- * @return void
- */
- function testMoveUpWithScope() {
- $this->Ad = new Ad();
- $this->Ad->Behaviors->attach('Tree', array('scope'=>'Campaign'));
- $this->Ad->moveUp(6);
- $this->Ad->id = 4;
- $result = $this->Ad->children();
- $this->assertEqual(Set::extract('/Ad/id', $result), array(6, 5));
- $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2));
- }
- /**
- * testMoveDownWithScope method
- *
- * @access public
- * @return void
- */
- function testMoveDownWithScope() {
- $this->Ad = new Ad();
- $this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
- $this->Ad->moveDown(6);
- $this->Ad->id = 4;
- $result = $this->Ad->children();
- $this->assertEqual(Set::extract('/Ad/id', $result), array(5, 6));
- $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2));
- }
- /**
- * Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
- * to callback hooks
- *
- * @access public
- * @return void
- */
- function testTranslatingTree() {
- $this->Tree = new FlagTree();
- $this->Tree->cacheQueries = false;
- $this->Tree->Behaviors->attach('Translate', array('name'));
- //Save
- $this->Tree->locale = 'eng';
- $data = array('FlagTree' => array(
- 'name' => 'name #1',
- 'locale' => 'eng',
- 'parent_id' => null,
- ));
- $this->Tree->save($data);
- $result = $this->Tree->find('all');
- $expected = array(array('FlagTree' => array(
- 'id' => 1,
- 'name' => 'name #1',
- 'parent_id' => null,
- 'lft' => 1,
- 'rght' => 2,
- 'flag' => 0,
- 'locale' => 'eng',
- )));
- $this->assertEqual($result, $expected);
- //update existing record, same locale
- $this->Tree->create();
- $data['FlagTree']['name'] = 'Named 2';
- $this->Tree->id = 1;
- $this->Tree->save($data);
- $result = $this->Tree->find('all');
- $expected = array(array('FlagTree' => array(
- 'id' => 1,
- 'name' => 'Named 2',
- 'parent_id' => null,
- 'lft' => 1,
- 'rght' => 2,
- 'flag' => 0,
- 'locale' => 'eng',
- )));
- $this->assertEqual($result, $expected);
- //update different locale, same record
- $this->Tree->create();
- $this->Tree->locale = 'deu';
- $this->Tree->id = 1;
- $data = array('FlagTree' => array(
- 'id' => 1,
- 'parent_id' => null,
- 'name' => 'namen #1',
- 'locale' => 'deu',
- ));
- $this->Tree->save($data);
- $this->Tree->locale = 'deu';
- $result = $this->Tree->find('all');
- $expected = array(array('FlagTree' => array(
- 'id' => 1,
- 'name' => 'namen #1',
- 'parent_id' => null,
- 'lft' => 1,
- 'rght' => 2,
- 'flag' => 0,
- 'locale' => 'deu',
- )));
- $this->assertEqual($result, $expected);
- //Save with bindTranslation
- $this->Tree->locale = 'eng';
- $data = array(
- 'name' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
- 'parent_id' => null
- );
- $this->Tree->create($data);
- $this->Tree->save();
- $this->Tree->unbindTranslation();
- $translations = array('name' => 'Name');
- $this->Tree->bindTranslation($translations, false);
- $this->Tree->locale = array('eng', 'spa');
- $result = $this->Tree->read();
- $expected = array(
- 'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => 0, 'lft' => 3, 'rght' => 4),
- 'Name' => array(
- array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'New title'),
- array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'Nuevo leyenda')
- ),
- );
- $this->assertEqual($result, $expected);
- }
- /**
- * testGenerateTreeListWithSelfJoin method
- *
- * @return void
- */
- public function testAliasesWithScopeInTwoTreeAssociations() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->TreeTwo = new NumberTreeTwo();
- $record = $this->Tree->find('first');
- $this->Tree->bindModel(array(
- 'hasMany' => array(
- 'SecondTree' => array(
- 'className' => 'NumberTreeTwo',
- 'foreignKey' => 'number_tree_id'
- )
- )
- ));
- $this->TreeTwo->bindModel(array(
- 'belongsTo' => array(
- 'FirstTree' => array(
- 'className' => $modelClass,
- 'foreignKey' => 'number_tree_id'
- )
- )
- ));
- $this->TreeTwo->Behaviors->attach('Tree', array(
- 'scope' => 'FirstTree'
- ));
- $data = array(
- 'NumberTreeTwo' => array(
- 'name' => 'First',
- 'number_tree_id' => $record['FlagTree']['id']
- )
- );
- $this->TreeTwo->create();
- $result = $this->TreeTwo->save($data);
- $this->assertFalse(empty($result));
- $result = $this->TreeTwo->find('first');
- $expected = array('NumberTreeTwo' => array(
- 'id' => 1,
- 'name' => 'First',
- 'number_tree_id' => $record['FlagTree']['id'],
- 'parent_id' => null,
- 'lft' => 1,
- 'rght' => 2
- ));
- $this->assertEqual($result, $expected);
- }
- }
- /**
- * AfterTreeTest class
- *
- * @package cake.tests.cases.libs.model.behaviors
- */
- class AfterTreeTest extends CakeTestCase {
- /**
- * Whether backup global state for each test method or not
- *
- * @var bool false
- * @access public
- */
- public $backupGlobals = false;
- /**
- * settings property
- *
- * @var array
- * @access public
- */
- public $settings = array(
- 'modelClass' => 'AfterTree',
- 'leftField' => 'lft',
- 'rightField' => 'rght',
- 'parentField' => 'parent_id'
- );
- /**
- * fixtures property
- *
- * @var array
- * @access public
- */
- public $fixtures = array('core.after_tree');
- /**
- * Tests the afterSave callback in the model
- *
- * @access public
- * @return void
- */
- function testAftersaveCallback() {
- $this->Tree = new AfterTree();
- $expected = array('AfterTree' => array('name' => 'Six and One Half Changed in AfterTree::afterSave() but not in database', 'parent_id' => 6, 'lft' => 11, 'rght' => 12));
- $result = $this->Tree->save(array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6)));
- $this->assertEqual($result, $expected);
- $expected = array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6, 'lft' => 11, 'rght' => 12, 'id' => 8));
- $result = $this->Tree->find('all');
- $this->assertEqual($result[7], $expected);
- }
- }
- /**
- * UuidTreeTest class
- *
- * @package cake.tests.cases.libs.model.behaviors
- */
- class UuidTreeTest extends CakeTestCase {
- /**
- * Whether backup global state for each test method or not
- *
- * @var bool false
- * @access public
- */
- public $backupGlobals = false;
- /**
- * settings property
- *
- * @var array
- * @access public
- */
- public $settings = array(
- 'modelClass' => 'UuidTree',
- 'leftField' => 'lft',
- 'rightField' => 'rght',
- 'parentField' => 'parent_id'
- );
- /**
- * fixtures property
- *
- * @var array
- * @access public
- */
- public $fixtures = array('core.uuid_tree');
- /**
- * testMovePromote method
- *
- * @return void
- */
- public function testMovePromote() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $parent_id = $parent[$modelClass]['id'];
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
- $this->Tree->id= $data[$modelClass]['id'];
- $this->Tree->saveField($parentField, $parent_id);
- $direct = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
- $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
- array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
- array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
- $this->assertEqual($direct, $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testMoveWithWhitelist method
- *
- * @return void
- */
- public function testMoveWithWhitelist() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->id = null;
- $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $parent_id = $parent[$modelClass]['id'];
- $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
- $this->Tree->id = $data[$modelClass]['id'];
- $this->Tree->whitelist = array($parentField, 'name', 'description');
- $this->Tree->saveField($parentField, $parent_id);
- $result = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
- $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
- array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
- array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
- $this->assertEqual($result, $expected);
- $this->assertTrue($this->Tree->verify());
- }
- /**
- * testRemoveNoChildren method
- *
- * @return void
- */
- public function testRemoveNoChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1.1');
- $this->Tree->removeFromTree($result[$modelClass]['id']);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount, $laterCount);
- $nodes = $this->Tree->find('list', array('order' => $leftField));
- $expects = array(
- '1. Root',
- '1.1',
- '1.1.2',
- '1.2',
- '1.2.1',
- '1.2.2',
- '1.1.1',
- );
- $this->assertEqual(array_values($nodes), $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testRemoveAndDeleteNoChildren method
- *
- * @return void
- */
- public function testRemoveAndDeleteNoChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $initialCount = $this->Tree->find('count');
- $result = $this->Tree->findByName('1.1.1');
- $this->Tree->removeFromTree($result[$modelClass]['id'], true);
- $laterCount = $this->Tree->find('count');
- $this->assertEqual($initialCount - 1, $laterCount);
- $nodes = $this->Tree->find('list', array('order' => $leftField));
- $expects = array(
- '1. Root',
- '1.1',
- '1.1.2',
- '1.2',
- '1.2.1',
- '1.2.2',
- );
- $this->assertEqual(array_values($nodes), $expects);
- $validTree = $this->Tree->verify();
- $this->assertIdentical($validTree, true);
- }
- /**
- * testChildren method
- *
- * @return void
- */
- public function testChildren() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $this->Tree->id = $data[$modelClass]['id'];
- $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
- $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
- array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
- $this->assertEqual($direct, $expects);
- $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
- $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
- array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
- array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
- array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
- array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
- array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
- $this->assertEqual($total, $expects);
- }
- /**
- * testNoAmbiguousColumn method
- *
- * @return void
- */
- public function testNoAmbiguousColumn() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->initialize(2, 2);
- $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
- array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
- $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
- $this->Tree->id = $data[$modelClass]['id'];
- $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
- $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
- array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
- $this->assertEqual($direct, $expects);
- $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
- $expects = array(
- array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
- array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
- array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
- array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
- array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
- array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
- );
- $this->assertEqual($total, $expects);
- }
- /**
- * testGenerateTreeListWithSelfJoin method
- *
- * @return void
- */
- public function testGenerateTreeListWithSelfJoin() {
- extract($this->settings);
- $this->Tree = new $modelClass();
- $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
- array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
- $this->Tree->initialize(2, 2);
- $result = $this->Tree->generateTreeList();
- $expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
- $this->assertIdentical(array_values($result), $expected);
- }
- }
|