|
|
@ -2,30 +2,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
use Engelsystem\Config\Config;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\NewsController;
|
|
|
|
use Engelsystem\Controllers\NewsController;
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
|
|
|
|
use Engelsystem\Http\Exceptions\HttpForbidden;
|
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
|
|
|
|
use Engelsystem\Http\Response;
|
|
|
|
|
|
|
|
use Engelsystem\Http\UrlGenerator;
|
|
|
|
|
|
|
|
use Engelsystem\Http\UrlGeneratorInterface;
|
|
|
|
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Models\News;
|
|
|
|
use Engelsystem\Models\News;
|
|
|
|
use Engelsystem\Models\NewsComment;
|
|
|
|
use Engelsystem\Models\NewsComment;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
use Psr\Log\Test\TestLogger;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NewsControllerTest extends TestCase
|
|
|
|
class NewsControllerTest extends ControllerTest
|
|
|
|
{
|
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
|
|
|
@ -75,15 +65,6 @@ class NewsControllerTest extends TestCase
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/** @var TestLogger */
|
|
|
|
|
|
|
|
protected $log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @var Response|MockObject */
|
|
|
|
|
|
|
|
protected $response;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @var Request */
|
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::__construct
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::__construct
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::index
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::index
|
|
|
@ -225,51 +206,108 @@ class NewsControllerTest extends TestCase
|
|
|
|
$this->log->hasInfoThatContains('Created news comment');
|
|
|
|
$this->log->hasInfoThatContains('Created news comment');
|
|
|
|
|
|
|
|
|
|
|
|
/** @var NewsComment $comment */
|
|
|
|
/** @var NewsComment $comment */
|
|
|
|
$comment = NewsComment::whereNewsId(1)->first();
|
|
|
|
$comment = NewsComment::whereNewsId(1)->get()[2];
|
|
|
|
$this->assertEquals('Foo bar!', $comment->text);
|
|
|
|
$this->assertEquals('Foo bar!', $comment->text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Setup environment
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
public function testDeleteCommentInvalidRequest()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
$this->initDatabase();
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
|
|
|
|
|
|
|
|
|
|
|
$this->request = new Request();
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$this->app->instance('request', $this->request);
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
$this->app->instance(Request::class, $this->request);
|
|
|
|
}
|
|
|
|
$this->app->instance(ServerRequestInterface::class, $this->request);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->response = $this->createMock(Response::class);
|
|
|
|
/**
|
|
|
|
$this->app->instance(Response::class, $this->response);
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testDeleteCommentNotFound()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->request = $this->request->withAttribute('id', 42)->withParsedBody(['delete' => '1']);
|
|
|
|
|
|
|
|
|
|
|
|
$this->app->instance(Config::class, new Config(['display_news' => 2]));
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
|
|
|
|
|
|
|
|
|
|
|
$this->log = new TestLogger();
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$this->app->instance(LoggerInterface::class, $this->log);
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->app->instance('session', new Session(new MockArraySessionStorage()));
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testDeleteCommentNotAllowed()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->request = $this->request->withAttribute('id', 2)->withParsedBody(['delete' => '1']);
|
|
|
|
|
|
|
|
|
|
|
|
$this->auth = $this->createMock(Authenticator::class);
|
|
|
|
$this->addUser(1);
|
|
|
|
$this->app->instance(Authenticator::class, $this->auth);
|
|
|
|
$this->addUser(2);
|
|
|
|
|
|
|
|
|
|
|
|
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
|
|
|
|
/** @var NewsController $controller */
|
|
|
|
|
|
|
|
$controller = $this->app->get(NewsController::class);
|
|
|
|
|
|
|
|
$controller->setValidator($this->app->get(Validator::class));
|
|
|
|
|
|
|
|
|
|
|
|
$this->app->instance('config', new Config());
|
|
|
|
$this->expectException(HttpForbidden::class);
|
|
|
|
|
|
|
|
$controller->deleteComment($this->request);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Controllers\NewsController::deleteComment
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testDeleteComment()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->request = $this->request->withAttribute('id', 1)->withParsedBody(['delete' => '1']);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->data as $news) {
|
|
|
|
foreach ($this->data as $news) {
|
|
|
|
(new News($news))->save();
|
|
|
|
(new News($news))->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ([1, 2] as $i) {
|
|
|
|
|
|
|
|
NewsComment::create([
|
|
|
|
|
|
|
|
'news_id' => 1,
|
|
|
|
|
|
|
|
'text' => 'test comment ' . $i,
|
|
|
|
|
|
|
|
'user_id' => $i,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a new user
|
|
|
|
* Creates a new user
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function addUser()
|
|
|
|
protected function addUser(int $id = 42)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$user = User::factory()->create(['id' => 42]);
|
|
|
|
$user = User::factory()->create(['id' => $id]);
|
|
|
|
|
|
|
|
|
|
|
|
$this->auth->expects($this->any())
|
|
|
|
$this->auth->expects($this->any())
|
|
|
|
->method('user')
|
|
|
|
->method('user')
|
|
|
|