|
|
@@ -192,34 +192,30 @@ class TelegramChannelTest extends TestCase
|
|
|
});
|
|
|
DB::table('rooms')->insert(['room_id' => '123', 'game_name' => '游戏', 'rounds' => 3, 'base_score' => 10, 'participants' => 4]);
|
|
|
|
|
|
- $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
$first = $this->service->save($this->message('@first_channel', '一'));
|
|
|
- $this->telegramChannel(-1001000000002, 'second_channel');
|
|
|
$second = $this->service->save(['chatId' => '@second_channel']);
|
|
|
$this->assertNull($second['type']);
|
|
|
$request = $this->request(['id' => $second['id']]);
|
|
|
$this->assertSame(-3, (new TelegramChannel())->publish($request, $this->service)->getData(true)['code']);
|
|
|
|
|
|
- $this->service->setDefault($second['id']);
|
|
|
- $this->assertSame('-1001000000002', ChannelService::createRoomNotice('123')['chat_id']);
|
|
|
+ DB::table('telegram_channel_settings')->where('id', 1)->update(['default_channel_id' => $second['id']]);
|
|
|
+ $this->assertSame('@second_channel', ChannelService::createRoomNotice('123')['chat_id']);
|
|
|
$this->service->save($this->message('@second_channel', '二') + ['id' => $second['id'], 'enabled' => false]);
|
|
|
$this->assertSame('一', $this->service->detail($first['id'])['text']);
|
|
|
$this->assertSame('二', $this->service->detail($second['id'])['text']);
|
|
|
$this->assertNull(ChannelService::createRoomNotice('123'));
|
|
|
|
|
|
- $this->service->setDefault($first['id']);
|
|
|
+ DB::table('telegram_channel_settings')->where('id', 1)->update(['default_channel_id' => $first['id']]);
|
|
|
$this->service->delete($first['id']);
|
|
|
$this->assertNull(ChannelService::createRoomNotice('123'));
|
|
|
- $this->assertCount(2, $this->requests);
|
|
|
+ $this->assertCount(0, $this->requests);
|
|
|
}
|
|
|
|
|
|
public function test_publishes_only_to_selected_channel_and_reports_pin_failure_separately(): void
|
|
|
{
|
|
|
$this->migrate();
|
|
|
- $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
$first = $this->service->save($this->message('@first_channel', '一'));
|
|
|
- $this->service->setDefault($first['id']);
|
|
|
- $this->telegramChannel(-1001000000002);
|
|
|
+ DB::table('telegram_channel_settings')->where('id', 1)->update(['default_channel_id' => $first['id']]);
|
|
|
$second = $this->service->save($this->message('-1001000000002', '二'));
|
|
|
|
|
|
$this->telegramResult(['message_id' => 37]);
|
|
|
@@ -229,12 +225,12 @@ class TelegramChannelTest extends TestCase
|
|
|
|
|
|
$this->assertSame(0, $result['code']);
|
|
|
$this->assertSame(['sent' => true, 'pinned' => false, 'message_id' => 37], array_intersect_key($result['data'], array_flip(['sent', 'pinned', 'message_id'])));
|
|
|
- $this->assertCount(4, $this->requests);
|
|
|
- parse_str((string) $this->requests[2]['request']->getBody(), $sent);
|
|
|
+ $this->assertCount(2, $this->requests);
|
|
|
+ parse_str((string) $this->requests[0]['request']->getBody(), $sent);
|
|
|
$this->assertSame('-1001000000002', $sent['chat_id']);
|
|
|
$this->assertSame('二', $sent['text']);
|
|
|
$this->assertSame($second['button'], json_decode($sent['reply_markup'], true)['inline_keyboard']);
|
|
|
- parse_str((string) $this->requests[3]['request']->getBody(), $pin);
|
|
|
+ parse_str((string) $this->requests[1]['request']->getBody(), $pin);
|
|
|
$this->assertSame(['chat_id' => '-1001000000002', 'message_id' => '37'], $pin);
|
|
|
}
|
|
|
|
|
|
@@ -289,19 +285,18 @@ class TelegramChannelTest extends TestCase
|
|
|
public function test_blank_channel_name_defaults_on_create_and_is_preserved_on_edit(): void
|
|
|
{
|
|
|
$this->migrate();
|
|
|
- $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
$request = $this->request(['chatId' => '@first_channel', 'name' => ' ']);
|
|
|
$controller = new TelegramChannel();
|
|
|
$saved = $controller->save($request, $this->service)->getData(true);
|
|
|
$this->assertSame(0, $saved['code']);
|
|
|
- $this->assertSame('频道', $saved['data']['name']);
|
|
|
+ $this->assertSame('first_channel', $saved['data']['name']);
|
|
|
|
|
|
$edit = ['id' => $saved['data']['id'], 'chatId' => '@first_channel'];
|
|
|
$request = $this->request($edit + ['name' => ' 后台自定义名称 ']);
|
|
|
$this->assertSame('后台自定义名称', $controller->save($request, $this->service)->getData(true)['data']['name']);
|
|
|
$request = $this->request($edit + ['name' => null]);
|
|
|
$this->assertSame('后台自定义名称', $controller->save($request, $this->service)->getData(true)['data']['name']);
|
|
|
- $this->assertCount(1, $this->requests);
|
|
|
+ $this->assertCount(0, $this->requests);
|
|
|
}
|
|
|
|
|
|
public function test_channel_tokens_are_encrypted_and_the_correct_bot_publishes_each_channel(): void
|
|
|
@@ -404,7 +399,8 @@ class TelegramChannelTest extends TestCase
|
|
|
$this->telegramResult(['id' => 222, 'is_bot' => true, 'username' => 'second_bot']);
|
|
|
$second = $this->service->saveBot(['bot_token' => '222:fixture_second']);
|
|
|
$this->service->savePrivateMenu($first['id'], 'zh', ['reply' => '欢迎一', 'buttons' => [['联系客服']]]);
|
|
|
- $this->service->savePrivateMenu($second['id'], 'zh', ['reply' => '欢迎二', 'buttons' => [['帮助']]]);
|
|
|
+ $buttons = [array_fill(0, 9, '帮助')];
|
|
|
+ $this->service->savePrivateMenu($second['id'], 'zh', ['reply' => '欢迎二', 'buttons' => $buttons]);
|
|
|
$this->telegramResult(['url' => '']);
|
|
|
$this->telegramResult(true);
|
|
|
$this->service->registerPrivateWebhook($second['id']);
|
|
|
@@ -423,30 +419,35 @@ class TelegramChannelTest extends TestCase
|
|
|
$this->assertSame('/bot222:fixture_second/sendMessage', $request->getUri()->getPath());
|
|
|
parse_str((string) $request->getBody(), $sent);
|
|
|
$this->assertSame('欢迎二', $sent['text']);
|
|
|
- $this->assertSame([['帮助']], json_decode($sent['reply_markup'], true)['keyboard']);
|
|
|
+ $this->assertSame($buttons, json_decode($sent['reply_markup'], true)['keyboard']);
|
|
|
}
|
|
|
|
|
|
- public function test_system_bot_channel_and_scheduled_message_are_saved_independently(): void
|
|
|
+ public function test_unbound_channel_can_be_saved_and_scheduled_messages_require_a_bot(): void
|
|
|
{
|
|
|
$this->migrate();
|
|
|
- $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
$channel = $this->service->save(['chatId' => '@first_channel', 'bot_id' => null]);
|
|
|
$this->assertNull($channel['bot_id']);
|
|
|
- $this->assertSame('/bot1:fixture/getChat', $this->requests[0]['request']->getUri()->getPath());
|
|
|
+ $this->assertCount(0, $this->requests);
|
|
|
$messages = new TelegramChannelMessageService($this->service);
|
|
|
$input = ['channel_id' => $channel['id'], 'type' => 'text', 'text' => '定时消息',
|
|
|
'scheduled_at' => now('UTC')->addHour()->toIso8601String()];
|
|
|
+ $request = $this->request($input);
|
|
|
+ $this->assertSame(-3, (new TelegramChannel())->createMessage($request, $messages)->getData(true)['code']);
|
|
|
+ $this->telegramResult(['id' => 111, 'is_bot' => true, 'username' => 'first_bot']);
|
|
|
+ $bot = $this->service->saveBot(['bot_token' => '111:fixture_first']);
|
|
|
+ $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
+ $this->service->save(['id' => $channel['id'], 'chatId' => '@first_channel', 'bot_id' => $bot['id']]);
|
|
|
$record = $messages->create($input);
|
|
|
$cancelled = $messages->create($input);
|
|
|
$messages->cancel($cancelled['id']);
|
|
|
$messages->dispatchDue();
|
|
|
$this->assertSame('scheduled', $record['status']);
|
|
|
- $this->assertCount(1, $this->requests);
|
|
|
+ $this->assertCount(2, $this->requests);
|
|
|
DB::table('telegram_channel_messages')->update(['scheduled_at' => now('UTC')->subMinute()]);
|
|
|
$this->telegramResult(['message_id' => 26]);
|
|
|
$messages->dispatchDue();
|
|
|
$messages->dispatchDue();
|
|
|
- $this->assertCount(2, $this->requests);
|
|
|
+ $this->assertCount(3, $this->requests);
|
|
|
$list = $messages->list(1);
|
|
|
$this->assertSame(['cancelled', 'sent'], array_column($list['list'], 'status'));
|
|
|
$this->assertSame(26, (int) $list['list'][1]['telegram_message_id']);
|
|
|
@@ -498,14 +499,14 @@ class TelegramChannelTest extends TestCase
|
|
|
{
|
|
|
$this->migrate();
|
|
|
$this->app->register(FilesystemServiceProvider::class);
|
|
|
- $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
+
|
|
|
$media = 'https://files.example.test/photo.jpg';
|
|
|
$channel = $this->service->save(['chatId' => '@first_channel', 'type' => 'image', 'image' => $media]);
|
|
|
$this->telegramResult(['message_id' => 18]);
|
|
|
|
|
|
$this->assertTrue($this->service->publish($channel['id'], false)['sent']);
|
|
|
- $this->assertStringEndsWith('/sendPhoto', $this->requests[1]['request']->getUri()->getPath());
|
|
|
- parse_str((string) $this->requests[1]['request']->getBody(), $sent);
|
|
|
+ $this->assertStringEndsWith('/sendPhoto', $this->requests[0]['request']->getUri()->getPath());
|
|
|
+ parse_str((string) $this->requests[0]['request']->getBody(), $sent);
|
|
|
$this->assertSame($media, $sent['photo']);
|
|
|
}
|
|
|
public function test_admin_upload_is_sent_as_a_file_and_migration_can_be_retried(): void
|
|
|
@@ -516,11 +517,11 @@ class TelegramChannelTest extends TestCase
|
|
|
$file = 'videos/' . bin2hex(random_bytes(16)) . '.mp4';
|
|
|
$disk->put($file, 'test video');
|
|
|
try {
|
|
|
- $this->telegramChannel(-1001000000001, 'first_channel');
|
|
|
+
|
|
|
$channel = $this->service->save([
|
|
|
'chatId' => '@first_channel', 'type' => 'video', 'video' => $disk->url($file),
|
|
|
]);
|
|
|
- $this->service->setDefault($channel['id']);
|
|
|
+ DB::table('telegram_channel_settings')->where('id', 1)->update(['default_channel_id' => $channel['id']]);
|
|
|
(require database_path('migrations/2026_11_13_150000_create_telegram_channels.php'))->up();
|
|
|
$this->assertTrue($this->service->detail($channel['id'])['is_default']);
|
|
|
$this->telegram->append(function ($request) {
|