MarkupPreviewV3Test.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. use App\Models\Channel;
  3. use App\Models\DhammaTerm;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Support\Str;
  6. uses(RefreshDatabase::class);
  7. /*
  8. * POST /v3/markup-previews:把还没保存的源码渲染成节点树(模板生成器的预览)。
  9. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  10. */
  11. beforeEach(function () {
  12. $this->me = makeStudio('preview-me');
  13. $this->channel = makeChannel($this->me, 'preview channel');
  14. });
  15. it('renders the source into a node tree, resolving templates against the given channel', function () {
  16. (new DhammaTerm)->forceFill([
  17. 'id' => random_int(1, PHP_INT_MAX), 'guid' => (string) Str::uuid(), 'word' => 'dhamma', 'word_en' => 'dhamma',
  18. 'meaning' => '法', 'language' => 'zh-Hans', 'channal' => $this->channel, 'owner' => $this->me,
  19. 'editor_id' => 0, 'create_time' => time() * 1000, 'modify_time' => time() * 1000,
  20. ])->save();
  21. $body = $this->postJson('/api/v3/markup-previews', [
  22. 'content' => '{{term|dhamma}}',
  23. 'channels' => [$this->channel],
  24. ], authHeader($this->me))->assertOk()->json('data.body');
  25. expect($body[0])->toMatchArray(['type' => 'tpl', 'name' => 'term'])
  26. ->and($body[0]['props'])->toMatchArray(['word' => 'dhamma', 'meaning' => '法', 'channel' => $this->channel]);
  27. });
  28. it('renders without channels, and keeps unknown templates as their source', function () {
  29. $body = $this->postJson('/api/v3/markup-previews', [
  30. 'content' => "{{v|url=https://example.com/a.mp4|style=card|title=视频}}\n\n{{nope|x}}",
  31. 'content_type' => 'markdown-e2',
  32. ], authHeader($this->me))->assertOk()->json('data.body');
  33. expect(json_encode($body))->toContain('"name":"video"')->toContain('"name":"unknown"');
  34. });
  35. it('requires login', function () {
  36. $this->postJson('/api/v3/markup-previews', ['content' => 'x'])->assertUnauthorized();
  37. });
  38. it('rejects a missing content, a bad content type and a non-uuid channel', function () {
  39. $this->postJson('/api/v3/markup-previews', [
  40. 'content_type' => 'html', 'channels' => ['nope'],
  41. ], authHeader($this->me))->assertUnprocessable()->assertJsonValidationErrors(['content', 'content_type', 'channels.0']);
  42. });
  43. it('refuses a private channel the user cannot read, and 404s an unknown one', function () {
  44. $other = makeStudio('preview-other');
  45. $private = makeChannel($other, 'private channel');
  46. Channel::where('uid', $private)->update(['status' => 10]);
  47. $this->postJson('/api/v3/markup-previews', ['content' => 'x', 'channels' => [$private]], authHeader($this->me))
  48. ->assertForbidden();
  49. $this->postJson('/api/v3/markup-previews', ['content' => 'x', 'channels' => [(string) Str::uuid()]], authHeader($this->me))
  50. ->assertNotFound();
  51. });