engelsystem/tests/Unit/Controllers/AuthControllerTest.php

205 lines
7.0 KiB
PHP
Raw Normal View History

2018-10-25 18:53:05 +02:00
<?php
declare(strict_types=1);
2018-10-25 18:53:05 +02:00
namespace Engelsystem\Test\Unit\Controllers;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Config\Config;
2018-10-25 18:53:05 +02:00
use Engelsystem\Controllers\AuthController;
2023-02-02 22:53:51 +01:00
use Engelsystem\Controllers\NotificationType;
use Engelsystem\Helpers\Authenticator;
2019-07-09 22:02:07 +02:00
use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request;
2018-10-25 18:53:05 +02:00
use Engelsystem\Http\Response;
2019-07-09 22:02:07 +02:00
use Engelsystem\Http\Validation\Validator;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
2018-10-25 18:53:05 +02:00
use PHPUnit\Framework\MockObject\MockObject;
2019-07-09 22:02:07 +02:00
use Symfony\Component\HttpFoundation\Session\Session;
2018-10-25 18:53:05 +02:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2019-07-09 22:02:07 +02:00
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
2018-10-25 18:53:05 +02:00
2023-02-02 22:53:51 +01:00
class AuthControllerTest extends ControllerTest
2018-10-25 18:53:05 +02:00
{
use ArraySubsetAsserts;
use HasDatabase;
2018-10-25 18:53:05 +02:00
/**
* @covers \Engelsystem\Controllers\AuthController::__construct
* @covers \Engelsystem\Controllers\AuthController::login
2019-07-09 22:02:07 +02:00
* @covers \Engelsystem\Controllers\AuthController::showLogin
2018-10-25 18:53:05 +02:00
*/
public function testLogin(): void
2018-10-25 18:53:05 +02:00
{
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var SessionInterface|MockObject $session */
/** @var Redirector|MockObject $redirect */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list(, $session, $redirect, $config, $auth) = $this->getMocks();
2018-10-25 18:53:05 +02:00
$response->expects($this->once())
->method('withView')
->with('pages/login')
->willReturn($response);
$controller = new AuthController($response, $session, $redirect, $config, $auth);
$controller->login();
}
/**
* @covers \Engelsystem\Controllers\AuthController::postLogin
*/
public function testPostLogin(): void
{
$this->initDatabase();
2018-10-25 18:53:05 +02:00
$request = new Request();
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var Redirector|MockObject $redirect */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list(, , $redirect, $config, $auth) = $this->getMocks();
2023-02-02 22:53:51 +01:00
$this->session = new Session(new MockArraySessionStorage());
$this->app->instance('session', $this->session);
2019-07-09 22:02:07 +02:00
/** @var Validator|MockObject $validator */
$validator = new Validator();
2020-11-15 18:47:30 +01:00
$user = $this->createUser();
$auth->expects($this->exactly(2))
->method('authenticate')
->with('foo', 'bar')
->willReturnOnConsecutiveCalls(null, $user);
2019-07-09 22:02:07 +02:00
$response->expects($this->once())
->method('withView')
->willReturnCallback(function ($view, $data = []) use ($response) {
$this->assertEquals('pages/login', $view);
return $response;
});
2020-11-15 18:47:30 +01:00
/** @var AuthController|MockObject $controller */
$controller = $this->getMockBuilder(AuthController::class)
2023-02-02 22:53:51 +01:00
->setConstructorArgs([$response, $this->session, $redirect, $config, $auth])
2020-11-15 18:47:30 +01:00
->onlyMethods(['loginUser'])
->getMock();
$controller->setValidator($validator);
$controller->expects($this->once())
->method('loginUser')
->with($user)
->willReturn($response);
2019-07-09 22:02:07 +02:00
// No credentials
try {
$controller->postLogin($request);
$this->fail('Login without credentials possible');
2024-03-25 00:03:39 +01:00
} catch (ValidationException) {
2019-07-09 22:02:07 +02:00
}
// Missing password
$request = new Request([], ['login' => 'foo']);
try {
$controller->postLogin($request);
$this->fail('Login without password possible');
2024-03-25 00:03:39 +01:00
} catch (ValidationException) {
2019-07-09 22:02:07 +02:00
}
// No user found
2019-07-09 22:02:07 +02:00
$request = new Request([], ['login' => 'foo', 'password' => 'bar']);
$controller->postLogin($request);
2023-02-02 22:53:51 +01:00
$this->assertHasNotification('auth.not-found', NotificationType::ERROR);
2019-07-09 22:02:07 +02:00
// Authenticated user
$controller->postLogin($request);
2020-11-15 18:47:30 +01:00
}
/**
* @covers \Engelsystem\Controllers\AuthController::loginUser
*/
public function testLoginUser(): void
2020-11-15 18:47:30 +01:00
{
$this->initDatabase();
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var Redirector|MockObject $redirect */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list(, , $redirect, $config, $auth) = $this->getMocks();
$session = new Session(new MockArraySessionStorage());
$session->set('foo', 'bar');
$user = $this->createUser();
$redirect->expects($this->exactly(2))
2020-11-15 18:47:30 +01:00
->method('to')
->withConsecutive(['news'], ['/test'])
2020-11-15 18:47:30 +01:00
->willReturn($response);
$controller = new AuthController($response, $session, $redirect, $config, $auth);
$controller->loginUser($user);
2020-11-15 18:47:30 +01:00
$this->assertFalse($session->has('foo'));
$this->assertNotNull($user->last_login_at);
2019-07-09 22:02:07 +02:00
$this->assertEquals(['user_id' => 42, 'locale' => 'de_DE'], $session->all());
// Redirect to previous page
$session->set('previous_page', '/test');
$controller->loginUser($user);
}
/**
* @covers \Engelsystem\Controllers\AuthController::logout
*/
public function testLogout(): void
{
/** @var Response $response */
/** @var SessionInterface|MockObject $session */
/** @var Redirector|MockObject $redirect */
/** @var Config $config */
/** @var Authenticator|MockObject $auth */
list($response, $session, $redirect, $config, $auth) = $this->getMocks();
$session->expects($this->once())
->method('invalidate');
2018-10-25 18:53:05 +02:00
$redirect->expects($this->once())
2018-10-25 18:53:05 +02:00
->method('to')
->with('/')
->willReturn($response);
2018-10-25 18:53:05 +02:00
$controller = new AuthController($response, $session, $redirect, $config, $auth);
$return = $controller->logout();
$this->assertEquals($response, $return);
}
2020-11-15 18:47:30 +01:00
protected function createUser(): User
{
2021-06-29 00:27:57 +02:00
return User::factory(['id' => 42])
->has(Settings::factory(['language' => 'de_DE']))
->create();
2020-11-15 18:47:30 +01:00
}
protected function getMocks(): array
{
$response = new Response();
/** @var SessionInterface|MockObject $session */
$session = $this->getMockForAbstractClass(SessionInterface::class);
/** @var Redirector|MockObject $redirect */
$redirect = $this->createMock(Redirector::class);
$config = new Config(['home_site' => 'news']);
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
$this->app->instance('session', $session);
return [$response, $session, $redirect, $config, $auth];
2018-10-25 18:53:05 +02:00
}
}