Setup unit tests

This commit is contained in:
Frederic Guillot 2016-08-17 21:06:43 -04:00
parent e00b208d4a
commit 38b08719da
No known key found for this signature in database
GPG Key ID: 92D77191BA7FBC99
9 changed files with 80 additions and 46 deletions

2
.gitignore vendored
View File

@ -58,4 +58,4 @@ config.php
!controllers/*
rules/*.php
data/favicons/*.*
/nbproject/private/
/nbproject/private/

View File

@ -1,10 +1,16 @@
language: php
sudo: false
php:
- '5.4'
- '5.5'
- '5.6'
- 7.0
- 5.6
- 5.5
- 5.4
- 5.3
- hhvm
install:
- composer install
before_script:
- composer install
script:
- phpunit -c tests/phpunit.unit.xml

View File

@ -2,6 +2,37 @@
namespace Helper;
// Get the real IP address of the connected user
function get_ip_address()
{
$keys = array(
'HTTP_X_REAL_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
);
foreach ($keys as $key) {
$value = get_server_variable($key);
if ($value !== '') {
foreach (explode(',', $value) as $ip_address) {
return trim($ip_address);
}
}
}
return t('Unknown');
}
function get_server_variable($variable)
{
return isset($_SERVER[$variable]) ? $_SERVER[$variable] : '';
}
function is_secure_connection()
{
return ! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';

View File

@ -369,37 +369,3 @@ function get_user_agent()
{
return empty($_SERVER['HTTP_USER_AGENT']) ? t('Unknown') : $_SERVER['HTTP_USER_AGENT'];
}
// Get the real IP address of the connected user
function get_ip_address($only_public = false)
{
$keys = array(
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
);
foreach ($keys as $key) {
if (isset($_SERVER[$key])) {
foreach (explode(',', $_SERVER[$key]) as $ip_address) {
$ip_address = trim($ip_address);
if ($only_public) {
// Return only public IP address
if (filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $ip_address;
}
} else {
return $ip_address;
}
}
}
}
return t('Unknown');
}

View File

@ -6,6 +6,7 @@ use SimpleValidator\Validator;
use SimpleValidator\Validators;
use PicoDb\Database;
use Session;
use Helper;
use Model\Config;
use Model\RememberMe;
use Model\Database as DatabaseModel;
@ -61,7 +62,7 @@ function validate_login(array $values)
// Setup the remember me feature
if (! empty($values['remember_me'])) {
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Config\get_ip_address(), Config\get_user_agent());
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Helper\get_ip_address(), Config\get_user_agent());
RememberMe\write_cookie($cookie['token'], $cookie['sequence'], $cookie['expiration']);
}
} else {

View File

@ -1,4 +1,4 @@
<phpunit bootstrap="tests/integration/minifluxTestCase.php" >
<phpunit bootstrap="tests/integration/minifluxTestCase.php" colors="true">
<php>
<const name="DB_FILENAME" value="unittest.sqlite" />
<const name="PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_BASEURL" value="http://localhost/miniflux/" />
@ -28,14 +28,13 @@
<listeners>
<listener class="PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener">
<arguments>
<string>./tests/</string>
<string>./integration/</string>
</arguments>
</listener>
</listeners>
<testsuites>
<testsuite name="Miniflux">
<directory>tests/</directory>
<directory>integration/</directory>
</testsuite>
</testsuites>
</phpunit>

10
tests/phpunit.unit.xml Normal file
View File

@ -0,0 +1,10 @@
<phpunit stopOnError="true" stopOnFailure="true" colors="true">
<testsuites>
<testsuite name="Miniflux Unit Tests">
<directory>unit</directory>
</testsuite>
</testsuites>
<php>
<const name="DB_FILENAME" value=":memory:" />
</php>
</phpunit>

8
tests/unit/BaseTest.php Normal file
View File

@ -0,0 +1,8 @@
<?php
require_once __DIR__.'/../../vendor/autoload.php';
abstract class BaseTest extends PHPUnit_Framework_TestCase
{
}

13
tests/unit/HelperTest.php Normal file
View File

@ -0,0 +1,13 @@
<?php
class HelperTest extends PHPUnit_Framework_TestCase
{
public function testGetIpAddress()
{
$_SERVER = array('HTTP_X_REAL_IP' => '127.0.0.1');
$this->assertEquals('127.0.0.1', Helper\get_ip_address());
$_SERVER = array('HTTP_FORWARDED_FOR' => ' 127.0.0.1, 192.168.0.1');
$this->assertEquals('127.0.0.1', Helper\get_ip_address());
}
}