TermV3Test.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. use App\Models\AiModel;
  3. use App\Models\Channel;
  4. use App\Models\DhammaTerm;
  5. use App\Models\TermCategory;
  6. use App\Services\AuthService;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Str;
  10. uses(RefreshDatabase::class);
  11. /*
  12. * v3 术语:/v3/terms、/v3/channels/{c}/terms、/v3/studios/{s}/terms。
  13. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  14. */
  15. /** 在 channel 里经 API 建一条术语,返回响应 data */
  16. function newTerm(string $channel, string $as, array $body = []): array
  17. {
  18. return test()->postJson("/api/v3/channels/{$channel}/terms", $body + [
  19. 'word' => 'dhamma', 'meaning' => '法', 'note' => "正文第一段。\n\n第二段。{{category|法义}} {{category|基本教义}}",
  20. ], authHeader($as))->assertCreated()->json('data');
  21. }
  22. function termGrant(string $human, AiModel $model, string $channel, int $book = 0): string
  23. {
  24. return test()->postJson('/api/v3/agent-grants', [
  25. 'ai_model' => $model->uid, 'target_type' => 'channel', 'target_id' => $channel, 'book' => $book,
  26. ], authHeader($human))->assertCreated()->json('data.token');
  27. }
  28. function modelAuth(AiModel $model, ?string $grant = null): array
  29. {
  30. return array_filter([
  31. 'Authorization' => 'Bearer '.AuthService::getUserToken($model->uid),
  32. 'X-Agent-Grant' => $grant,
  33. ]);
  34. }
  35. /* ------------------------------------------------------------------ 建 */
  36. it('creates a term in a channel, extracting categories and defaulting to markdown-e2', function () {
  37. $owner = makeStudio('owner');
  38. $channel = makeChannel($owner);
  39. $data = newTerm($channel, $owner, ['tag' => '十七特征', 'quality' => 'draft', 'summary' => '一句话摘要。']);
  40. expect($data)
  41. ->word->toBe('dhamma')
  42. ->tag->toBe('十七特征')
  43. ->channel_id->toBe($channel)
  44. ->owner_id->toBe($owner)
  45. ->language->toBe('zh-Hans')
  46. ->content_type->toBe('markdown-e2')
  47. ->quality->toBe('draft')
  48. ->categories->toBe(['法义', '基本教义'])
  49. ->summary_text->toBe('一句话摘要。')
  50. ->co_editor->toBeNull()
  51. ->abilities->toBe(['update' => true, 'delete' => true]);
  52. expect($data['editor']['id'])->toBe($owner);
  53. expect($data['body'])->toBeArray()->not->toBeEmpty();
  54. expect(DhammaTerm::find($data['id']))->version->toBe(1)->editor_id->toBeGreaterThan(0);
  55. });
  56. it('never exposes the auto-increment id of term_categories', function () {
  57. $owner = makeStudio('owner');
  58. $channel = makeChannel($owner);
  59. $term = newTerm($channel, $owner);
  60. $ids = TermCategory::where('term_guid', $term['id'])->pluck('id')->all();
  61. expect($ids)->toHaveCount(2);
  62. $responses = [
  63. $this->getJson("/api/v3/terms/{$term['id']}")->json('data'),
  64. $this->getJson("/api/v3/channels/{$channel}/terms")->json('data.0'),
  65. $this->getJson('/api/v3/terms?word=dhamma&with_body=1')->json('data.0'),
  66. ];
  67. foreach ($responses as $data) {
  68. // 分类只以名字出现,没有对象、没有 id
  69. expect($data['categories'])->toBe(['法义', '基本教义']);
  70. foreach ($ids as $id) {
  71. expect(json_encode($data))->not->toMatch('/"id":\s*'.$id.'\b/');
  72. }
  73. }
  74. });
  75. it('rejects a duplicate word@tag in the same channel, treating an empty tag as none', function () {
  76. $owner = makeStudio('owner');
  77. $channel = makeChannel($owner);
  78. newTerm($channel, $owner);
  79. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'dhamma', 'tag' => '', 'meaning' => '又一个'], authHeader($owner))
  80. ->assertStatus(409)
  81. ->assertJsonPath('type', fn ($type) => str_contains($type, 'term-exists'));
  82. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'dhamma', 'tag' => '另一义', 'meaning' => '又一个'], authHeader($owner))
  83. ->assertCreated();
  84. });
  85. it('validates term input', function () {
  86. $owner = makeStudio('owner');
  87. $channel = makeChannel($owner);
  88. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x'], authHeader($owner))
  89. ->assertUnprocessable()->assertJsonValidationErrors('meaning');
  90. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x', 'meaning' => 'y', 'tag' => 'a@b'], authHeader($owner))
  91. ->assertUnprocessable()->assertJsonValidationErrors('tag');
  92. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x', 'meaning' => 'y', 'quality' => 'great'], authHeader($owner))
  93. ->assertUnprocessable()->assertJsonValidationErrors('quality');
  94. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x', 'meaning' => 'y', 'language' => 'en'], authHeader($owner))
  95. ->assertUnprocessable()->assertJsonValidationErrors('language');
  96. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x', 'meaning' => 'y', 'redirect' => 'x'], authHeader($owner))
  97. ->assertUnprocessable()->assertJsonValidationErrors('redirect');
  98. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x', 'meaning' => 'y'])->assertUnauthorized();
  99. });
  100. it('lets team editors and above create terms in a team channel', function () {
  101. [$team, $users] = makeTeam();
  102. $channel = makeChannel($team);
  103. foreach (['owner', 'manager', 'editor'] as $role) {
  104. newTerm($channel, $users[$role], ['word' => "w-{$role}"]);
  105. }
  106. foreach (['reader', 'outsider'] as $role) {
  107. $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'x', 'meaning' => 'y'], authHeader($users[$role]))
  108. ->assertForbidden();
  109. }
  110. expect(DhammaTerm::where('channal', $channel)->value('owner'))->toBe($team);
  111. });
  112. it('creates studio-level terms for owners and managers, and lets editors update them', function () {
  113. [$team, $users] = makeTeam();
  114. $body = ['word' => 'sīla', 'meaning' => '戒', 'language' => 'zh-Hans'];
  115. $this->postJson("/api/v3/studios/{$team}/terms", $body, authHeader($users['editor']))->assertForbidden();
  116. $created = $this->postJson("/api/v3/studios/{$team}/terms", $body, authHeader($users['manager']))
  117. ->assertCreated()
  118. ->assertJsonPath('data.channel_id', null)
  119. ->json('data');
  120. $this->patchJson("/api/v3/terms/{$created['id']}", ['meaning' => '戒律'], authHeader($users['editor']) + ['If-Match' => $created['etag']])
  121. ->assertOk()->assertJsonPath('data.meaning', '戒律');
  122. $this->patchJson("/api/v3/terms/{$created['id']}", ['meaning' => 'x'], authHeader($users['reader']) + ['If-Match' => 'x'])
  123. ->assertForbidden();
  124. });
  125. /* ------------------------------------------------------------------ 读 */
  126. it('shows a term with ETag, and hides terms of private channels', function () {
  127. $owner = makeStudio('owner');
  128. $channel = makeChannel($owner);
  129. $term = newTerm($channel, $owner);
  130. $res = $this->getJson("/api/v3/terms/{$term['id']}")->assertOk();
  131. expect($res->headers->get('ETag'))->toBe($term['etag']);
  132. expect($res->json('data.abilities'))->toBe(['update' => false, 'delete' => false]);
  133. expect($res->json('data.summary_text'))->toBe('正文第一段。');
  134. Channel::where('uid', $channel)->update(['status' => Channel::STATUS_PRIVATE]);
  135. $this->getJson("/api/v3/terms/{$term['id']}")->assertForbidden();
  136. $this->getJson("/api/v3/terms/{$term['id']}", authHeader($owner))->assertOk();
  137. $this->getJson('/api/v3/terms/'.Str::uuid())->assertNotFound();
  138. });
  139. it('takes the first natural paragraph of the note as summary_text, in both dialects', function (string $contentType) {
  140. $owner = makeStudio('owner');
  141. $channel = makeChannel($owner);
  142. $note = "## 标题\n\n{{toc}}\n\n{{ai}}\n第一段第一行,\n第一段第二行。\n\n```\n围栏\n\n里的空行\n```\n\n第二段。";
  143. $term = newTerm($channel, $owner, ['note' => $note, 'content_type' => $contentType]);
  144. $text = $this->getJson("/api/v3/terms/{$term['id']}")->assertOk()->json('data.summary_text');
  145. expect($text)->toContain('第一段第一行')->toContain('第一段第二行')
  146. ->not->toContain('AI')->not->toContain('标题')->not->toContain('第二段');
  147. $listed = $this->getJson("/api/v3/terms?word={$term['word']}")->assertOk()->json('data.0.summary_text');
  148. expect($listed)->toBe($text);
  149. })->with(['markdown', 'markdown-e2']);
  150. it('treats legacy studio-level terms with an empty-string channal like null ones', function () {
  151. [$team, $users] = makeTeam();
  152. $created = $this->postJson("/api/v3/studios/{$team}/terms", ['word' => 'legacy', 'meaning' => '老', 'language' => 'zh-Hans'], authHeader($users['owner']))
  153. ->assertCreated()->json('data');
  154. newTerm(makeChannel($team), $users['owner'], ['word' => 'in-channel']);
  155. DhammaTerm::where('guid', $created['id'])->update(['channal' => '']);
  156. $this->getJson("/api/v3/terms/{$created['id']}")
  157. ->assertOk()->assertJsonPath('data.channel_id', null)->assertJsonPath('data.channel', null);
  158. $this->getJson("/api/v3/studios/{$team}/terms?order=word", authHeader($users['reader']))
  159. ->assertOk()->assertJsonPath('data.*.word', ['in-channel', 'legacy']);
  160. $this->getJson("/api/v3/studios/{$team}/terms?general=true", authHeader($users['reader']))
  161. ->assertJsonPath('data.*.word', ['legacy']);
  162. $this->getJson('/api/v3/terms?word=legacy')->assertOk()->assertJsonPath('data.*.id', [$created['id']]);
  163. $this->postJson("/api/v3/studios/{$team}/terms", ['word' => 'legacy', 'meaning' => '重', 'language' => 'zh-Hans'], authHeader($users['owner']))
  164. ->assertConflict();
  165. });
  166. it('renders the body against the requested channels, ignoring those the viewer cannot see', function () {
  167. $owner = makeStudio('owner');
  168. $home = makeChannel($owner, 'home');
  169. $other = makeChannel($owner, 'other');
  170. $private = makeChannel($owner, 'private');
  171. Channel::where('uid', $private)->update(['status' => Channel::STATUS_PRIVATE]);
  172. newTerm($home, $owner, ['word' => 'citta', 'meaning' => '本版心']);
  173. newTerm($other, $owner, ['word' => 'citta', 'meaning' => '他版心']);
  174. newTerm($private, $owner, ['word' => 'citta', 'meaning' => '私有心']);
  175. $term = newTerm($home, $owner, ['word' => 'host', 'note' => '见[[citta]]。']);
  176. $text = fn (array $res) => json_encode($res['data']['body'], JSON_UNESCAPED_UNICODE);
  177. expect($text($this->getJson("/api/v3/terms/{$term['id']}")->json()))->toContain('本版心');
  178. expect($text($this->getJson("/api/v3/terms/{$term['id']}?channels={$other}")->json()))->toContain('他版心');
  179. // 看不见的 channel 被忽略,退回术语所在 channel
  180. expect($text($this->getJson("/api/v3/terms/{$term['id']}?channels={$private}")->json()))->toContain('本版心');
  181. expect($text($this->getJson("/api/v3/terms/{$term['id']}?channels={$private}", authHeader($owner))->json()))->toContain('私有心');
  182. $this->getJson("/api/v3/terms/{$term['id']}?mode=edit")->assertOk();
  183. // format=html:正文换成新渲染器的 HTML 字符串
  184. $html = $this->getJson("/api/v3/terms/{$term['id']}?format=html")->assertOk()->json('data');
  185. expect($html)->not->toHaveKey('body');
  186. expect($html['html'])->toContain('class="term-ref"')->toContain('本版心');
  187. $this->getJson("/api/v3/terms/{$term['id']}?format=pdf")->assertUnprocessable()->assertJsonValidationErrors('format');
  188. $this->getJson("/api/v3/terms/{$term['id']}?mode=write")->assertUnprocessable()->assertJsonValidationErrors('mode');
  189. $this->getJson("/api/v3/terms/{$term['id']}?channels=nope")->assertUnprocessable()->assertJsonValidationErrors('channels');
  190. });
  191. it('lists the terms of a channel with filters', function () {
  192. $owner = makeStudio('owner');
  193. $channel = makeChannel($owner);
  194. newTerm($channel, $owner, ['word' => 'a', 'quality' => 'featured']);
  195. newTerm($channel, $owner, ['word' => 'b', 'note' => '{{category|律学}}']);
  196. $this->getJson("/api/v3/channels/{$channel}/terms?order=word")
  197. ->assertOk()->assertJsonPath('meta.total', 2)->assertJsonPath('data.*.word', ['a', 'b']);
  198. $this->getJson("/api/v3/channels/{$channel}/terms?quality=featured")->assertJsonPath('data.*.word', ['a']);
  199. $this->getJson("/api/v3/channels/{$channel}/terms?quality=pending")->assertJsonPath('data.*.word', ['b']);
  200. $this->getJson("/api/v3/channels/{$channel}/terms?category=".urlencode('律学'))->assertJsonPath('data.*.word', ['b']);
  201. $this->getJson("/api/v3/channels/{$channel}/terms?search=A")->assertJsonPath('data.*.word', ['a']);
  202. $this->getJson("/api/v3/channels/{$channel}/terms?quality=great")->assertUnprocessable();
  203. Channel::where('uid', $channel)->update(['status' => Channel::STATUS_PRIVATE]);
  204. $this->getJson("/api/v3/channels/{$channel}/terms")->assertForbidden();
  205. });
  206. it('lists the terms of a studio for its members only', function () {
  207. [$team, $users] = makeTeam();
  208. newTerm(makeChannel($team), $users['owner'], ['word' => 'in-channel']);
  209. $this->postJson("/api/v3/studios/{$team}/terms", ['word' => 'general', 'meaning' => 'g', 'language' => 'zh-Hans'], authHeader($users['owner']))->assertCreated();
  210. $this->getJson("/api/v3/studios/{$team}/terms?order=word", authHeader($users['reader']))
  211. ->assertOk()->assertJsonPath('data.*.word', ['general', 'in-channel']);
  212. $this->getJson("/api/v3/studios/{$team}/terms?general=1", authHeader($users['reader']))
  213. ->assertJsonPath('data.*.word', ['general']);
  214. $this->getJson("/api/v3/studios/{$team}/terms?general=true", authHeader($users['reader']))
  215. ->assertJsonPath('data.*.word', ['general']);
  216. $this->getJson("/api/v3/studios/{$team}/terms?general=false&order=word", authHeader($users['reader']))
  217. ->assertJsonPath('data.*.word', ['general', 'in-channel']);
  218. $this->getJson("/api/v3/studios/{$team}/terms", authHeader($users['outsider']))->assertForbidden();
  219. $this->getJson("/api/v3/studios/{$team}/terms")->assertUnauthorized();
  220. });
  221. it('looks terms up by word across visible channels only', function () {
  222. $owner = makeStudio('owner');
  223. $public = makeChannel($owner, 'public');
  224. $private = makeChannel($owner, 'private');
  225. Channel::where('uid', $private)->update(['status' => Channel::STATUS_PRIVATE]);
  226. newTerm($public, $owner, ['word' => 'citta', 'meaning' => '心']);
  227. newTerm($private, $owner, ['word' => 'citta', 'meaning' => '心(私有)']);
  228. newTerm($public, $owner, ['word' => 'citta', 'tag' => '十七特征', 'meaning' => '心(特征)']);
  229. $this->getJson('/api/v3/terms?word=citta')->assertOk()->assertJsonPath('meta.total', 2);
  230. $this->getJson('/api/v3/terms?word=citta', authHeader($owner))->assertJsonPath('meta.total', 3);
  231. $this->getJson('/api/v3/terms?word=citta&tag='.urlencode('十七特征'))->assertJsonPath('data.*.meaning', ['心(特征)']);
  232. $this->getJson('/api/v3/terms?word=citta&tag=')->assertJsonPath('data.*.meaning', ['心']);
  233. $this->getJson('/api/v3/terms')->assertUnprocessable()->assertJsonValidationErrors('word');
  234. // 默认不带正文;with_body 时每条都带,并带上 channel 名字
  235. expect($this->getJson('/api/v3/terms?word=citta')->json('data.0'))->not->toHaveKey('body');
  236. // 查询串里的 true / false(v6 发的就是它们),不能 422,false 也不能当真
  237. expect($this->getJson('/api/v3/terms?word=citta&with_body=false')->assertOk()->json('data.0'))->not->toHaveKey('body');
  238. $this->getJson('/api/v3/terms?word=citta&with_body=true')->assertOk()->assertJsonPath('data.0.body', fn ($body) => is_array($body) && $body !== []);
  239. $this->getJson('/api/v3/terms?word=citta&with_body=1')
  240. ->assertJsonPath('data.0.body', fn ($body) => is_array($body) && $body !== [])
  241. ->assertJsonPath('data.0.channel.name', 'public');
  242. });
  243. it('loads a page of terms with a constant number of queries', function () {
  244. $owner = makeStudio('owner');
  245. $count = function (int $n) use ($owner) {
  246. $channel = makeChannel($owner, "c{$n}");
  247. foreach (range(1, $n) as $i) {
  248. newTerm($channel, $owner, ['word' => "w{$i}"]);
  249. }
  250. DB::flushQueryLog();
  251. DB::enableQueryLog();
  252. $this->getJson("/api/v3/channels/{$channel}/terms", authHeader($owner))->assertOk();
  253. $queries = count(DB::getQueryLog());
  254. DB::disableQueryLog();
  255. return $queries;
  256. };
  257. expect($count(8))->toBe($count(2));
  258. });
  259. /* ------------------------------------------------------------------ 改:乐观锁、word/tag 不可改 */
  260. it('requires If-Match, rejects a stale one, and bumps the version', function () {
  261. $owner = makeStudio('owner');
  262. $term = newTerm(makeChannel($owner), $owner);
  263. $url = "/api/v3/terms/{$term['id']}";
  264. $this->patchJson($url, ['meaning' => '新释义'], authHeader($owner))->assertStatus(428);
  265. $this->patchJson($url, ['meaning' => '新释义'], authHeader($owner) + ['If-Match' => '"9.9"'])
  266. ->assertStatus(412)->assertJsonPath('etag', $term['etag']);
  267. $updated = $this->patchJson($url, ['meaning' => '新释义'], authHeader($owner) + ['If-Match' => $term['etag']])
  268. ->assertOk()->assertJsonPath('data.meaning', '新释义')->json('data');
  269. expect($updated['etag'])->not->toBe($term['etag']);
  270. expect(DhammaTerm::find($term['id'])->version)->toBe(2);
  271. // 用旧的 etag 再改一次:412
  272. $this->patchJson($url, ['meaning' => '再改'], authHeader($owner) + ['If-Match' => $term['etag']])->assertStatus(412);
  273. });
  274. it('notices changes made through v2, which only touch modify_time', function () {
  275. $owner = makeStudio('owner');
  276. $term = newTerm(makeChannel($owner), $owner);
  277. DhammaTerm::where('guid', $term['id'])->update(['modify_time' => DB::raw('modify_time + 1'), 'meaning' => 'v2 改的']);
  278. $this->patchJson("/api/v3/terms/{$term['id']}", ['meaning' => 'v3'], authHeader($owner) + ['If-Match' => $term['etag']])
  279. ->assertStatus(412);
  280. });
  281. it('never lets word or tag change, and re-extracts categories when the note changes', function () {
  282. $owner = makeStudio('owner');
  283. $term = newTerm(makeChannel($owner), $owner);
  284. $url = "/api/v3/terms/{$term['id']}";
  285. $this->patchJson($url, ['word' => 'other'], authHeader($owner) + ['If-Match' => $term['etag']])
  286. ->assertUnprocessable()->assertJsonValidationErrors('word');
  287. $this->patchJson($url, ['tag' => 'x'], authHeader($owner) + ['If-Match' => $term['etag']])
  288. ->assertUnprocessable()->assertJsonValidationErrors('tag');
  289. $this->patchJson($url, ['note' => '{{category|律学}}', 'redirect' => 'dhamma@新义'], authHeader($owner) + ['If-Match' => $term['etag']])
  290. ->assertOk()
  291. ->assertJsonPath('data.categories', ['律学'])
  292. ->assertJsonPath('data.redirect', 'dhamma@新义');
  293. expect(TermCategory::where('term_guid', $term['id'])->pluck('category')->all())->toBe(['律学']);
  294. });
  295. it('keeps the language of a channel term tied to the channel', function () {
  296. $owner = makeStudio('owner');
  297. $term = newTerm(makeChannel($owner), $owner);
  298. $this->patchJson("/api/v3/terms/{$term['id']}", ['language' => 'en'], authHeader($owner) + ['If-Match' => $term['etag']])
  299. ->assertUnprocessable()->assertJsonValidationErrors('language');
  300. });
  301. /* ------------------------------------------------------------------ agent 代写 */
  302. it('records the grantor as editor and the model as co-editor, and clears it when a person edits', function () {
  303. $human = makeStudio('owner');
  304. $channel = makeChannel($human);
  305. $model = AiModel::factory()->ownedBy($human)->create();
  306. $grant = termGrant($human, $model, $channel);
  307. $term = $this->postJson("/api/v3/channels/{$channel}/terms", ['word' => 'kamma', 'meaning' => '业'], modelAuth($model, $grant))
  308. ->assertCreated()
  309. ->assertJsonPath('data.editor.id', $human)
  310. ->assertJsonPath('data.co_editor.id', $model->uid)
  311. ->json('data');
  312. $edited = $this->patchJson("/api/v3/terms/{$term['id']}", ['meaning' => '业力'], modelAuth($model, $grant) + ['If-Match' => $term['etag']])
  313. ->assertOk()->assertJsonPath('data.co_editor.id', $model->uid)->json('data');
  314. $this->patchJson("/api/v3/terms/{$term['id']}", ['meaning' => '業'], authHeader($human) + ['If-Match' => $edited['etag']])
  315. ->assertOk()->assertJsonPath('data.co_editor', null);
  316. });
  317. it('rejects agent writes without a covering grant, studio-level agent writes, and agent deletes', function () {
  318. $human = makeStudio('owner');
  319. $channel = makeChannel($human);
  320. $other = makeChannel($human, 'other');
  321. $model = AiModel::factory()->ownedBy($human)->create();
  322. $body = ['word' => 'kamma', 'meaning' => '业'];
  323. $this->postJson("/api/v3/channels/{$channel}/terms", $body, modelAuth($model))->assertForbidden();
  324. $this->postJson("/api/v3/channels/{$channel}/terms", $body, modelAuth($model, termGrant($human, $model, $other)))->assertForbidden();
  325. // 按 book 限定的授权不覆盖术语(术语不属于某本书)
  326. $this->postJson("/api/v3/channels/{$channel}/terms", $body, modelAuth($model, termGrant($human, $model, $channel, 3)))->assertForbidden();
  327. $this->postJson("/api/v3/studios/{$human}/terms", $body + ['language' => 'zh-Hans'], modelAuth($model, termGrant($human, $model, $channel)))
  328. ->assertForbidden();
  329. $term = newTerm($channel, $human);
  330. $this->deleteJson("/api/v3/terms/{$term['id']}", [], modelAuth($model))->assertForbidden();
  331. });
  332. /* ------------------------------------------------------------------ 删 */
  333. it('soft deletes for the owner or team owner only', function () {
  334. [$team, $users] = makeTeam();
  335. $term = newTerm(makeChannel($team), $users['editor']);
  336. $url = "/api/v3/terms/{$term['id']}";
  337. $this->deleteJson($url, [], authHeader($users['editor']))->assertForbidden();
  338. $this->deleteJson($url, [], authHeader($users['manager']))->assertForbidden();
  339. $this->deleteJson($url, [], authHeader($users['owner']))
  340. ->assertOk()->assertJsonPath('data.id', $term['id'])
  341. ->assertJsonPath('data.deleted_at', fn ($at) => $at !== null);
  342. $this->assertSoftDeleted('dhamma_terms', ['guid' => $term['id']]);
  343. $this->getJson($url)->assertNotFound();
  344. });