You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wx.sstbc.com/tests/Feature/QccEnterpriseStudentFilterT...

110 lines
7.2 KiB

<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Http\Controllers\Admin\QccEnterpriseAccountController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
class QccEnterpriseStudentFilterTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['database.default' => 'sqlite', 'database.connections.sqlite.database' => ':memory:', 'audit.enabled' => false]);
DB::purge('sqlite');
Schema::create('companies', function (Blueprint $t) { $t->id(); $t->string('company_name'); $t->string('credit_code'); $t->timestamps(); $t->softDeletes(); });
Schema::create('company_qcc_accounts', function (Blueprint $t) { $t->id(); $t->integer('company_id')->unique(); $t->string('status'); $t->integer('selected_by')->nullable(); $t->dateTime('selected_at')->nullable(); $t->timestamps(); });
Schema::create('users', function (Blueprint $t) { $t->id(); $t->integer('company_id'); $t->string('name'); $t->string('username'); $t->string('no')->nullable(); $t->string('company_position')->nullable(); $t->integer('is_schoolmate'); $t->softDeletes(); });
Schema::create('courses', function (Blueprint $t) { $t->id(); $t->string('name'); $t->integer('type'); $t->date('start_date')->nullable(); $t->softDeletes(); });
Schema::create('course_signs', function (Blueprint $t) { $t->id(); $t->integer('course_id'); $t->integer('user_id'); $t->integer('status'); $t->softDeletes(); });
foreach ([1, 2, 3] as $id) {
DB::table('companies')->insert(['id' => $id, 'company_name' => '企业'.$id, 'credit_code' => 'credit'.$id]);
DB::table('company_qcc_accounts')->insert(['company_id' => $id, 'status' => 'occupied']);
}
DB::table('users')->insert([
['id' => 1, 'company_id' => 1, 'name' => '张三', 'username' => 'zhang', 'is_schoolmate' => 1],
['id' => 2, 'company_id' => 1, 'name' => '李四', 'username' => 'li', 'is_schoolmate' => 0],
['id' => 3, 'company_id' => 2, 'name' => '王五', 'username' => 'wang', 'is_schoolmate' => 0],
]);
DB::table('courses')->insert([['id' => 1, 'name' => '管理课程', 'type' => 1], ['id' => 2, 'name' => '金融课程', 'type' => 2]]);
DB::table('course_signs')->insert([
['user_id' => 1, 'course_id' => 1, 'status' => 1], ['user_id' => 2, 'course_id' => 2, 'status' => 1],
['user_id' => 1, 'course_id' => 2, 'status' => 0], ['user_id' => 3, 'course_id' => 2, 'status' => 0],
]);
}
private function query(array $params, string $method = 'candidates'): array
{
return (new QccEnterpriseAccountController())->$method(Request::create('/', 'GET', $params))->getData(true);
}
public function test_candidates_require_an_alumnus_and_do_not_duplicate_companies(): void
{
$result = $this->query([]);
$this->assertEquals(1, $result['total']);
$company = collect($result['data'])->firstWhere('id', 1);
$this->assertCount(2, $company['users']);
$this->assertCount(1, $company['users'][0]['course_signs']);
$this->assertEquals('管理课程', $company['users'][0]['course_signs'][0]['course']['name']);
$this->assertEquals([1], array_column($result['data'], 'id'));
DB::table('users')->where('id', 2)->update(['is_schoolmate' => 1]);
$this->assertEquals(1, $this->query([])['total']);
DB::table('users')->where('company_id', 1)->update(['deleted_at' => now()]);
$this->assertEquals(0, $this->query([])['total']);
}
public function test_combined_filters_match_same_student_and_same_approved_course(): void
{
foreach (['candidates', 'index'] as $method) {
$this->assertEquals(1, $this->query(['user_name' => '张', 'is_schoolmate' => '1', 'course_type_id' => 1, 'course_name' => '管理'], $method)['total']);
$this->assertEquals(0, $this->query(['user_name' => '张', 'course_name' => '金融'], $method)['total']);
$this->assertEquals(0, $this->query(['course_type_id' => 1, 'course_name' => '金融'], $method)['total']);
$this->assertEquals(1, $this->query(['is_schoolmate' => '0'], $method)['total']);
$this->assertEquals(0, $this->query(['user_name' => '王'], $method)['total']);
}
$this->assertCount(2, $this->query(['keyword' => 'credit1'], 'index')['data'][0]['company']['users']);
}
private function actionController(): QccEnterpriseAccountController
{
return new class extends QccEnterpriseAccountController {
public function getUserId() { return 7; }
protected function log(string $name, string $remark = ''): void {}
};
}
public function test_bulk_non_occupying_enrollment_and_status_filter(): void
{
DB::table('company_qcc_accounts')->where('company_id', 1)->delete();
$controller = $this->actionController();
$controller->selectNonOccupying(Request::create('/', 'POST', ['company_ids' => [1]]));
$row = DB::table('company_qcc_accounts')->where('company_id', 1)->first();
$this->assertEquals('non_occupying', $row->status);
$this->assertEquals(7, $row->selected_by);
$this->assertNotNull($row->selected_at);
$this->assertFalse(\App\Models\CompanyQccAccount::where('company_id', 1)->first()->isCallable());
foreach (['candidates', 'index'] as $method) {
$this->assertEquals(1, $this->query(['status' => 'non_occupying'], $method)['total']);
$this->assertEquals(0, $this->query(['status' => 'selected'], $method)['total']);
}
$this->assertEquals(0, $this->query(['status' => 'none'])['total']);
}
public function test_bulk_non_occupying_rejects_existing_states_without_partial_changes(): void
{
DB::table('company_qcc_accounts')->where('company_id', 1)->delete();
foreach (['occupied', 'selected', 'unknown', 'non_occupying'] as $status) {
DB::table('company_qcc_accounts')->where('company_id', 2)->update(['status' => $status]);
$result = $this->actionController()->selectNonOccupying(Request::create('/', 'POST', ['company_ids' => [1, 2]]))->getData(true);
$this->assertArrayHasKey('errcode', $result);
$this->assertFalse(DB::table('company_qcc_accounts')->where('company_id', 1)->exists());
$this->assertEquals($status, DB::table('company_qcc_accounts')->where('company_id', 2)->value('status'));
}
$result = $this->actionController()->selectNonOccupying(Request::create('/', 'POST', ['company_ids' => [1, 999]]))->getData(true);
$this->assertArrayHasKey('errcode', $result);
$this->assertFalse(DB::table('company_qcc_accounts')->where('company_id', 1)->exists());
}
public function test_candidate_enrollment_still_creates_selected_status(): void
{
DB::table('company_qcc_accounts')->where('company_id', 1)->delete();
$this->actionController()->select(Request::create('/', 'POST', ['company_ids' => [1]]));
$this->assertEquals('selected', DB::table('company_qcc_accounts')->where('company_id', 1)->value('status'));
}
}