2020-04-05 16:54:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Admin;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\Admin\NewsController;
|
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;
|
|
|
|
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 Illuminate\Support\Collection;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
|
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;
|
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,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
/** @var Collection $warnings */
|
|
|
|
$warnings = $data['warnings'];
|
|
|
|
$this->assertNotEmpty($data['news']);
|
|
|
|
$this->assertTrue($warnings->isEmpty());
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
|
|
|
|
$controller->edit($this->request);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function saveCreateEditProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-12-30 19:18:54 +01:00
|
|
|
['Some test', true],
|
|
|
|
['Some test', false],
|
|
|
|
['Some test', false, 1],
|
|
|
|
['Some test', true, 1],
|
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,
|
|
|
|
int $id = null
|
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-04-05 16:54:45 +02:00
|
|
|
$id = $id ?: 2;
|
2020-06-01 12:25:30 +02:00
|
|
|
$body = [
|
2020-04-05 16:54:45 +02:00
|
|
|
'title' => 'Some Title',
|
|
|
|
'text' => $text,
|
2020-06-01 12:25:30 +02:00
|
|
|
];
|
|
|
|
if ($isMeeting) {
|
|
|
|
$body['is_meeting'] = '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request = $this->request->withParsedBody($body);
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->addUser();
|
|
|
|
$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'));
|
|
|
|
|
|
|
|
/** @var Session $session */
|
|
|
|
$session = $this->app->get('session');
|
|
|
|
$messages = $session->get('messages');
|
|
|
|
$this->assertEquals('news.edit.success', $messages[0]);
|
|
|
|
|
|
|
|
$news = (new News())->find($id);
|
2021-12-30 19:18:54 +01:00
|
|
|
$this->assertEquals($text, $news->text);
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->assertEquals($isMeeting, (bool)$news->is_meeting);
|
|
|
|
}
|
|
|
|
|
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([
|
|
|
|
'title' => 'New title',
|
|
|
|
'text' => 'New text',
|
|
|
|
'is_meeting' => '1',
|
2020-12-27 02:51:05 +01:00
|
|
|
'is_pinned' => '1',
|
2020-10-21 00:12:29 +02:00
|
|
|
'preview' => '1',
|
|
|
|
]);
|
|
|
|
$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);
|
2020-10-21 00:12:29 +02:00
|
|
|
$this->assertEquals('New title', $news->title);
|
|
|
|
$this->assertEquals('New text', $news->text);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
/** @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);
|
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'));
|
|
|
|
|
|
|
|
/** @var Session $session */
|
|
|
|
$session = $this->app->get('session');
|
|
|
|
$messages = $session->get('messages');
|
|
|
|
$this->assertEquals('news.delete.success', $messages[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new user
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function addUser(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2021-06-29 00:27:57 +02:00
|
|
|
$user = User::factory(['id' => 42])->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);
|
|
|
|
|
2020-12-28 16:04:05 +01:00
|
|
|
$eventDispatcher = $this->createMock(EventDispatcher::class);
|
2022-12-14 19:15:20 +01:00
|
|
|
$eventDispatcher->expects(self::any())
|
|
|
|
->method('dispatch')
|
|
|
|
->willReturnSelf();
|
2020-12-28 16:04:05 +01:00
|
|
|
$this->app->instance('events.dispatcher', $eventDispatcher);
|
|
|
|
|
2020-12-15 13:39:00 +01:00
|
|
|
(new News([
|
|
|
|
'title' => 'Foo',
|
2021-12-30 19:18:54 +01:00
|
|
|
'text' => '**foo**',
|
2020-12-15 13:39:00 +01:00
|
|
|
'user_id' => 1,
|
|
|
|
]))->save();
|
|
|
|
}
|
2020-04-05 16:54:45 +02:00
|
|
|
}
|