Skip to content

Commit 0ded8b2

Browse files
committed
Assert same scope
1 parent 774a648 commit 0ded8b2

File tree

4 files changed

+78
-30
lines changed

4 files changed

+78
-30
lines changed

src/NodeTrait.php

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected function actionAppendOrPrepend(self $parent, $prepend = false)
189189
protected function setParent($value)
190190
{
191191
$this->setParentId($value ? $value->getKey() : null)
192-
->setRelation('parent', $value);
192+
->setRelation('parent', $value);
193193

194194
return $this;
195195
}
@@ -230,7 +230,7 @@ public function refreshNode()
230230
public function parent()
231231
{
232232
return $this->belongsTo(get_class($this), $this->getParentIdName())
233-
->setModel($this);
233+
->setModel($this);
234234
}
235235

236236
/**
@@ -241,7 +241,7 @@ public function parent()
241241
public function children()
242242
{
243243
return $this->hasMany(get_class($this), $this->getParentIdName())
244-
->setModel($this);
244+
->setModel($this);
245245
}
246246

247247
/**
@@ -262,25 +262,25 @@ public function descendants()
262262
public function siblings()
263263
{
264264
return $this->newScopedQuery()
265-
->where($this->getKeyName(), '<>', $this->getKey())
266-
->where($this->getParentIdName(), '=', $this->getParentId());
265+
->where($this->getKeyName(), '<>', $this->getKey())
266+
->where($this->getParentIdName(), '=', $this->getParentId());
267267
}
268268

269-
/**
269+
/**
270270
* Get the node siblings and the node itself.
271271
*
272272
* @return \Kalnoy\Nestedset\QueryBuilder
273273
*/
274274
public function siblingsAndSelf()
275275
{
276276
return $this->newScopedQuery()
277-
->where($this->getParentIdName(), '=', $this->getParentId());
277+
->where($this->getParentIdName(), '=', $this->getParentId());
278278
}
279279

280280
/**
281281
* Get query for the node siblings and the node itself.
282282
*
283-
* @param array $columns
283+
* @param array $columns
284284
*
285285
* @return \Illuminate\Database\Eloquent\Collection
286286
*/
@@ -297,7 +297,7 @@ public function getSiblingsAndSelf(array $columns = [ '*' ])
297297
public function nextSiblings()
298298
{
299299
return $this->nextNodes()
300-
->where($this->getParentIdName(), '=', $this->getParentId());
300+
->where($this->getParentIdName(), '=', $this->getParentId());
301301
}
302302

303303
/**
@@ -308,7 +308,7 @@ public function nextSiblings()
308308
public function prevSiblings()
309309
{
310310
return $this->prevNodes()
311-
->where($this->getParentIdName(), '=', $this->getParentId());
311+
->where($this->getParentIdName(), '=', $this->getParentId());
312312
}
313313

314314
/**
@@ -319,7 +319,7 @@ public function prevSiblings()
319319
public function nextNodes()
320320
{
321321
return $this->newScopedQuery()
322-
->where($this->getLftName(), '>', $this->getLft());
322+
->where($this->getLftName(), '>', $this->getLft());
323323
}
324324

325325
/**
@@ -330,7 +330,7 @@ public function nextNodes()
330330
public function prevNodes()
331331
{
332332
return $this->newScopedQuery()
333-
->where($this->getLftName(), '<', $this->getLft());
333+
->where($this->getLftName(), '<', $this->getLft());
334334
}
335335

336336
/**
@@ -426,7 +426,8 @@ public function prependToNode(self $parent)
426426
public function appendOrPrependTo(self $parent, $prepend = false)
427427
{
428428
$this->assertNodeExists($parent)
429-
->assertNotDescendant($parent);
429+
->assertNotDescendant($parent)
430+
->assertSameScope($parent);
430431

431432
$this->setParent($parent)->dirtyBounds();
432433

@@ -465,7 +466,9 @@ public function beforeNode(self $node)
465466
*/
466467
public function beforeOrAfterNode(self $node, $after = false)
467468
{
468-
$this->assertNodeExists($node)->assertNotDescendant($node);
469+
$this->assertNodeExists($node)
470+
->assertNotDescendant($node)
471+
->assertSameScope($node);
469472

470473
if ( ! $this->isSiblingOf($node)) {
471474
$this->setParent($node->getRelationValue('parent'));
@@ -529,9 +532,9 @@ public function rawNode($lft, $rgt, $parentId)
529532
public function up($amount = 1)
530533
{
531534
$sibling = $this->prevSiblings()
532-
->defaultOrder('desc')
533-
->skip($amount - 1)
534-
->first();
535+
->defaultOrder('desc')
536+
->skip($amount - 1)
537+
->first();
535538

536539
if ( ! $sibling) return false;
537540

@@ -548,9 +551,9 @@ public function up($amount = 1)
548551
public function down($amount = 1)
549552
{
550553
$sibling = $this->nextSiblings()
551-
->defaultOrder()
552-
->skip($amount - 1)
553-
->first();
554+
->defaultOrder()
555+
->skip($amount - 1)
556+
->first();
554557

555558
if ( ! $sibling) return false;
556559

@@ -587,7 +590,7 @@ protected function insertAt($position)
587590
protected function moveNode($position)
588591
{
589592
$updated = $this->newNestedSetQuery()
590-
->moveNode($this->getKey(), $position) > 0;
593+
->moveNode($this->getKey(), $position) > 0;
591594

592595
if ($updated) $this->refreshNode();
593596

@@ -649,8 +652,8 @@ protected function deleteDescendants()
649652
protected function restoreDescendants($deletedAt)
650653
{
651654
$this->descendants()
652-
->where($this->getDeletedAtColumn(), '>=', $deletedAt)
653-
->restore();
655+
->where($this->getDeletedAtColumn(), '>=', $deletedAt)
656+
->restore();
654657
}
655658

656659
/**
@@ -932,8 +935,8 @@ public function getPrevNode(array $columns = [ '*' ])
932935
public function getAncestors(array $columns = [ '*' ])
933936
{
934937
return $this->newScopedQuery()
935-
->defaultOrder()
936-
->ancestorsOf($this, $columns);
938+
->defaultOrder()
939+
->ancestorsOf($this, $columns);
937940
}
938941

939942
/**
@@ -1006,7 +1009,7 @@ public function getPrevSibling(array $columns = [ '*' ])
10061009
public function isDescendantOf(self $other)
10071010
{
10081011
return $this->getLft() > $other->getLft() &&
1009-
$this->getLft() < $other->getRgt();
1012+
$this->getLft() < $other->getRgt();
10101013
}
10111014

10121015
/**
@@ -1019,7 +1022,7 @@ public function isDescendantOf(self $other)
10191022
public function isSelfOrDescendantOf(self $other)
10201023
{
10211024
return $this->getLft() >= $other->getLft() &&
1022-
$this->getLft() < $other->getRgt();
1025+
$this->getLft() < $other->getRgt();
10231026
}
10241027

10251028
/**
@@ -1186,4 +1189,19 @@ protected function assertNodeExists(self $node)
11861189
return $this;
11871190
}
11881191

1192+
/**
1193+
* @param self $node
1194+
*/
1195+
protected function assertSameScope(self $node)
1196+
{
1197+
if ( ! $scoped = $this->getScopeAttributes()) {
1198+
return;
1199+
}
1200+
1201+
foreach ($scoped as $attr) {
1202+
if ($this->getAttribute($attr) != $node->getAttribute($attr)) {
1203+
throw new LogicException('Nodes must be in the same scope');
1204+
}
1205+
}
1206+
}
11891207
}

tests/NodeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public static function setUpBeforeClass()
1111

1212
$schema->dropIfExists('categories');
1313

14+
Capsule::disableQueryLog();
15+
1416
$schema->create('categories', function (\Illuminate\Database\Schema\Blueprint $table) {
1517
$table->increments('id');
1618
$table->string('name');
@@ -27,6 +29,8 @@ public function setUp()
2729

2830
Capsule::table('categories')->insert($data);
2931

32+
Capsule::flushQueryLog();
33+
3034
Category::resetActionsPerformed();
3135

3236
date_default_timezone_set('America/Denver');

tests/ScopedNodeTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function testSaveAsRoot()
130130
$node->saveAsRoot();
131131

132132
$this->assertEquals(5, $node->getLft());
133+
$this->assertEquals(null, $node->parent_id);
133134

134135
$this->assertOtherScopeNotAffected();
135136
}
@@ -138,11 +139,14 @@ public function testInsertion()
138139
{
139140
$node = MenuItem::create([ 'menu_id' => 1, 'parent_id' => 5 ]);
140141

142+
$this->assertEquals(5, $node->parent_id);
143+
$this->assertEquals(5, $node->getLft());
144+
141145
$this->assertOtherScopeNotAffected();
142146
}
143147

144-
/*
145-
* @expectedException
148+
/**
149+
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
146150
*/
147151
public function testInsertionToParentFromOtherScope()
148152
{
@@ -180,4 +184,26 @@ public function testRebuildsTree()
180184
$data = [];
181185
MenuItem::scoped([ 'menu_id' => 2 ])->rebuildTree($data);
182186
}
187+
188+
/**
189+
* @expectedException LogicException
190+
*/
191+
public function testAppendingToAnotherScopeFails()
192+
{
193+
$a = MenuItem::find(1);
194+
$b = MenuItem::find(3);
195+
196+
$a->appendToNode($b)->save();
197+
}
198+
199+
/**
200+
* @expectedException LogicException
201+
*/
202+
public function testInsertingBeforeAnotherScopeFails()
203+
{
204+
$a = MenuItem::find(1);
205+
$b = MenuItem::find(3);
206+
207+
$a->insertAfterNode($b);
208+
}
183209
}

tests/models/MenuItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class MenuItem extends \Illuminate\Database\Eloquent\Model
77

88
public $timestamps = false;
99

10-
protected $fillable = ['menu_id'];
10+
protected $fillable = ['menu_id','parent_id'];
1111

1212
public static function resetActionsPerformed()
1313
{

0 commit comments

Comments
 (0)