|
3 | 3 | namespace Tests\Data; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\Attributes\Test; |
| 6 | +use Statamic\Contracts\Search\Searchable; |
6 | 7 | use Statamic\Data\DataCollection; |
| 8 | +use Statamic\Search\Result; |
7 | 9 | use Tests\TestCase; |
8 | 10 |
|
9 | 11 | class DataCollectionTest extends TestCase |
@@ -31,4 +33,69 @@ public function it_sorts_by_first_item_in_arrays() |
31 | 33 |
|
32 | 34 | $this->assertEquals([1, 3, 2], $collection->multisort('foos')->pluck('id')->all()); |
33 | 35 | } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function sorting_by_unsafe_method_does_not_invoke_it() |
| 39 | + { |
| 40 | + $a = new DataCollectionTestObject('alfa'); |
| 41 | + $b = new DataCollectionTestObject('bravo'); |
| 42 | + |
| 43 | + $collection = new DataCollection([$a, $b]); |
| 44 | + |
| 45 | + $collection->multisort('delete'); |
| 46 | + |
| 47 | + $this->assertFalse($a->deleted); |
| 48 | + $this->assertFalse($b->deleted); |
| 49 | + } |
| 50 | + |
| 51 | + #[Test] |
| 52 | + public function sorting_search_results_by_unsafe_method_does_not_invoke_it() |
| 53 | + { |
| 54 | + $a = new DataCollectionTestObject('alfa'); |
| 55 | + $b = new DataCollectionTestObject('bravo'); |
| 56 | + |
| 57 | + $collection = new DataCollection([ |
| 58 | + new Result($a, 'test'), |
| 59 | + new Result($b, 'test'), |
| 60 | + ]); |
| 61 | + |
| 62 | + $collection->multisort('delete'); |
| 63 | + |
| 64 | + $this->assertFalse($a->deleted); |
| 65 | + $this->assertFalse($b->deleted); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class DataCollectionTestObject implements Searchable |
| 70 | +{ |
| 71 | + public bool $deleted = false; |
| 72 | + |
| 73 | + public function __construct(public string $title) |
| 74 | + { |
| 75 | + } |
| 76 | + |
| 77 | + public function delete() |
| 78 | + { |
| 79 | + $this->deleted = true; |
| 80 | + } |
| 81 | + |
| 82 | + public function get($key) |
| 83 | + { |
| 84 | + return $this->{$key} ?? null; |
| 85 | + } |
| 86 | + |
| 87 | + public function getSearchValue(string $field) |
| 88 | + { |
| 89 | + return $this->{$field} ?? null; |
| 90 | + } |
| 91 | + |
| 92 | + public function getSearchReference(): string |
| 93 | + { |
| 94 | + return 'test::'.$this->title; |
| 95 | + } |
| 96 | + |
| 97 | + public function toSearchResult(): Result |
| 98 | + { |
| 99 | + return new Result($this, 'test'); |
| 100 | + } |
34 | 101 | } |
0 commit comments