2020-04-05 16:54:45 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-05 16:54:45 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Admin;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\Admin\NewsController;
|
2023-02-02 22:53:51 +01:00
|
|
|
use Engelsystem\Controllers\NotificationType;
|
2020-12-28 16:04:05 +01:00
|
|
|
use Engelsystem\Events\EventDispatcher;
|
2020-04-05 16:54:45 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Models\News;
|
2023-12-21 13:41:10 +01:00
|
|
|
use Engelsystem\Models\User\Settings;
|
2020-04-05 16:54:45 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2020-12-15 13:39:00 +01:00
|
|
|
use Engelsystem\Test\Unit\Controllers\ControllerTest;
|
2020-04-05 16:54:45 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
2020-12-15 13:39:00 +01:00
|
|
|
class NewsControllerTest extends ControllerTest
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2022-12-15 19:50:56 +01:00
|
|
|
protected Authenticator|MockObject $auth;
|
2023-12-21 13:41:10 +01:00
|
|
|
protected EventDispatcher|MockObject $eventDispatcher;
|
2020-04-05 16:54:45 +02:00
|
|
|
|
|
|
|
/** @var array */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $data = [
|
2020-04-05 16:54:45 +02:00
|
|
|
[
|
|
|
|
'title' => 'Foo',
|
2021-12-30 19:18:54 +01:00
|
|
|
'text' => '**foo**',
|
2020-04-05 16:54:45 +02:00
|
|
|
'user_id' => 1,
|
2023-02-05 18:03:00 +01:00
|
|
|
],
|
2020-04-05 16:54:45 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::__construct
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::edit
|
2022-01-01 16:21:40 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::showEdit
|
2020-04-05 16:54:45 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testEdit(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', 1);
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/news/edit.twig', $view);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($data['news']);
|
2023-12-27 15:48:57 +01:00
|
|
|
$this->assertFalse($data['send_notification']);
|
2020-04-05 16:54:45 +02:00
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
|
|
|
|
$controller->edit($this->request);
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNoNotifications(NotificationType::WARNING);
|
2020-04-05 16:54:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 00:12:49 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::edit
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testEditIsMeeting(): void
|
2020-05-12 00:12:49 +02:00
|
|
|
{
|
|
|
|
$isMeeting = false;
|
|
|
|
$this->response->expects($this->exactly(3))
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(
|
|
|
|
function ($view, $data) use (&$isMeeting) {
|
|
|
|
$this->assertEquals($isMeeting, $data['is_meeting']);
|
|
|
|
$isMeeting = !$isMeeting;
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
|
|
|
|
// Is no meeting
|
|
|
|
$controller->edit($this->request);
|
|
|
|
|
|
|
|
// Is meeting
|
|
|
|
$this->request->query->set('meeting', 1);
|
|
|
|
$controller->edit($this->request);
|
|
|
|
|
|
|
|
// Should stay no meeting
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', 1);
|
2020-05-12 00:12:49 +02:00
|
|
|
$controller->edit($this->request);
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:54:45 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::save
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveCreateInvalid(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$controller->save($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveCreateEditProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
2023-12-21 13:41:10 +01:00
|
|
|
// Text, isMeeting, id, sendNotification
|
2021-12-30 19:18:54 +01:00
|
|
|
['Some test', true],
|
|
|
|
['Some test', false],
|
|
|
|
['Some test', false, 1],
|
|
|
|
['Some test', true, 1],
|
2023-12-21 13:41:10 +01:00
|
|
|
['Some test', false, null, true],
|
|
|
|
['Some test', false, 1, true],
|
2020-04-05 16:54:45 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::save
|
|
|
|
* @dataProvider saveCreateEditProvider
|
|
|
|
*
|
|
|
|
* @param int|null $id
|
|
|
|
*/
|
|
|
|
public function testSaveCreateEdit(
|
|
|
|
string $text,
|
|
|
|
bool $isMeeting,
|
2023-12-21 13:41:10 +01:00
|
|
|
int $id = null,
|
|
|
|
bool $sendNotification = false
|
2022-12-14 19:15:20 +01:00
|
|
|
): void {
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', $id);
|
2020-06-01 12:25:30 +02:00
|
|
|
$body = [
|
2023-12-21 13:41:10 +01:00
|
|
|
'title' => 'Some Title',
|
|
|
|
'text' => $text,
|
2020-06-01 12:25:30 +02:00
|
|
|
];
|
|
|
|
if ($isMeeting) {
|
|
|
|
$body['is_meeting'] = '1';
|
|
|
|
}
|
2023-12-21 13:41:10 +01:00
|
|
|
if ($sendNotification) {
|
|
|
|
$body['send_notification'] = '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch')
|
|
|
|
->willReturnCallback(function (string $event, array $payload) use ($id, $sendNotification) {
|
|
|
|
$this->assertEquals($id ? 'news.updated' : 'news.created', $event);
|
|
|
|
$this->assertEquals($sendNotification, $payload['sendNotification']);
|
|
|
|
$this->assertInstanceOf(News::class, $payload['news']);
|
|
|
|
|
|
|
|
return $this->eventDispatcher;
|
|
|
|
});
|
2020-06-01 12:25:30 +02:00
|
|
|
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->addUser();
|
2023-12-21 13:41:10 +01:00
|
|
|
$this->request = $this->request->withParsedBody($body);
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
2020-12-18 17:52:45 +01:00
|
|
|
->with('http://localhost/news')
|
2020-04-05 16:54:45 +02:00
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$controller->save($this->request);
|
|
|
|
|
|
|
|
$this->assertTrue($this->log->hasInfoThatContains('Updated'));
|
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNotification('news.edit.success');
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2023-12-21 13:41:10 +01:00
|
|
|
$news = (new News())->find($id ?: 2);
|
2021-12-30 19:18:54 +01:00
|
|
|
$this->assertEquals($text, $news->text);
|
2022-12-25 11:59:45 +01:00
|
|
|
$this->assertEquals($isMeeting, (bool) $news->is_meeting);
|
2020-04-05 16:54:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 00:12:29 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::save
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSavePreview(): void
|
2020-10-21 00:12:29 +02:00
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', 1);
|
2020-10-21 00:12:29 +02:00
|
|
|
$this->request = $this->request->withParsedBody([
|
2023-09-27 18:15:12 +02:00
|
|
|
'title' => 'New title',
|
|
|
|
'text' => 'New text',
|
|
|
|
'is_meeting' => '1',
|
|
|
|
'is_pinned' => '1',
|
|
|
|
'is_highlighted' => '1',
|
|
|
|
'preview' => '1',
|
2023-12-21 13:41:10 +01:00
|
|
|
'send_notification' => '1',
|
2020-10-21 00:12:29 +02:00
|
|
|
]);
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function ($view, $data) {
|
|
|
|
$this->assertEquals('pages/news/edit.twig', $view);
|
|
|
|
|
|
|
|
/** @var News $news */
|
|
|
|
$news = $data['news'];
|
|
|
|
// Contains new text
|
|
|
|
$this->assertTrue($news->is_meeting);
|
2020-12-27 02:51:05 +01:00
|
|
|
$this->assertTrue($news->is_pinned);
|
2023-09-27 18:15:12 +02:00
|
|
|
$this->assertTrue($news->is_highlighted);
|
2020-10-21 00:12:29 +02:00
|
|
|
$this->assertEquals('New title', $news->title);
|
|
|
|
$this->assertEquals('New text', $news->text);
|
|
|
|
|
2023-12-21 13:41:10 +01:00
|
|
|
$this->assertTrue($data['send_notification']);
|
|
|
|
|
2020-10-21 00:12:29 +02:00
|
|
|
return $this->response;
|
|
|
|
});
|
2023-02-13 21:19:45 +01:00
|
|
|
$this->auth->expects($this->atLeastOnce())
|
|
|
|
->method('can')
|
2023-09-27 18:15:12 +02:00
|
|
|
->with('news.highlight')
|
2023-02-13 21:19:45 +01:00
|
|
|
->willReturn(true);
|
2020-10-21 00:12:29 +02:00
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$controller->save($this->request);
|
|
|
|
|
|
|
|
// Assert no changes
|
|
|
|
$news = News::find(1);
|
|
|
|
$this->assertEquals('Foo', $news->title);
|
2021-12-30 19:18:54 +01:00
|
|
|
$this->assertEquals('**foo**', $news->text);
|
2020-10-21 00:12:29 +02:00
|
|
|
$this->assertFalse($news->is_meeting);
|
2020-12-27 02:51:05 +01:00
|
|
|
$this->assertFalse($news->is_pinned);
|
2023-09-27 18:15:12 +02:00
|
|
|
$this->assertFalse($news->is_highlighted);
|
2020-10-21 00:12:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:54:45 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\NewsController::save
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSaveDelete(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', 1);
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->request = $this->request->withParsedBody([
|
|
|
|
'title' => '.',
|
|
|
|
'text' => '.',
|
|
|
|
'delete' => '1',
|
|
|
|
]);
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
2020-04-07 15:08:49 +02:00
|
|
|
->with('http://localhost/news')
|
2020-04-05 16:54:45 +02:00
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$controller->save($this->request);
|
|
|
|
|
|
|
|
$this->assertTrue($this->log->hasInfoThatContains('Deleted'));
|
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->assertHasNotification('news.delete.success');
|
2020-04-05 16:54:45 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 17:59:23 +02:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:54:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new user
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function addUser(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2023-12-21 13:41:10 +01:00
|
|
|
$user = User::factory(['id' => 42])
|
|
|
|
->has(Settings::factory(['email_news' => true]))
|
|
|
|
->create();
|
2020-04-05 16:54:45 +02:00
|
|
|
|
|
|
|
$this->auth->expects($this->any())
|
|
|
|
->method('user')
|
|
|
|
->willReturn($user);
|
|
|
|
}
|
2020-12-15 13:39:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup environment
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->auth = $this->createMock(Authenticator::class);
|
|
|
|
$this->app->instance(Authenticator::class, $this->auth);
|
|
|
|
|
2023-12-21 13:41:10 +01:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
|
|
|
|
$this->eventDispatcher->expects(self::any())
|
2022-12-14 19:15:20 +01:00
|
|
|
->method('dispatch')
|
|
|
|
->willReturnSelf();
|
2023-12-21 13:41:10 +01:00
|
|
|
$this->app->instance('events.dispatcher', $this->eventDispatcher);
|
2020-12-28 16:04:05 +01:00
|
|
|
|
2023-10-24 23:00:04 +02:00
|
|
|
$user = User::factory()->create();
|
2020-12-15 13:39:00 +01:00
|
|
|
(new News([
|
|
|
|
'title' => 'Foo',
|
2021-12-30 19:18:54 +01:00
|
|
|
'text' => '**foo**',
|
2023-10-24 23:00:04 +02:00
|
|
|
'user_id' => $user->id,
|
2020-12-15 13:39:00 +01:00
|
|
|
]))->save();
|
|
|
|
}
|
2020-04-05 16:54:45 +02:00
|
|
|
}
|