diff --git a/README.md b/README.md index e5ac77f4..dbfb131e 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ by [Algolia](https://www.algolia.com/). Clone the repository to your local machine: ```sh -git clone https://github.com/YilanBoy/blog.git +git clone https://github.com/YilanBoy/docfunc.git ``` Change the current working directory to the repository: diff --git a/app/Livewire/Forms/PostForm.php b/app/Livewire/Forms/PostForm.php index 81e4fd4b..89bb488f 100644 --- a/app/Livewire/Forms/PostForm.php +++ b/app/Livewire/Forms/PostForm.php @@ -139,7 +139,18 @@ public function createPost(): Post $this->setBody(); $this->setExcerpt(); - $post = Post::query()->create($this->all()); + $post = Post::query()->create( + $this->only([ + 'title', + 'body', + 'category_id', + 'excerpt', + 'slug', + 'user_id', + 'preview_url', + 'is_private', + ]) + ); // create new tags relation with post in database $post->tags()->attach( @@ -155,7 +166,18 @@ public function updatePost(Post $post): Post $this->setBody(); $this->setExcerpt(); - $post->update($this->all()); + $post->update( + $this->only([ + 'title', + 'body', + 'category_id', + 'excerpt', + 'slug', + 'user_id', + 'preview_url', + 'is_private', + ]) + ); // update tags relation with post in database $post->tags()->sync( diff --git a/app/Models/User.php b/app/Models/User.php index 3aeb7402..ce83ee3b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -75,14 +75,11 @@ public function postNotify(PostComment $instance): void return; } - $this->increment('notification_count'); $this->notify($instance); } public function markAsRead(): void { - $this->notification_count = 0; - $this->save(); $this->unreadNotifications->markAsRead(); } diff --git a/app/Notifications/PostComment.php b/app/Notifications/PostComment.php index b4fcc66f..1d6d10c6 100644 --- a/app/Notifications/PostComment.php +++ b/app/Notifications/PostComment.php @@ -10,21 +10,17 @@ class PostComment extends Notification { use Queueable; - protected $comment; - - public function __construct(Comment $comment) + public function __construct(protected Comment $comment) { - // 注入留言實體,方便 toDatabase 方法中的使用 - $this->comment = $comment; } - public function via($notifiable) + public function via(object $notifiable): array { // 開啟通知的頻道 return ['database']; } - public function toDatabase($notifiable) + public function toDatabase(object $notifiable): array { $post = $this->comment->post; $link = $post->link_with_slug.'#comments'; @@ -37,18 +33,4 @@ public function toDatabase($notifiable) 'post_title' => $post->title, ]; } - - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage - */ - // public function toMail($notifiable) - // { - // return (new MailMessage) - // ->line('The introduction to the notification.') - // ->action('Notification Action', url('/')) - // ->line('Thank you for using our application!'); - // } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index c4d44182..6c6b1948 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -21,7 +21,7 @@ public function register(): void */ public function boot(): void { - Model::preventLazyLoading(! app()->isProduction()); + Model::shouldBeStrict(); Config::set('livewire.temporary_file_upload.disk', 'local'); } diff --git a/database/migrations/2024_01_05_141709_remove_notification_count_from_users.php b/database/migrations/2024_01_05_141709_remove_notification_count_from_users.php new file mode 100644 index 00000000..87907c1d --- /dev/null +++ b/database/migrations/2024_01_05_141709_remove_notification_count_from_users.php @@ -0,0 +1,28 @@ +dropColumn('notification_count'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->integer('notification_count')->unsigned()->default(0); + }); + } +}; diff --git a/resources/views/livewire/shared/layouts/desktop-header-menu.blade.php b/resources/views/livewire/shared/layouts/desktop-header-menu.blade.php index 8fdcfb95..a182a248 100644 --- a/resources/views/livewire/shared/layouts/desktop-header-menu.blade.php +++ b/resources/views/livewire/shared/layouts/desktop-header-menu.blade.php @@ -101,7 +101,7 @@ class="flex size-10 items-center justify-center rounded-lg text-xl text-gray-500 - @if (auth()->user()->notification_count > 0) + @if (auth()->user()->unreadNotifications->count() > 0) diff --git a/resources/views/livewire/shared/layouts/mobile-header-menu.blade.php b/resources/views/livewire/shared/layouts/mobile-header-menu.blade.php index 65ea2288..79e4eea7 100644 --- a/resources/views/livewire/shared/layouts/mobile-header-menu.blade.php +++ b/resources/views/livewire/shared/layouts/mobile-header-menu.blade.php @@ -100,7 +100,7 @@ class="rounded-full text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:ho - @if (auth()->user()->notification_count > 0) + @if (auth()->user()->unreadNotifications->count() > 0) diff --git a/tests/Feature/Api/ImageUploadApiTest.php b/tests/Feature/Api/ImageUploadApiTest.php index 8ad6bad8..0951fd18 100644 --- a/tests/Feature/Api/ImageUploadApiTest.php +++ b/tests/Feature/Api/ImageUploadApiTest.php @@ -18,7 +18,7 @@ }); test('logged-in users can upload images', function () { - login(); + loginAsUser(); $file = UploadedFile::fake()->image('photo.jpg')->size(100); @@ -30,7 +30,7 @@ }); test('the size of the uploaded image must be less than 1024 kb', function () { - login(); + loginAsUser(); post(route('images.store'), [ 'upload' => UploadedFile::fake()->image('photo.jpg')->size(1025), diff --git a/tests/Feature/Posts/CreatePostTest.php b/tests/Feature/Posts/CreatePostTest.php index 5c5c30ad..c15cca8b 100644 --- a/tests/Feature/Posts/CreatePostTest.php +++ b/tests/Feature/Posts/CreatePostTest.php @@ -26,7 +26,7 @@ }); test('authenticated user can create post', function ($categoryId) { - $this->actingAs(User::factory()->create()); + $user = loginAsUser(); $title = str()->random(4); $body = str()->random(500); @@ -59,16 +59,17 @@ expect($post) ->title->toBe($title) - ->slug->toBe($contentService->makeSlug($title)) + ->body->toBe($body) ->category_id->toBe($categoryId) + ->user_id->toBe($user->id) + ->slug->toBe($contentService->makeSlug($title)) ->excerpt->toBe($contentService->makeExcerpt($body)) - ->body->toBe($body) ->is_private->toBe($privateStatus) ->and($post->tags->pluck('id')->toArray())->toBe($tagIdsArray); })->with('defaultCategoryIds'); test('title at least 4 characters', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); livewire(Create::class, [ 'categories' => Category::all(['id', 'name']), @@ -81,7 +82,7 @@ }); test('title at most 50 characters', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); livewire(Create::class, [ 'categories' => Category::all(['id', 'name']), @@ -94,7 +95,7 @@ }); test('body at least 500 characters', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); livewire(Create::class, [ 'categories' => Category::all(['id', 'name']), @@ -107,7 +108,7 @@ }); test('body at most 20000 characters', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); livewire(Create::class, [ 'categories' => Category::all(['id', 'name']), @@ -120,7 +121,7 @@ }); it('can check image type', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); $file = UploadedFile::fake()->create('document.pdf', 512); @@ -132,7 +133,7 @@ }); it('can check image size', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); $file = UploadedFile::fake()->image('image.jpg')->size(1025); @@ -144,7 +145,7 @@ }); it('can upload image', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); Storage::fake(); @@ -166,7 +167,7 @@ }); it('can\'t upload non image', function () { - $this->actingAs(User::factory()->create()); + loginAsUser(); Storage::fake(); @@ -186,7 +187,7 @@ }); it('can get auto save key property', function () { - $user = User::factory()->create(); + $user = loginAsUser(); $this->actingAs($user); @@ -196,7 +197,7 @@ }); it('can auto save the post to cache', function () { - $user = User::factory()->create(); + $user = loginAsUser(); $autoSaveKey = 'auto_save_user_'.$user->id.'_create_post'; diff --git a/tests/Feature/Posts/DeletePostTest.php b/tests/Feature/Posts/DeletePostTest.php index f7cb8649..ecd67280 100644 --- a/tests/Feature/Posts/DeletePostTest.php +++ b/tests/Feature/Posts/DeletePostTest.php @@ -39,11 +39,10 @@ }); test('user cannot delete others\' post in desktop show post page', function () { - $user = User::factory()->create(); - $post = Post::factory()->create(); - $this->actingAs($user); + // Login as another user + loginAsUser(); livewire(ShowPostSidemenu::class, [ 'postId' => $post->id, @@ -59,7 +58,7 @@ test('author can soft delete own post in mobile show post page', function () { $post = Post::factory()->create(); - $this->actingAs(User::find($post->user_id)); + loginAsUser(User::find($post->user_id)); livewire(ShowPostDropdowns::class, ['postId' => $post->id]) ->call('deletePost', $post->id) @@ -79,11 +78,9 @@ }); test('user cannot delete others\' post in mobile show post page', function () { - $user = User::factory()->create(); - $post = Post::factory()->create(); - $this->actingAs($user); + loginAsUser(); livewire(ShowPostDropdowns::class, ['postId' => $post->id]) ->call('deletePost', $post->id) @@ -95,7 +92,7 @@ test('author can soft delete own post in user information post card', function () { $post = Post::factory()->create(); - $this->actingAs(User::find($post->user_id)); + loginAsUser(User::find($post->user_id)); livewire(PostsGroupByYear::class, [ 'posts' => [$post], @@ -123,11 +120,9 @@ }); test('user cannot delete others\' post in user information post card', function () { - $user = User::factory()->create(); - $post = Post::factory()->create(); - $this->actingAs($user); + loginAsUser(); livewire(PostsGroupByYear::class, [ 'posts' => [$post], @@ -141,9 +136,7 @@ }); test('author can restore deleted post', function () { - $user = User::factory()->create(); - - $this->actingAs($user); + $user = loginAsUser(); $post = Post::factory()->create([ 'title' => 'This is a test post title', @@ -166,9 +159,7 @@ }); test('users cannot restore other users\' post', function () { - $user = User::factory()->create(); - - $this->actingAs($user); + $user = loginAsUser(); $author = User::factory()->create(); diff --git a/tests/Feature/Posts/EditPostTest.php b/tests/Feature/Posts/EditPostTest.php index 0b612dac..f27f6eec 100644 --- a/tests/Feature/Posts/EditPostTest.php +++ b/tests/Feature/Posts/EditPostTest.php @@ -20,18 +20,16 @@ test('authors can access the edit page of their post', function () { $post = Post::factory()->create(); - $this->actingAs($post->user); + loginAsUser($post->user); get(route('posts.edit', ['post' => $post->id])) ->assertSuccessful(); }); test('users cannot access the edit page of other people\'s post', function () { - $user = User::factory()->create(); - $post = Post::factory()->create(); - $this->actingAs($user); + loginAsUser(); get(route('posts.edit', ['post' => $post->id])) ->assertForbidden(); @@ -40,7 +38,7 @@ test('authors can update their posts', function ($categoryId) { $post = Post::factory()->create(); - $this->actingAs($post->user); + loginAsUser($post->user); $newTitle = str()->random(4); $newBody = str()->random(500); @@ -85,7 +83,7 @@ 'created_at' => now(), ]); - $this->actingAs($post->user); + loginAsUser($post->user); livewire(PostsGroupByYear::class, [ 'year' => now()->year, diff --git a/tests/Feature/Posts/PostTest.php b/tests/Feature/Posts/PostTest.php index e04832a7..8aed322f 100644 --- a/tests/Feature/Posts/PostTest.php +++ b/tests/Feature/Posts/PostTest.php @@ -14,7 +14,7 @@ }); test('posts index can be rendered', function () { - $user = login(); + $user = loginAsUser(); Post::factory(10)->create([ 'category_id' => rand(1, 3), @@ -101,7 +101,7 @@ ]); test('user can view a post', function () { - $user = login(); + $user = loginAsUser(); $post = Post::factory()->make(); $post->user_id = $user->id; diff --git a/tests/Pest.php b/tests/Pest.php index fe869f26..ae70fa6e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -42,7 +42,7 @@ | */ -function login(?User $user = null) +function loginAsUser(?User $user = null) { if (is_null($user)) { $user = User::factory()->create();