Ver Fonte

Fix for php8, adding test for login

Sergio Brighenti há 4 anos atrás
pai
commit
125ae162ff
5 ficheiros alterados com 58 adições e 38 exclusões
  1. 1 1
      index.php
  2. 1 1
      install/index.php
  3. 0 34
      tests/Client.php
  4. 41 2
      tests/Feature/LoginControllerTest.php
  5. 15 0
      tests/TestCase.php

+ 1 - 1
index.php

@@ -1,6 +1,6 @@
 <?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';
 
 define('BASE_DIR', realpath(__DIR__).DIRECTORY_SEPARATOR);

+ 1 - 1
install/index.php

@@ -1,6 +1,6 @@
 <?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';
 
 use App\Database\Migrator;

+ 0 - 34
tests/Client.php

@@ -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());
-    }
-}

+ 41 - 2
tests/Feature/LoginControllerTest.php

@@ -28,7 +28,7 @@ class LoginControllerTest extends TestCase
     /** @test */
     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'));
         $form = $this->getCrawler($response)
@@ -57,10 +57,49 @@ class LoginControllerTest extends TestCase
     /** @test */
     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'));
         $this->assertSame(200, $response->getStatusCode());
         $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());
+
+
+    }
 }

+ 15 - 0
tests/TestCase.php

@@ -14,4 +14,19 @@ abstract class TestCase extends BaseTestCase
         $_SESSION = []; // ugly workaround to the the session superglobal between tests
         $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();
+    }
 }