Skip to content

Commit f27409a

Browse files
MateuszKolankowskitomaszszopinski
authored andcommitted
Fix: Ensure last ancestor is valid before comparing paths in TrashItemData
1 parent 6fd7513 commit f27409a

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6396,12 +6396,6 @@ parameters:
63966396
count: 1
63976397
path: src/lib/Form/Data/Trash/TrashItemRestoreData.php
63986398

6399-
-
6400-
message: '#^Cannot access property \$path on Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location\|false\.$#'
6401-
identifier: property.nonObject
6402-
count: 1
6403-
path: src/lib/Form/Data/TrashItemData.php
6404-
64056399
-
64066400
message: '#^Method Ibexa\\AdminUi\\Form\\Data\\TrashItemData\:\:setAncestors\(\) has no return type specified\.$#'
64076401
identifier: missingType.return

src/lib/Form/Data/TrashItemData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function isParentInTrash(): bool
9595
{
9696
$lastAncestor = end($this->ancestors);
9797

98-
return $this->location->path !== array_merge($lastAncestor->path, [(string)$this->location->id]);
98+
return $lastAncestor !== false && $this->location->path !== array_merge($lastAncestor->path, [(string)$this->location->id]);
9999
}
100100

101101
public function getCreator(): ?User
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\AdminUi\Form\Data;
10+
11+
use Ibexa\AdminUi\Form\Data\TrashItemData;
12+
use Ibexa\Core\Repository\Values\Content\Location;
13+
use Ibexa\Core\Repository\Values\Content\TrashItem;
14+
use PHPUnit\Framework\TestCase;
15+
16+
final class TrashItemDataTest extends TestCase
17+
{
18+
public function testIsParentInTrashReturnsFalseWhenNoAncestors(): void
19+
{
20+
$trashItem = new TrashItem(['path' => ['1', '2', '3'], 'id' => 3]);
21+
$data = new TrashItemData($trashItem, null, []);
22+
23+
self::assertFalse($data->isParentInTrash());
24+
}
25+
26+
public function testIsParentInTrashReturnsFalseWhenPathsMatch(): void
27+
{
28+
$trashItem = new TrashItem(['path' => ['1', '2', '3'], 'id' => 3]);
29+
30+
$ancestor = new Location(['path' => ['1', '2']]);
31+
$data = new TrashItemData($trashItem, null, [$ancestor]);
32+
33+
self::assertFalse($data->isParentInTrash());
34+
}
35+
36+
public function testIsParentInTrashReturnsTrueWhenPathsDoNotMatch(): void
37+
{
38+
$trashItem = new TrashItem(['path' => ['1', '2', '3'], 'id' => 3]);
39+
$ancestor = new Location(['path' => ['1']]);
40+
41+
$data = new TrashItemData($trashItem, null, [$ancestor]);
42+
43+
self::assertTrue($data->isParentInTrash());
44+
}
45+
46+
public function testIsParentInTrashWithMultipleAncestors(): void
47+
{
48+
$trashItem = new TrashItem(['path' => ['1', '2', '3', '4'], 'id' => 4]);
49+
50+
$ancestor1 = new Location(['path' => ['1']]);
51+
$ancestor2 = new Location(['path' => ['1', '2', '3']]);
52+
53+
$data = new TrashItemData($trashItem, null, [$ancestor1, $ancestor2]);
54+
55+
self::assertFalse($data->isParentInTrash());
56+
}
57+
}

0 commit comments

Comments
 (0)