select box
This commit is contained in:
@@ -770,4 +770,205 @@ class AdminUserListStatsTest extends TestCase
|
||||
$member->refresh();
|
||||
$this->assertEquals(16, $member->card);
|
||||
}
|
||||
|
||||
public function test_delete_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' => 15,
|
||||
'flag_send' => config('constants.FLAG_SEND_ENABLED'),
|
||||
'first_login' => config('constants.FIRST_LOGIN_FALSE'),
|
||||
]);
|
||||
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 5,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Action: delete the allocation
|
||||
$response = $this->actingAs($admin)->delete(route('admin.add_cards.destroy', $addCard->id));
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
$this->assertDatabaseMissing('add_card', ['id' => $addCard->id]);
|
||||
|
||||
$member->refresh();
|
||||
$this->assertEquals(10, $member->card);
|
||||
}
|
||||
|
||||
public function test_update_add_card_fails_if_balance_drops_below_sent_cards(): 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'),
|
||||
]);
|
||||
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 10,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Member sent 6 cards this month
|
||||
Administration::create([
|
||||
'msnv' => 9002,
|
||||
'received' => 0,
|
||||
'sent' => 6,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Try to update num_card from 10 to 4 (decrease of 6, new user balance would be 10 - 6 = 4).
|
||||
// Since member sent 6 cards, 4 is less than 6, so this should fail!
|
||||
$response = $this->actingAs($admin)->put(route('admin.add_cards.update', $addCard->id), [
|
||||
'num_card' => 4,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasErrors('error');
|
||||
$this->assertStringContainsString('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 số card đã gửi trong tháng', session('errors')->first('error'));
|
||||
}
|
||||
|
||||
public function test_delete_add_card_fails_if_balance_drops_below_sent_cards(): 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'),
|
||||
]);
|
||||
|
||||
$addCard = \App\Models\AddCard::create([
|
||||
'buyer' => 9002,
|
||||
'num_card' => 10,
|
||||
'seller' => 9001,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Member sent 6 cards this month
|
||||
Administration::create([
|
||||
'msnv' => 9002,
|
||||
'received' => 0,
|
||||
'sent' => 6,
|
||||
'date' => Carbon::now()->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
// Try to delete the 10 cards allocation (user balance would be 10 - 10 = 0).
|
||||
// Since member sent 6 cards, 0 is less than 6, so this should fail!
|
||||
$response = $this->actingAs($admin)->delete(route('admin.add_cards.destroy', $addCard->id));
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasErrors('error');
|
||||
$this->assertStringContainsString('Không thể xóa lịch sử cấp phát thẻ này vì tổng số card của user không được nhỏ hơn số card đã gửi trong tháng', session('errors')->first('error'));
|
||||
}
|
||||
|
||||
public function test_card_additions_on_same_day_are_accumulated(): 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' => 0,
|
||||
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
|
||||
'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'),
|
||||
]);
|
||||
|
||||
// First allocation: add 10 cards
|
||||
$this->actingAs($admin)->put(route('admin.users.update', 9002), [
|
||||
'card' => 15,
|
||||
]);
|
||||
|
||||
// Second allocation: add 5 cards
|
||||
$this->actingAs($admin)->put(route('admin.users.update', 9002), [
|
||||
'card' => 20,
|
||||
]);
|
||||
|
||||
$member->refresh();
|
||||
$this->assertEquals(20, $member->card);
|
||||
|
||||
// There should be only 1 AddCard record with 15 cards (10 + 5)
|
||||
$addCards = \App\Models\AddCard::where('buyer', 9002)->where('seller', 9001)->get();
|
||||
$this->assertCount(1, $addCards);
|
||||
$this->assertEquals(15, $addCards->first()->num_card);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user