diff --git a/database/migrations/create_websockets_statistics_entries_table.php.stub b/database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php similarity index 93% rename from database/migrations/create_websockets_statistics_entries_table.php.stub rename to database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php index b56e525..1b89b4a 100644 --- a/database/migrations/create_websockets_statistics_entries_table.php.stub +++ b/database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php @@ -1,13 +1,15 @@ nullableTimestamps(); }); } + /** * Reverse the migrations. + * + * @return void */ public function down() { diff --git a/src/WebSockets/Channels/PresenceChannel.php b/src/WebSockets/Channels/PresenceChannel.php index f389674..a4b94e9 100644 --- a/src/WebSockets/Channels/PresenceChannel.php +++ b/src/WebSockets/Channels/PresenceChannel.php @@ -139,12 +139,18 @@ class PresenceChannel extends Channel return array_values($userIds); } + /** + * Compute the hash for the presence channel integrity. + * + * @param array $users + * @return array + */ protected function getHash(array $users): array { $hash = []; foreach ($users as $socketId => $channelData) { - $hash[$channelData->user_id] = $channelData->user_info; + $hash[$channelData->user_id] = $channelData->user_info ?? []; } return $hash; diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index cbed590..faeefb5 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -20,7 +20,6 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChanne use Illuminate\Broadcasting\BroadcastManager; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; use Psr\Log\LoggerInterface; use Pusher\Pusher; @@ -34,11 +33,9 @@ class WebSocketsServiceProvider extends ServiceProvider __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), ], 'config'); - if (! Schema::hasTable('websockets_statistics_entries')) { - $this->publishes([ - __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_statistics_entries_table.php'), - ], 'migrations'); - } + $this->publishes([ + __DIR__.'/../database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php' => database_path('migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php'), + ], 'migrations'); $this ->registerRoutes() diff --git a/tests/Channels/PresenceChannelTest.php b/tests/Channels/PresenceChannelTest.php index 3f3bb0a..e2d4de1 100644 --- a/tests/Channels/PresenceChannelTest.php +++ b/tests/Channels/PresenceChannelTest.php @@ -69,9 +69,6 @@ class PresenceChannelTest extends TestCase $channelData = [ 'user_id' => 1, - 'user_info' => [ - 'name' => 'Marcel', - ], ]; $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); @@ -102,6 +99,35 @@ class PresenceChannelTest extends TestCase $this->pusherServer->onMessage($connection, $message); } + /** @test */ + public function clients_with_no_user_info_can_join_presence_channels() + { + $connection = $this->getWebSocketConnection(); + + $this->pusherServer->onOpen($connection); + + $channelData = [ + 'user_id' => 1, + ]; + + $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), + 'channel' => 'presence-channel', + 'channel_data' => json_encode($channelData), + ], + ])); + + $this->pusherServer->onMessage($connection, $message); + + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ + 'channel' => 'presence-channel', + ]); + } + /** @test */ public function clients_with_valid_auth_signatures_cannot_leave_channels_they_are_not_in() { @@ -128,6 +154,6 @@ class PresenceChannelTest extends TestCase $this->pusherServer->onMessage($connection, $message); - $this->markTestAsPassed(); + $this->assertTrue(true); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 54e9f7a..2cf9ac9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -34,6 +34,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $this->channelManager, Mockery::mock(Browser::class) )); + + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); } protected function getPackageProviders($app) @@ -67,10 +69,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase 'websockets.replication.driver', getenv('REPLICATION_DRIVER') ?: 'local' ); - - include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub'; - - (new \CreateWebSocketsStatisticsEntriesTable())->up(); } protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection