Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit d76d7bb

Browse files
committed
Merge branch 'master' of github.com:beyondcode/laravel-websockets into 2.x
2 parents 8e6ca73 + 20d45e8 commit d76d7bb

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

database/migrations/create_websockets_statistics_entries_table.php.stub renamed to database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Schema;
4-
use Illuminate\Database\Schema\Blueprint;
53
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
66

77
class CreateWebSocketsStatisticsEntriesTable extends Migration
88
{
99
/**
1010
* Run the migrations.
11+
*
12+
* @return void
1113
*/
1214
public function up()
1315
{
@@ -20,8 +22,11 @@ public function up()
2022
$table->nullableTimestamps();
2123
});
2224
}
25+
2326
/**
2427
* Reverse the migrations.
28+
*
29+
* @return void
2530
*/
2631
public function down()
2732
{

src/WebSockets/Channels/PresenceChannel.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,18 @@ protected function getUserIds(array $users): array
139139
return array_values($userIds);
140140
}
141141

142+
/**
143+
* Compute the hash for the presence channel integrity.
144+
*
145+
* @param array $users
146+
* @return array
147+
*/
142148
protected function getHash(array $users): array
143149
{
144150
$hash = [];
145151

146152
foreach ($users as $socketId => $channelData) {
147-
$hash[$channelData->user_id] = $channelData->user_info;
153+
$hash[$channelData->user_id] = $channelData->user_info ?? [];
148154
}
149155

150156
return $hash;

src/WebSocketsServiceProvider.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Illuminate\Broadcasting\BroadcastManager;
2121
use Illuminate\Support\Facades\Gate;
2222
use Illuminate\Support\Facades\Route;
23-
use Illuminate\Support\Facades\Schema;
2423
use Illuminate\Support\ServiceProvider;
2524
use Psr\Log\LoggerInterface;
2625
use Pusher\Pusher;
@@ -33,11 +32,9 @@ public function boot()
3332
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
3433
], 'config');
3534

36-
if (! Schema::hasTable('websockets_statistics_entries')) {
37-
$this->publishes([
38-
__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'),
39-
], 'migrations');
40-
}
35+
$this->publishes([
36+
__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'),
37+
], 'migrations');
4138

4239
$this
4340
->registerRoutes()

tests/Channels/PresenceChannelTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
6969

7070
$channelData = [
7171
'user_id' => 1,
72-
'user_info' => [
73-
'name' => 'Marcel',
74-
],
7572
];
7673

7774
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
@@ -102,6 +99,35 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
10299
$this->pusherServer->onMessage($connection, $message);
103100
}
104101

102+
/** @test */
103+
public function clients_with_no_user_info_can_join_presence_channels()
104+
{
105+
$connection = $this->getWebSocketConnection();
106+
107+
$this->pusherServer->onOpen($connection);
108+
109+
$channelData = [
110+
'user_id' => 1,
111+
];
112+
113+
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
114+
115+
$message = new Message(json_encode([
116+
'event' => 'pusher:subscribe',
117+
'data' => [
118+
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
119+
'channel' => 'presence-channel',
120+
'channel_data' => json_encode($channelData),
121+
],
122+
]));
123+
124+
$this->pusherServer->onMessage($connection, $message);
125+
126+
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
127+
'channel' => 'presence-channel',
128+
]);
129+
}
130+
105131
/** @test */
106132
public function clients_with_valid_auth_signatures_cannot_leave_channels_they_are_not_in()
107133
{
@@ -128,6 +154,6 @@ public function clients_with_valid_auth_signatures_cannot_leave_channels_they_ar
128154

129155
$this->pusherServer->onMessage($connection, $message);
130156

131-
$this->markTestAsPassed();
157+
$this->assertTrue(true);
132158
}
133159
}

tests/TestCase.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function setUp(): void
3434
$this->channelManager,
3535
Mockery::mock(Browser::class)
3636
));
37+
38+
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
3739
}
3840

3941
protected function getPackageProviders($app)
@@ -55,10 +57,6 @@ protected function getEnvironmentSetUp($app)
5557
'enable_statistics' => true,
5658
],
5759
]);
58-
59-
include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub';
60-
61-
(new \CreateWebSocketsStatisticsEntriesTable())->up();
6260
}
6361

6462
protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection

0 commit comments

Comments
 (0)