|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backend\Tests\Widgets; |
| 4 | + |
| 5 | +use System\Tests\Bootstrap\PluginTestCase; |
| 6 | +use Winter\Storm\Exception\ApplicationException; |
| 7 | +use Backend\Tests\Fixtures\Models\UserFixture; |
| 8 | +use Backend\Widgets\Lists; |
| 9 | +use Illuminate\Http\Request as HttpRequest; |
| 10 | +use Illuminate\Support\Facades\Schema; |
| 11 | +use Winter\Test\Models\Attribute; |
| 12 | + |
| 13 | +class ListsSortableTest extends PluginTestCase |
| 14 | +{ |
| 15 | + public function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + if (!Schema::hasTable('winter_test_attributes')) { |
| 20 | + Schema::create('winter_test_attributes', function ($table) { |
| 21 | + $table->increments('id'); |
| 22 | + $table->string('type')->nullable(); |
| 23 | + $table->string('name')->nullable(); |
| 24 | + $table->string('label')->nullable(); |
| 25 | + $table->string('code')->nullable(); |
| 26 | + $table->boolean('is_default')->default(false); |
| 27 | + $table->integer('sort_order')->nullable(); |
| 28 | + $table->timestamps(); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + $this->actingAs((new UserFixture)->asSuperUser()); |
| 33 | + } |
| 34 | + |
| 35 | + protected function makeList(array $overrides = []): Lists |
| 36 | + { |
| 37 | + return new Lists(null, array_merge([ |
| 38 | + 'model' => new Attribute, |
| 39 | + 'alias' => 'testlist', |
| 40 | + 'arrayName' => 'array', |
| 41 | + 'sortable' => true, |
| 42 | + 'sortOrderColumn' => 'sort_order', |
| 43 | + 'columns' => [ |
| 44 | + 'name' => ['type' => 'text', 'label' => 'Name'], |
| 45 | + 'label' => ['type' => 'text', 'label' => 'Label'], |
| 46 | + ], |
| 47 | + ], $overrides)); |
| 48 | + } |
| 49 | + |
| 50 | + protected function seedAttributes(): array |
| 51 | + { |
| 52 | + $records = []; |
| 53 | + foreach (['Alpha', 'Bravo', 'Charlie'] as $i => $name) { |
| 54 | + $records[] = Attribute::create([ |
| 55 | + 'type' => 'general.type', |
| 56 | + 'name' => strtolower($name), |
| 57 | + 'label' => $name, |
| 58 | + 'sort_order' => $i + 1, |
| 59 | + ]); |
| 60 | + } |
| 61 | + return $records; |
| 62 | + } |
| 63 | + |
| 64 | + protected function postRequest(array $data): void |
| 65 | + { |
| 66 | + $request = HttpRequest::create('/', 'POST', $data); |
| 67 | + $this->app->instance('request', $request); |
| 68 | + \Request::swap($request); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSortableDisablesPaginationAndColumnSorting() |
| 72 | + { |
| 73 | + $list = $this->makeList(); |
| 74 | + $list->render(); |
| 75 | + |
| 76 | + $this->assertFalse($list->showPagination); |
| 77 | + // With every column forced non-sortable, no sort column is resolved. |
| 78 | + $this->assertFalse($list->getSortColumn()); |
| 79 | + |
| 80 | + foreach ($list->getColumns() as $column) { |
| 81 | + $this->assertFalse($column->sortable, "Column {$column->columnName} should not be sortable"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public function testSortableAddsDragHandleToColumnTotal() |
| 86 | + { |
| 87 | + $sortable = $this->makeList(); |
| 88 | + $plain = $this->makeList(['sortable' => false]); |
| 89 | + |
| 90 | + $method = new \ReflectionMethod(Lists::class, 'getTotalColumns'); |
| 91 | + $method->setAccessible(true); |
| 92 | + |
| 93 | + $this->assertSame( |
| 94 | + $method->invoke($plain) + 1, |
| 95 | + $method->invoke($sortable), |
| 96 | + 'Sortable list should reserve one extra column for the drag handle' |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + public function testGetRecordSortOrderReadsDirectColumn() |
| 101 | + { |
| 102 | + $list = $this->makeList(); |
| 103 | + $record = new Attribute(['sort_order' => 7]); |
| 104 | + |
| 105 | + $this->assertSame(7, (int) $list->getRecordSortOrder($record)); |
| 106 | + } |
| 107 | + |
| 108 | + public function testGetRecordSortOrderReadsPivotPath() |
| 109 | + { |
| 110 | + $list = $this->makeList(['sortOrderColumn' => 'pivot[sort_order]']); |
| 111 | + |
| 112 | + $record = new \stdClass(); |
| 113 | + $record->pivot = new \stdClass(); |
| 114 | + $record->pivot->sort_order = 4; |
| 115 | + |
| 116 | + $this->assertSame(4, (int) $list->getRecordSortOrder($record)); |
| 117 | + } |
| 118 | + |
| 119 | + public function testGetRecordSortOrderReturnsNullWhenMissing() |
| 120 | + { |
| 121 | + $list = $this->makeList(['sortOrderColumn' => 'pivot[sort_order]']); |
| 122 | + $record = new Attribute(['sort_order' => 7]); // no pivot relation |
| 123 | + |
| 124 | + $this->assertNull($list->getRecordSortOrder($record)); |
| 125 | + } |
| 126 | + |
| 127 | + public function testOnReorderFiresEventWithIdsAndOrders() |
| 128 | + { |
| 129 | + $records = $this->seedAttributes(); |
| 130 | + $ids = [$records[2]->id, $records[0]->id, $records[1]->id]; |
| 131 | + |
| 132 | + $list = $this->makeList(); |
| 133 | + |
| 134 | + $captured = null; |
| 135 | + $list->bindEvent('list.reorder', function ($eventIds, $eventOrders) use (&$captured) { |
| 136 | + $captured = [$eventIds, $eventOrders]; |
| 137 | + }); |
| 138 | + |
| 139 | + $this->postRequest(['record_ids' => $ids, 'sort_orders' => [1, 2, 3]]); |
| 140 | + $list->onReorder(); |
| 141 | + |
| 142 | + $this->assertNotNull($captured, 'list.reorder event should have fired'); |
| 143 | + $this->assertSame(array_map('strval', $ids), array_map('strval', $captured[0])); |
| 144 | + $this->assertSame([1, 2, 3], $captured[1]); |
| 145 | + } |
| 146 | + |
| 147 | + public function testOnReorderRejectsRecordsOutsideQueryScope() |
| 148 | + { |
| 149 | + $records = $this->seedAttributes(); |
| 150 | + |
| 151 | + $list = $this->makeList(); |
| 152 | + |
| 153 | + // 99999 is not a seeded record id. |
| 154 | + $this->postRequest(['record_ids' => [$records[0]->id, 99999], 'sort_orders' => [1, 2]]); |
| 155 | + |
| 156 | + $this->expectException(ApplicationException::class); |
| 157 | + $list->onReorder(); |
| 158 | + } |
| 159 | + |
| 160 | + public function testOnReorderThrowsWhenNotSortable() |
| 161 | + { |
| 162 | + $list = $this->makeList(['sortable' => false]); |
| 163 | + $this->postRequest(['record_ids' => [1], 'sort_orders' => [1]]); |
| 164 | + |
| 165 | + $this->expectException(ApplicationException::class); |
| 166 | + $list->onReorder(); |
| 167 | + } |
| 168 | +} |
0 commit comments