-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from wri/feat/TM-804-bulk-delete
[TM-804] Media bulk deletion
- Loading branch information
Showing
8 changed files
with
157 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
database/migrations/2024_04_19_190707_add_created_by_to_media_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use App\Models\V2\User; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('media', function (Blueprint $table) { | ||
$table->foreignIdFor(User::class, 'created_by')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('media', function (Blueprint $table) { | ||
$table->dropColumn(['created_by']); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Tests\V2; | ||
|
||
use App\Models\User; | ||
use App\Models\V2\Sites\Site; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Illuminate\Http\UploadedFile; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Tests\TestCase; | ||
|
||
final class MediaControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use WithFaker; | ||
|
||
public function test_bulk_delete(): void | ||
{ | ||
$service = User::factory()->serviceAccount()->create(); | ||
$admin = User::factory()->admin()->create(); | ||
Artisan::call('v2migration:roles'); | ||
|
||
$site = Site::factory()->ppc()->create(); | ||
$photo1 = $site->addMedia(UploadedFile::fake()->image('photo1'))->toMediaCollection('photos'); | ||
$photo1->update(['created_by' => $service->id]); | ||
$photo2 = $site->addMedia(UploadedFile::fake()->image('photo2'))->toMediaCollection('photos'); | ||
$photo2->update(['created_by' => $admin->id]); | ||
$photo3 = $site->addMedia(UploadedFile::fake()->image('photo3'))->toMediaCollection('photos'); | ||
$photo3->update(['created_by' => $service->id]); | ||
|
||
// No UUIDS is a 404 | ||
$this->actingAs($service) | ||
->delete('/api/v2/media') | ||
->assertNotFound(); | ||
|
||
// Can't delete photo created by admin | ||
$this->actingAs($service) | ||
->delete($this->buildBulkDeleteUrl([$photo1->uuid, $photo2->uuid])) | ||
->assertForbidden(); | ||
$this->assertEquals(3, $site->refresh()->getMedia('photos')->count()); | ||
|
||
// Only service accounts can use bulk delete | ||
$this->actingAs($admin) | ||
->delete($this->buildBulkDeleteUrl([$photo2->uuid])) | ||
->assertForbidden(); | ||
$this->assertEquals(3, $site->refresh()->getMedia('photos')->count()); | ||
|
||
// Success case | ||
$this->actingAs($service) | ||
->delete($this->buildBulkDeleteUrl([$photo1->uuid, $photo3->uuid])) | ||
->assertSuccessful(); | ||
$this->assertEquals(1, $site->refresh()->getMedia('photos')->count()); | ||
} | ||
|
||
private function buildBulkDeleteUrl($uuids): string | ||
{ | ||
return '/api/v2/media?uuids[]=' . implode('&uuids[]=', $uuids); | ||
} | ||
} |