From 06fef7eec0ff9d22f987e4e150f51aaaeb9fa896 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Thu, 9 May 2024 16:25:04 +0100 Subject: [PATCH] fix --- .../src/Api/Resource/NotificationResource.php | 26 ++++--- framework/core/src/Foundation/Config.php | 2 +- .../api/notifications/UpdateTest.php | 67 +++++++++++++++++++ .../extenders/ApiSerializerTest.php | 1 + .../testing/src/integration/TestCase.php | 4 ++ 5 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 framework/core/tests/integration/api/notifications/UpdateTest.php diff --git a/framework/core/src/Api/Resource/NotificationResource.php b/framework/core/src/Api/Resource/NotificationResource.php index 827200485..17a69a0b9 100644 --- a/framework/core/src/Api/Resource/NotificationResource.php +++ b/framework/core/src/Api/Resource/NotificationResource.php @@ -23,10 +23,13 @@ use Tobyz\JsonApiServer\Pagination\OffsetPagination; */ class NotificationResource extends AbstractDatabaseResource { + protected bool $initialized = false; + public function __construct( protected Dispatcher $bus, protected NotificationRepository $notifications, ) { + $this->initialized = true; } public function type(): string @@ -63,21 +66,19 @@ class NotificationResource extends AbstractDatabaseResource ->before(function (Context $context) { $context->getActor()->markNotificationsAsRead()->save(); }) - ->defaultInclude([ + ->defaultInclude(array_filter([ 'fromUser', 'subject', - 'subject.discussion' - ]) + $this->initialized && count($this->subjectTypes()) > 1 + ? 'subject.discussion' + : null, + ])) ->paginate(), ]; } public function fields(): array { - $subjectTypes = $this->api->typesForModels( - (new Notification())->getSubjectModels() - ); - return [ Schema\Str::make('contentType') ->property('type'), @@ -87,7 +88,7 @@ class NotificationResource extends AbstractDatabaseResource Schema\Boolean::make('isRead') ->writable() ->get(fn (Notification $notification) => (bool) $notification->read_at) - ->set(function (Notification $notification, Context $context) { + ->set(function (Notification $notification, bool $value, Context $context) { $this->bus->dispatch( new ReadNotification($notification->id, $context->getActor()) ); @@ -99,8 +100,15 @@ class NotificationResource extends AbstractDatabaseResource ->type('users') ->includable(), Schema\Relationship\ToOne::make('subject') - ->collection($subjectTypes) + ->collection($this->subjectTypes()) ->includable(), ]; } + + protected function subjectTypes(): array + { + return $this->api->typesForModels( + (new Notification())->getSubjectModels() + ); + } } diff --git a/framework/core/src/Foundation/Config.php b/framework/core/src/Foundation/Config.php index c2924414a..4d5efd4a3 100644 --- a/framework/core/src/Foundation/Config.php +++ b/framework/core/src/Foundation/Config.php @@ -65,7 +65,7 @@ class Config implements ArrayAccess public function safeModeExtensions(): ?array { - return $this->data['safe_mode_extensions']; + return $this->data['safe_mode_extensions'] ?? null; } private function requireKeys(mixed ...$keys): void diff --git a/framework/core/tests/integration/api/notifications/UpdateTest.php b/framework/core/tests/integration/api/notifications/UpdateTest.php new file mode 100644 index 000000000..4a34b4739 --- /dev/null +++ b/framework/core/tests/integration/api/notifications/UpdateTest.php @@ -0,0 +1,67 @@ +prepareDatabase([ + User::class => [ + $this->normalUser(), + ], + Discussion::class => [ + ['id' => 1, 'title' => 'Foo', 'comment_count' => 1, 'user_id' => 2], + ], + Post::class => [ + ['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => 'Foo'], + ], + Notification::class => [ + ['id' => 1, 'user_id' => 2, 'from_user_id' => 1, 'type' => 'discussionRenamed', 'subject_id' => 1, 'read_at' => null], + ] + ]); + } + + /** + * @test + */ + public function can_mark_all_as_read() + { + $response = $this->send( + $this->request('PATCH', '/api/notifications/1', [ + 'authenticatedAs' => 2, + 'json' => [ + 'data' => [ + 'type' => 'notifications', + 'attributes' => [ + 'isRead' => true + ], + ], + ], + ]) + ); + + $this->assertEquals(200, $response->getStatusCode(), (string) $response->getBody()); + } +} diff --git a/framework/core/tests/integration/extenders/ApiSerializerTest.php b/framework/core/tests/integration/extenders/ApiSerializerTest.php index 6e775c302..c0281ea3b 100644 --- a/framework/core/tests/integration/extenders/ApiSerializerTest.php +++ b/framework/core/tests/integration/extenders/ApiSerializerTest.php @@ -17,6 +17,7 @@ use Flarum\Api\Resource\UserResource; use Flarum\Api\Schema; use Flarum\Discussion\Discussion; use Flarum\Extend; +use Flarum\Post\Post; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Flarum\User\User; diff --git a/php-packages/testing/src/integration/TestCase.php b/php-packages/testing/src/integration/TestCase.php index 68f2d4764..487084a63 100644 --- a/php-packages/testing/src/integration/TestCase.php +++ b/php-packages/testing/src/integration/TestCase.php @@ -208,6 +208,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase 'unique' => $instance->uniqueKeys ?? null, ]; } else { + if (class_exists($tableOrModelClass) && is_subclass_of($tableOrModelClass, Model::class)) { + $tableOrModelClass = (new $tableOrModelClass)->getTable(); + } + $databaseContent[$tableOrModelClass] = [ 'rows' => $_rows, 'unique' => null,