feature: user list
This commit is contained in:
@@ -30,11 +30,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'usera@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$userB = User::create([
|
||||
@@ -43,11 +43,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'userb@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$selectedMonth = '2026-07';
|
||||
@@ -110,11 +110,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'usera@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$selectedMonth = '2026-07';
|
||||
@@ -167,11 +167,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'alpha@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
User::create([
|
||||
@@ -180,11 +180,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'beta@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// 2. Fetch statistics via AdminService with search for '2001' (msnv)
|
||||
@@ -211,11 +211,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'active@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
User::create([
|
||||
@@ -224,11 +224,11 @@ class AdminUserListStatsTest extends TestCase
|
||||
'mail' => 'inactive@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_INACTIVE,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_INACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_DISABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// 2. Fetch statistics via AdminService with status = '1'
|
||||
@@ -252,12 +252,12 @@ class AdminUserListStatsTest extends TestCase
|
||||
'name' => 'ADMIN DEV ENABLED',
|
||||
'mail' => 'admin_dev@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => User::ROLE_ADMIN,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'departments' => config('constants.DEPARTMENTS')[0],
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_ENABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
User::create([
|
||||
@@ -265,12 +265,12 @@ class AdminUserListStatsTest extends TestCase
|
||||
'name' => 'MEMBER HR DISABLED',
|
||||
'mail' => 'member_hr@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 2,
|
||||
'role' => User::ROLE_MEMBER,
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'departments' => config('constants.DEPARTMENTS')[13],
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => User::FLAG_SEND_DISABLED,
|
||||
'first_login' => User::FIRST_LOGIN_FALSE,
|
||||
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// Filter by role = Admin ('1')
|
||||
@@ -279,8 +279,8 @@ class AdminUserListStatsTest extends TestCase
|
||||
$this->assertTrue($users->contains('msnv', 4001));
|
||||
$this->assertFalse($users->contains('msnv', 4002));
|
||||
|
||||
// Filter by department = HR ('2')
|
||||
$stats2 = $this->adminService->getUserListWithStats('2026-07', null, '1', null, '2');
|
||||
// Filter by department = HR (config('constants.DEPARTMENTS')[13])
|
||||
$stats2 = $this->adminService->getUserListWithStats('2026-07', null, '1', null, config('constants.DEPARTMENTS')[13]);
|
||||
$users2 = collect($stats2['users']->items());
|
||||
$this->assertTrue($users2->contains('msnv', 4002));
|
||||
$this->assertFalse($users2->contains('msnv', 4001));
|
||||
@@ -291,4 +291,483 @@ class AdminUserListStatsTest extends TestCase
|
||||
$this->assertTrue($users3->contains('msnv', 4001));
|
||||
$this->assertFalse($users3->contains('msnv', 4002));
|
||||
}
|
||||
|
||||
public function test_admin_can_update_user_fields_including_card_role_status_flag_send_first_login(): void
|
||||
{
|
||||
$admin = User::create([
|
||||
'msnv' => 9001,
|
||||
'name' => 'Admin User',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$member = User::create([
|
||||
'msnv' => 9002,
|
||||
'name' => 'Member User',
|
||||
'mail' => 'member@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 5,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($admin)->put(route('admin.users.update', 9002), [
|
||||
'card' => 20, // increased by 15
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_INACTIVE'),
|
||||
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_TRUE'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
$member->refresh();
|
||||
$this->assertEquals(20, $member->card);
|
||||
$this->assertEquals(config('constants.ROLE_ADMIN'), $member->role);
|
||||
$this->assertEquals(config('constants.STATUS_INACTIVE'), $member->status);
|
||||
$this->assertEquals(config('constants.FLAG_SEND_DISABLED'), $member->flag_send);
|
||||
$this->assertEquals(config('constants.FIRST_LOGIN_TRUE'), $member->first_login);
|
||||
|
||||
// Assert AddCard record was created with diff (15)
|
||||
$this->assertDatabaseHas('add_card', [
|
||||
'buyer' => 9002,
|
||||
'num_card' => 15,
|
||||
'seller' => 9001,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_user_list_stats_handles_ties_for_top_sender_receiver(): void
|
||||
{
|
||||
// Clear existing mock data first to have a clean slate for ties
|
||||
User::truncate();
|
||||
Administration::truncate();
|
||||
|
||||
// 1. Create three users
|
||||
$user1 = User::create([
|
||||
'msnv' => 3001,
|
||||
'name' => 'User One',
|
||||
'mail' => 'user1@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => config('constants.DEPARTMENTS')[0],
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$user2 = User::create([
|
||||
'msnv' => 3002,
|
||||
'name' => 'User Two',
|
||||
'mail' => 'user2@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => config('constants.DEPARTMENTS')[0],
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$user3 = User::create([
|
||||
'msnv' => 3003,
|
||||
'name' => 'User Three',
|
||||
'mail' => 'user3@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => config('constants.DEPARTMENTS')[0],
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$selectedMonth = '2026-07';
|
||||
$testDate = '2026-07-15';
|
||||
|
||||
// User 1 sends 5 cards to User 2
|
||||
Administration::create([
|
||||
'msnv' => 3001,
|
||||
'received' => 0,
|
||||
'sender' => null,
|
||||
'sent' => 5,
|
||||
'receiver' => 3002,
|
||||
'date' => $testDate,
|
||||
]);
|
||||
Administration::create([
|
||||
'msnv' => 3002,
|
||||
'received' => 5,
|
||||
'sender' => 3001,
|
||||
'sent' => 0,
|
||||
'receiver' => null,
|
||||
'date' => $testDate,
|
||||
]);
|
||||
|
||||
// User 3 sends 5 cards to User 1
|
||||
Administration::create([
|
||||
'msnv' => 3003,
|
||||
'received' => 0,
|
||||
'sender' => null,
|
||||
'sent' => 5,
|
||||
'receiver' => 3001,
|
||||
'date' => $testDate,
|
||||
]);
|
||||
Administration::create([
|
||||
'msnv' => 3001,
|
||||
'received' => 5,
|
||||
'sender' => 3003,
|
||||
'sent' => 0,
|
||||
'receiver' => null,
|
||||
'date' => $testDate,
|
||||
]);
|
||||
|
||||
// Now:
|
||||
// User 1 has sent = 5, received = 5
|
||||
// User 2 has sent = 0, received = 5
|
||||
// User 3 has sent = 5, received = 0
|
||||
// Top senders should be: User 1 and User 3 (both sent 5)
|
||||
// Top receivers should be: User 1 and User 2 (both received 5)
|
||||
|
||||
$stats = $this->adminService->getUserListWithStats($selectedMonth);
|
||||
|
||||
$this->assertCount(2, $stats['topReceivedUsers']);
|
||||
$this->assertCount(2, $stats['topSentUsers']);
|
||||
|
||||
$topReceivedMsnvs = $stats['topReceivedUsers']->pluck('msnv')->toArray();
|
||||
$this->assertContains(3001, $topReceivedMsnvs);
|
||||
$this->assertContains(3002, $topReceivedMsnvs);
|
||||
|
||||
$topSentMsnvs = $stats['topSentUsers']->pluck('msnv')->toArray();
|
||||
$this->assertContains(3001, $topSentMsnvs);
|
||||
$this->assertContains(3003, $topSentMsnvs);
|
||||
}
|
||||
|
||||
public function test_user_list_stats_handles_10_way_tie_for_top_sender_receiver(): void
|
||||
{
|
||||
$users = [];
|
||||
$selectedMonth = '2026-07';
|
||||
$testDate = '2026-07-15';
|
||||
|
||||
// Create 10 users
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$users[$i] = User::create([
|
||||
'msnv' => 5000 + $i,
|
||||
'name' => "User Tenfold {$i}",
|
||||
'mail' => "user_tenfold_{$i}@example.com",
|
||||
'pass' => md5('password'),
|
||||
'departments' => config('constants.DEPARTMENTS')[0],
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
}
|
||||
|
||||
// Each user sends 1 card to the next user, and user 10 sends to user 1
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$senderMsnv = 5000 + $i;
|
||||
$receiverMsnv = 5000 + ($i % 10 + 1);
|
||||
|
||||
// Sender record
|
||||
Administration::create([
|
||||
'msnv' => $senderMsnv,
|
||||
'received' => 0,
|
||||
'sender' => null,
|
||||
'sent' => 1,
|
||||
'receiver' => $receiverMsnv,
|
||||
'date' => $testDate,
|
||||
]);
|
||||
|
||||
// Receiver record
|
||||
Administration::create([
|
||||
'msnv' => $receiverMsnv,
|
||||
'received' => 1,
|
||||
'sender' => $senderMsnv,
|
||||
'sent' => 0,
|
||||
'receiver' => null,
|
||||
'date' => $testDate,
|
||||
]);
|
||||
}
|
||||
|
||||
// Run the service call
|
||||
$stats = $this->adminService->getUserListWithStats($selectedMonth);
|
||||
|
||||
// Assert all 10 users are returned in topReceivedUsers and topSentUsers
|
||||
$this->assertCount(10, $stats['topReceivedUsers']);
|
||||
$this->assertCount(10, $stats['topSentUsers']);
|
||||
|
||||
// Verify all their MSNVs are present
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$msnv = 5000 + $i;
|
||||
$this->assertContains($msnv, $stats['topReceivedUsers']->pluck('msnv')->toArray());
|
||||
$this->assertContains($msnv, $stats['topSentUsers']->pluck('msnv')->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_update_add_card_successfully_updates_balance(): void
|
||||
{
|
||||
$admin = User::create([
|
||||
'msnv' => 9001,
|
||||
'name' => 'Admin User',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$member = User::create([
|
||||
'msnv' => 9002,
|
||||
'name' => 'Member User',
|
||||
'mail' => 'member@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10, // currently has 10 cards
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// Created in the current month
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 5,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Action: Update it from 5 to 3 (decrease of 2, new user balance should be 10 - 2 = 8)
|
||||
$response = $this->actingAs($admin)->put(route('admin.add_cards.update', $addCard->id), [
|
||||
'num_card' => 3,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
$addCard->refresh();
|
||||
$this->assertEquals(3, $addCard->num_card);
|
||||
|
||||
$member->refresh();
|
||||
$this->assertEquals(8, $member->card);
|
||||
}
|
||||
|
||||
public function test_update_add_card_fails_if_past_month(): void
|
||||
{
|
||||
$admin = User::create([
|
||||
'msnv' => 9001,
|
||||
'name' => 'Admin User',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$member = User::create([
|
||||
'msnv' => 9002,
|
||||
'name' => 'Member User',
|
||||
'mail' => 'member@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// Created in a past month
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 5,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->subMonth()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($admin)->put(route('admin.add_cards.update', $addCard->id), [
|
||||
'num_card' => 3,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasErrors('error');
|
||||
$this->assertTrue(session('errors')->has('error'));
|
||||
$this->assertEquals('Không cho phép chỉnh sửa dữ liệu card của các tháng trước.', session('errors')->first('error'));
|
||||
}
|
||||
|
||||
public function test_update_add_card_fails_if_new_date_not_current_month(): void
|
||||
{
|
||||
$admin = User::create([
|
||||
'msnv' => 9001,
|
||||
'name' => 'Admin User',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$member = User::create([
|
||||
'msnv' => 9002,
|
||||
'name' => 'Member User',
|
||||
'mail' => 'member@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// Created in current month
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 5,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Update with date in a past month
|
||||
$response = $this->actingAs($admin)->put(route('admin.add_cards.update', $addCard->id), [
|
||||
'num_card' => 3,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->subMonth()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasErrors('error');
|
||||
$this->assertEquals('Chỉ được phép chỉnh sửa dữ liệu của tháng hiện tại.', session('errors')->first('error'));
|
||||
}
|
||||
|
||||
public function test_update_add_card_fails_if_new_balance_negative(): void
|
||||
{
|
||||
$admin = User::create([
|
||||
'msnv' => 9001,
|
||||
'name' => 'Admin User',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$member = User::create([
|
||||
'msnv' => 9002,
|
||||
'name' => 'Member User',
|
||||
'mail' => 'member@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 2, // has 2 cards left
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// Created in current month
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 5,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Decrease it from 5 to 1 (decrease of 4). Since member only has 2 cards, this would make balance -2 (2 - 4 = -2), which is illegal
|
||||
$response = $this->actingAs($admin)->put(route('admin.add_cards.update', $addCard->id), [
|
||||
'num_card' => 1,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasErrors('error');
|
||||
$this->assertEquals('Số lượng card cập nhật không hợp lệ vì tổng số card của user không được nhỏ hơn 0.', session('errors')->first('error'));
|
||||
}
|
||||
|
||||
public function test_update_add_card_succeeds_for_older_transaction_in_current_month(): void
|
||||
{
|
||||
$admin = User::create([
|
||||
'msnv' => 9001,
|
||||
'name' => 'Admin User',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_ADMIN'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 10,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$member = User::create([
|
||||
'msnv' => 9002,
|
||||
'name' => 'Member User',
|
||||
'mail' => 'member@example.com',
|
||||
'pass' => md5('password'),
|
||||
'departments' => 1,
|
||||
'role' => config('constants.ROLE_MEMBER'),
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 15, // has 15 cards
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
// Transaction 1 (older)
|
||||
$oldAddCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 5,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Transaction 2 (newest)
|
||||
$newAddCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 10,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Try to update older Transaction 1: change num_card from 5 to 6 (increase of 1)
|
||||
$response = $this->actingAs($admin)->put(route('admin.add_cards.update', $oldAddCard->id), [
|
||||
'num_card' => 6,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasNoErrors();
|
||||
|
||||
$oldAddCard->refresh();
|
||||
$this->assertEquals(6, $oldAddCard->num_card);
|
||||
|
||||
$member->refresh();
|
||||
$this->assertEquals(16, $member->card);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user