diff --git a/src/Services/MediaService.php b/src/Services/MediaService.php index 2950969..6086eaf 100644 --- a/src/Services/MediaService.php +++ b/src/Services/MediaService.php @@ -209,7 +209,11 @@ protected function createPreviews(string $fileName, array &$data, int $ownerId = protected function prepareMediaData(array $data, string $filePath): array { if (empty($data['owner_id'])) { - $data['owner_id'] = (Auth::check()) ? Auth::id() : null; + $user = Auth::user(); + + $data['owner_id'] = ($user && (config('media.classes.user_model') === get_class($user))) + ? $user->getAuthIdentifier() + : null; } $data['name'] = $filePath; diff --git a/tests/MediaTest.php b/tests/MediaTest.php index c110ebd..21740a8 100755 --- a/tests/MediaTest.php +++ b/tests/MediaTest.php @@ -48,6 +48,19 @@ public function testCreateFromStream(): void $this->assertEqualsFixture('create_from_stream_response', $response->json()); } + public function testCreateNotConfigUserModel(): void + { + Config::set('media.classes.user_model', 'RonasIT\Tests\Models\User'); + + $response = $this->actingAs(self::$user)->json('post', '/media', ['file' => self::$file]); + + $response->assertCreated(); + + self::$mediaTestState->assertChangesEqualsFixture('create_null_owner_id'); + + $this->assertEqualsFixture('create_null_owner_id', $response->json()); + } + public function testCreatePublic(): void { $response = $this->actingAs(self::$user)->json('post', '/media', [ diff --git a/tests/fixtures/MediaTest/create_null_owner_id.json b/tests/fixtures/MediaTest/create_null_owner_id.json new file mode 100755 index 0000000..0b7a055 --- /dev/null +++ b/tests/fixtures/MediaTest/create_null_owner_id.json @@ -0,0 +1,14 @@ +{ + "id": 12, + "link": "\/storage\/WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "name": "WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "is_public": false, + "meta": [], + "preview": { + "id": 11, + "link": "\/storage\/preview_WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "name": "preview_WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "is_public": false, + "meta": [] + } +} \ No newline at end of file diff --git a/tests/fixtures/MediaTest/db_changes/media/create_null_owner_id.json b/tests/fixtures/MediaTest/db_changes/media/create_null_owner_id.json new file mode 100755 index 0000000..186330a --- /dev/null +++ b/tests/fixtures/MediaTest/db_changes/media/create_null_owner_id.json @@ -0,0 +1,30 @@ +{ + "updated": [], + "created": [ + { + "id": 11, + "created_at": "2024-01-01 00:00:00", + "updated_at": "2024-01-01 00:00:00", + "link": "\/storage\/preview_WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "name": "preview_WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "is_public": false, + "meta": [], + "owner_id": null, + "preview_id": null, + "blur_hash": null + }, + { + "id": 12, + "created_at": "2024-01-01 00:00:00", + "updated_at": "2024-01-01 00:00:00", + "link": "\/storage\/WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "name": "WpaDXtsDIc4IbC19IqHClOEHwTTlpyszZsm7Sb20.png", + "is_public": false, + "meta": [], + "owner_id": null, + "preview_id": 11, + "blur_hash": null + } + ], + "deleted": [] +} \ No newline at end of file