From 5e702cd17742f202aca39fc07702f1907c62134f Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 27 Aug 2023 17:59:23 +0200 Subject: [PATCH] News creation: Show error on duplicates --- resources/lang/de_DE/additional.po | 3 +++ resources/lang/en_US/additional.po | 3 +++ src/Controllers/Admin/NewsController.php | 5 ++++ .../Controllers/Admin/NewsControllerTest.php | 23 +++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/resources/lang/de_DE/additional.po b/resources/lang/de_DE/additional.po index 0e8cd7ce..094c6635 100644 --- a/resources/lang/de_DE/additional.po +++ b/resources/lang/de_DE/additional.po @@ -80,6 +80,9 @@ msgstr "Kommentar gespeichert." msgid "news.comment-delete.success" msgstr "Kommentar erfolgreich gelöscht." +msgid "news.edit.duplicate" +msgstr "Diese News wurde bereits erstellt." + msgid "news.edit.success" msgstr "News erfolgreich aktualisiert." diff --git a/resources/lang/en_US/additional.po b/resources/lang/en_US/additional.po index 6d3f1864..d72fdfa7 100644 --- a/resources/lang/en_US/additional.po +++ b/resources/lang/en_US/additional.po @@ -78,6 +78,9 @@ msgstr "Comment saved." msgid "news.comment-delete.success" msgstr "Comment successfully deleted." +msgid "news.edit.duplicate" +msgstr "Diese News wurde bereits erstellt." + msgid "news.edit.success" msgstr "News successfully updated." diff --git a/src/Controllers/Admin/NewsController.php b/src/Controllers/Admin/NewsController.php index 848b259d..d921667e 100644 --- a/src/Controllers/Admin/NewsController.php +++ b/src/Controllers/Admin/NewsController.php @@ -6,6 +6,7 @@ namespace Engelsystem\Controllers\Admin; use Engelsystem\Controllers\BaseController; use Engelsystem\Controllers\HasUserNotifications; +use Engelsystem\Controllers\NotificationType; use Engelsystem\Helpers\Authenticator; use Engelsystem\Http\Redirector; use Engelsystem\Http\Request; @@ -104,6 +105,10 @@ class NewsController extends BaseController } $isNewNews = !$news->id; + if ($isNewNews && News::where('title', $news->title)->where('text', $news->text)->count()) { + $this->addNotification('news.edit.duplicate', NotificationType::ERROR); + return $this->showEdit($news); + } $news->save(); if ($isNewNews) { diff --git a/tests/Unit/Controllers/Admin/NewsControllerTest.php b/tests/Unit/Controllers/Admin/NewsControllerTest.php index 3943aa14..e367de9a 100644 --- a/tests/Unit/Controllers/Admin/NewsControllerTest.php +++ b/tests/Unit/Controllers/Admin/NewsControllerTest.php @@ -228,6 +228,29 @@ class NewsControllerTest extends ControllerTest $this->assertHasNotification('news.delete.success'); } + /** + * @covers \Engelsystem\Controllers\Admin\NewsController::save + */ + public function testSaveDuplicated(): void + { + $previousNews = News::first(); + $this->request = $this->request->withParsedBody([ + 'title' => $previousNews->title, + 'text' => $previousNews->text, + ]); + $this->response->expects($this->once()) + ->method('withView') + ->willReturn($this->response); + + /** @var NewsController $controller */ + $controller = $this->app->make(NewsController::class); + $controller->setValidator(new Validator()); + + $controller->save($this->request); + + $this->assertHasNotification('news.edit.duplicate', NotificationType::ERROR); + } + /** * Creates a new user */