|
| 1 | +<?php |
| 2 | + |
| 3 | +use Code16\Sharp\Filters\GlobalRequiredFilter; |
| 4 | +use Code16\Sharp\Tests\Fixtures\Entities\DashboardEntity; |
| 5 | +use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity; |
| 6 | +use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + sharp()->config()->declareEntity(PersonEntity::class); |
| 10 | + login(); |
| 11 | +}); |
| 12 | + |
| 13 | +it('redirects to route with default filterKey when missing', function () { |
| 14 | + sharp()->config() |
| 15 | + ->declareEntity(DashboardEntity::class) |
| 16 | + ->declareEntity(SinglePersonEntity::class); |
| 17 | + |
| 18 | + $this->get('/sharp/s-list/person') |
| 19 | + ->assertRedirect('/sharp/root/s-list/person'); |
| 20 | + |
| 21 | + $this->get('/sharp/s-list/person/s-show/person/1') |
| 22 | + ->assertRedirect('/sharp/root/s-list/person/s-show/person/1'); |
| 23 | + |
| 24 | + $this->get('/sharp/s-list/person/s-show/person/1/s-form/person/1') |
| 25 | + ->assertRedirect('/sharp/root/s-list/person/s-show/person/1/s-form/person/1'); |
| 26 | + |
| 27 | + $this->get('/sharp/s-list/person/s-show/person/1/s-show/person/2/s-form/person/2') |
| 28 | + ->assertRedirect('/sharp/root/s-list/person/s-show/person/1/s-show/person/2/s-form/person/2'); |
| 29 | + |
| 30 | + $this->get('/sharp/s-dashboard/dashboard') |
| 31 | + ->assertRedirect('/sharp/root/s-dashboard/dashboard'); |
| 32 | + |
| 33 | + $this->get('/sharp/s-show/single-person') |
| 34 | + ->assertRedirect('/sharp/root/s-show/single-person'); |
| 35 | +}); |
| 36 | + |
| 37 | +it('redirects to route with correct filterKey when missing and global filters are defined', function () { |
| 38 | + fakeGlobalFilter(); |
| 39 | + |
| 40 | + $this->get('/sharp/s-list/person') |
| 41 | + ->assertRedirect('/sharp/two/s-list/person'); |
| 42 | + |
| 43 | + $this->post(route('code16.sharp.filters.update', 'test'), ['value' => 'three']); |
| 44 | + |
| 45 | + $this->get('/sharp/s-list/person/s-show/person/1') |
| 46 | + ->assertRedirect('/sharp/three/s-list/person/s-show/person/1'); |
| 47 | +}); |
| 48 | + |
| 49 | +it('sets the current filterKey according to the URL', function () { |
| 50 | + fakeGlobalFilter(); |
| 51 | + |
| 52 | + $this->post(route('code16.sharp.filters.update', 'test'), ['value' => 'three']); |
| 53 | + $this->get('/sharp/one/s-list/person/s-show/person/1') |
| 54 | + ->assertOk(); |
| 55 | + |
| 56 | + expect(sharp()->context()->globalFilterValue('test'))->toEqual('one'); |
| 57 | +}); |
| 58 | + |
| 59 | +it('wont set an invalid value of the current filterKey according to the URL', function () { |
| 60 | + fakeGlobalFilter(); |
| 61 | + |
| 62 | + $this->get('/sharp/five/s-list/person/s-show/person/1') |
| 63 | + ->assertNotFound(); |
| 64 | + |
| 65 | + expect(sharp()->context()->globalFilterValue('test'))->toEqual('two'); |
| 66 | +}); |
| 67 | + |
| 68 | +// TODO test multiple filters |
| 69 | + |
| 70 | +function fakeGlobalFilter(): void |
| 71 | +{ |
| 72 | + sharp()->config()->addGlobalFilter( |
| 73 | + new class() extends GlobalRequiredFilter |
| 74 | + { |
| 75 | + public function buildFilterConfig(): void |
| 76 | + { |
| 77 | + $this->configureKey('test'); |
| 78 | + } |
| 79 | + |
| 80 | + public function values(): array |
| 81 | + { |
| 82 | + return [ |
| 83 | + 'one' => 'Company One', |
| 84 | + 'two' => 'Company Two', |
| 85 | + 'three' => 'Company Three', |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + public function defaultValue(): mixed |
| 90 | + { |
| 91 | + return 'two'; |
| 92 | + } |
| 93 | + } |
| 94 | + ); |
| 95 | +} |
0 commit comments