You've already forked postfixadmin
mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2025-08-06 06:42:37 +03:00
add more unit tests; re-enable random_int warning in functions.inc.php for old php versions etc etc
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
],
|
],
|
||||||
"check-format": "php-cs-fixer fix --ansi --dry-run --diff",
|
"check-format": "php-cs-fixer fix --ansi --dry-run --diff",
|
||||||
"format": "php-cs-fixer fix --ansi",
|
"format": "php-cs-fixer fix --ansi",
|
||||||
"lint": "@php ./vendor/bin/parallel-lint --exclude vendor/ .",
|
"lint": "@php ./vendor/bin/parallel-lint --exclude vendor/ --exclude lib/block_random_int.php .",
|
||||||
"test": "@php ./vendor/bin/phpunit tests/",
|
"test": "@php ./vendor/bin/phpunit tests/",
|
||||||
"psalm": "@php ./vendor/bin/psalm --show-info=false "
|
"psalm": "@php ./vendor/bin/psalm --show-info=false "
|
||||||
},
|
},
|
||||||
|
@@ -845,13 +845,11 @@ function encode_header($string, $default_charset = "utf-8") {
|
|||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
if (!function_exists('random_int')) { // PHP version < 7.0
|
if (!function_exists('random_int')) { // PHP version < 7.0
|
||||||
function random_int() { // someone might not be using php_crypt or ask for password generation, in which case random_int() won't be called
|
require_once(dirname(__FILE__) . '/lib/block_random_int.php');
|
||||||
die(__FILE__ . " Postfixadmin security: Please install https://github.com/paragonie/random_compat OR enable the 'Phar' extension.");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random password of $length characters.
|
* Generate a random password of $length characters.
|
||||||
@@ -1946,8 +1944,9 @@ function db_update_q($table, $where, $values, $timestamp = array('modified')) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* @todo this needs refactoring/moving out from here */
|
||||||
if (Config::bool('password_expiration')) {
|
if (Config::bool('password_expiration')) {
|
||||||
if ($table == 'mailbox') {
|
if ($table == 'mailbox' && preg_match('/@/', $where)) {
|
||||||
$where_type = explode('=', $where);
|
$where_type = explode('=', $where);
|
||||||
$email = ($where_type[1]);
|
$email = ($where_type[1]);
|
||||||
$domain_dirty = explode('@',$email)[1];
|
$domain_dirty = explode('@',$email)[1];
|
||||||
|
14
lib/block_random_int.php
Normal file
14
lib/block_random_int.php
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file should only be loaded if you're :
|
||||||
|
* a. running PHP < 7.0, and
|
||||||
|
* b. have the php_crypt password hash configured, and
|
||||||
|
* c. have not loaded paragonie's random_compat library.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(function_exists('random_int')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
function random_int() { // someone might not be using php_crypt or ask for password generation, in which case random_int() won't be called
|
||||||
|
die(__FILE__ . " Postfixadmin security: Please install https://github.com/paragonie/random_compat OR enable the 'Phar' extension.");
|
||||||
|
}
|
16
tests/CheckDomainTest.php
Normal file
16
tests/CheckDomainTest.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obviously replies on working DNS service
|
||||||
|
*/
|
||||||
|
class CheckDomainTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$this->assertEquals('', check_domain('example.com'));
|
||||||
|
$this->assertEquals('', check_domain('google.com'));
|
||||||
|
$this->assertRegExp('/ not discoverable in DNS/', check_domain('fishbeansblahblahblah' . uniqid() . '.com'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
15
tests/CheckEmailTest.php
Normal file
15
tests/CheckEmailTest.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obviously relies on working DNS service etc.
|
||||||
|
*/
|
||||||
|
class CheckEmailTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$this->assertEquals('', check_email('test@example.com'));
|
||||||
|
$this->assertRegExp('/ not discoverable in DNS/', check_email('test@fishbeansblahblahblah' . uniqid() . '.com'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
63
tests/CheckLanguageTest.php
Normal file
63
tests/CheckLanguageTest.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class CheckLanguageTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
global $supported_languages;
|
||||||
|
|
||||||
|
$this->assertNotEmpty($supported_languages);
|
||||||
|
|
||||||
|
$config = Config::getInstance();
|
||||||
|
Config::write('default_language', 'test');
|
||||||
|
|
||||||
|
unset($_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
||||||
|
|
||||||
|
$lang = check_language(false);
|
||||||
|
|
||||||
|
$this->assertEquals('test', $lang);
|
||||||
|
|
||||||
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en';
|
||||||
|
|
||||||
|
$lang = check_language(false);
|
||||||
|
$this->assertEquals('en', $lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCookie() {
|
||||||
|
global $supported_languages;
|
||||||
|
|
||||||
|
$this->assertNotEmpty($supported_languages);
|
||||||
|
|
||||||
|
|
||||||
|
$config = Config::getInstance();
|
||||||
|
Config::write('default_language', 'test');
|
||||||
|
|
||||||
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foo';
|
||||||
|
|
||||||
|
$_COOKIE['lang'] = 'en';
|
||||||
|
|
||||||
|
$lang = check_language(false);
|
||||||
|
|
||||||
|
$this->assertEquals('en', $lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPost() {
|
||||||
|
global $supported_languages;
|
||||||
|
|
||||||
|
$this->assertNotEmpty($supported_languages);
|
||||||
|
|
||||||
|
|
||||||
|
$config = Config::getInstance();
|
||||||
|
Config::write('default_language', 'test');
|
||||||
|
|
||||||
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foo';
|
||||||
|
|
||||||
|
$_POST['lang'] = 'en';
|
||||||
|
|
||||||
|
$lang = check_language(true);
|
||||||
|
|
||||||
|
$this->assertEquals('en', $lang);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
13
tests/CheckOwnerTest.php
Normal file
13
tests/CheckOwnerTest.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class CheckOwnerTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$check = check_owner('random@example.com', 'test.com');
|
||||||
|
|
||||||
|
$this->assertFalse($check);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
48
tests/DbBasicTest.php
Normal file
48
tests/DbBasicTest.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class DbBasicTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testInsertDeleteDomain() {
|
||||||
|
$domain = "test". uniqid() . '.com';
|
||||||
|
|
||||||
|
$username = 'testusername' . uniqid();
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
1,
|
||||||
|
db_insert(
|
||||||
|
'domain',
|
||||||
|
array('domain' => $domain, 'description' => '', 'transport' => '', 'password_expiry' => 99)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(1,
|
||||||
|
db_insert(
|
||||||
|
'mailbox',
|
||||||
|
array('username' => $username, 'password' => 'blah', 'name' => 'blah', 'maildir' => 'blah', 'local_part' => 'blah', 'domain' => $domain,)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(1,
|
||||||
|
db_update(
|
||||||
|
'mailbox',
|
||||||
|
'username',
|
||||||
|
$username,
|
||||||
|
array('name' => 'blah updated')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$ret = db_query("SELECT * FROM mailbox WHERE username = '$username'");
|
||||||
|
|
||||||
|
$this->assertEquals(1, $ret['rows']);
|
||||||
|
$data = db_assoc($ret['result']);
|
||||||
|
|
||||||
|
$this->assertEquals($data['name'], 'blah updated');
|
||||||
|
|
||||||
|
$this->assertEquals(1, db_delete('mailbox', 'username', $username));
|
||||||
|
$this->assertEquals(1, db_delete('domain', 'domain', $domain));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
15
tests/ListAdminsTest.php
Normal file
15
tests/ListAdminsTest.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class ListAdminsTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$list= list_admins();
|
||||||
|
|
||||||
|
// may be empty, depending on db.
|
||||||
|
|
||||||
|
$this->assertTrue(is_array($list));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
11
tests/ListDomainsForAdminTest.php
Normal file
11
tests/ListDomainsForAdminTest.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class ListDomainsForAdminTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$this->assertEquals([], list_domains_for_admin('test@test.com'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
13
tests/ListDomainsTest.php
Normal file
13
tests/ListDomainsTest.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class ListDomainsTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$domains = list_domains();
|
||||||
|
|
||||||
|
$this->assertTrue(is_array($domains));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
23
tests/RemoveFromArrayTest.php
Normal file
23
tests/RemoveFromArrayTest.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('common.php');
|
||||||
|
|
||||||
|
class RemoveFromArrayTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
public function testBasic() {
|
||||||
|
$list = array('a','b','c','d');
|
||||||
|
|
||||||
|
list($found, $new) = remove_from_array($list, 'd');
|
||||||
|
$this->assertEquals(1, $found);
|
||||||
|
$this->assertEquals(array('a','b','c'), $new);
|
||||||
|
|
||||||
|
list($found, $new) = remove_from_array($list, 'a');
|
||||||
|
$this->assertEquals(1, $found);
|
||||||
|
$this->assertEquals(array(1 => 'b',2 => 'c',3=>'d'), $new);
|
||||||
|
|
||||||
|
list($found, $new) = remove_from_array($list, 'x');
|
||||||
|
$this->assertEquals(0, $found);
|
||||||
|
$this->assertEquals(array('a','b','c','d'), $new);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
Reference in New Issue
Block a user