| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace Tests\Feature;
- use App\Models\ExamPaper;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class ExamPdfPreviewTest extends TestCase
- {
- use RefreshDatabase;
- protected function setUp(): void
- {
- parent::setUp();
-
- // 创建测试用的 exam_papers 表
- \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
- \Illuminate\Support\Facades\Schema::connection('remote_mysql')->create('exam_papers', function (\Illuminate\Database\Schema\Blueprint $table) {
- $table->string('id', 191)->primary();
- $table->string('title');
- $table->integer('total_score');
- $table->integer('duration');
- $table->timestamps();
- });
- }
- /** @test */
- public function it_displays_pdf_preview_for_existing_exam()
- {
- $exam = ExamPaper::create([
- 'id' => 'test_pdf_exam',
- 'title' => 'PDF 测试试卷',
- 'total_score' => 100,
- 'duration' => 120,
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));
- $response->assertStatus(200)
- ->assertSee('PDF 测试试卷')
- ->assertSee('选择题')
- ->assertSee('填空题')
- ->assertSee('解答题');
- }
- /** @test */
- public function it_returns_404_for_non_existent_exam()
- {
- $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => 'non_existent']));
- $response->assertStatus(404);
- }
- /** @test */
- public function it_displays_omr_markers_for_all_question_types()
- {
- $exam = ExamPaper::create([
- 'id' => 'omr_test_exam',
- 'title' => 'OMR 标记测试',
- 'total_score' => 100,
- 'duration' => 120,
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));
- // 检查 OMR 标记的 CSS 类是否存在
- $response->assertSee('omr-marker', false);
- }
- /** @test */
- public function it_correctly_renders_newlines_in_questions()
- {
- $exam = ExamPaper::create([
- 'id' => 'newline_test_exam',
- 'title' => '换行测试试卷',
- 'total_score' => 100,
- 'duration' => 120,
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));
- // 检查换行符是否被转换为 <br> 标签
- $response->assertSee('<br', false);
- }
- /** @test */
- public function it_creates_exam_papers_table_if_not_exists()
- {
- // 删除表
- \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
- // 访问 PDF 页面应该自动创建表
- $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => 'test']));
- // 表应该已经被创建
- $this->assertTrue(\Illuminate\Support\Facades\Schema::connection('remote_mysql')->hasTable('exam_papers'));
- }
- protected function tearDown(): void
- {
- \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
- parent::tearDown();
- }
- }
|