started \Settings\Store unit tests

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2019-06-10 13:37:22 +02:00
parent 028524291e
commit 7a94a43053
3 changed files with 145 additions and 81 deletions

View File

@@ -27,29 +27,58 @@ class Store
$returnvalue = self::storeSettingField($fieldname, $fielddata, $newfieldvalue);
if ($returnvalue !== false && is_array($fielddata) && isset($fielddata['settinggroup']) && $fielddata['settinggroup'] == 'system' && isset($fielddata['varname']) && $fielddata['varname'] == 'defaultip') {
self::updateStdSubdomainDefaultIp($newfieldvalue, $defaultips_old);
}
$customerstddomains_result_stmt = Database::prepare("
SELECT `standardsubdomain` FROM `" . TABLE_PANEL_CUSTOMERS . "` WHERE `standardsubdomain` <> '0'
");
Database::pexecute($customerstddomains_result_stmt);
return $returnvalue;
}
$ids = array();
public static function storeSettingDefaultSslIp($fieldname, $fielddata, $newfieldvalue)
{
$defaultips_old = Settings::Get('system.defaultsslip');
while ($customerstddomains_row = $customerstddomains_result_stmt->fetch(\PDO::FETCH_ASSOC)) {
$ids[] = (int) $customerstddomains_row['standardsubdomain'];
$returnvalue = self::storeSettingField($fieldname, $fielddata, $newfieldvalue);
if ($returnvalue !== false && is_array($fielddata) && isset($fielddata['settinggroup']) && $fielddata['settinggroup'] == 'system' && isset($fielddata['varname']) && $fielddata['varname'] == 'defaultsslip') {
self::updateStdSubdomainDefaultIp($newfieldvalue, $defaultips_old);
}
return $returnvalue;
}
private static function updateStdSubdomainDefaultIp($newfieldvalue, $defaultips_old)
{
// update standard-subdomain of customer if exists
$customerstddomains_result_stmt = Database::prepare("
SELECT `standardsubdomain` FROM `" . TABLE_PANEL_CUSTOMERS . "` WHERE `standardsubdomain` <> '0'
");
Database::pexecute($customerstddomains_result_stmt);
$ids = array();
while ($customerstddomains_row = $customerstddomains_result_stmt->fetch(\PDO::FETCH_ASSOC)) {
$ids[] = (int) $customerstddomains_row['standardsubdomain'];
}
if (count($ids) > 0) {
$defaultips_new = explode(',', $newfieldvalue);
if (! empty($defaultips_old) && ! empty($newfieldvalue)) {
$in_value = $defaultips_old . ", " . $newfieldvalue;
} elseif (! empty($defaultips_old) && empty($newfieldvalue)) {
$in_value = $defaultips_old;
} else {
$in_value = $newfieldvalue;
}
if (count($ids) > 0) {
$defaultips_new = explode(',', $newfieldvalue);
// Delete the existing mappings linking to default IPs
$del_stmt = Database::prepare("
DELETE FROM `" . TABLE_DOMAINTOIP . "`
WHERE `id_domain` IN (" . implode(', ', $ids) . ")
AND `id_ipandports` IN (" . $defaultips_old . ", " . $newfieldvalue . ")
");
Database::pexecute($del_stmt);
// Delete the existing mappings linking to default IPs
$del_stmt = Database::prepare("
DELETE FROM `" . TABLE_DOMAINTOIP . "`
WHERE `id_domain` IN (" . implode(', ', $ids) . ")
AND `id_ipandports` IN (" . $in_value . ")
");
Database::pexecute($del_stmt);
if (count($defaultips_new) > 0) {
// Insert the new mappings
$ins_stmt = Database::prepare("
INSERT INTO `" . TABLE_DOMAINTOIP . "`
@@ -66,68 +95,6 @@ class Store
}
}
}
return $returnvalue;
}
public static function storeSettingDefaultSslIp($fieldname, $fielddata, $newfieldvalue)
{
$defaultips_old = Settings::Get('system.defaultsslip');
$returnvalue = self::storeSettingField($fieldname, $fielddata, $newfieldvalue);
if ($returnvalue !== false && is_array($fielddata) && isset($fielddata['settinggroup']) && $fielddata['settinggroup'] == 'system' && isset($fielddata['varname']) && $fielddata['varname'] == 'defaultsslip') {
$customerstddomains_result_stmt = Database::prepare("
SELECT `standardsubdomain` FROM `" . TABLE_PANEL_CUSTOMERS . "` WHERE `standardsubdomain` <> '0'
");
Database::pexecute($customerstddomains_result_stmt);
$ids = array();
while ($customerstddomains_row = $customerstddomains_result_stmt->fetch(\PDO::FETCH_ASSOC)) {
$ids[] = (int) $customerstddomains_row['standardsubdomain'];
}
if (count($ids) > 0) {
$defaultips_new = explode(',', $newfieldvalue);
if (! empty($defaultips_old) && ! empty($newfieldvalue)) {
$in_value = $defaultips_old . ", " . $newfieldvalue;
} elseif (! empty($defaultips_old) && empty($newfieldvalue)) {
$in_value = $defaultips_old;
} else {
$in_value = $newfieldvalue;
}
// Delete the existing mappings linking to default IPs
$del_stmt = Database::prepare("
DELETE FROM `" . TABLE_DOMAINTOIP . "`
WHERE `id_domain` IN (" . implode(', ', $ids) . ")
AND `id_ipandports` IN (" . $in_value . ")
");
Database::pexecute($del_stmt);
if (count($defaultips_new) > 0) {
// Insert the new mappings
$ins_stmt = Database::prepare("
INSERT INTO `" . TABLE_DOMAINTOIP . "`
SET `id_domain` = :domainid, `id_ipandports` = :ipandportid
");
foreach ($ids as $id) {
foreach ($defaultips_new as $defaultip_new) {
Database::pexecute($ins_stmt, array(
'domainid' => $id,
'ipandportid' => $defaultip_new
));
}
}
}
}
}
return $returnvalue;
}
/**

View File

@@ -230,7 +230,7 @@ class CustomersTest extends TestCase
/**
*
* @depends testAdminCustomersAdd
* @depends testAdminCustomerUpdateDeactivate
*/
public function testCustomerCustomersGetWhenDeactivated()
{
@@ -252,7 +252,7 @@ class CustomersTest extends TestCase
/**
*
* @depends testAdminCustomersAdd
* @depends testCustomerCustomersGetWhenDeactivated
*/
public function testCustomerCustomersUpdate()
{

View File

@@ -0,0 +1,97 @@
<?php
use PHPUnit\Framework\TestCase;
use Froxlor\Settings;
use Froxlor\Api\Commands\Customers;
use Froxlor\Database\Database;
use Froxlor\Settings\Store;
/**
*
* @covers \Froxlor\Settings\Store
*/
class StoreTest extends TestCase
{
public function testStoreSettingClearCertificates()
{
// when froxlor vhost setting "use lets encrypt" is set to false, the corresponding
// certificate needs to be cleaned
// for testing purposes, let's add some entry to the table
Database::query("INSERT INTO `domain_ssl_settings` SET `domainid` = '0', `ssl_cert_file` = 'test-content'");
$fielddata = array(
'label' => 'le_froxlor_enabled',
'settinggroup' => 'system',
'varname' => 'le_froxlor_enabled'
);
Store::storeSettingClearCertificates('system_le_froxlor_enabled', $fielddata, 0);
// there should be no entry in domain_ssl_settings now
$result = Database::query("SELECT COUNT(*) as entries FROM `domain_ssl_settings` WHERE `domainid` = '0'");
$result = $result->fetch(\PDO::FETCH_ASSOC);
$this->assertEquals(0, (int) $result['entries']);
// truncate the table for other tests
Database::query("TRUNCATE TABLE `" . TABLE_PANEL_DOMAIN_SSL_SETTINGS . "`;");
}
public function testStoreSettingDefaultIp()
{
global $admin_userdata;
// the customer should have a std-subdomin
Customers::getLocal($admin_userdata, array(
'id' => 1,
'createstdsubdomain' => 1
))->update();
// we need a second non-ssl IP
Database::query("INSERT INTO `panel_ipsandports` SET `ip` = '82.149.225.47', `port` = '80'");
$ip_id = Database::lastInsertId();
$default_ip = Settings::Get('system.defaultip');
// get all std-subdomains
$customerstddomains_result_stmt = Database::prepare("
SELECT `standardsubdomain` FROM `" . TABLE_PANEL_CUSTOMERS . "` WHERE `standardsubdomain` <> '0'
");
Database::pexecute($customerstddomains_result_stmt);
$ids = array();
while ($customerstddomains_row = $customerstddomains_result_stmt->fetch(\PDO::FETCH_ASSOC)) {
$ids[] = (int) $customerstddomains_row['standardsubdomain'];
}
if (count($ids) <= 0) {
$this->fail("There should be customer std-subdomains for this test to make sense");
}
// check that they have the current default IP set
$sel_stmt = Database::prepare("
SELECT * FROM `" . TABLE_DOMAINTOIP . "`
WHERE `id_domain` IN (" . implode(', ', $ids) . ") AND `id_ipandports` = :ipid
");
Database::pexecute($sel_stmt, array('ipid' => $default_ip));
$current_result = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC);
// we assume there are entries
$this->assertTrue(count($current_result) > 0);
$fielddata = array(
'label' => 'serversettingsipaddress',
'settinggroup' => 'system',
'varname' => 'defaultip'
);
Store::storeSettingDefaultIp('serversettings_ipaddress', $fielddata, $ip_id);
// check that they do not have the current default IP set anymore
Database::pexecute($sel_stmt, array('ipid' => $default_ip));
$current_result = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC);
// we assume there are entries
$this->assertTrue(count($current_result) == 0);
// check that they have the new default IP set
Database::pexecute($sel_stmt, array('ipid' => $ip_id));
$current_result = $sel_stmt->fetchAll(\PDO::FETCH_ASSOC);
// we assume there are entries
$this->assertTrue(count($current_result) > 0);
}
}