Files
-S.I.S-QuaySo_v2/tests/Browser/AdminParameterTest.php
T

133 lines
4.2 KiB
PHP

<?php
namespace Tests\Browser;
use App\Models\Parameter;
use App\Models\Role;
use App\Models\User;
use App\Models\UserRole;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class AdminParameterTest extends DuskTestCase
{
private User $adminUser;
protected function setUp(): void
{
parent::setUp();
$this->adminUser = User::create([
'name' => 'DUSK PARAM ADMIN',
'email' => 'dusk_param_admin@test.local',
'phone' => '09' . rand(10000000, 99999999),
'password' => bcrypt('dusk_admin_pwd_2026'),
'status' => 1,
'address' => 'Test',
'known_sis'=> 0,
'job' => 'Admin',
'spined' => 0,
]);
$roles = Role::where('role_status', 1)->get();
foreach ($roles as $role) {
UserRole::create(['user_id' => $this->adminUser->id, 'role_id' => $role->id]);
}
}
protected function tearDown(): void
{
UserRole::where('user_id', $this->adminUser->id)->delete();
$this->adminUser->delete();
parent::tearDown();
}
/**
* Trang cấu hình hệ thống hiển thị.
*/
public function test_parameter_page_loads(): void
{
$this->browse(function (Browser $browser) {
$browser->loginAs($this->adminUser)
->visit('/admin/parameter')
->assertPresent('form');
});
}
/**
* Cấu hình game_rule hiển thị trong textarea.
*/
public function test_parameter_page_shows_game_rule_field(): void
{
$this->browse(function (Browser $browser) {
$browser->loginAs($this->adminUser)
->visit('/admin/parameter')
->assertPresent('[name="game_rule"], textarea');
});
}
/**
* Cập nhật thông số game_rule thành công và kiểm tra DB.
*/
public function test_update_game_rule_successfully(): void
{
// Backup giá trị hiện tại
$original = Parameter::where('parameter_key', 'game_rule')->first();
$originalValue = $original->parameter_value ?? '';
$newValue = 'Dusk Test Rule Content ' . rand(1000, 9999);
$this->browse(function (Browser $browser) use ($newValue) {
$browser->loginAs($this->adminUser)
->visit('/admin/parameter')
->pause(300);
// Gán trực tiếp qua JS vì có thể là CKEditor
$browser->script("
var el = document.querySelector('[name=\"game_rule\"]');
if (el) {
el.value = '{$newValue}';
}
if (typeof CKEDITOR !== 'undefined' && CKEDITOR.instances['game_rule']) {
CKEDITOR.instances['game_rule'].setData('{$newValue}');
}
");
$browser->press('Cập nhật')
->waitFor('.alert-success', 10);
});
// Kiểm tra DB: game_rule đã được cập nhật và chứa nội dung mới
$updated = Parameter::where('parameter_key', 'game_rule')->first();
$this->assertNotNull($updated, 'Tham số game_rule phải tồn tại trong DB');
$this->assertStringContainsString($newValue, $updated->parameter_value,
'Giá trị DB phải chứa nội dung vừa lưu');
// Restore giá trị gốc
$original->parameter_value = $originalValue;
$original->save();
}
/**
* Trang cấu hình có nút upload logo và banner.
*/
public function test_parameter_page_has_image_upload_fields(): void
{
$this->browse(function (Browser $browser) {
$browser->loginAs($this->adminUser)
->visit('/admin/parameter')
->assertPresent('input[type="file"][name="logo_image"], input[name="logo_image"]');
});
}
/**
* Truy cập /admin/parameter khi chưa đăng nhập bị redirect.
*/
public function test_parameter_page_requires_auth(): void
{
$this->browse(function (Browser $browser) {
$browser->visit('/admin/logout')
->visit('/admin/parameter')
->assertPathIs('/admin');
});
}
}