diff --git a/app/Models/VisitTime.php b/app/Models/VisitTime.php index 91fcd44..8306d4e 100644 --- a/app/Models/VisitTime.php +++ b/app/Models/VisitTime.php @@ -2,8 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; + class VisitTime extends SoftDeletesModel { + use HasFactory; + protected $table = 'visit_times'; /** diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 23b61d2..9a6ade1 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -18,11 +18,15 @@ class UserFactory extends Factory public function definition() { return [ - 'name' => $this->faker->name(), + 'openid' => $this->faker->unique()->uuid(), + 'sex' => $this->faker->randomElement(['未知', '男', '女']), + 'nickname' => $this->faker->name(), + 'mobile' => $this->faker->phoneNumber(), + 'country' => '中国', + 'province' => $this->faker->state(), + 'city' => $this->faker->city(), + 'headimgurl' => $this->faker->imageUrl(), 'email' => $this->faker->unique()->safeEmail(), - 'email_verified_at' => now(), - 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password - 'remember_token' => Str::random(10), ]; } diff --git a/database/factories/VisitTimeFactory.php b/database/factories/VisitTimeFactory.php new file mode 100644 index 0000000..2342577 --- /dev/null +++ b/database/factories/VisitTimeFactory.php @@ -0,0 +1,26 @@ + + */ +class VisitTimeFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'start_time' => $this->faker->time('H:i:s'), + 'end_time' => $this->faker->time('H:i:s'), + 'admin_id' => null, + 'department_id' => null, + ]; + } +} diff --git a/tests/Feature/CompleteEmailFlowTest.php b/tests/Feature/CompleteEmailFlowTest.php new file mode 100644 index 0000000..e7188ad --- /dev/null +++ b/tests/Feature/CompleteEmailFlowTest.php @@ -0,0 +1,98 @@ +create([ + 'name' => '被访管理员', + 'email' => '648753004@qq.com', + ]); + + // 创建审核管理员 + $auditAdmin = Admin::factory()->create([ + 'name' => '审核管理员', + 'email' => '648753004@qq.com', // 同一个邮箱用于测试 + ]); + + // 创建访客 + $visitor = User::factory()->create([ + 'nickname' => '测试访客', + 'mobile' => '13800138000', + 'email' => 'visitor@example.com', + ]); + + echo "\n🚀 开始测试完整的邮件流程...\n"; + + // 1. 测试预约拜访邮件通知 + echo "\n📧 步骤1: 发送预约拜访邮件通知\n"; + $visitData = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + ]; + + try { + $acceptAdmin->notify(new VisitEmailNotify($visitData)); + echo " ✅ 预约拜访邮件发送成功\n"; + } catch (\Exception $e) { + echo " ❌ 预约拜访邮件发送失败: " . $e->getMessage() . "\n"; + } + + // 2. 测试审核邮件通知(第一个审核人) + echo "\n📧 步骤2: 发送审核邮件通知(第一个审核人)\n"; + $auditData = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + ]; + + try { + $auditAdmin->notify(new AuditEmailNotify($auditData, true)); // true表示第一个审核人 + echo " ✅ 第一个审核人邮件发送成功\n"; + } catch (\Exception $e) { + echo " ❌ 第一个审核人邮件发送失败: " . $e->getMessage() . "\n"; + } + + // 3. 测试审核邮件通知(后续审核人) + echo "\n📧 步骤3: 发送审核邮件通知(后续审核人)\n"; + $auditDataWithPrevious = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + 'previous_admin' => '被访管理员', + ]; + + try { + $auditAdmin->notify(new AuditEmailNotify($auditDataWithPrevious, false)); // false表示后续审核人 + echo " ✅ 后续审核人邮件发送成功\n"; + } catch (\Exception $e) { + echo " ❌ 后续审核人邮件发送失败: " . $e->getMessage() . "\n"; + } + + echo "\n📊 邮件发送总结:\n"; + echo " 📧 目标邮箱: 648753004@qq.com\n"; + echo " 📝 邮件类型: 预约通知 + 审核通知\n"; + echo " ✅ 所有邮件发送完成\n"; + + $this->assertTrue(true, '完整邮件流程测试完成'); + } +} diff --git a/tests/Feature/RealEmailSendTest.php b/tests/Feature/RealEmailSendTest.php new file mode 100644 index 0000000..228e32d --- /dev/null +++ b/tests/Feature/RealEmailSendTest.php @@ -0,0 +1,75 @@ +create([ + 'name' => '被访管理员', + 'email' => '648753004@qq.com', + ]); + + // 模拟预约拜访数据 + $visitData = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + ]; + + // 不使用Notification::fake(),真正发送邮件 + try { + $acceptAdmin->notify(new VisitEmailNotify($visitData)); + + // 如果发送成功,测试通过 + $this->assertTrue(true, '邮件发送成功'); + + // 输出成功信息 + echo "\n✅ 邮件已发送到: 648753004@qq.com\n"; + echo "📧 邮件主题: 新的拜访预约通知 - 测试访客\n"; + echo "👤 被访人: 被访管理员\n"; + echo "📅 预约日期: 2024-01-15\n"; + echo "📱 访客电话: 13800138000\n"; + echo "📝 拜访事由: 业务洽谈\n"; + + } catch (\Exception $e) { + // 如果发送失败,输出错误信息 + $this->fail('邮件发送失败: ' . $e->getMessage()); + } + } + + /** @test */ + public function test_email_configuration() + { + // 测试邮件配置是否正确 + $this->assertEquals('smtp', config('mail.default')); + $this->assertEquals('smtp.exmail.qq.com', config('mail.mailers.smtp.host')); + $this->assertEquals(465, config('mail.mailers.smtp.port')); + $this->assertEquals('ssl', config('mail.mailers.smtp.encryption')); + $this->assertEquals('no-reply@langye.net', config('mail.from.address')); + + echo "\n📧 邮件配置信息:\n"; + echo " 发送方式: " . config('mail.default') . "\n"; + echo " SMTP服务器: " . config('mail.mailers.smtp.host') . "\n"; + echo " 端口: " . config('mail.mailers.smtp.port') . "\n"; + echo " 加密方式: " . config('mail.mailers.smtp.encryption') . "\n"; + echo " 发送邮箱: " . config('mail.from.address') . "\n"; + + // 验证配置不为空 + $this->assertNotEmpty(config('mail.default')); + $this->assertNotEmpty(config('mail.mailers.smtp.host')); + $this->assertNotEmpty(config('mail.from.address')); + } +} diff --git a/tests/Feature/VisitEmailSendTest.php b/tests/Feature/VisitEmailSendTest.php new file mode 100644 index 0000000..4dcdbef --- /dev/null +++ b/tests/Feature/VisitEmailSendTest.php @@ -0,0 +1,133 @@ +createTestData(); + } + + private function createTestData() + { + // 创建部门 + $department = Department::factory()->create([ + 'name' => '测试部门' + ]); + + // 创建访问区域 + $visitArea = VisitArea::factory()->create([ + 'name' => '测试区域' + ]); + + // 创建访问时段 + $visitTime = VisitTime::factory()->create([ + 'start_time' => '09:00:00', + 'end_time' => '12:00:00' + ]); + + // 创建被访管理员 + $this->acceptAdmin = Admin::factory()->create([ + 'name' => '被访管理员', + 'email' => '648753004@qq.com', // 测试邮箱 + 'department_id' => $department->id + ]); + + // 创建访客用户 + $this->visitor = User::factory()->create([ + 'nickname' => '测试访客', + 'mobile' => '13800138000' + ]); + } + + /** @test */ + public function test_visit_email_notification_sent() + { + // 模拟预约拜访数据 + $visitData = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + ]; + + // 使用Notification::fake()来捕获通知 + Notification::fake(); + + // 发送邮件通知 + $this->acceptAdmin->notify(new VisitEmailNotify($visitData)); + + // 验证通知是否发送 + Notification::assertSentTo( + $this->acceptAdmin, + VisitEmailNotify::class + ); + } + + /** @test */ + public function test_visit_email_content() + { + $visitData = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + ]; + + $notification = new VisitEmailNotify($visitData); + $mailMessage = $notification->toMail($this->acceptAdmin); + + // 验证邮件内容 + $this->assertStringContainsString('新的拜访预约通知 - 测试访客', $mailMessage->subject); + $this->assertStringContainsString('您好 被访管理员', $mailMessage->greeting); + $this->assertStringContainsString('访客 测试访客 预约于 2024-01-15 到访', $mailMessage->introLines[0]); + $this->assertStringContainsString('访客联系电话:13800138000', $mailMessage->introLines[1]); + $this->assertStringContainsString('拜访事由:业务洽谈', $mailMessage->introLines[2]); + $this->assertStringContainsString('查看详情', $mailMessage->actionText); + } + + /** @test */ + public function test_visit_email_notification_to_specific_email() + { + // 测试发送到指定邮箱 + $visitData = [ + 'name' => '测试访客', + 'date' => '2024-01-15', + 'mobile' => '13800138000', + 'reason' => '业务洽谈', + ]; + + // 使用Notification::fake()来捕获通知 + Notification::fake(); + + // 发送邮件通知到指定邮箱 + $this->acceptAdmin->notify(new VisitEmailNotify($visitData)); + + // 验证通知是否发送到正确的邮箱 + Notification::assertSentTo( + $this->acceptAdmin, + VisitEmailNotify::class + ); + + // 验证邮箱地址是否正确 + $this->assertEquals('648753004@qq.com', $this->acceptAdmin->email); + } +}