add controllers, servoices
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Users;
|
||||
|
||||
use App\Services\Users\LoginActivityService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LoginActivityServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_list_returns_paginated_payload(): void
|
||||
{
|
||||
DB::table('login_activity')->insert([
|
||||
'user_id' => 1,
|
||||
'email' => 'user@example.com',
|
||||
'login_time' => '2025-01-02 09:00:00',
|
||||
'logout_time' => null,
|
||||
'ip_address' => '10.0.0.1',
|
||||
'user_agent' => 'TestAgent',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('login_activity')->insert([
|
||||
'user_id' => 1,
|
||||
'email' => 'user@example.com',
|
||||
'login_time' => '2025-01-01 09:00:00',
|
||||
'logout_time' => null,
|
||||
'ip_address' => '10.0.0.2',
|
||||
'user_agent' => 'TestAgent',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$service = app(LoginActivityService::class);
|
||||
$payload = $service->list(1, 2);
|
||||
|
||||
$this->assertSame(2, $payload['pagination']['total']);
|
||||
$this->assertSame(2, $payload['pagination']['currentPage']);
|
||||
$this->assertSame('10.0.0.2', $payload['activities'][0]['ip_address']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Users;
|
||||
|
||||
use App\Services\EmailService;
|
||||
use App\Services\Users\UserEventService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserEventServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_handle_new_account_sends_email(): void
|
||||
{
|
||||
$emailService = Mockery::mock(EmailService::class);
|
||||
$emailService->shouldReceive('send')
|
||||
->once()
|
||||
->with(
|
||||
'user@example.com',
|
||||
'Welcome to Al Rahma Sunday School!',
|
||||
Mockery::type('string'),
|
||||
'registration'
|
||||
)
|
||||
->andReturn(true);
|
||||
|
||||
$service = new UserEventService($emailService);
|
||||
|
||||
$result = $service->handleNewAccountAdded([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'email' => 'user@example.com',
|
||||
]);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function test_handle_new_account_requires_email(): void
|
||||
{
|
||||
$emailService = Mockery::mock(EmailService::class);
|
||||
$emailService->shouldNotReceive('send');
|
||||
|
||||
$service = new UserEventService($emailService);
|
||||
$result = $service->handleNewAccountAdded([
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
]);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Users;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Users\UserListService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserListServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_list_includes_roles_and_second_parent_flag(): void
|
||||
{
|
||||
DB::table('roles')->insert([
|
||||
'id' => 5,
|
||||
'name' => 'Parent',
|
||||
'slug' => 'parent',
|
||||
'description' => 'Parent role',
|
||||
'dashboard_route' => 'parent/dashboard',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
DB::table('users')->insert([
|
||||
'id' => 1,
|
||||
'school_id' => 1,
|
||||
'firstname' => 'Jane',
|
||||
'lastname' => 'Doe',
|
||||
'cellphone' => '5551112222',
|
||||
'email' => 'second.parent@example.com',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'is_verified' => 1,
|
||||
'status' => 'Active',
|
||||
'is_suspended' => 0,
|
||||
'failed_attempts' => 0,
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => 1,
|
||||
'role_id' => 5,
|
||||
]);
|
||||
|
||||
DB::table('parents')->insert([
|
||||
'secondparent_firstname' => 'Jane',
|
||||
'secondparent_lastname' => 'Doe',
|
||||
'secondparent_gender' => 'Female',
|
||||
'secondparent_email' => 'second.parent@example.com',
|
||||
'secondparent_phone' => '5551112222',
|
||||
'firstparent_id' => 10,
|
||||
'secondparent_id' => 11,
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$service = app(UserListService::class);
|
||||
$rows = $service->list('id', 'asc');
|
||||
|
||||
$this->assertCount(1, $rows);
|
||||
$this->assertSame('second.parent@example.com', $rows[0]['user']['email']);
|
||||
$this->assertSame(['Parent'], $rows[0]['roles']);
|
||||
$this->assertSame('Yes', $rows[0]['second_from_parents']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Users;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Users\UserEventService;
|
||||
use App\Services\Users\UserManagementService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserManagementServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_create_persists_user_and_role_and_sends_welcome(): void
|
||||
{
|
||||
DB::table('roles')->insert([
|
||||
'id' => 1,
|
||||
'name' => 'Parent',
|
||||
'slug' => 'parent',
|
||||
'description' => 'Parent role',
|
||||
'dashboard_route' => 'parent/dashboard',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$eventService = Mockery::mock(UserEventService::class);
|
||||
$eventService->shouldReceive('handleNewAccountAdded')
|
||||
->once()
|
||||
->andReturn(true);
|
||||
|
||||
$service = new UserManagementService($eventService);
|
||||
|
||||
$result = $service->create([
|
||||
'firstname' => 'New',
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '5554443333',
|
||||
'email' => 'new.user@example.com',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => true,
|
||||
'role_id' => 1,
|
||||
'password' => 'secret123',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$this->assertTrue($result['ok']);
|
||||
$this->assertInstanceOf(User::class, $result['user']);
|
||||
$this->assertDatabaseHas('users', [
|
||||
'email' => 'new.user@example.com',
|
||||
'firstname' => 'New',
|
||||
'lastname' => 'User',
|
||||
]);
|
||||
$this->assertDatabaseHas('user_roles', [
|
||||
'user_id' => $result['user']->id,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_changes_user_fields(): void
|
||||
{
|
||||
DB::table('users')->insert([
|
||||
'id' => 1,
|
||||
'school_id' => 1,
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '5555555555',
|
||||
'email' => 'test@example.com',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'is_verified' => 1,
|
||||
'status' => 'Active',
|
||||
'is_suspended' => 0,
|
||||
'failed_attempts' => 0,
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
$eventService = Mockery::mock(UserEventService::class);
|
||||
$service = new UserManagementService($eventService);
|
||||
|
||||
$result = $service->update(1, [
|
||||
'firstname' => 'Updated',
|
||||
'email' => 'updated@example.com',
|
||||
'accept_school_policy' => false,
|
||||
]);
|
||||
|
||||
$this->assertTrue($result['ok']);
|
||||
$this->assertDatabaseHas('users', [
|
||||
'id' => 1,
|
||||
'firstname' => 'Updated',
|
||||
'email' => 'updated@example.com',
|
||||
'accept_school_policy' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_delete_removes_user_and_roles(): void
|
||||
{
|
||||
DB::table('users')->insert([
|
||||
'id' => 1,
|
||||
'school_id' => 1,
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '5555555555',
|
||||
'email' => 'test@example.com',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'ST',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'is_verified' => 1,
|
||||
'status' => 'Active',
|
||||
'is_suspended' => 0,
|
||||
'failed_attempts' => 0,
|
||||
'password' => bcrypt('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
|
||||
DB::table('roles')->insert([
|
||||
'id' => 1,
|
||||
'name' => 'Parent',
|
||||
'slug' => 'parent',
|
||||
'description' => 'Parent role',
|
||||
'dashboard_route' => 'parent/dashboard',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => 1,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
|
||||
$eventService = Mockery::mock(UserEventService::class);
|
||||
$service = new UserManagementService($eventService);
|
||||
|
||||
$result = $service->delete(1);
|
||||
|
||||
$this->assertTrue($result['ok']);
|
||||
$this->assertDatabaseMissing('users', ['id' => 1]);
|
||||
$this->assertDatabaseMissing('user_roles', ['user_id' => 1]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user