ChannelLinkV3Test.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. use App\Models\AiModel;
  3. use App\Models\Channel;
  4. use App\Models\ChannelLink;
  5. use App\Services\AuthService;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Str;
  9. uses(RefreshDatabase::class);
  10. /*
  11. * GET / PATCH /v3/channels/{channel}/links:channel 之间的关系设置。
  12. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  13. */
  14. function linksUrl(string $channel): string
  15. {
  16. return "/api/v3/channels/{$channel}/links";
  17. }
  18. function setChannel(string $uid, array $attrs): void
  19. {
  20. Channel::where('uid', $uid)->update($attrs);
  21. }
  22. it('saves all four kinds and returns them in order', function () {
  23. $owner = makeStudio('owner');
  24. $channel = makeChannel($owner, 'en');
  25. $zh = makeChannel($owner, 'zh');
  26. $wbw = makeChannel($owner, 'wbw');
  27. setChannel($wbw, ['type' => 'wbw']);
  28. [$a, $b, $c] = [makeChannel($owner, 'a'), makeChannel($owner, 'b'), makeChannel($owner, 'c')];
  29. $this->patchJson(linksUrl($channel), [
  30. 'derived_from' => $zh, 'wbw' => $wbw, 'fallback' => [$b, $a], 'term_source' => [$c, $zh],
  31. ], authHeader($owner))
  32. ->assertOk()
  33. ->assertJsonPath('data.channel_id', $channel)
  34. ->assertJsonPath('data.derived_from.id', $zh)
  35. ->assertJsonPath('data.derived_from.name', 'zh')
  36. ->assertJsonPath('data.wbw.type', 'wbw')
  37. ->assertJsonPath('data.fallback.*.id', [$b, $a])
  38. ->assertJsonPath('data.term_source.*.id', [$c, $zh])
  39. ->assertJsonPath('data.can_update', true);
  40. $this->getJson(linksUrl($channel))
  41. ->assertOk()
  42. ->assertJsonPath('data.fallback.*.id', [$b, $a])
  43. ->assertJsonPath('data.can_update', false);
  44. });
  45. it('returns empty settings for a channel without links', function () {
  46. $channel = makeChannel(makeStudio('owner'));
  47. $this->getJson(linksUrl($channel))
  48. ->assertOk()
  49. ->assertJsonPath('data.derived_from', null)
  50. ->assertJsonPath('data.wbw', null)
  51. ->assertJsonPath('data.fallback', [])
  52. ->assertJsonPath('data.term_source', []);
  53. });
  54. it('replaces only the kinds that were sent, and clears with null or []', function () {
  55. $owner = makeStudio('owner');
  56. $channel = makeChannel($owner);
  57. [$a, $b] = [makeChannel($owner, 'a'), makeChannel($owner, 'b')];
  58. $this->patchJson(linksUrl($channel), ['derived_from' => $a, 'fallback' => [$a, $b]], authHeader($owner))->assertOk();
  59. $this->patchJson(linksUrl($channel), ['fallback' => [$b]], authHeader($owner))
  60. ->assertJsonPath('data.derived_from.id', $a)
  61. ->assertJsonPath('data.fallback.*.id', [$b]);
  62. $this->patchJson(linksUrl($channel), ['derived_from' => null, 'fallback' => []], authHeader($owner))
  63. ->assertJsonPath('data.derived_from', null)
  64. ->assertJsonPath('data.fallback', []);
  65. expect(ChannelLink::where('channel_uid', $channel)->count())->toBe(0);
  66. });
  67. it('hides targets that were deleted or that the viewer cannot see', function () {
  68. $owner = makeStudio('owner');
  69. $channel = makeChannel($owner);
  70. [$private, $gone, $public] = [makeChannel($owner, 'private'), makeChannel($owner, 'gone'), makeChannel($owner, 'public')];
  71. $this->patchJson(linksUrl($channel), ['term_source' => [$private, $gone, $public]], authHeader($owner))->assertOk();
  72. setChannel($private, ['status' => Channel::STATUS_PRIVATE]);
  73. Channel::where('uid', $gone)->first()->delete();
  74. $this->getJson(linksUrl($channel))->assertJsonPath('data.term_source.*.id', [$public]);
  75. $this->getJson(linksUrl($channel), authHeader($owner))->assertJsonPath('data.term_source.*.id', [$private, $public]);
  76. });
  77. it('lets only those who can update the channel change its links', function () {
  78. [$team, $users] = makeTeam();
  79. $channel = makeChannel($team);
  80. $target = makeChannel($team, 'target');
  81. $this->patchJson(linksUrl($channel), ['derived_from' => $target])->assertUnauthorized();
  82. foreach (['owner', 'manager', 'editor'] as $role) {
  83. $this->patchJson(linksUrl($channel), ['derived_from' => $target], authHeader($users[$role]))->assertOk();
  84. }
  85. foreach (['reader', 'outsider'] as $role) {
  86. $this->patchJson(linksUrl($channel), ['derived_from' => $target], authHeader($users[$role]))->assertForbidden();
  87. }
  88. $model = AiModel::factory()->ownedBy($users['owner'])->create();
  89. $this->patchJson(linksUrl($channel), ['derived_from' => $target], [
  90. 'Authorization' => 'Bearer '.AuthService::getUserToken($model->uid),
  91. ])->assertForbidden();
  92. });
  93. it('hides the settings of a private channel from those who cannot see it', function () {
  94. $owner = makeStudio('owner');
  95. $channel = makeChannel($owner);
  96. setChannel($channel, ['status' => Channel::STATUS_PRIVATE]);
  97. $this->getJson(linksUrl($channel))->assertForbidden();
  98. $this->getJson(linksUrl($channel), authHeader(makeStudio('stranger')))->assertForbidden();
  99. $this->getJson(linksUrl($channel), authHeader($owner))->assertOk();
  100. $this->getJson(linksUrl((string) Str::uuid()))->assertNotFound();
  101. });
  102. it('rejects invalid targets with field errors', function () {
  103. $owner = makeStudio('owner');
  104. $channel = makeChannel($owner);
  105. $notWbw = makeChannel($owner, 'translation');
  106. $hidden = makeChannel(makeStudio('other'), 'hidden');
  107. setChannel($hidden, ['status' => Channel::STATUS_PRIVATE]);
  108. $headers = authHeader($owner);
  109. $this->patchJson(linksUrl($channel), ['derived_from' => $channel], $headers)
  110. ->assertUnprocessable()->assertJsonValidationErrors('derived_from');
  111. $this->patchJson(linksUrl($channel), ['fallback' => [$notWbw, $hidden]], $headers)
  112. ->assertUnprocessable()->assertJsonValidationErrors('fallback.1');
  113. $this->patchJson(linksUrl($channel), ['term_source' => [(string) Str::uuid()]], $headers)
  114. ->assertUnprocessable()->assertJsonValidationErrors('term_source.0');
  115. $this->patchJson(linksUrl($channel), ['wbw' => $notWbw], $headers)
  116. ->assertUnprocessable()->assertJsonValidationErrors('wbw');
  117. expect(ChannelLink::count())->toBe(0);
  118. });
  119. it('requires fallback channels of the same type as the channel', function () {
  120. $owner = makeStudio('owner');
  121. $channel = makeChannel($owner); // translation
  122. $same = makeChannel($owner, 'same');
  123. $nissaya = makeChannel($owner, 'nissaya');
  124. setChannel($nissaya, ['type' => 'nissaya']);
  125. $this->patchJson(linksUrl($channel), ['fallback' => [$same, $nissaya]], authHeader($owner))
  126. ->assertUnprocessable()
  127. ->assertJsonValidationErrors('fallback.1')
  128. ->assertJsonMissingValidationErrors('fallback.0');
  129. $other = makeChannel($owner, 'nissaya-2');
  130. setChannel($other, ['type' => 'nissaya']);
  131. $this->patchJson(linksUrl($nissaya), ['fallback' => [$other]], authHeader($owner))
  132. ->assertOk()
  133. ->assertJsonPath('data.fallback.*.id', [$other]);
  134. });
  135. it('allows term sources only on translation channels, and only translation targets', function () {
  136. $owner = makeStudio('owner');
  137. $channel = makeChannel($owner); // translation
  138. $translation = makeChannel($owner, 'translation');
  139. $nissaya = makeChannel($owner, 'nissaya');
  140. setChannel($nissaya, ['type' => 'nissaya']);
  141. $this->patchJson(linksUrl($channel), ['term_source' => [$translation, $nissaya]], authHeader($owner))
  142. ->assertUnprocessable()
  143. ->assertJsonValidationErrors('term_source.1')
  144. ->assertJsonMissingValidationErrors('term_source.0');
  145. // 非 translation 的 channel 不能设术语路径,但清空总是允许
  146. $this->patchJson(linksUrl($nissaya), ['term_source' => [$translation]], authHeader($owner))
  147. ->assertUnprocessable()
  148. ->assertJsonValidationErrors('term_source');
  149. $this->patchJson(linksUrl($nissaya), ['term_source' => []], authHeader($owner))->assertOk();
  150. expect(ChannelLink::count())->toBe(0);
  151. });
  152. it('validates the shape of the input', function () {
  153. $owner = makeStudio('owner');
  154. $channel = makeChannel($owner);
  155. $a = makeChannel($owner, 'a');
  156. $many = array_map(fn ($i) => makeChannel($owner, "c{$i}"), range(1, 4));
  157. $this->patchJson(linksUrl($channel), ['derived_from' => 'nope'], authHeader($owner))
  158. ->assertUnprocessable()->assertJsonValidationErrors('derived_from');
  159. $this->patchJson(linksUrl($channel), ['fallback' => $many], authHeader($owner))
  160. ->assertUnprocessable()->assertJsonValidationErrors('fallback');
  161. $this->patchJson(linksUrl($channel), ['term_source' => [$a, $a]], authHeader($owner))
  162. ->assertUnprocessable()->assertJsonValidationErrors('term_source.0');
  163. });
  164. it('never exposes the auto-increment id of channel_links', function () {
  165. $owner = makeStudio('owner');
  166. $channel = makeChannel($owner);
  167. [$a, $b] = [makeChannel($owner, 'a'), makeChannel($owner, 'b')];
  168. $patched = $this->patchJson(linksUrl($channel), ['derived_from' => $a, 'fallback' => [$b], 'term_source' => [$a, $b]], authHeader($owner))
  169. ->assertOk();
  170. $shown = $this->getJson(linksUrl($channel), authHeader($owner))->assertOk();
  171. $rowIds = ChannelLink::where('channel_uid', $channel)->pluck('id')->all();
  172. expect($rowIds)->toHaveCount(4);
  173. foreach ([$patched, $shown] as $res) {
  174. $data = $res->json('data');
  175. expect(array_keys($data))->toBe(['channel_id', 'derived_from', 'wbw', 'fallback', 'term_source', 'can_update']);
  176. $ids = [$data['channel_id'], $data['derived_from']['id'], ...array_column($data['fallback'], 'id'), ...array_column($data['term_source'], 'id')];
  177. foreach ($ids as $id) {
  178. expect(Str::isUuid($id))->toBeTrue();
  179. }
  180. foreach ([$data['derived_from'], ...$data['fallback'], ...$data['term_source']] as $ref) {
  181. expect(array_keys($ref))->toBe(['id', 'name', 'type', 'lang', 'status']);
  182. }
  183. expect(array_intersect($rowIds, $ids))->toBe([]);
  184. }
  185. });
  186. it('reads the settings in a constant number of queries', function () {
  187. $owner = makeStudio('owner');
  188. $count = function (int $targets) use ($owner) {
  189. $channel = makeChannel($owner);
  190. $uids = array_map(fn ($i) => makeChannel($owner, "t{$targets}-{$i}"), range(1, $targets));
  191. $this->patchJson(linksUrl($channel), ['term_source' => $uids], authHeader($owner))->assertOk();
  192. DB::flushQueryLog();
  193. DB::enableQueryLog();
  194. $this->getJson(linksUrl($channel), authHeader($owner))->assertOk();
  195. $n = count(DB::getQueryLog());
  196. DB::disableQueryLog();
  197. return $n;
  198. };
  199. expect($count(8))->toBe($count(2));
  200. });