Fix for php8, adding test for login

This commit is contained in:
Sergio Brighenti 2020-10-03 16:01:00 +02:00
parent 333926bbe9
commit 125ae162ff
5 changed files with 58 additions and 38 deletions

View file

@ -1,6 +1,6 @@
<?php <?php
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.'); ((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) || PHP_MAJOR_VERSION > 7) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
require __DIR__.'/vendor/autoload.php'; require __DIR__.'/vendor/autoload.php';
define('BASE_DIR', realpath(__DIR__).DIRECTORY_SEPARATOR); define('BASE_DIR', realpath(__DIR__).DIRECTORY_SEPARATOR);

View file

@ -1,6 +1,6 @@
<?php <?php
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.'); ((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) || PHP_MAJOR_VERSION > 7) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
require __DIR__.'/../vendor/autoload.php'; require __DIR__.'/../vendor/autoload.php';
use App\Database\Migrator; use App\Database\Migrator;

View file

@ -1,34 +0,0 @@
<?php
namespace Tests;
use GuzzleHttp\Psr7\ServerRequest;
use Slim\App;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\BrowserKit\Response;
class Client extends AbstractBrowser
{
private $app;
public function __construct(App $app, $server = [], History $history = null, CookieJar $cookieJar = null)
{
parent::__construct($server, $history, $cookieJar);
$this->app = $app;
}
protected function doRequest($request)
{
$response = $this->app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
$body = $response->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
return new Response($body->getContents(), $response->getStatusCode(), $response->getHeaders());
}
}

View file

@ -28,7 +28,7 @@ class LoginControllerTest extends TestCase
/** @test */ /** @test */
public function it_login_with_correct_data() public function it_login_with_correct_data()
{ {
$this->database()->query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES ('admin@example.com', 'admin', ?, 1, ?)", [password_hash('admin', PASSWORD_DEFAULT), humanRandomString(5)]); $this->createAdminUser();
$response = $this->get(route('login.show')); $response = $this->get(route('login.show'));
$form = $this->getCrawler($response) $form = $this->getCrawler($response)
@ -57,10 +57,49 @@ class LoginControllerTest extends TestCase
/** @test */ /** @test */
public function it_show_register_when_enabled() public function it_show_register_when_enabled()
{ {
$this->database()->query("INSERT INTO `settings`(`key`, `value`) VALUES ('register_enabled', 'on')"); $this->updateSetting('register_enabled', 'on');
$response = $this->get(route('login.show')); $response = $this->get(route('login.show'));
$this->assertSame(200, $response->getStatusCode()); $this->assertSame(200, $response->getStatusCode());
$this->assertStringContainsString('Register', $this->getCrawler($response)->text()); $this->assertStringContainsString('Register', $this->getCrawler($response)->text());
} }
/** @test */
public function it_redirect_to_home_if_logged_in()
{
$this->createAdminUser();
$response = $this->get(route('login.show'));
$form = $this->getCrawler($response)
->selectButton('Login')
->form([
'username' => 'admin@example.com',
'password' => 'admin',
], 'POST');
$this->submitForm($form);
$response = $this->get(route('login'));
$this->assertSame(302, $response->getStatusCode());
}
/** @test */
public function it_set_the_remember_token()
{
$this->createAdminUser();
$response = $this->get(route('login.show'));
$form = $this->getCrawler($response)
->selectButton('Login')
->form([
'username' => 'admin@example.com',
'password' => 'admin',
'remember' => 'on',
], 'POST');
$response = $this->submitForm($form);
dd($response->getHeaders());
}
} }

View file

@ -14,4 +14,19 @@ abstract class TestCase extends BaseTestCase
$_SESSION = []; // ugly workaround to the the session superglobal between tests $_SESSION = []; // ugly workaround to the the session superglobal between tests
$this->createApplication(); $this->createApplication();
} }
public function updateSetting($key, $value = null)
{
if (!$this->database()->query('SELECT `value` FROM `settings` WHERE `key` = '.$this->database()->getPdo()->quote($key))->fetch()) {
$this->database()->query('INSERT INTO `settings`(`key`, `value`) VALUES ('.$this->database()->getPdo()->quote($key).', ?)', $value);
} else {
$this->database()->query('UPDATE `settings` SET `value`=? WHERE `key` = '.$this->database()->getPdo()->quote($key), $value);
}
}
public function createAdminUser($email = 'admin@example.com', $username = 'admin', $password = 'admin')
{
$this->database()->query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES (?, ?, ?, 1, ?)", [$email, $username, password_hash($password, PASSWORD_DEFAULT), humanRandomString(5)]);
return $this->database()->getPdo()->lastInsertId();
}
} }