2
0

ChannelServiceTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. use App\Http\Api\ChannelApi;
  3. use App\Models\Channel;
  4. use App\Services\ChannelService;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Str;
  7. uses(RefreshDatabase::class);
  8. /*
  9. * ChannelService 是 ChannelApi 纯能力部分的新家;ChannelApi 那几个方法只剩转发壳,返回值必须与原来一致。
  10. */
  11. it('summarises a channel the way v2 getById did', function () {
  12. $owner = makeStudio('owner');
  13. $uid = makeChannel($owner, 'my channel');
  14. $expected = [
  15. 'id' => $uid, 'name' => 'my channel', 'type' => 'translation',
  16. 'lang' => 'zh-Hans', 'studio_id' => $owner, 'status' => 30,
  17. ];
  18. expect(app(ChannelService::class)->summary($uid))->toBe($expected)
  19. ->and(ChannelApi::getById($uid))->toBe($expected)
  20. ->and(ChannelApi::getById('not-a-uuid'))->toBeFalse()
  21. ->and(ChannelApi::getById(null))->toBeFalse()
  22. ->and(ChannelApi::getById((string) Str::uuid()))->toBeFalse();
  23. });
  24. it('summarises many channels in input order with false for misses, in one query', function () {
  25. $owner = makeStudio('owner');
  26. $a = makeChannel($owner, 'a');
  27. $b = makeChannel($owner, 'b');
  28. $missing = (string) Str::uuid();
  29. DB::enableQueryLog();
  30. $result = ChannelApi::getByIds([$b, 'bad', $missing, $a, $b]);
  31. expect(DB::getQueryLog())->toHaveCount(1);
  32. expect(array_map(fn ($row) => $row === false ? false : $row['name'], $result))
  33. ->toBe(['b', false, false, 'a', 'b']);
  34. });
  35. it('finds a system channel by name under the root account, with a fallback', function () {
  36. $root = config('mint.admin.root_uuid');
  37. $en = makeChannel($root, '_System_Grammar_Term_en_');
  38. makeChannel(makeStudio('someone'), '_System_Grammar_Term_my_'); // 不是 root 名下的不算
  39. $service = app(ChannelService::class);
  40. expect($service->systemUid('_System_Grammar_Term_en_'))->toBe($en)
  41. ->and($service->systemUid('_System_Grammar_Term_my_'))->toBeFalse()
  42. ->and($service->systemUid('_System_Grammar_Term_my_', '_System_Grammar_Term_en_'))->toBe($en)
  43. ->and(ChannelApi::getSysChannel('_nope_', '_also_nope_'))->toBeFalse();
  44. });
  45. it('gets or creates the user book channel of a studio', function () {
  46. $studio = makeStudio('owner');
  47. $existing = makeChannel($studio, '_user_book_pali');
  48. expect(ChannelApi::userBookGetOrCreate($studio, 'pali', 30))->toBe($existing);
  49. $created = ChannelApi::userBookGetOrCreate($studio, 'zh-Hans', '10');
  50. expect($created)->toBeString()
  51. ->and(Channel::find($created))
  52. ->name->toBe('_user_book_zh-Hans')
  53. ->owner_uid->toBe($studio)
  54. ->is_system->toBeTrue()
  55. ->status->toBe(10);
  56. expect(ChannelApi::userBookGetOrCreate($studio, 'zh-Hans', 10))->toBe($created);
  57. });
  58. it('gets a channel by name or throws', function () {
  59. $uid = makeChannel(makeStudio('owner'), '_System_commentary_');
  60. expect(ChannelApi::getChannelByName('_System_commentary_')->uid)->toBe($uid);
  61. ChannelApi::getChannelByName('_missing_');
  62. })->throws(Exception::class, 'channel is invalid');