Skip to content

Commit

Permalink
feat: (LAR-8) rebase develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed Oct 9, 2024
1 parent e4a72ca commit 1e3b979
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 35 deletions.
26 changes: 13 additions & 13 deletions app/Actions/Article/CreateArticleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
Expand All @@ -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'])) {
Expand Down
14 changes: 8 additions & 6 deletions app/Data/Article/CreateArticleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Data\Article;

use Carbon\Carbon;
use Illuminate\Http\UploadedFile;
use Spatie\LaravelData\Data;

final class CreateArticleData extends Data
Expand All @@ -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 = [],
) {}
}
11 changes: 5 additions & 6 deletions app/Livewire/Articles/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') ?
Expand Down
1 change: 1 addition & 0 deletions app/Traits/WithArticleAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lang/en/notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return [

'article' => [
'created' => 'Thank you for submitting your article. We will only contact you once we have accepted your article.',
],

];
11 changes: 11 additions & 0 deletions lang/fr/notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return [

'article' => [
'created' => 'Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.',
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@

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 {
it('return the created article', function (): void {
$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);

expect($article)
->toBeInstanceOf(Article::class)
->and($article->tags)
->toHaveCount(0)
->toHaveCount(2)
->and($article->user_id)
->toBe($this->user->id);
});
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/Article/SendTelegramNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')
Expand All @@ -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())
Expand Down

0 comments on commit 1e3b979

Please sign in to comment.