Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sortable to Attachment model #2714

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Attachment/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
use Orchid\Attachment\MimeTypes;
use Orchid\Filters\Filterable;
use Orchid\Filters\Types\Like;
use Orchid\Platform\Concerns\Sortable;
use Orchid\Platform\Dashboard;
use Orchid\Platform\Models\User;
use Orchid\Screen\AsSource;

/**
* Class Attachment.
*/
class Attachment extends Model
{
use Filterable, HasFactory;
use AsSource, Filterable, HasFactory, Sortable;

/**
* @var array
Expand Down Expand Up @@ -84,6 +86,16 @@ class Attachment extends Model
'group',
];

/**
* Get the column name for sorting.
*
* @return string
*/
public function getSortColumnName(): string
{
return 'sort';
}

public function user(): BelongsTo
{
return $this->belongsTo(Dashboard::model(User::class));
Expand Down
54 changes: 54 additions & 0 deletions tests/Feature/Platform/SortableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Orchid\Tests\Feature\Platform;

use Illuminate\Http\UploadedFile;
use Orchid\Attachment\Models\Attachment;
use Orchid\Tests\TestFeatureCase;

class SortableTest extends TestFeatureCase
{
public function testAttachmentHttpSort(): void
{
$response = $this
->actingAs($this->createAdminUser())
->post(route('platform.systems.files.upload'), [
'files' => [
UploadedFile::fake()->image('first.jpg'),
UploadedFile::fake()->image('second.png'),
],
]);

$attachments = $response->decodeResponseJson()->json();

$originalFiles = [];
$files = [];

foreach ($attachments as $attachment) {
$files[] = $originalFiles[] = $attachment['id'];
}

arsort($originalFiles);
$sort = array_flip($files);
$sortItems = collect($sort)
->map(fn ($sort, $id) => ['id' => $id, 'sortOrder' => $sort])
->all();

$response = $this
->actingAs($this->createAdminUser())
->post(route('platform.systems.sorting'), [
'items' => $sortItems,
'model' => Attachment::class,
]);

$response->isOk();

$attachments = Attachment::whereIn('id', $originalFiles)
->pluck('sort', 'id')
->toArray();

$this->assertEquals($sort, $attachments);
}
}
67 changes: 67 additions & 0 deletions tests/Unit/SortableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Orchid\Tests\Unit;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Orchid\Attachment\File;
use Orchid\Attachment\Models\Attachment;
use Orchid\Platform\Concerns\Sortable;
use Orchid\Tests\TestUnitCase;

class SortableTest extends TestUnitCase
{
public function testNewModel(): void
{
$model = new class extends Model
{
use Sortable;
/**
* @var array
*/
protected $fillable = [
'name',
'order',

];

/**
* @var \string[][][]
*/
protected $attributes = [
'order' => 0,
];
};

$model->id = 1;

$this->assertEquals('order', $model->getSortColumnName());
$this->assertEquals(0, $model->getSortColumnValue());

$model->setSortColumn('3');
$this->assertEquals(3, $model->getSortColumnValue());

}

public function testForAttachment()
{
$first_file = UploadedFile::fake()->create('first-file');
$first_upload = (new File($first_file, 'public'))->load();

$two_file = UploadedFile::fake()->create('two-file');
$two_upload = (new File($two_file, 'public'))->load();

$this->assertEquals('sort', $first_upload->getSortColumnName());
$this->assertEquals(0, $first_upload->getSortColumnValue());
$this->assertEquals(0, $first_upload->sort);

$first_upload->setSortColumn('3')->save();
$this->assertEquals(3, $first_upload->getSortColumnValue());
$this->assertEquals(3, $first_upload->sort);

$this->assertEquals($two_upload->id, Attachment::sorted()->get()->first()->id);

$two_upload->setSortColumn('9')->save();
$this->assertEquals($first_upload->id, Attachment::sorted()->get()->first()->id);
}
}