engelsystem/tests/Unit/Controllers/Admin/NewsControllerTest.php

269 lines
8.0 KiB
PHP
Raw Permalink Normal View History

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
{
/** @var Authenticator|MockObject */
protected $auth;
/** @var array */
protected $data = [
[
'title' => 'Foo',
'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
*/
public function testEdit()
{
$this->request->attributes->set('id', 1);
$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);
}
/**
* @covers \Engelsystem\Controllers\Admin\NewsController::edit
*/
public function testEditIsMeeting()
{
$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
$this->request->attributes->set('id', 1);
$controller->edit($this->request);
}
2020-04-05 16:54:45 +02:00
/**
* @covers \Engelsystem\Controllers\Admin\NewsController::save
*/
public function testSaveCreateInvalid()
{
/** @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 [
['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 string $text
* @param bool $isMeeting
* @param int|null $id
*/
public function testSaveCreateEdit(
string $text,
bool $isMeeting,
int $id = null
) {
$this->request->attributes->set('id', $id);
$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')
->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);
$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
*/
public function testSavePreview()
{
$this->request->attributes->set('id', 1);
$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);
$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
*/
public function testSaveDelete()
{
$this->request->attributes->set('id', 1);
$this->request = $this->request->withParsedBody([
'title' => '.',
'text' => '.',
'delete' => '1',
]);
$this->response->expects($this->once())
->method('redirectTo')
->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
*/
protected function addUser()
{
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);
$this->app->instance('events.dispatcher', $eventDispatcher);
2020-12-15 13:39:00 +01:00
(new News([
'title' => 'Foo',
'text' => '**foo**',
2020-12-15 13:39:00 +01:00
'user_id' => 1,
]))->save();
}
2020-04-05 16:54:45 +02:00
}