Skip to content

Commit

Permalink
refactor: use shouldBeStrict in boot
Browse files Browse the repository at this point in the history
- remove notification_count from users table
- refactor tests
- update readme
  • Loading branch information
yilanboy committed Jan 5, 2024
1 parent e0a55d6 commit e0ea71f
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 24 additions & 2 deletions app/Livewire/Forms/PostForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
24 changes: 3 additions & 21 deletions app/Notifications/PostComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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!');
// }
}
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('notification_count');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->integer('notification_count')->unsigned()->default(0);
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class="flex size-10 items-center justify-center rounded-lg text-xl text-gray-500
<i class="bi bi-bell-fill"></i>
</a>

@if (auth()->user()->notification_count > 0)
@if (auth()->user()->unreadNotifications->count() > 0)
<span class="absolute right-2 top-2 -mr-1 -mt-1 flex h-3 w-3">
<span class="absolute inline-flex h-full w-full animate-ping rounded-full bg-red-400 opacity-75"></span>
<span class="relative inline-flex h-3 w-3 rounded-full bg-red-500"></span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class="rounded-full text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:ho
<i class="bi bi-bell-fill"></i>
</a>

@if (auth()->user()->notification_count > 0)
@if (auth()->user()->unreadNotifications->count() > 0)
<span class="absolute right-2 top-2 -mr-1 -mt-1 flex h-3 w-3">
<span class="absolute inline-flex h-full w-full animate-ping rounded-full bg-red-400 opacity-75"></span>
<span class="relative inline-flex h-3 w-3 rounded-full bg-red-500"></span>
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Api/ImageUploadApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
});

test('logged-in users can upload images', function () {
login();
loginAsUser();

$file = UploadedFile::fake()->image('photo.jpg')->size(100);

Expand All @@ -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),
Expand Down
27 changes: 14 additions & 13 deletions tests/Feature/Posts/CreatePostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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']),
Expand All @@ -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']),
Expand All @@ -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']),
Expand All @@ -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']),
Expand All @@ -120,7 +121,7 @@
});

it('can check image type', function () {
$this->actingAs(User::factory()->create());
loginAsUser();

$file = UploadedFile::fake()->create('document.pdf', 512);

Expand All @@ -132,7 +133,7 @@
});

it('can check image size', function () {
$this->actingAs(User::factory()->create());
loginAsUser();

$file = UploadedFile::fake()->image('image.jpg')->size(1025);

Expand All @@ -144,7 +145,7 @@
});

it('can upload image', function () {
$this->actingAs(User::factory()->create());
loginAsUser();

Storage::fake();

Expand All @@ -166,7 +167,7 @@
});

it('can\'t upload non image', function () {
$this->actingAs(User::factory()->create());
loginAsUser();

Storage::fake();

Expand All @@ -186,7 +187,7 @@
});

it('can get auto save key property', function () {
$user = User::factory()->create();
$user = loginAsUser();

$this->actingAs($user);

Expand All @@ -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';

Expand Down
25 changes: 8 additions & 17 deletions tests/Feature/Posts/DeletePostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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],
Expand Down Expand Up @@ -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],
Expand All @@ -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',
Expand All @@ -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();

Expand Down
Loading

0 comments on commit e0ea71f

Please sign in to comment.