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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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',
|
|
|
|
'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,
|
|
|
|
],
|
|
|
|
[
|
2023-09-27 18:15:12 +02:00
|
|
|
'title' => 'baz',
|
|
|
|
'text' => 'baz',
|
|
|
|
'is_meeting' => true,
|
|
|
|
'is_pinned' => true,
|
|
|
|
'is_highlighted' => true,
|
|
|
|
'user_id' => 1,
|
2020-04-05 16:54:45 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Lorem',
|
|
|
|
'text' => 'lorem',
|
|
|
|
'is_meeting' => false,
|
2023-09-25 17:43:39 +02:00
|
|
|
'is_pinned' => false,
|
2020-04-05 16:54:45 +02:00
|
|
|
'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,
|
2023-09-25 17:43:39 +02:00
|
|
|
'is_pinned' => false,
|
2020-04-05 16:54:45 +02:00
|
|
|
'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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIndex(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
|
|
|
$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);
|
2023-02-09 14:26:48 +01:00
|
|
|
$this->assertEquals('Bar', $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());
|
2023-08-14 16:30:14 +02:00
|
|
|
$this->assertEquals(2, $data['pages']);
|
|
|
|
$this->assertEquals(2, $data['page']);
|
2020-04-05 16:54:45 +02:00
|
|
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testShow(): 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')
|
|
|
|
->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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testShowNotFound(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testCommentInvalid(): 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->comment($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::comment
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testCommentNewsNotFound(): void
|
2020-04-05 16:54:45 +02:00
|
|
|
{
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testComment(): 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(['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
|
|
|
*/
|
2022-12-14 19:15:20 +01: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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testDeleteCommentNotFound(): void
|
2021-07-14 01:56:03 +02:00
|
|
|
{
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testDeleteCommentNotAllowed(): void
|
2021-07-14 01:56:03 +02:00
|
|
|
{
|
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
|
|
|
/** @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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testDeleteComment(): void
|
2021-07-14 01:56:03 +02:00
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
/** @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
|
|
|
|
2023-10-24 23:00:04 +02:00
|
|
|
$this->addUser(1);
|
|
|
|
$this->addUser(2);
|
|
|
|
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01: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);
|
|
|
|
}
|
|
|
|
}
|