- implementasi testing - use HashableId di model - kasih return type - menyesuaikan factory - menyesuaikan modalname - menambahkan permission untuk delet
195 lines
6.3 KiB
PHP
195 lines
6.3 KiB
PHP
<?php
|
|
|
|
use App\Jobs\StorePushNotification;
|
|
use App\Livewire\Studio\Information\Broadcast;
|
|
use App\Models\Broadcast as BroadcastModel;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Livewire\Livewire;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
// Create permissions
|
|
Permission::create(['name' => 'create broadcast', 'guard_name' => 'web']);
|
|
Permission::create(['name' => 'update broadcast', 'guard_name' => 'web']);
|
|
Permission::create(['name' => 'delete broadcast', 'guard_name' => 'web']);
|
|
|
|
// Create role and assign permissions
|
|
$role = Role::create(['name' => 'Admin', 'guard_name' => 'web']);
|
|
$role->givePermissionTo(['create broadcast', 'update broadcast', 'delete broadcast']);
|
|
|
|
// Create admin user
|
|
$this->adminUser = User::factory()->create();
|
|
$this->adminUser->assignRole('Admin');
|
|
|
|
// Create additional roles for testing
|
|
Role::create(['name' => 'User', 'guard_name' => 'web']);
|
|
Role::create(['name' => 'Manager', 'guard_name' => 'web']);
|
|
});
|
|
|
|
it('renders page successfully', function () {
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->assertOk()
|
|
->assertSet('method', 'create')
|
|
->assertSet('modalTitle', '')
|
|
->assertSee('Broadcast')
|
|
->assertSet('roles', function ($roles) {
|
|
return count($roles) > 0 && ! isset($roles['Developer']);
|
|
});
|
|
});
|
|
|
|
it('has title attribute', function () {
|
|
$component = new Broadcast;
|
|
$reflection = new \ReflectionClass($component);
|
|
$attributes = $reflection->getAttributes(\Livewire\Attributes\Title::class);
|
|
|
|
expect($attributes)->not->toBeEmpty();
|
|
});
|
|
|
|
it('excludes developer role on mount', function () {
|
|
Role::create(['name' => 'Developer', 'guard_name' => 'web']);
|
|
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->assertSet('roles', function ($roles) {
|
|
return ! isset($roles['Developer']);
|
|
});
|
|
});
|
|
|
|
it('opens modal for create', function () {
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->call('openModal', 'create', 'Tambah Broadcast')
|
|
->assertSet('method', 'create')
|
|
->assertSet('modalTitle', 'Tambah Broadcast')
|
|
->assertSet('form.title', '')
|
|
->assertSet('form.body', '')
|
|
->assertSet('form.role_ids', []);
|
|
});
|
|
|
|
it('opens modal for update', function () {
|
|
$broadcast = BroadcastModel::factory()->create([
|
|
'title' => 'Test Title',
|
|
'body' => 'Test Body',
|
|
]);
|
|
|
|
$role = Role::create(['name' => 'Test Role', 'guard_name' => 'web']);
|
|
$broadcast->audiences()->attach($role->id);
|
|
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->call('openModal', 'update', 'Ubah Broadcast', $broadcast->hash)
|
|
->assertSet('method', 'update')
|
|
->assertSet('modalTitle', 'Ubah Broadcast');
|
|
});
|
|
|
|
it('creates broadcast successfully', function () {
|
|
Queue::fake();
|
|
|
|
$role1 = Role::where('name', 'User')->first();
|
|
$role2 = Role::where('name', 'Manager')->first();
|
|
|
|
$user1 = User::factory()->create();
|
|
$user2 = User::factory()->create();
|
|
$user1->assignRole('User');
|
|
$user2->assignRole('Manager');
|
|
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->set('form.title', 'Important Announcement')
|
|
->set('form.body', 'This is a test broadcast message.')
|
|
->set('form.role_ids', [$role1->id, $role2->id])
|
|
->call('create')
|
|
->assertDispatched('refreshDatatable');
|
|
|
|
$this->assertDatabaseHas('broadcasts', [
|
|
'user_id' => $this->adminUser->id,
|
|
'title' => 'Important Announcement',
|
|
'body' => 'This is a test broadcast message.',
|
|
]);
|
|
|
|
$broadcast = BroadcastModel::latest()->first();
|
|
expect($broadcast->audiences)->toHaveCount(2);
|
|
|
|
Queue::assertPushed(StorePushNotification::class);
|
|
});
|
|
|
|
it('requires permission to create broadcast', function () {
|
|
$userWithoutPermission = User::factory()->create();
|
|
|
|
Livewire::actingAs($userWithoutPermission)
|
|
->test(Broadcast::class)
|
|
->set('form.title', 'Test Title')
|
|
->set('form.body', 'Test Body')
|
|
->set('form.role_ids', [1])
|
|
->call('create')
|
|
->assertForbidden();
|
|
});
|
|
|
|
it('broadcasts cannot be updated', function () {
|
|
$broadcast = BroadcastModel::factory()->create();
|
|
|
|
// Broadcast component doesn't have update method, only create and delete
|
|
expect(method_exists(Broadcast::class, 'update'))->toBeFalse();
|
|
});
|
|
|
|
it('broadcasts do not support update permission check', function () {
|
|
// Broadcast component doesn't have update method, so no update permission test needed
|
|
expect(method_exists(Broadcast::class, 'update'))->toBeFalse();
|
|
});
|
|
|
|
it('deletes broadcast successfully', function () {
|
|
$broadcast = BroadcastModel::factory()->create();
|
|
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->call('delete', $broadcast)
|
|
->assertDispatched('refreshDatatable');
|
|
|
|
$this->assertDatabaseMissing('broadcasts', [
|
|
'id' => $broadcast->id,
|
|
]);
|
|
});
|
|
|
|
it('displays validation errors', function () {
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->set('form.title', '') // Empty title
|
|
->set('form.body', '') // Empty body
|
|
->set('form.role_ids', []) // Empty role_ids
|
|
->call('create')
|
|
->assertHasErrors(['form.title', 'form.body', 'form.role_ids']);
|
|
});
|
|
|
|
it('opens modal correctly', function () {
|
|
$broadcast = BroadcastModel::factory()->create();
|
|
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->call('openModal', 'create', 'Tambah Broadcast')
|
|
->assertSet('method', 'create')
|
|
->assertSet('modalTitle', 'Tambah Broadcast');
|
|
});
|
|
|
|
it('creates broadcast with push notification job', function () {
|
|
Queue::fake();
|
|
|
|
$role = Role::where('name', 'User')->first();
|
|
$user = User::factory()->create();
|
|
$user->assignRole('User');
|
|
|
|
Livewire::actingAs($this->adminUser)
|
|
->test(Broadcast::class)
|
|
->set('form.title', 'Test Title')
|
|
->set('form.body', 'Test Body')
|
|
->set('form.role_ids', [$role->id])
|
|
->call('create');
|
|
|
|
Queue::assertPushed(StorePushNotification::class);
|
|
});
|