| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM\Behavior;
- use Cake\ORM\Entity;
- use Cake\TestSuite\TestCase;
- /**
- * Translate behavior test case
- */
- class TreeBehaviorTest extends TestCase
- {
- /**
- * fixtures
- *
- * @var array
- */
- public $fixtures = [
- 'core.MenuLinkTrees',
- 'core.NumberTrees',
- ];
- public function setUp()
- {
- parent::setUp();
- $this->table = $this->getTableLocator()->get('NumberTrees');
- $this->table->setPrimaryKey(['id']);
- $this->table->addBehavior('Tree');
- }
- /**
- * Sanity test
- *
- * Make sure the assert method acts as you'd expect, this is the expected
- * initial db state
- *
- * @return void
- */
- public function testAssertMpttValues()
- {
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:19 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '__17:18 - 10:radios',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- $table->removeBehavior('Tree');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'categories']]);
- $expected = [
- ' 1:10 - 9:electronics',
- '_ 2: 9 - 10:televisions',
- '__ 3: 4 - 11:tube',
- '__ 5: 8 - 12:lcd',
- '___ 6: 7 - 13:plasma',
- '11:20 - 14:portable',
- '_12:15 - 15:mp3',
- '__13:14 - 16:flash',
- '_16:17 - 17:cd',
- '_18:19 - 18:radios',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * 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->assertSame([1, 6, 10], $nodes->extract('id')->toArray());
- $nodes = $this->table->find('path', ['for' => 5]);
- $this->assertSame([1, 2, 5], $nodes->extract('id')->toArray());
- $nodes = $this->table->find('path', ['for' => 1]);
- $this->assertSame([1], $nodes->extract('id')->toArray());
- $entity = $this->table->newEntity(['name' => 'odd one', 'parent_id' => 1]);
- $entity = $this->table->save($entity);
- $newId = $entity->id;
- $entity = $this->table->get(2);
- $entity->parent_id = $newId;
- $this->table->save($entity);
- $nodes = $this->table->find('path', ['for' => 4]);
- $this->assertSame([1, $newId, 2, 4], $nodes->extract('id')->toArray());
- // find path with scope
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $nodes = $table->find('path', ['for' => 5]);
- $this->assertSame([1, 3, 4, 5], $nodes->extract('id')->toArray());
- }
- /**
- * Tests the childCount() method
- *
- * @return void
- */
- public function testChildCount()
- {
- // direct children for the root node
- $table = $this->table;
- $countDirect = $this->table->childCount($table->get(1), true);
- $this->assertEquals(2, $countDirect);
- // counts all the children of root
- $count = $this->table->childCount($table->get(1), false);
- $this->assertEquals(9, $count);
- // counts direct children
- $count = $this->table->childCount($table->get(2), false);
- $this->assertEquals(3, $count);
- // count children for a middle-node
- $count = $this->table->childCount($table->get(6), false);
- $this->assertEquals(4, $count);
- // count leaf children
- $count = $this->table->childCount($table->get(10), false);
- $this->assertEquals(0, $count);
- // test scoping
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $count = $table->childCount($table->get(3), false);
- $this->assertEquals(2, $count);
- }
- /**
- * Tests that childCount will provide the correct lft and rght values
- *
- * @return void
- */
- public function testChildCountNoTreeColumns()
- {
- $table = $this->table;
- $node = $table->get(6);
- $node->unsetProperty('lft');
- $node->unsetProperty('rght');
- $count = $this->table->childCount($node, false);
- $this->assertEquals(4, $count);
- }
- /**
- * Tests the childCount() plus callable scoping
- *
- * @return void
- */
- public function testScopeCallable()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', [
- 'scope' => function ($query) {
- return $query->where(['menu' => 'main-menu']);
- },
- ]);
- $count = $table->childCount($table->get(1), false);
- $this->assertEquals(4, $count);
- }
- /**
- * Tests the find('children') method
- *
- * @return void
- */
- public function testFindChildren()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- // root
- $nodeIds = [];
- $nodes = $table->find('children', ['for' => 1])->all();
- $this->assertEquals([2, 3, 4, 5], $nodes->extract('id')->toArray());
- // leaf
- $nodeIds = [];
- $nodes = $table->find('children', ['for' => 5])->all();
- $this->assertCount(0, $nodes->extract('id')->toArray());
- // direct children
- $nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
- $this->assertEquals([2, 3], $nodes->extract('id')->toArray());
- }
- /**
- * Tests the find('children') plus scope=null
- *
- * @return void
- */
- public function testScopeNull()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree');
- $table->behaviors()->get('Tree')->setConfig('scope', null);
- $nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
- $this->assertEquals([2, 3], $nodes->extract('id')->toArray());
- }
- /**
- * Tests that find('children') will throw an exception if the node was not found
- *
- * @return void
- */
- public function testFindChildrenException()
- {
- $this->expectException(\Cake\Datasource\Exception\RecordNotFoundException::class);
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $query = $table->find('children', ['for' => 500]);
- }
- /**
- * Tests the find('treeList') method
- *
- * @return void
- */
- public function testFindTreeList()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $query = $table->find('treeList');
- $result = null;
- $query->clause('order')->iterateParts(function ($dir, $field) use (&$result) {
- $result = $field;
- });
- $this->assertEquals('MenuLinkTrees.lft', $result);
- $result = $query->toArray();
- $expected = [
- 1 => 'Link 1',
- 2 => '_Link 2',
- 3 => '_Link 3',
- 4 => '__Link 4',
- 5 => '___Link 5',
- 6 => 'Link 6',
- 7 => '_Link 7',
- 8 => 'Link 8',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests the find('treeList') method after moveUp, moveDown
- *
- * @return void
- */
- public function testFindTreeListAfterMove()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- // moveUp
- $table->moveUp($table->get(3), 1);
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 7 - 3:Link 3',
- '__ 3: 6 - 4:Link 4',
- '___ 4: 5 - 5:Link 5',
- '_ 8: 9 - 2:Link 2',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- // moveDown
- $table->moveDown($table->get(6), 1);
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 7 - 3:Link 3',
- '__ 3: 6 - 4:Link 4',
- '___ 4: 5 - 5:Link 5',
- '_ 8: 9 - 2:Link 2',
- '11:12 - 8:Link 8',
- '13:16 - 6:Link 6',
- '_14:15 - 7:Link 7',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests the find('treeList') method with custom options
- *
- * @return void
- */
- public function testFindTreeListCustom()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $result = $table
- ->find('treeList', ['keyPath' => 'url', 'valuePath' => 'id', 'spacer' => ' '])
- ->toArray();
- $expected = [
- '/link1.html' => '1',
- 'http://example.com' => ' 2',
- '/what/even-more-links.html' => ' 3',
- '/lorem/ipsum.html' => ' 4',
- '/what/the.html' => ' 5',
- '/yeah/another-link.html' => '6',
- 'https://cakephp.org' => ' 7',
- '/page/who-we-are.html' => '8',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests the testFormatTreeListCustom() method.
- *
- * @return void
- */
- public function testFormatTreeListCustom()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree');
- $query = $table
- ->find('threaded')
- ->where(['menu' => 'main-menu']);
- $options = ['keyPath' => 'url', 'valuePath' => 'id', 'spacer' => ' '];
- $result = $table->formatTreeList($query, $options)->toArray();
- $expected = [
- '/link1.html' => '1',
- 'http://example.com' => ' 2',
- '/what/even-more-links.html' => ' 3',
- '/lorem/ipsum.html' => ' 4',
- '/what/the.html' => ' 5',
- '/yeah/another-link.html' => '6',
- 'https://cakephp.org' => ' 7',
- '/page/who-we-are.html' => '8',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests the moveUp() method
- *
- * @return void
- */
- public function testMoveUp()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- // top level, won't move
- $node = $this->table->moveUp($table->get(1), 10);
- $this->assertEquals(['lft' => 1, 'rght' => 10], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- // edge cases
- $this->assertFalse($this->table->moveUp($table->get(1), 0));
- $this->assertFalse($this->table->moveUp($table->get(1), -10));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- // move inner node
- $node = $table->moveUp($table->get(3), 1);
- $nodes = $table->find('children', ['for' => 1])->all();
- $this->assertEquals(['lft' => 2, 'rght' => 7], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 7 - 3:Link 3',
- '__ 3: 6 - 4:Link 4',
- '___ 4: 5 - 5:Link 5',
- '_ 8: 9 - 2:Link 2',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a node with no siblings
- *
- * @return void
- */
- public function testMoveLeaf()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $node = $table->moveUp($table->get(5), 1);
- $this->assertEquals(['lft' => 6, 'rght' => 7], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a node to the top
- *
- * @return void
- */
- public function testMoveTop()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $node = $table->moveUp($table->get(8), true);
- $expected = [
- ' 1: 2 - 8:Link 8',
- ' 3:12 - 1:Link 1',
- '_ 4: 5 - 2:Link 2',
- '_ 6:11 - 3:Link 3',
- '__ 7:10 - 4:Link 4',
- '___ 8: 9 - 5:Link 5',
- '13:16 - 6:Link 6',
- '_14:15 - 7:Link 7',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a node with no lft and rght
- *
- * @return void
- */
- public function testMoveNoTreeColumns()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $node = $table->get(8);
- $node->unsetProperty('lft');
- $node->unsetProperty('rght');
- $node = $table->moveUp($node, true);
- $this->assertEquals(['lft' => 1, 'rght' => 2], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1: 2 - 8:Link 8',
- ' 3:12 - 1:Link 1',
- '_ 4: 5 - 2:Link 2',
- '_ 6:11 - 3:Link 3',
- '__ 7:10 - 4:Link 4',
- '___ 8: 9 - 5:Link 5',
- '13:16 - 6:Link 6',
- '_14:15 - 7:Link 7',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests the moveDown() method
- *
- * @return void
- */
- public function testMoveDown()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- // latest node, won't move
- $node = $table->moveDown($table->get(8), 10);
- $this->assertEquals(['lft' => 15, 'rght' => 16], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- // edge cases
- $this->assertFalse($this->table->moveDown($table->get(8), 0));
- $this->assertFalse($this->table->moveDown($table->get(8), -10));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- // move inner node
- $node = $table->moveDown($table->get(2), 1);
- $this->assertEquals(['lft' => 8, 'rght' => 9], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 7 - 3:Link 3',
- '__ 3: 6 - 4:Link 4',
- '___ 4: 5 - 5:Link 5',
- '_ 8: 9 - 2:Link 2',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a node that has no siblings
- *
- * @return void
- */
- public function testMoveLeafDown()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $node = $table->moveDown($table->get(5), 1);
- $this->assertEquals(['lft' => 6, 'rght' => 7], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a node to the bottom
- *
- * @return void
- */
- public function testMoveToBottom()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $node = $table->moveDown($table->get(1), true);
- $this->assertEquals(['lft' => 7, 'rght' => 16], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1: 4 - 6:Link 6',
- '_ 2: 3 - 7:Link 7',
- ' 5: 6 - 8:Link 8',
- ' 7:16 - 1:Link 1',
- '_ 8: 9 - 2:Link 2',
- '_10:15 - 3:Link 3',
- '__11:14 - 4:Link 4',
- '___12:13 - 5:Link 5',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a node with no lft and rght columns
- *
- * @return void
- */
- public function testMoveDownNoTreeColumns()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $node = $table->get(1);
- $node->unsetProperty('lft');
- $node->unsetProperty('rght');
- $node = $table->moveDown($node, true);
- $this->assertEquals(['lft' => 7, 'rght' => 16], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1: 4 - 6:Link 6',
- '_ 2: 3 - 7:Link 7',
- ' 5: 6 - 8:Link 8',
- ' 7:16 - 1:Link 1',
- '_ 8: 9 - 2:Link 2',
- '_10:15 - 3:Link 3',
- '__11:14 - 4:Link 4',
- '___12:13 - 5:Link 5',
- ];
- $this->assertMpttValues($expected, $table);
- }
- public function testMoveDownMultiplePositions()
- {
- $node = $this->table->moveDown($this->table->get(3), 2);
- $this->assertEquals(['lft' => 7, 'rght' => 8], $node->extract(['lft', 'rght']));
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 4:lcd',
- '__ 5: 6 - 5:plasma',
- '__ 7: 8 - 3:tube',
- '_10:19 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '__17:18 - 10:radios',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests the recover function
- *
- * @return void
- */
- public function testRecover()
- {
- $table = $this->table;
- $expectedLevels = $table
- ->find('list', ['valueField' => 'depth'])
- ->order('lft')
- ->toArray();
- $table->updateAll(['lft' => null, 'rght' => null, 'depth' => null], []);
- $table->behaviors()->Tree->setConfig('level', 'depth');
- $table->recover();
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:19 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '__17:18 - 10:radios',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $table);
- $result = $table
- ->find('list', ['valueField' => 'depth'])
- ->order('lft')
- ->toArray();
- $this->assertSame($expectedLevels, $result);
- }
- /**
- * Tests the recover function with a custom scope
- *
- * @return void
- */
- public function testRecoverScoped()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
- $table->recover();
- $expected = [
- ' 1:10 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- '_ 4: 9 - 3:Link 3',
- '__ 5: 8 - 4:Link 4',
- '___ 6: 7 - 5:Link 5',
- '11:14 - 6:Link 6',
- '_12:13 - 7:Link 7',
- '15:16 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- $table->removeBehavior('Tree');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'categories']]);
- $expected = [
- ' 1:10 - 9:electronics',
- '_ 2: 9 - 10:televisions',
- '__ 3: 4 - 11:tube',
- '__ 5: 8 - 12:lcd',
- '___ 6: 7 - 13:plasma',
- '11:20 - 14:portable',
- '_12:15 - 15:mp3',
- '__13:14 - 16:flash',
- '_16:17 - 17:cd',
- '_18:19 - 18:radios',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Test recover function with a custom order clause
- *
- * @return void
- */
- public function testRecoverWithCustomOrder()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu'], 'recoverOrder' => ['MenuLinkTrees.title' => 'desc']]);
- $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
- $table->recover();
- $expected = [
- ' 1: 2 - 8:Link 8',
- ' 3: 6 - 6:Link 6',
- '_ 4: 5 - 7:Link 7',
- ' 7:16 - 1:Link 1',
- '_ 8:13 - 3:Link 3',
- '__ 9:12 - 4:Link 4',
- '___10:11 - 5:Link 5',
- '_14:15 - 2:Link 2',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests adding a new orphan node
- *
- * @return void
- */
- public function testAddOrphan()
- {
- $table = $this->table;
- $entity = new Entity(
- ['name' => 'New Orphan', 'parent_id' => null, 'level' => null],
- ['markNew' => true]
- );
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(23, $entity->lft);
- $this->assertEquals(24, $entity->rght);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:19 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '__17:18 - 10:radios',
- '21:22 - 11:alien hardware',
- '23:24 - 12:New Orphan',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests that adding a child node as a descendant of one of the roots works
- *
- * @return void
- */
- public function testAddMiddle()
- {
- $table = $this->table;
- $entity = new Entity(
- ['name' => 'laptops', 'parent_id' => 1],
- ['markNew' => true]
- );
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(20, $entity->lft);
- $this->assertEquals(21, $entity->rght);
- $expected = [
- ' 1:22 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:19 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '__17:18 - 10:radios',
- '_20:21 - 12:laptops',
- '23:24 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests adding a leaf to the tree
- *
- * @return void
- */
- public function testAddLeaf()
- {
- $table = $this->table;
- $entity = new Entity(
- ['name' => 'laptops', 'parent_id' => 2],
- ['markNew' => true]
- );
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(9, $entity->lft);
- $this->assertEquals(10, $entity->rght);
- $expected = [
- ' 1:22 - 1:electronics',
- '_ 2:11 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '__ 9:10 - 12:laptops',
- '_12:21 - 6:portable',
- '__13:16 - 7:mp3',
- '___14:15 - 8:flash',
- '__17:18 - 9:cd',
- '__19:20 - 10:radios',
- '23:24 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests adding a root element to the tree when all other root elements have children
- *
- * @return void
- */
- public function testAddRoot()
- {
- $table = $this->table;
- //First add a child to the empty root element
- $alien = $table->find()->where(['name' => 'alien hardware'])->first();
- $entity = new Entity(['name' => 'plasma rifle', 'parent_id' => $alien->id], ['markNew' => true]);
- $table->save($entity);
- $entity = new Entity(['name' => 'carpentry', 'parent_id' => null], ['markNew' => true]);
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(25, $entity->lft);
- $this->assertEquals(26, $entity->rght);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:19 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '__17:18 - 10:radios',
- '21:24 - 11:alien hardware',
- '_22:23 - 12:plasma rifle',
- '25:26 - 13:carpentry',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests making a node its own parent as an existing entity
- *
- * @return void
- */
- public function testReParentSelf()
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Cannot set a node\'s parent as itself');
- $entity = $this->table->get(1);
- $entity->parent_id = $entity->id;
- $this->table->save($entity);
- }
- /**
- * Tests making a node its own parent as a new entity.
- *
- * @return void
- */
- public function testReParentSelfNewEntity()
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Cannot set a node\'s parent as itself');
- $entity = $this->table->newEntity(['name' => 'root']);
- $entity->id = 1;
- $entity->parent_id = $entity->id;
- $this->table->save($entity);
- }
- /**
- * Tests moving a subtree to the right
- *
- * @return void
- */
- public function testReParentSubTreeRight()
- {
- $table = $this->table;
- $entity = $table->get(2);
- $entity->parent_id = 6;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(11, $entity->lft);
- $this->assertEquals(18, $entity->rght);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2:19 - 6:portable',
- '__ 3: 6 - 7:mp3',
- '___ 4: 5 - 8:flash',
- '__ 7: 8 - 9:cd',
- '__ 9:10 - 10:radios',
- '__11:18 - 2:televisions',
- '___12:13 - 3:tube',
- '___14:15 - 4:lcd',
- '___16:17 - 5:plasma',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a subtree to the left
- *
- * @return void
- */
- public function testReParentSubTreeLeft()
- {
- $table = $this->table;
- $entity = $table->get(6);
- $entity->parent_id = 2;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(9, $entity->lft);
- $this->assertEquals(18, $entity->rght);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2:19 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '__ 9:18 - 6:portable',
- '___10:13 - 7:mp3',
- '____11:12 - 8:flash',
- '___14:15 - 9:cd',
- '___16:17 - 10:radios',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Test moving a leaft to the left
- *
- * @return void
- */
- public function testReParentLeafLeft()
- {
- $table = $this->table;
- $entity = $table->get(10);
- $entity->parent_id = 2;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(9, $entity->lft);
- $this->assertEquals(10, $entity->rght);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2:11 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '__ 9:10 - 10:radios',
- '_12:19 - 6:portable',
- '__13:16 - 7:mp3',
- '___14:15 - 8:flash',
- '__17:18 - 9:cd',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Test moving a leaf to the left
- *
- * @return void
- */
- public function testReParentLeafRight()
- {
- $table = $this->table;
- $entity = $table->get(5);
- $entity->parent_id = 6;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(17, $entity->lft);
- $this->assertEquals(18, $entity->rght);
- $result = $table->find()->order('lft')->enableHydration(false);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2: 7 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '_ 8:19 - 6:portable',
- '__ 9:12 - 7:mp3',
- '___10:11 - 8:flash',
- '__13:14 - 9:cd',
- '__15:16 - 10:radios',
- '__17:18 - 5:plasma',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a subtree with a node having no lft and rght columns
- *
- * @return void
- */
- public function testReParentNoTreeColumns()
- {
- $table = $this->table;
- $entity = $table->get(6);
- $entity->unsetProperty('lft');
- $entity->unsetProperty('rght');
- $entity->parent_id = 2;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(9, $entity->lft);
- $this->assertEquals(18, $entity->rght);
- $expected = [
- ' 1:20 - 1:electronics',
- '_ 2:19 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '__ 9:18 - 6:portable',
- '___10:13 - 7:mp3',
- '____11:12 - 8:flash',
- '___14:15 - 9:cd',
- '___16:17 - 10:radios',
- '21:22 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests moving a subtree as a new root
- *
- * @return void
- */
- public function testRootingSubTree()
- {
- $table = $this->table;
- $entity = $table->get(2);
- $entity->parent_id = null;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(15, $entity->lft);
- $this->assertEquals(22, $entity->rght);
- $expected = [
- ' 1:12 - 1:electronics',
- '_ 2:11 - 6:portable',
- '__ 3: 6 - 7:mp3',
- '___ 4: 5 - 8:flash',
- '__ 7: 8 - 9:cd',
- '__ 9:10 - 10:radios',
- '13:14 - 11:alien hardware',
- '15:22 - 2:televisions',
- '_16:17 - 3:tube',
- '_18:19 - 4:lcd',
- '_20:21 - 5:plasma',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests moving a subtree with no tree columns
- *
- * @return void
- */
- public function testRootingNoTreeColumns()
- {
- $table = $this->table;
- $entity = $table->get(2);
- $entity->unsetProperty('lft');
- $entity->unsetProperty('rght');
- $entity->parent_id = null;
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals(15, $entity->lft);
- $this->assertEquals(22, $entity->rght);
- $expected = [
- ' 1:12 - 1:electronics',
- '_ 2:11 - 6:portable',
- '__ 3: 6 - 7:mp3',
- '___ 4: 5 - 8:flash',
- '__ 7: 8 - 9:cd',
- '__ 9:10 - 10:radios',
- '13:14 - 11:alien hardware',
- '15:22 - 2:televisions',
- '_16:17 - 3:tube',
- '_18:19 - 4:lcd',
- '_20:21 - 5:plasma',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests that trying to create a cycle throws an exception
- *
- * @return void
- */
- public function testReparentCycle()
- {
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Cannot use node "5" as parent for entity "2"');
- $table = $this->table;
- $entity = $table->get(2);
- $entity->parent_id = 5;
- $table->save($entity);
- }
- /**
- * Tests deleting a leaf in the tree
- *
- * @return void
- */
- public function testDeleteLeaf()
- {
- $table = $this->table;
- $entity = $table->get(4);
- $this->assertTrue($table->delete($entity));
- $expected = [
- ' 1:18 - 1:electronics',
- '_ 2: 7 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 5:plasma',
- '_ 8:17 - 6:portable',
- '__ 9:12 - 7:mp3',
- '___10:11 - 8:flash',
- '__13:14 - 9:cd',
- '__15:16 - 10:radios',
- '19:20 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests deleting a subtree
- *
- * @return void
- */
- public function testDeleteSubTree()
- {
- $table = $this->table;
- $entity = $table->get(6);
- $this->assertTrue($table->delete($entity));
- $expected = [
- ' 1:10 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '11:12 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests deleting a subtree in a scoped tree
- *
- * @return void
- */
- public function testDeleteSubTreeScopedTree()
- {
- $table = $this->getTableLocator()->get('MenuLinkTrees');
- $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
- $entity = $table->get(3);
- $this->assertTrue($table->delete($entity));
- $expected = [
- ' 1: 4 - 1:Link 1',
- '_ 2: 3 - 2:Link 2',
- ' 5: 8 - 6:Link 6',
- '_ 6: 7 - 7:Link 7',
- ' 9:10 - 8:Link 8',
- ];
- $this->assertMpttValues($expected, $table);
- $table->behaviors()->get('Tree')->setConfig('scope', ['menu' => 'categories']);
- $expected = [
- ' 1:10 - 9:electronics',
- '_ 2: 9 - 10:televisions',
- '__ 3: 4 - 11:tube',
- '__ 5: 8 - 12:lcd',
- '___ 6: 7 - 13:plasma',
- '11:20 - 14:portable',
- '_12:15 - 15:mp3',
- '__13:14 - 16:flash',
- '_16:17 - 17:cd',
- '_18:19 - 18:radios',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Test deleting a root node
- *
- * @return void
- */
- public function testDeleteRoot()
- {
- $table = $this->table;
- $entity = $table->get(1);
- $this->assertTrue($table->delete($entity));
- $expected = [
- ' 1: 2 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Test deleting a node with no tree columns
- *
- * @return void
- */
- public function testDeleteRootNoTreeColumns()
- {
- $table = $this->table;
- $entity = $table->get(1);
- $entity->unsetProperty('lft');
- $entity->unsetProperty('rght');
- $this->assertTrue($table->delete($entity));
- $expected = [
- ' 1: 2 - 11:alien hardware',
- ];
- $this->assertMpttValues($expected, $this->table);
- }
- /**
- * Tests that a leaf can be taken out of the tree and put in as a root
- *
- * @return void
- */
- public function testRemoveFromLeafFromTree()
- {
- $table = $this->table;
- $entity = $table->get(10);
- $this->assertSame($entity, $table->removeFromTree($entity));
- $this->assertEquals(21, $entity->lft);
- $this->assertEquals(22, $entity->rght);
- $this->assertNull($entity->parent_id);
- $result = $table->find()->order('lft')->enableHydration(false);
- $expected = [
- ' 1:18 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:17 - 6:portable',
- '__11:14 - 7:mp3',
- '___12:13 - 8:flash',
- '__15:16 - 9:cd',
- '19:20 - 11:alien hardware',
- '21:22 - 10:radios',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Test removing a middle node from a tree
- *
- * @return void
- */
- public function testRemoveMiddleNodeFromTree()
- {
- $table = $this->table;
- $entity = $table->get(6);
- $this->assertSame($entity, $table->removeFromTree($entity));
- $result = $table->find('threaded')->order('lft')->enableHydration(false)->toArray();
- $this->assertEquals(21, $entity->lft);
- $this->assertEquals(22, $entity->rght);
- $this->assertNull($entity->parent_id);
- $result = $table->find()->order('lft')->enableHydration(false);
- $expected = [
- ' 1:18 - 1:electronics',
- '_ 2: 9 - 2:televisions',
- '__ 3: 4 - 3:tube',
- '__ 5: 6 - 4:lcd',
- '__ 7: 8 - 5:plasma',
- '_10:13 - 7:mp3',
- '__11:12 - 8:flash',
- '_14:15 - 9:cd',
- '_16:17 - 10:radios',
- '19:20 - 11:alien hardware',
- '21:22 - 6:portable',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests removing the root of a tree
- *
- * @return void
- */
- public function testRemoveRootFromTree()
- {
- $table = $this->table;
- $entity = $table->get(1);
- $this->assertSame($entity, $table->removeFromTree($entity));
- $result = $table->find('threaded')->order('lft')->enableHydration(false)->toArray();
- $this->assertEquals(21, $entity->lft);
- $this->assertEquals(22, $entity->rght);
- $this->assertNull($entity->parent_id);
- $expected = [
- ' 1: 8 - 2:televisions',
- '_ 2: 3 - 3:tube',
- '_ 4: 5 - 4:lcd',
- '_ 6: 7 - 5:plasma',
- ' 9:18 - 6:portable',
- '_10:13 - 7:mp3',
- '__11:12 - 8:flash',
- '_14:15 - 9:cd',
- '_16:17 - 10:radios',
- '19:20 - 11:alien hardware',
- '21:22 - 1:electronics',
- ];
- $this->assertMpttValues($expected, $table);
- }
- /**
- * Tests that using associations having tree fields in the schema
- * does not generate SQL errors
- *
- * @return void
- */
- public function testFindPathWithAssociation()
- {
- $table = $this->table;
- $other = $this->getTableLocator()->get('FriendlyTrees', [
- 'table' => $table->getTable(),
- ]);
- $table->hasOne('FriendlyTrees', [
- 'foreignKey' => 'id',
- ]);
- $result = $table
- ->find('children', ['for' => 1])
- ->contain('FriendlyTrees')
- ->toArray();
- $this->assertCount(9, $result);
- }
- /**
- * Tests getting the depth level of a node in the tree.
- *
- * @return void
- */
- public function testGetLevel()
- {
- $entity = $this->table->get(8);
- $result = $this->table->getLevel($entity);
- $this->assertEquals(3, $result);
- $result = $this->table->getLevel($entity->id);
- $this->assertEquals(3, $result);
- $result = $this->table->getLevel(5);
- $this->assertEquals(2, $result);
- $result = $this->table->getLevel(99999);
- $this->assertFalse($result);
- }
- /**
- * Test setting level for new nodes
- *
- * @return void
- */
- public function testSetLevelNewNode()
- {
- $this->table->behaviors()->Tree->setConfig('level', 'depth');
- $entity = new Entity(['parent_id' => null, 'name' => 'Depth 0']);
- $this->table->save($entity);
- $entity = $this->table->get(12);
- $this->assertEquals(0, $entity->depth);
- $entity = new Entity(['parent_id' => 1, 'name' => 'Depth 1']);
- $this->table->save($entity);
- $entity = $this->table->get(13);
- $this->assertEquals(1, $entity->depth);
- $entity = new Entity(['parent_id' => 8, 'name' => 'Depth 4']);
- $this->table->save($entity);
- $entity = $this->table->get(14);
- $this->assertEquals(4, $entity->depth);
- }
- /**
- * Test setting level for existing nodes
- *
- * @return void
- */
- public function testSetLevelExistingNode()
- {
- $this->table->behaviors()->Tree->setConfig('level', 'depth');
- // Leaf node
- $entity = $this->table->get(4);
- $this->assertEquals(2, $entity->depth);
- $this->table->save($entity);
- $entity = $this->table->get(4);
- $this->assertEquals(2, $entity->depth);
- // Non leaf node so depth of descendants will also change
- $entity = $this->table->get(6);
- $this->assertEquals(1, $entity->depth);
- $entity->parent_id = null;
- $this->table->save($entity);
- $entity = $this->table->get(6);
- $this->assertEquals(0, $entity->depth);
- $entity = $this->table->get(7);
- $this->assertEquals(1, $entity->depth);
- $entity = $this->table->get(8);
- $this->assertEquals(2, $entity->depth);
- $entity->parent_id = 6;
- $this->table->save($entity);
- $entity = $this->table->get(8);
- $this->assertEquals(1, $entity->depth);
- }
- /**
- * Assert MPTT values
- *
- * Custom assert method to make identifying the differences between expected
- * and actual db state easier to identify.
- *
- * @param array $expected tree state to be expected
- * @param \Cake\ORM\Table $table Table instance
- * @param \Cake\ORM\Query $query Optional query object
- * @return void
- */
- public function assertMpttValues($expected, $table, $query = null)
- {
- $query = $query ?: $table->find();
- $primaryKey = $table->getPrimaryKey();
- if (is_array($primaryKey)) {
- $primaryKey = $primaryKey[0];
- }
- $displayField = $table->getDisplayField();
- $options = [
- 'valuePath' => function ($item, $key, $iterator) use ($primaryKey, $displayField) {
- return sprintf(
- '%s:%s - %s:%s',
- str_pad($item->lft, 2, ' ', STR_PAD_LEFT),
- str_pad($item->rght, 2, ' ', STR_PAD_LEFT),
- str_pad($item->$primaryKey, 2, ' ', STR_PAD_LEFT),
- $item->{$displayField}
- );
- },
- ];
- $result = array_values($query->find('treeList', $options)->toArray());
- if (count($result) === count($expected)) {
- $subExpected = array_diff($expected, $result);
- if ($subExpected) {
- $subResult = array_intersect_key($result, $subExpected);
- $this->assertSame($subExpected, $subResult, 'Differences in the tree were found (lft:rght id:display-name)');
- }
- }
- $this->assertSame($expected, $result, 'The tree is not the same (lft:rght id:display-name)');
- }
- }
|