Files
moonwell-web/tests/Feature/AdminShopFeatureTest.php
T

116 lines
5.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\GameAccountUser;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class AdminShopFeatureTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['database.connections.store' => ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '']]);
DB::purge('store');
Schema::connection('store')->create('store_config', function (Blueprint $table): void {
$table->unsignedInteger('id')->primary();
$table->unsignedInteger('enabled')->default(1);
});
DB::connection('store')->table('store_config')->insert(['id' => 1, 'enabled' => 1]);
Schema::connection('store')->create('store_categories', function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->nullable();
$table->text('icon')->nullable();
$table->integer('requiredRank')->nullable();
$table->unsignedInteger('flags')->default(0);
$table->unsignedInteger('enabled')->default(1);
});
Schema::connection('store')->create('store_services', function (Blueprint $table): void {
$table->increments('id');
foreach (['type', 'price', 'currency', 'hyperlinkId', 'creatureEntry', 'discountAmount', 'flags'] as $column) {
$table->integer($column)->nullable();
}
foreach (['name', 'tooltipName', 'tooltipType', 'tooltipText', 'icon'] as $column) {
$table->text($column)->nullable();
}
foreach (range(1, 8) as $slot) {
$table->unsignedInteger('reward_'.$slot)->nullable();
$table->unsignedInteger('rewardcount_'.$slot)->nullable();
}
$table->unsignedInteger('new')->default(0);
$table->unsignedInteger('enabled')->nullable();
});
Schema::connection('store')->create('store_category_service_link', function (Blueprint $table): void {
$table->unsignedInteger('category');
$table->unsignedInteger('service');
$table->primary(['category', 'service']);
});
}
public function test_admin_can_manage_game_store_category_and_product(): void
{
$admin = $this->admin();
$this->actingAs($admin)->post(route('admin.shop.categories.store'), [
'name' => 'Маунты', 'icon' => 'ability_mount', 'requiredRank' => 0, 'flags' => 0, 'enabled' => 1,
])->assertSessionHas('status', 'Категория создана.');
$categoryId = (int) DB::connection('store')->table('store_categories')->value('id');
$payload = [
'category_ids' => [$categoryId], 'type' => 3, 'name' => 'Спектральный тигр',
'tooltipName' => 'Маунт', 'tooltipType' => 'spell', 'tooltipText' => 'Редкий транспорт',
'icon' => 'ability_mount_spectraltiger', 'price' => 3000, 'hyperlinkId' => 42777,
'creatureEntry' => 24004, 'discountAmount' => 0, 'flags' => 0,
'reward_1' => 42777, 'rewardcount_1' => 1, 'new' => 1, 'enabled' => 1,
];
$this->actingAs($admin)->post(route('admin.shop.products.store'), $payload)
->assertSessionHas('status', 'Товар создан.');
$this->assertDatabaseHas('store_services', ['name' => 'Спектральный тигр', 'price' => 3000], 'store');
$this->assertDatabaseHas('store_category_service_link', ['category' => $categoryId], 'store');
$this->actingAs($admin)
->get(route('admin.shop.index'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('Admin/Shop', false)
->where('categories.0.name', 'Маунты')
->where('products.0.name', 'Спектральный тигр')
->where('products.0.category_ids.0', $categoryId)
->where('shopEnabled', true)
->etc()
);
}
public function test_admin_can_disable_and_enable_store(): void
{
$admin = $this->admin();
$this->actingAs($admin)
->patch(route('admin.shop.availability.update'), ['enabled' => false])
->assertSessionHas('status', 'Магазин и пополнение выключены.');
$this->assertSame(0, (int) DB::connection('store')->table('store_config')->where('id', 1)->value('enabled'));
$this->actingAs($admin)
->patch(route('admin.shop.availability.update'), ['enabled' => true])
->assertSessionHas('status', 'Магазин и пополнение включены.');
$this->assertSame(1, (int) DB::connection('store')->table('store_config')->where('id', 1)->value('enabled'));
}
public function test_regular_player_cannot_manage_store(): void
{
$this->actingAs($this->admin(0))->get(route('admin.shop.index'))->assertForbidden();
}
private function admin(int $gmLevel = 3): GameAccountUser
{
return new GameAccountUser(1, 'ADMIN', 'ADMIN@EXAMPLE.COM', 'ADMIN@EXAMPLE.COM', $gmLevel, false, null, null, str_repeat('AA', 32), str_repeat('BB', 32));
}
}