TipitakaChannelV3Test.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. use App\Models\Channel;
  3. use App\Models\ProgressChapter;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Support\Str;
  6. uses(RefreshDatabase::class);
  7. /**
  8. * 阅读领域的 channel 查询:
  9. * GET /api/v3/tipitaka-paragraph-channels/{book}/{para} 某段有内容的 channel(取代 channel?view=paragraphs)
  10. * GET /api/v3/tipitaka-channels 有译文的公开 channel 目录(取代 progress?view=channel)
  11. * GET /api/v3/tipitaka-chapter-channels/{book}/{para} (已有)这里只补团队名下私有 channel 的可见性
  12. *
  13. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  14. */
  15. function chapterProgress(string $channel, int $book, int $para, float $progress, string $lang = 'zh'): void
  16. {
  17. (new ProgressChapter)->forceFill([
  18. 'uid' => (string) Str::uuid(), 'book' => $book, 'para' => $para, 'channel_id' => $channel,
  19. 'lang' => $lang, 'all_trans' => 100, 'public' => 100, 'progress' => $progress,
  20. ])->save();
  21. }
  22. function hiddenChannel(string $ownerUid, string $name): string
  23. {
  24. $uid = makeChannel($ownerUid, $name);
  25. Channel::where('uid', $uid)->update(['status' => Channel::STATUS_PRIVATE]);
  26. return $uid;
  27. }
  28. /* ---------------- 某段有内容的 channel ---------------- */
  29. it('lists channels with content in a paragraph, honouring visibility', function () {
  30. [$team, $users] = makeTeam();
  31. $public = makeChannel(makeStudio('a'), 'public');
  32. $teamPrivate = hiddenChannel($team, 'team private');
  33. $otherPrivate = hiddenChannel(makeStudio('b'), 'other private');
  34. $elsewhere = makeChannel(makeStudio('c'), 'elsewhere');
  35. foreach ([$public, $teamPrivate, $otherPrivate] as $channel) {
  36. makeSentence($channel, 5, 7, 1, 'text');
  37. }
  38. makeSentence($elsewhere, 5, 8, 1, 'text');
  39. $ids = fn (array $headers = []) => array_column(
  40. $this->getJson('/api/v3/tipitaka-paragraph-channels/5/7', $headers)->assertOk()->json('data'),
  41. 'id'
  42. );
  43. expect($ids())->toBe([$public]);
  44. expect($ids(authHeader($users['reader'])))->toEqualCanonicalizing([$public, $teamPrivate]);
  45. });
  46. it('returns 404 for a non-numeric paragraph', function () {
  47. $this->getJson('/api/v3/tipitaka-paragraph-channels/5/abc')->assertNotFound();
  48. });
  49. /* ---------------- 有译文的公开 channel 目录 ---------------- */
  50. it('lists public channels with translated chapters, most chapters first', function () {
  51. $owner = makeStudio('owner');
  52. $big = makeChannel($owner, 'big');
  53. $small = makeChannel($owner, 'small');
  54. $hidden = hiddenChannel($owner, 'hidden');
  55. $english = makeChannel($owner, 'english');
  56. foreach ([1, 2, 3] as $para) {
  57. chapterProgress($big, 1, $para, 0.95);
  58. }
  59. chapterProgress($small, 1, 1, 0.9);
  60. chapterProgress($small, 1, 2, 0.5); // 没过门槛
  61. chapterProgress($hidden, 1, 1, 1.0);
  62. chapterProgress($english, 1, 1, 1.0, 'en');
  63. $data = $this->getJson('/api/v3/tipitaka-channels?lang=zh&type=translation')->assertOk()->json('data');
  64. expect(array_column($data, 'id'))->toBe([$big, $small]);
  65. expect(array_column($data, 'chapter_count'))->toBe([3, 1]);
  66. expect($data[0]['owner']['id'])->toBe($owner);
  67. // 门槛可调
  68. expect(array_column($this->getJson('/api/v3/tipitaka-channels?lang=zh&min_progress=0.4')->json('data'), 'chapter_count'))
  69. ->toBe([3, 2]);
  70. });
  71. it('validates the progress threshold', function () {
  72. $this->getJson('/api/v3/tipitaka-channels?min_progress=2')
  73. ->assertUnprocessable()
  74. ->assertJsonValidationErrors('min_progress');
  75. });
  76. /* ---------------- 某章的 channel:团队名下的私有 channel ---------------- */
  77. it('shows team-owned private channels of a chapter to team members only', function () {
  78. [$team, $users] = makeTeam();
  79. $teamPrivate = hiddenChannel($team, 'team private');
  80. chapterProgress($teamPrivate, 9, 9, 0.5);
  81. $ids = fn (array $headers = []) => array_column(
  82. $this->getJson('/api/v3/tipitaka-chapter-channels/9/9', $headers)->assertOk()->json('data'),
  83. 'channel'
  84. );
  85. expect($ids())->toBe([]);
  86. expect($ids(authHeader($users['outsider'])))->toBe([]);
  87. expect(array_column($ids(authHeader($users['reader'])), 'id'))->toBe([$teamPrivate]);
  88. });
  89. it('keeps system channels out of a chapter\'s version list', function () {
  90. $pali = makeChannel(makeStudio('root'), '_System_Pali_VRI_');
  91. Channel::where('uid', $pali)->update(['is_system' => true, 'status' => Channel::STATUS_PRIVATE]);
  92. chapterProgress($pali, 9, 9, 1.0);
  93. expect($this->getJson('/api/v3/tipitaka-chapter-channels/9/9')->json('data'))->toBe([]);
  94. });
  95. it('drops soft-deleted channels from a chapter', function () {
  96. $owner = makeStudio('owner');
  97. $channel = makeChannel($owner, 'gone');
  98. chapterProgress($channel, 9, 9, 0.5);
  99. Channel::where('uid', $channel)->first()->delete();
  100. expect($this->getJson('/api/v3/tipitaka-chapter-channels/9/9')->json('data'))->toBe([]);
  101. });