This commit is contained in:
134
tests/Feature/AdminProductImageUploadTest.php
Normal file
134
tests/Feature/AdminProductImageUploadTest.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminProductImageUploadTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_admin_can_create_product_with_main_and_gallery_images(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$admin = User::factory()->create([
|
||||
'is_admin' => true,
|
||||
]);
|
||||
|
||||
$category = Category::query()->create([
|
||||
'name' => 'Видеокарты',
|
||||
'slug' => 'video-cards',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$response = $this
|
||||
->actingAs($admin)
|
||||
->post(route('admin.products.store'), [
|
||||
'category_id' => $category->id,
|
||||
'name' => 'RTX 5090',
|
||||
'slug' => '',
|
||||
'sku' => 'RTX-5090',
|
||||
'price' => '199999.99',
|
||||
'old_price' => '209999.99',
|
||||
'stock' => 3,
|
||||
'short_description' => 'Флагманская видеокарта',
|
||||
'description' => 'Описание товара',
|
||||
'image' => UploadedFile::fake()->create('main.jpg', 120, 'image/jpeg'),
|
||||
'gallery_images' => [
|
||||
UploadedFile::fake()->create('gallery-1.jpg', 120, 'image/jpeg'),
|
||||
UploadedFile::fake()->create('gallery-2.webp', 120, 'image/webp'),
|
||||
],
|
||||
'is_active' => '1',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertRedirect(route('admin.products.index'))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$product = Product::query()->where('name', 'RTX 5090')->firstOrFail();
|
||||
|
||||
$this->assertSame('video-cards', $product->category->slug);
|
||||
$this->assertNotNull($product->image_path);
|
||||
$this->assertIsArray($product->gallery_paths);
|
||||
$this->assertCount(2, $product->gallery_paths);
|
||||
$this->assertStringStartsWith('products/', $product->image_path);
|
||||
|
||||
Storage::disk('public')->assertExists($product->image_path);
|
||||
|
||||
foreach ($product->gallery_paths as $path) {
|
||||
$this->assertStringStartsWith('products/', $path);
|
||||
Storage::disk('public')->assertExists($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_admin_can_remove_product_images_from_edit_form(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$admin = User::factory()->create([
|
||||
'is_admin' => true,
|
||||
]);
|
||||
|
||||
$category = Category::query()->create([
|
||||
'name' => 'Ноутбуки',
|
||||
'slug' => 'laptops',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
Storage::disk('public')->put('products/main.jpg', 'main-image');
|
||||
Storage::disk('public')->put('products/gallery-1.jpg', 'gallery-one');
|
||||
Storage::disk('public')->put('products/gallery-2.webp', 'gallery-two');
|
||||
|
||||
$product = Product::query()->create([
|
||||
'category_id' => $category->id,
|
||||
'name' => 'Lenovo Legion',
|
||||
'slug' => 'lenovo-legion',
|
||||
'sku' => 'LEGION-01',
|
||||
'price' => '159999.99',
|
||||
'old_price' => '169999.99',
|
||||
'stock' => 5,
|
||||
'short_description' => 'Игровой ноутбук',
|
||||
'description' => 'Описание ноутбука',
|
||||
'image_path' => 'products/main.jpg',
|
||||
'gallery_paths' => ['products/gallery-1.jpg', 'products/gallery-2.webp'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$response = $this
|
||||
->actingAs($admin)
|
||||
->put(route('admin.products.update', $product), [
|
||||
'category_id' => $category->id,
|
||||
'name' => 'Lenovo Legion',
|
||||
'slug' => 'lenovo-legion',
|
||||
'sku' => 'LEGION-01',
|
||||
'price' => '159999.99',
|
||||
'old_price' => '169999.99',
|
||||
'stock' => 5,
|
||||
'short_description' => 'Игровой ноутбук',
|
||||
'description' => 'Описание ноутбука',
|
||||
'remove_image' => '1',
|
||||
'remove_gallery_paths' => ['products/gallery-1.jpg'],
|
||||
'is_active' => '1',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertRedirect(route('admin.products.index'))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$product->refresh();
|
||||
|
||||
$this->assertSame('products/gallery-2.webp', $product->image_path);
|
||||
$this->assertSame([], $product->gallery_paths ?? []);
|
||||
|
||||
Storage::disk('public')->assertMissing('products/main.jpg');
|
||||
Storage::disk('public')->assertMissing('products/gallery-1.jpg');
|
||||
Storage::disk('public')->assertExists('products/gallery-2.webp');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user