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.

112 lines
4.4 KiB

<?php
namespace Tests\Unit;
use App\Models\ResearchDirection;
use App\Models\Teacher;
use App\Services\Crawl\CrawlImportService;
use App\Support\TeacherLibraryStatus;
use Tests\TestCase;
class CrawlImportTeacherFillEmptyTest extends TestCase
{
public function test_fill_empty_teacher_fields_only_updates_blank_values(): void
{
$teacher = Teacher::query()->create([
'name' => '补空字段测试老师',
'university_id' => null,
'department' => '计算机学院',
'bio' => null,
'city' => '待补充',
'title' => '待补充',
'email' => null,
'phone' => null,
'library_status' => TeacherLibraryStatus::Active,
'remark' => '高校抓取入库',
]);
$direction = ResearchDirection::query()->firstOrCreate(
['name' => '补空字段测试方向'],
['sort' => 0, 'status' => 1, 'remark' => 'test'],
);
try {
$service = app(CrawlImportService::class);
$changed = $service->fillEmptyTeacherFields($teacher, [
'email' => 'fill-empty-test@sjtu.edu.cn',
'phone' => '021-12345678',
'title' => '教授',
'bio' => '长期从事操作系统研究。',
'city' => '上海',
'profile_url' => 'https://www.cs.sjtu.edu.cn/jiaoshiml/fill-empty-test.html',
'research_direction_ids' => [$direction->id],
]);
$this->assertTrue($changed);
$teacher->refresh();
$this->assertSame('fill-empty-test@sjtu.edu.cn', $teacher->email);
$this->assertSame('021-12345678', $teacher->phone);
$this->assertSame('教授', $teacher->title);
$this->assertSame('长期从事操作系统研究。', $teacher->bio);
$this->assertSame('上海', $teacher->city);
$this->assertStringContainsString('fill-empty-test.html', (string) $teacher->remark);
$this->assertSame([$direction->id], $teacher->researchDirections()->pluck('research_directions.id')->all());
// 已有值不应被覆盖
$changedAgain = $service->fillEmptyTeacherFields($teacher, [
'email' => 'other@sjtu.edu.cn',
'phone' => '021-87654321',
'title' => '副教授',
'bio' => '另一段简介',
'city' => '北京',
'research_direction_ids' => [$direction->id],
]);
$this->assertFalse($changedAgain);
$teacher->refresh();
$this->assertSame('fill-empty-test@sjtu.edu.cn', $teacher->email);
$this->assertSame('021-12345678', $teacher->phone);
$this->assertSame('教授', $teacher->title);
$this->assertSame('长期从事操作系统研究。', $teacher->bio);
$this->assertSame('上海', $teacher->city);
} finally {
$teacher->researchDirections()->detach();
$teacher->forceDelete();
}
}
public function test_fill_empty_overwrites_invalid_nav_title(): void
{
$teacher = Teacher::query()->create([
'name' => '误抓职称测试老师',
'university_id' => null,
'department' => '电子信息与电气工程学院',
'bio' => '已有简介',
'city' => '上海',
'title' => '电气首页',
'email' => 'nav-title-test@sjtu.edu.cn',
'phone' => '021-11112222',
'library_status' => TeacherLibraryStatus::Active,
'remark' => '高校抓取入库',
]);
try {
$service = app(CrawlImportService::class);
$changed = $service->fillEmptyTeacherFields($teacher, [
'title' => '教授',
'email' => 'other@sjtu.edu.cn',
'phone' => '021-99998888',
'bio' => '不应覆盖的简介',
]);
$this->assertTrue($changed);
$teacher->refresh();
$this->assertSame('教授', $teacher->title);
$this->assertSame('nav-title-test@sjtu.edu.cn', $teacher->email);
$this->assertSame('021-11112222', $teacher->phone);
$this->assertSame('已有简介', $teacher->bio);
} finally {
$teacher->forceDelete();
}
}
}