Render login page through Slim controller
This commit is contained in:
parent
1410dce2db
commit
6c50a6ccce
20 changed files with 728 additions and 100 deletions
tests
49
tests/container/ContainerBuilderTest.php
Normal file
49
tests/container/ContainerBuilderTest.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shaarli\Container;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shaarli\Bookmark\BookmarkServiceInterface;
|
||||
use Shaarli\Config\ConfigManager;
|
||||
use Shaarli\History;
|
||||
use Shaarli\Render\PageBuilder;
|
||||
use Shaarli\Security\LoginManager;
|
||||
use Shaarli\Security\SessionManager;
|
||||
|
||||
class ContainerBuilderTest extends TestCase
|
||||
{
|
||||
/** @var ConfigManager */
|
||||
protected $conf;
|
||||
|
||||
/** @var SessionManager */
|
||||
protected $sessionManager;
|
||||
|
||||
/** @var LoginManager */
|
||||
protected $loginManager;
|
||||
|
||||
/** @var ContainerBuilder */
|
||||
protected $containerBuilder;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
||||
$this->sessionManager = $this->createMock(SessionManager::class);
|
||||
$this->loginManager = $this->createMock(LoginManager::class);
|
||||
|
||||
$this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager);
|
||||
}
|
||||
|
||||
public function testBuildContainer(): void
|
||||
{
|
||||
$container = $this->containerBuilder->build();
|
||||
|
||||
static::assertInstanceOf(ConfigManager::class, $container->conf);
|
||||
static::assertInstanceOf(SessionManager::class, $container->sessionManager);
|
||||
static::assertInstanceOf(LoginManager::class, $container->loginManager);
|
||||
static::assertInstanceOf(History::class, $container->history);
|
||||
static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService);
|
||||
static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
|
||||
}
|
||||
}
|
70
tests/front/ShaarliMiddlewareTest.php
Normal file
70
tests/front/ShaarliMiddlewareTest.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shaarli\Front;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shaarli\Config\ConfigManager;
|
||||
use Shaarli\Container\ShaarliContainer;
|
||||
use Shaarli\Front\Exception\LoginBannedException;
|
||||
use Shaarli\Render\PageBuilder;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
class ShaarliMiddlewareTest extends TestCase
|
||||
{
|
||||
/** @var ShaarliContainer */
|
||||
protected $container;
|
||||
|
||||
/** @var ShaarliMiddleware */
|
||||
protected $middleware;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->container = $this->createMock(ShaarliContainer::class);
|
||||
$this->middleware = new ShaarliMiddleware($this->container);
|
||||
}
|
||||
|
||||
public function testMiddlewareExecution(): void
|
||||
{
|
||||
$request = $this->createMock(Request::class);
|
||||
$response = new Response();
|
||||
$controller = function (Request $request, Response $response): Response {
|
||||
return $response->withStatus(418); // I'm a tea pot
|
||||
};
|
||||
|
||||
/** @var Response $result */
|
||||
$result = $this->middleware->__invoke($request, $response, $controller);
|
||||
|
||||
static::assertInstanceOf(Response::class, $result);
|
||||
static::assertSame(418, $result->getStatusCode());
|
||||
}
|
||||
|
||||
public function testMiddlewareExecutionWithException(): void
|
||||
{
|
||||
$request = $this->createMock(Request::class);
|
||||
$response = new Response();
|
||||
$controller = function (): void {
|
||||
$exception = new LoginBannedException();
|
||||
|
||||
throw new $exception;
|
||||
};
|
||||
|
||||
$pageBuilder = $this->createMock(PageBuilder::class);
|
||||
$pageBuilder->method('render')->willReturnCallback(function (string $message): string {
|
||||
return $message;
|
||||
});
|
||||
$this->container->pageBuilder = $pageBuilder;
|
||||
|
||||
$conf = $this->createMock(ConfigManager::class);
|
||||
$this->container->conf = $conf;
|
||||
|
||||
/** @var Response $result */
|
||||
$result = $this->middleware->__invoke($request, $response, $controller);
|
||||
|
||||
static::assertInstanceOf(Response::class, $result);
|
||||
static::assertSame(401, $result->getStatusCode());
|
||||
static::assertContains('error', (string) $result->getBody());
|
||||
}
|
||||
}
|
173
tests/front/controller/LoginControllerTest.php
Normal file
173
tests/front/controller/LoginControllerTest.php
Normal file
|
@ -0,0 +1,173 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shaarli\Front\Controller;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shaarli\Config\ConfigManager;
|
||||
use Shaarli\Container\ShaarliContainer;
|
||||
use Shaarli\Front\Exception\LoginBannedException;
|
||||
use Shaarli\Render\PageBuilder;
|
||||
use Shaarli\Security\LoginManager;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
class LoginControllerTest extends TestCase
|
||||
{
|
||||
/** @var ShaarliContainer */
|
||||
protected $container;
|
||||
|
||||
/** @var LoginController */
|
||||
protected $controller;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->container = $this->createMock(ShaarliContainer::class);
|
||||
$this->controller = new LoginController($this->container);
|
||||
}
|
||||
|
||||
public function testValidControllerInvoke(): void
|
||||
{
|
||||
$this->createValidContainerMockSet();
|
||||
|
||||
$request = $this->createMock(Request::class);
|
||||
$request->expects(static::once())->method('getServerParam')->willReturn('> referer');
|
||||
$response = new Response();
|
||||
|
||||
$assignedVariables = [];
|
||||
$this->container->pageBuilder
|
||||
->expects(static::exactly(3))
|
||||
->method('assign')
|
||||
->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
|
||||
$assignedVariables[$key] = $value;
|
||||
|
||||
return $this;
|
||||
})
|
||||
;
|
||||
|
||||
$result = $this->controller->index($request, $response);
|
||||
|
||||
static::assertInstanceOf(Response::class, $result);
|
||||
static::assertSame(200, $result->getStatusCode());
|
||||
static::assertSame('loginform', (string) $result->getBody());
|
||||
|
||||
static::assertSame('> referer', $assignedVariables['returnurl']);
|
||||
static::assertSame(true, $assignedVariables['remember_user_default']);
|
||||
static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
|
||||
}
|
||||
|
||||
public function testValidControllerInvokeWithUserName(): void
|
||||
{
|
||||
$this->createValidContainerMockSet();
|
||||
|
||||
$request = $this->createMock(Request::class);
|
||||
$request->expects(static::once())->method('getServerParam')->willReturn('> referer');
|
||||
$request->expects(static::exactly(2))->method('getParam')->willReturn('myUser>');
|
||||
$response = new Response();
|
||||
|
||||
$assignedVariables = [];
|
||||
$this->container->pageBuilder
|
||||
->expects(static::exactly(4))
|
||||
->method('assign')
|
||||
->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
|
||||
$assignedVariables[$key] = $value;
|
||||
|
||||
return $this;
|
||||
})
|
||||
;
|
||||
|
||||
$result = $this->controller->index($request, $response);
|
||||
|
||||
static::assertInstanceOf(Response::class, $result);
|
||||
static::assertSame(200, $result->getStatusCode());
|
||||
static::assertSame('loginform', (string) $result->getBody());
|
||||
|
||||
static::assertSame('myUser>', $assignedVariables['username']);
|
||||
static::assertSame('> referer', $assignedVariables['returnurl']);
|
||||
static::assertSame(true, $assignedVariables['remember_user_default']);
|
||||
static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
|
||||
}
|
||||
|
||||
public function testLoginControllerWhileLoggedIn(): void
|
||||
{
|
||||
$request = $this->createMock(Request::class);
|
||||
$response = new Response();
|
||||
|
||||
$loginManager = $this->createMock(LoginManager::class);
|
||||
$loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
|
||||
$this->container->loginManager = $loginManager;
|
||||
|
||||
$result = $this->controller->index($request, $response);
|
||||
|
||||
static::assertInstanceOf(Response::class, $result);
|
||||
static::assertSame(302, $result->getStatusCode());
|
||||
static::assertSame(['./'], $result->getHeader('Location'));
|
||||
}
|
||||
|
||||
public function testLoginControllerOpenShaarli(): void
|
||||
{
|
||||
$this->createValidContainerMockSet();
|
||||
|
||||
$request = $this->createMock(Request::class);
|
||||
$response = new Response();
|
||||
|
||||
$conf = $this->createMock(ConfigManager::class);
|
||||
$conf->method('get')->willReturnCallback(function (string $parameter, $default) {
|
||||
if ($parameter === 'security.open_shaarli') {
|
||||
return true;
|
||||
}
|
||||
return $default;
|
||||
});
|
||||
$this->container->conf = $conf;
|
||||
|
||||
$result = $this->controller->index($request, $response);
|
||||
|
||||
static::assertInstanceOf(Response::class, $result);
|
||||
static::assertSame(302, $result->getStatusCode());
|
||||
static::assertSame(['./'], $result->getHeader('Location'));
|
||||
}
|
||||
|
||||
public function testLoginControllerWhileBanned(): void
|
||||
{
|
||||
$this->createValidContainerMockSet();
|
||||
|
||||
$request = $this->createMock(Request::class);
|
||||
$response = new Response();
|
||||
|
||||
$loginManager = $this->createMock(LoginManager::class);
|
||||
$loginManager->method('isLoggedIn')->willReturn(false);
|
||||
$loginManager->method('canLogin')->willReturn(false);
|
||||
$this->container->loginManager = $loginManager;
|
||||
|
||||
$this->expectException(LoginBannedException::class);
|
||||
|
||||
$this->controller->index($request, $response);
|
||||
}
|
||||
|
||||
protected function createValidContainerMockSet(): void
|
||||
{
|
||||
// User logged out
|
||||
$loginManager = $this->createMock(LoginManager::class);
|
||||
$loginManager->method('isLoggedIn')->willReturn(false);
|
||||
$loginManager->method('canLogin')->willReturn(true);
|
||||
$this->container->loginManager = $loginManager;
|
||||
|
||||
// Config
|
||||
$conf = $this->createMock(ConfigManager::class);
|
||||
$conf->method('get')->willReturnCallback(function (string $parameter, $default) {
|
||||
return $default;
|
||||
});
|
||||
$this->container->conf = $conf;
|
||||
|
||||
// PageBuilder
|
||||
$pageBuilder = $this->createMock(PageBuilder::class);
|
||||
$pageBuilder
|
||||
->method('render')
|
||||
->willReturnCallback(function (string $template): string {
|
||||
return $template;
|
||||
})
|
||||
;
|
||||
$this->container->pageBuilder = $pageBuilder;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue