News creation: Show error on duplicates

This commit is contained in:
Igor Scheller 2023-08-27 17:59:23 +02:00 committed by Michael Weimann
parent f966b1521f
commit 5e702cd177
4 changed files with 34 additions and 0 deletions

View File

@ -80,6 +80,9 @@ msgstr "Kommentar gespeichert."
msgid "news.comment-delete.success" msgid "news.comment-delete.success"
msgstr "Kommentar erfolgreich gelöscht." msgstr "Kommentar erfolgreich gelöscht."
msgid "news.edit.duplicate"
msgstr "Diese News wurde bereits erstellt."
msgid "news.edit.success" msgid "news.edit.success"
msgstr "News erfolgreich aktualisiert." msgstr "News erfolgreich aktualisiert."

View File

@ -78,6 +78,9 @@ msgstr "Comment saved."
msgid "news.comment-delete.success" msgid "news.comment-delete.success"
msgstr "Comment successfully deleted." msgstr "Comment successfully deleted."
msgid "news.edit.duplicate"
msgstr "Diese News wurde bereits erstellt."
msgid "news.edit.success" msgid "news.edit.success"
msgstr "News successfully updated." msgstr "News successfully updated."

View File

@ -6,6 +6,7 @@ namespace Engelsystem\Controllers\Admin;
use Engelsystem\Controllers\BaseController; use Engelsystem\Controllers\BaseController;
use Engelsystem\Controllers\HasUserNotifications; use Engelsystem\Controllers\HasUserNotifications;
use Engelsystem\Controllers\NotificationType;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Redirector; use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
@ -104,6 +105,10 @@ class NewsController extends BaseController
} }
$isNewNews = !$news->id; $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(); $news->save();
if ($isNewNews) { if ($isNewNews) {

View File

@ -228,6 +228,29 @@ class NewsControllerTest extends ControllerTest
$this->assertHasNotification('news.delete.success'); $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 * Creates a new user
*/ */