2020-04-05 16:54:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\NewsController;
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2021-07-14 01:56:03 +02:00
|
|
|
use Engelsystem\Http\Exceptions\HttpForbidden;
|
2020-04-05 16:54:45 +02:00
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Models\News;
|
|
|
|
use Engelsystem\Models\NewsComment;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
class NewsControllerTest extends ControllerTest
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
protected Authenticator|MockObject $auth;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected array $data = [
|
|
|
|
[
|
|
|
|
'title' => 'Foo',
|
|
|
|
'text' => 'foo',
|
|
|
|
'is_meeting' => false,
|
|
|
|
'user_id' => 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Bar',
|
|
|
|
'text' => 'bar',
|
|
|
|
'is_meeting' => false,
|
2020-12-27 02:51:05 +01:00
|
|
|
'is_pinned' => true,
|
2020-04-05 16:54:45 +02:00
|
|
|
'user_id' => 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'baz',
|
|
|
|
'text' => 'baz',
|
|
|
|
'is_meeting' => true,
|
2020-12-27 02:51:05 +01:00
|
|
|
'is_pinned' => true,
|
2020-04-05 16:54:45 +02:00
|
|
|
'user_id' => 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Lorem',
|
|
|
|
'text' => 'lorem',
|
|
|
|
'is_meeting' => false,
|
|
|
|
'user_id' => 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Ipsum',
|
|
|
|
'text' => 'ipsum',
|
|
|
|
'is_meeting' => true,
|
2020-12-27 02:51:05 +01:00
|
|
|
'is_pinned' => true,
|
2020-04-05 16:54:45 +02:00
|
|
|
'user_id' => 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Dolor',
|
|
|
|
'text' => 'test',
|
|
|
|
'is_meeting' => true,
|
|
|
|
'user_id' => 1,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::__construct
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::index
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::meetings
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::showOverview
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::renderView
|
|
|
|
*/
|
|
|
|
public function testIndex(): void
|
|
|
|
{
|
|
|
|
$this->request->attributes->set('page', 2);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
|
|
|
|
$n = 1;
|
|
|
|
$this->response->expects($this->exactly(3))
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(
|
|
|
|
function (string $page, array $data) use (&$n) {
|
|
|
|
$this->assertEquals('pages/news/overview.twig', $page);
|
|
|
|
/** @var Collection $news */
|
|
|
|
$news = $data['news'];
|
|
|
|
|
|
|
|
switch ($n) {
|
|
|
|
case 1:
|
|
|
|
// Show everything
|
|
|
|
$this->assertFalse($data['only_meetings']);
|
|
|
|
$this->assertTrue($news->isNotEmpty());
|
|
|
|
$this->assertEquals(3, $data['pages']);
|
|
|
|
$this->assertEquals(2, $data['page']);
|
2020-12-27 02:51:05 +01:00
|
|
|
$this->assertTrue($news[0]->is_pinned);
|
|
|
|
$this->assertEquals('Ipsum', $news[0]->title);
|
2020-04-05 16:54:45 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// Show meetings
|
|
|
|
$this->assertTrue($data['only_meetings']);
|
|
|
|
$this->assertTrue($news->isNotEmpty());
|
|
|
|
$this->assertEquals(1, $data['pages']);
|
|
|
|
$this->assertEquals(1, $data['page']);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// No news found
|
|
|
|
$this->assertTrue($news->isEmpty());
|
|
|
|
$this->assertEquals(1, $data['pages']);
|
|
|
|
$this->assertEquals(1, $data['page']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$n++;
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$controller->index();
|
|
|
|
$controller->meetings();
|
|
|
|
|
|
|
|
News::query()->truncate();
|
|
|
|
$controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::show
|
|
|
|
*/
|
|
|
|
public function testShow(): void
|
|
|
|
{
|
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')
|
|
|
|
->with('pages/news/news.twig')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
|
|
|
|
$controller->show($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::show
|
|
|
|
*/
|
|
|
|
public function testShowNotFound(): void
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', 42);
|
2020-04-05 16:54:45 +02:00
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$controller->show($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::comment
|
|
|
|
*/
|
|
|
|
public function testCommentInvalid(): void
|
|
|
|
{
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$controller->comment($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::comment
|
|
|
|
*/
|
|
|
|
public function testCommentNewsNotFound(): void
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request->attributes->set('news_id', 42);
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['comment' => 'Foo bar!']);
|
|
|
|
$this->addUser();
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$controller->comment($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::comment
|
|
|
|
*/
|
|
|
|
public function testComment(): void
|
|
|
|
{
|
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(['comment' => 'Foo bar!']);
|
|
|
|
$this->addUser();
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->make(NewsController::class);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
$controller->comment($this->request);
|
|
|
|
$this->log->hasInfoThatContains('Created news comment');
|
|
|
|
|
|
|
|
/** @var NewsComment $comment */
|
2021-07-14 01:56:03 +02:00
|
|
|
$comment = NewsComment::whereNewsId(1)->get()[2];
|
2020-04-05 16:54:45 +02:00
|
|
|
$this->assertEquals('Foo bar!', $comment->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-14 01:56:03 +02:00
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
2020-04-05 16:54:45 +02:00
|
|
|
*/
|
2021-07-14 01:56:03 +02:00
|
|
|
public function testDeleteCommentInvalidRequest(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2021-07-14 01:56:03 +02:00
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
}
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
*/
|
|
|
|
public function testDeleteCommentNotFound(): void
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request = $this->request->withAttribute('news_id', 42)->withParsedBody(['delete' => '1']);
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
}
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
*/
|
|
|
|
public function testDeleteCommentNotAllowed(): void
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request = $this->request->withAttribute('comment_id', 2)->withParsedBody(['delete' => '1']);
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
$this->addUser(1);
|
|
|
|
$this->addUser(2);
|
2020-04-05 16:54:45 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
2020-04-07 15:08:49 +02:00
|
|
|
|
2021-07-14 01:56:03 +02:00
|
|
|
$this->expectException(HttpForbidden::class);
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
*/
|
|
|
|
public function testDeleteComment(): void
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$this->request = $this->request->withAttribute('comment_id', 1)->withParsedBody(['delete' => '1']);
|
2021-07-14 01:56:03 +02:00
|
|
|
$this->setExpects($this->response, 'redirectTo', ['http://localhost/news/1'], $this->response);
|
|
|
|
|
|
|
|
$this->addUser(1);
|
|
|
|
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
|
|
|
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
|
|
|
|
$this->assertCount(1, NewsComment::all());
|
|
|
|
$this->assertTrue($this->log->hasInfoThatContains('Deleted comment'));
|
|
|
|
$this->assertHasNotification('news.comment-delete.success');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup environment
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->config->set(['display_news' => 2]);
|
|
|
|
|
|
|
|
$this->auth = $this->createMock(Authenticator::class);
|
|
|
|
$this->app->instance(Authenticator::class, $this->auth);
|
2020-04-07 15:08:49 +02:00
|
|
|
|
2020-04-05 16:54:45 +02:00
|
|
|
foreach ($this->data as $news) {
|
|
|
|
(new News($news))->save();
|
|
|
|
}
|
2021-07-14 01:56:03 +02:00
|
|
|
|
|
|
|
foreach ([1, 2] as $i) {
|
|
|
|
NewsComment::create([
|
|
|
|
'news_id' => 1,
|
|
|
|
'text' => 'test comment ' . $i,
|
|
|
|
'user_id' => $i,
|
|
|
|
]);
|
|
|
|
}
|
2020-04-05 16:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new user
|
|
|
|
*/
|
2021-07-14 01:56:03 +02:00
|
|
|
protected function addUser(int $id = 42): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
2021-07-14 01:56:03 +02:00
|
|
|
$user = User::factory()->create(['id' => $id]);
|
2020-04-05 16:54:45 +02:00
|
|
|
|
|
|
|
$this->auth->expects($this->any())
|
|
|
|
->method('user')
|
|
|
|
->willReturn($user);
|
|
|
|
}
|
|
|
|
}
|