From 38b08719da78c0873ff2242048d88bdba0d0dabf Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Wed, 17 Aug 2016 21:06:43 -0400 Subject: [PATCH] Setup unit tests --- .gitignore | 2 +- .travis.yml | 18 +++++++---- lib/helpers.php | 31 ++++++++++++++++++ models/config.php | 34 -------------------- models/user.php | 3 +- phpunit.xml => tests/phpunit.integration.xml | 7 ++-- tests/phpunit.unit.xml | 10 ++++++ tests/unit/BaseTest.php | 8 +++++ tests/unit/HelperTest.php | 13 ++++++++ 9 files changed, 80 insertions(+), 46 deletions(-) rename phpunit.xml => tests/phpunit.integration.xml (87%) create mode 100644 tests/phpunit.unit.xml create mode 100644 tests/unit/BaseTest.php create mode 100644 tests/unit/HelperTest.php diff --git a/.gitignore b/.gitignore index be097b2..fe61f8e 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,4 @@ config.php !controllers/* rules/*.php data/favicons/*.* -/nbproject/private/ \ No newline at end of file +/nbproject/private/ diff --git a/.travis.yml b/.travis.yml index 6eee84f..e9cda58 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/helpers.php b/lib/helpers.php index 4c6c63b..42ccaab 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -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'; diff --git a/models/config.php b/models/config.php index 04f33d2..7fb6a7a 100644 --- a/models/config.php +++ b/models/config.php @@ -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'); -} diff --git a/models/user.php b/models/user.php index ef6c364..b2f2b42 100644 --- a/models/user.php +++ b/models/user.php @@ -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 { diff --git a/phpunit.xml b/tests/phpunit.integration.xml similarity index 87% rename from phpunit.xml rename to tests/phpunit.integration.xml index 5b4227e..1504f06 100644 --- a/phpunit.xml +++ b/tests/phpunit.integration.xml @@ -1,4 +1,4 @@ - + @@ -28,14 +28,13 @@ - ./tests/ + ./integration/ - tests/ + integration/ - diff --git a/tests/phpunit.unit.xml b/tests/phpunit.unit.xml new file mode 100644 index 0000000..fd91a00 --- /dev/null +++ b/tests/phpunit.unit.xml @@ -0,0 +1,10 @@ + + + + unit + + + + + + diff --git a/tests/unit/BaseTest.php b/tests/unit/BaseTest.php new file mode 100644 index 0000000..a2ef938 --- /dev/null +++ b/tests/unit/BaseTest.php @@ -0,0 +1,8 @@ + '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()); + } +}