diff --git a/app/Actions/Article/CreateArticleAction.php b/app/Actions/Article/CreateArticleAction.php index 7d09426d..a960ba73 100644 --- a/app/Actions/Article/CreateArticleAction.php +++ b/app/Actions/Article/CreateArticleAction.php @@ -16,9 +16,9 @@ final class CreateArticleAction { public function execute(CreateArticleData $articleData): Article { - if ($articleData->published_at && ! ($articleData->published_at instanceof DateTimeInterface)) { - $articleData->published_at = new Carbon( - time: $articleData->published_at, + if ($articleData->publishedAt && ! ($articleData->publishedAt instanceof DateTimeInterface)) { + $articleData->publishedAt = new Carbon( + time: $articleData->publishedAt, tz: config('app.timezone') ); } @@ -28,26 +28,26 @@ public function execute(CreateArticleData $articleData): Article 'title' => $articleData->title, 'slug' => $articleData->title, 'body' => $articleData->body, - 'published_at' => $articleData->published_at, - 'submitted_at' => $articleData->submitted_at, - 'approved_at' => $articleData->approved_at, - 'show_toc' => $articleData->show_toc, - 'canonical_url' => $articleData->canonical_url, + 'published_at' => $articleData->publishedAt, + 'submitted_at' => $articleData->submittedAt, + 'approved_at' => $articleData->approvedAt, + 'show_toc' => $articleData->showToc, + 'canonical_url' => $articleData->canonicalUrl, 'user_id' => Auth::id(), ]); - if (collect($article->associateTags)->isNotEmpty()) { - $article->syncTags(tags: $article->associateTags); + if (collect($articleData->tags)->isNotEmpty()) { + $article->syncTags(tags: $articleData->tags); } - if ($article->file) { - $article->addMedia($article->file->getRealPath())->toMediaCollection('media'); + if ($articleData->file) { + $article->addMedia($articleData->file->getRealPath())->toMediaCollection('media'); } if ($article->isAwaitingApproval()) { // Envoi de la notification sur le channel Telegram pour la validation de l'article. Auth::user()?->notify(new PostArticleToTelegram($article)); - session()->flash('status', __('Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.')); + session()->flash('status', __('notifications.article.created')); } if (Auth::user()?->hasAnyRole(['admin', 'moderator'])) { diff --git a/app/Data/Article/CreateArticleData.php b/app/Data/Article/CreateArticleData.php index cc1d9958..63896500 100644 --- a/app/Data/Article/CreateArticleData.php +++ b/app/Data/Article/CreateArticleData.php @@ -5,6 +5,7 @@ namespace App\Data\Article; use Carbon\Carbon; +use Illuminate\Http\UploadedFile; use Spatie\LaravelData\Data; final class CreateArticleData extends Data @@ -13,11 +14,12 @@ public function __construct( public string $title, public string $slug, public string $body, - public Carbon $published_at, - public ?Carbon $submitted_at, - public ?Carbon $approved_at, - public string $show_toc, - public string $canonical_url, - public array $associateTags = [], + public string $showToc, + public string $canonicalUrl, + public ?Carbon $publishedAt, + public ?Carbon $submittedAt, + public ?Carbon $approvedAt, + public ?UploadedFile $file, + public array $tags = [], ) {} } diff --git a/app/Livewire/Articles/Create.php b/app/Livewire/Articles/Create.php index d67aa4c8..29b66235 100644 --- a/app/Livewire/Articles/Create.php +++ b/app/Livewire/Articles/Create.php @@ -53,12 +53,11 @@ public function store(): void 'title' => $this->title, 'slug' => $this->slug, 'body' => $this->body, - 'published_at' => $this->published_at, - 'submitted_at' => $this->submitted_at, - 'approved_at' => $this->approved_at, - 'show_toc' => $this->show_toc, - 'canonical_url' => $this->canonical_url, - 'user_id' => $user->id, + 'publishedAt' => $this->published_at, + 'submittedAt' => $this->submitted_at, + 'approvedAt' => $this->approved_at, + 'showToc' => $this->show_toc, + 'canonicalUrl' => $this->canonical_url, ])); $user->hasRole('user') ? diff --git a/app/Traits/WithArticleAttributes.php b/app/Traits/WithArticleAttributes.php index cdc6fb22..1192446c 100644 --- a/app/Traits/WithArticleAttributes.php +++ b/app/Traits/WithArticleAttributes.php @@ -39,6 +39,7 @@ trait WithArticleAttributes 'tags_selected' => 'nullable|array', 'canonical_url' => 'nullable|url', 'file' => 'nullable|image|max:2048', // 1MB Max + 'show_toc' => 'boolean', ]; public function removeImage(): void diff --git a/lang/en/notifications.php b/lang/en/notifications.php new file mode 100644 index 00000000..24685a37 --- /dev/null +++ b/lang/en/notifications.php @@ -0,0 +1,11 @@ + [ + 'created' => 'Thank you for submitting your article. We will only contact you once we have accepted your article.', + ], + +]; diff --git a/lang/fr/notifications.php b/lang/fr/notifications.php new file mode 100644 index 00000000..7da5bcdb --- /dev/null +++ b/lang/fr/notifications.php @@ -0,0 +1,11 @@ + [ + 'created' => 'Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.', + ], + +]; diff --git a/tests/Feature/Actions/Article/createArticleActionTest.php b/tests/Feature/Actions/Article/CreateArticleActionTest.php similarity index 72% rename from tests/Feature/Actions/Article/createArticleActionTest.php rename to tests/Feature/Actions/Article/CreateArticleActionTest.php index 9124e049..13ac6b35 100644 --- a/tests/Feature/Actions/Article/createArticleActionTest.php +++ b/tests/Feature/Actions/Article/CreateArticleActionTest.php @@ -9,8 +9,8 @@ beforeEach(function (): void { $this->user = $this->login(); - $this->tagOne = Tag::factory()->create(['name' => 'Tag 1', 'concerns' => ['article']]); - $this->tagTwo = Tag::factory()->create(['name' => 'Tag 2', 'concerns' => ['article', 'post']]); + $this->tagOne = Tag::factory()->create(['name' => 'Tag 1', 'concerns' => ['post']]); + $this->tagTwo = Tag::factory()->create(['name' => 'Tag 2', 'concerns' => ['tutorial', 'post']]); }); describe(CreateArticleAction::class, function (): void { @@ -18,13 +18,13 @@ $articleDataWithoutTag = CreateArticleData::from([ 'title' => 'Article title', 'slug' => 'Article slug', - 'published_at' => Now(), - 'submitted_at' => null, - 'approved_at' => null, - 'show_toc' => 'Article show_toc', - 'canonical_url' => 'Article canonical_url', + 'publishedAt' => now(), + 'submittedAt' => null, + 'approvedAt' => null, + 'showToc' => 'Article show_toc', + 'canonicalUrl' => 'Article canonical_url', 'body' => 'Article body', - 'associateTags' => [], + 'tags' => [$this->tagOne->id, $this->tagTwo->id], ]); $article = app(CreateArticleAction::class)->execute($articleDataWithoutTag); @@ -32,7 +32,7 @@ expect($article) ->toBeInstanceOf(Article::class) ->and($article->tags) - ->toHaveCount(0) + ->toHaveCount(2) ->and($article->user_id) ->toBe($this->user->id); }); diff --git a/tests/Feature/Article/SendTelegramNotificationTest.php b/tests/Feature/Article/SendTelegramNotificationTest.php index 40a02fa4..ebf8d03f 100644 --- a/tests/Feature/Article/SendTelegramNotificationTest.php +++ b/tests/Feature/Article/SendTelegramNotificationTest.php @@ -5,6 +5,7 @@ use App\Livewire\Articles\Create; use App\Models\Article; use App\Notifications\PostArticleToTelegram; +use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Notification; use Livewire\Livewire; @@ -15,6 +16,8 @@ test('Send notification on telegram after submition on article', function (): void { // 2- soumission d'article par le user connecté + $file = UploadedFile::fake()->image('article.png'); + $article = Livewire::actingAs($this->user)->test(Create::class) ->set('title', 'Test Article') ->set('slug', 'test-article') @@ -23,8 +26,8 @@ ->set('submitted_at', now()) ->set('approved_at', null) ->set('show_toc', true) + ->set('file', $file) ->set('canonical_url', 'https://laravel.cm') - ->set('associateTags', ['tag1', 'tag2']) ->call('store'); expect(Article::count())