From 9312229f83240ece90e5d256c8714fa7d6c9768c Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Tue, 22 Oct 2019 16:15:42 -0400 Subject: [PATCH] PMC-1306 Add new attribute mobile_offline_tables_download_interval related to the env.ini configuration --- tests/bootstrap.php | 1 + .../ProcessMaker/BusinessModel/LightTest.php | 101 ++++++++++++++ .../src/ProcessMaker/Core/SystemTest.php | 124 ++++++++++++++++-- .../src/ProcessMaker/BusinessModel/Light.php | 15 ++- .../engine/src/ProcessMaker/Core/System.php | 12 +- 5 files changed, 238 insertions(+), 15 deletions(-) create mode 100644 tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/LightTest.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e84e1991b..2429864c7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -32,6 +32,7 @@ define('PMTABLE_KEY', 'pmtable'); define('DB_ADAPTER', 'mysql'); // Path related some specific directories define('PATH_SEP', '/'); +define("PATH_PLUGINS", PATH_CORE . "plugins" . PATH_SEP); define('PATH_WORKSPACE', PATH_TRUNK . '/shared/sites/' . SYS_SYS . '/'); define('PATH_METHODS', dirname(__DIR__) . '/workflow/engine/methods/'); define('PATH_WORKFLOW_MYSQL_DATA', PATH_TRUNK . '/workflow/engine/data/mysql/'); diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/LightTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/LightTest.php new file mode 100644 index 000000000..b860a6390 --- /dev/null +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/LightTest.php @@ -0,0 +1,101 @@ + true, + 'tz' => true, + ]; + $light = new Light(); + + /** + * In the getConfiguration() method, the next section: + * + * $postMaxSize = $this->return_bytes(ini_get('post_max_size')); + * $uploadMaxFileSize = $this->return_bytes(ini_get('upload_max_filesize')); + * if ($postMaxSize < $uploadMaxFileSize) { + * $uploadMaxFileSize = $postMaxSize; + * } + * + * It can only be tested if you change the values of "post_max_size" and "upload_max_filesize" + * in php.ini, you can't use the ini_set() function. + * The mode change of these directives is "PHP_INI_PERDIR", where is entry can be + * set in php.ini, .htaccess, httpd.conf or .user.ini, see here: + * https://www.php.net/manual/es/ini.list.php + * https://www.php.net/manual/en/configuration.changes.modes.php + */ + $result = $light->getConfiguration($param); + + $this->assertArrayHasKey('mobile_offline_tables_download_interval', $result); + } + + /** + * This returns the value of mobile_offline_tables_download_interval + * @test + * @covers \ProcessMaker\BusinessModel\Light::getConfiguration + */ + public function this_should_return_mobile_offline_tables_download_interval_inside_env() + { + $oldContent = ""; + $path = PATH_CONFIG . "env.ini"; + if (file_exists($path)) { + $oldContent = file_get_contents($path); + } + + $expected = 30; + + $content = "mobile_offline_tables_download_interval = {$expected};"; + file_put_contents($path, $content); + + $light = new Light(); + $result = $light->getConfiguration([]); + $actual = $result['mobile_offline_tables_download_interval']; + + file_put_contents($path, $oldContent); + + $this->assertEquals($expected, $actual); + } + + /** + * This returns the default value of mobile_offline_tables_download_interval. + * @test + * @covers \ProcessMaker\BusinessModel\Light::getConfiguration + */ + public function this_should_return_default_value_if_mobile_offline_tables_download_interval_inside_env_is_not_an_integer() + { + $oldContent = ""; + $path = PATH_CONFIG . "env.ini"; + if (file_exists($path)) { + $oldContent = file_get_contents($path); + } + + $faker = $faker = Factory::create(); + $alphanumeric = $faker->regexify('[A-Za-z0-9]{20}'); + $content = "mobile_offline_tables_download_interval = '{$alphanumeric}';"; + file_put_contents($path, $content); + + $light = new Light(); + $result = $light->getConfiguration([]); + $expected = (string) $result['mobile_offline_tables_download_interval']; + + file_put_contents($path, $oldContent); + + $this->assertTrue(ctype_digit($expected)); + } +} diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php index 19634a5a4..c03e3da6a 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php @@ -2,22 +2,20 @@ namespace Tests\unit\workflow\engine\src\ProcessMaker\Core; +use G; +use Faker\Factory; use ProcessMaker\Core\System; use Tests\TestCase; class SystemTest extends TestCase { + /** * Define the required variables */ - protected function setUp() + public function setUp() { - $this->markTestIncomplete();//@todo: Please correct this unit test - $config = config('database.connections.testexternal'); - define('DB_HOST', $config['host']); - define('DB_NAME', $config['database']); - define('DB_USER', $config['username']); - define('DB_PASS', $config['password']); + parent::setUp(); } /** @@ -27,6 +25,8 @@ class SystemTest extends TestCase */ public function it_should_init_laravel_configurations() { + $this->markTestIncomplete("@todo: Please correct this unit test"); + $object = new System(); $object->initLaravel(); @@ -36,4 +36,112 @@ class SystemTest extends TestCase $this->assertEquals(DB_USER, config('database.connections.workflow.username')); $this->assertEquals(DB_PASS, config('database.connections.workflow.password')); } -} \ No newline at end of file + + /** + * It should return default system configuration parameters. + * @test + * @covers \ProcessMaker\Core\System::getSystemConfiguration() + */ + public function it_should_return_default_system_configuration_parameters() + { + $result = System::getSystemConfiguration(); + + $this->assertArrayHasKey('server_hostname_requests_frontend', $result); + $this->assertArrayHasKey('disable_php_upload_execution', $result); + $this->assertArrayHasKey('mobile_offline_tables_download_interval', $result); + } + + /** + * It should return default system configuration parameters without workspace. + * @test + * @covers \ProcessMaker\Core\System::getSystemConfiguration() + */ + public function it_should_return_default_system_configuration_parameters_without_workspace() + { + config(["system.workspace" => '']); + putenv("REQUEST_URI=/sysworkflow"); + + $result = System::getSystemConfiguration(); + + $this->assertArrayHasKey('server_hostname_requests_frontend', $result); + $this->assertArrayHasKey('disable_php_upload_execution', $result); + $this->assertArrayHasKey('mobile_offline_tables_download_interval', $result); + } + + /** + * It should return system configuration parameters defined inside env file. + * @test + * @covers \ProcessMaker\Core\System::getSystemConfiguration() + */ + public function it_should_return_system_configuration_parameters_defined_inside_env_file() + { + $oldContent = ""; + $path = PATH_CONFIG . "env.ini"; + if (file_exists($path)) { + $oldContent = file_get_contents($path); + } + + $expected = 30; + + $content = "mobile_offline_tables_download_interval = {$expected};"; + file_put_contents($path, $content); + + $result = System::getSystemConfiguration(); + $actual = $result['mobile_offline_tables_download_interval']; + + file_put_contents($path, $oldContent); + + $this->assertEquals($expected, $actual); + } + + /** + * It should return default system configuration parameters defined inside env file when is not integer. + * @test + * @covers \ProcessMaker\Core\System::getSystemConfiguration() + */ + public function it_should_return_default_system_configuration_parameters_defined_inside_env_file_when_is_not_an_integer() + { + $oldContent = ""; + $path = PATH_CONFIG . "env.ini"; + if (file_exists($path)) { + $oldContent = file_get_contents($path); + } + + $faker = $faker = Factory::create(); + $alphanumeric = $faker->regexify('[A-Za-z0-9]{20}'); + $content = "mobile_offline_tables_download_interval = '{$alphanumeric}';"; + file_put_contents($path, $content); + + $result = System::getSystemConfiguration(); + + $expected = (string) $result['mobile_offline_tables_download_interval']; + + file_put_contents($path, $oldContent); + + $this->assertTrue(is_numeric($expected)); + } + + /** + * It should return proxy_pass defined inside env file. + * @test + * @covers \ProcessMaker\Core\System::getSystemConfiguration() + */ + public function it_should_return_proxy_pass_defined_inside_env_file() + { + $oldContent = ""; + $path = PATH_CONFIG . "env.ini"; + if (file_exists($path)) { + $oldContent = file_get_contents($path); + } + + $faker = $faker = Factory::create(); + $content = "proxy_pass = '{$faker->password}';"; + file_put_contents($path, $content); + + $result = System::getSystemConfiguration(); + + file_put_contents($path, $oldContent); + + $this->assertArrayHasKey("proxy_pass", $result); + } +} diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Light.php b/workflow/engine/src/ProcessMaker/BusinessModel/Light.php index f6a135071..e81fde5a5 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Light.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Light.php @@ -696,9 +696,8 @@ class Light $sAppDocUid = $oAppDocument->getAppDocUid(); $iDocVersion = $oAppDocument->getDocVersion(); $info = pathinfo($oAppDocument->getAppDocFilename()); - $ext = (isset($info['extension']) ? $info['extension'] : '');//BUG fix: must handle files without any extension + $ext = (isset($info['extension']) ? $info['extension'] : ''); //BUG fix: must handle files without any extension - //$app_uid = G::getPathFromUID($oAppDocument->Fields['APP_UID']); $file = G::getPathFromFileUID($oAppDocument->Fields['APP_UID'], $sAppDocUid); $realPath = PATH_DOCUMENT . G::getPathFromUID($app_uid) . '/' . $file[0] . $file[1] . '_' . $iDocVersion . '.' . $ext; @@ -1358,12 +1357,14 @@ class Light */ public function getConfiguration($params) { + $response = []; + $sysConf = Bootstrap::getSystemConfiguration('', '', config("system.workspace")); $multiTimeZone = false; //Set Time Zone /*----------------------------------********---------------------------------*/ if (\PMLicensedFeatures::getSingleton()->verifyfeature('oq3S29xemxEZXJpZEIzN01qenJUaStSekY4cTdJVm5vbWtVM0d4S2lJSS9qUT0=')) { - $multiTimeZone = (int)($sysConf['system_utc_time_zone']) == 1; + $multiTimeZone = (int) ($sysConf['system_utc_time_zone']) == 1; } /*----------------------------------********---------------------------------*/ $tz = isset($_SESSION['USR_TIME_ZONE']) ? $_SESSION['USR_TIME_ZONE'] : $sysConf['time_zone']; @@ -1423,6 +1424,8 @@ class Light $response['tz'] = isset($_SESSION['USR_TIME_ZONE']) ? $_SESSION['USR_TIME_ZONE'] : $sysConf['time_zone']; } + $response['mobile_offline_tables_download_interval'] = $sysConf['mobile_offline_tables_download_interval']; + return $response; } @@ -1431,13 +1434,13 @@ class Light switch (substr($size_str, -1)) { case 'M': case 'm': - return (int)$size_str * 1048576; + return (int) $size_str * 1048576; case 'K': case 'k': - return (int)$size_str * 1024; + return (int) $size_str * 1024; case 'G': case 'g': - return (int)$size_str * 1073741824; + return (int) $size_str * 1073741824; default: return $size_str; } diff --git a/workflow/engine/src/ProcessMaker/Core/System.php b/workflow/engine/src/ProcessMaker/Core/System.php index 7de260e0c..beeb70ce8 100644 --- a/workflow/engine/src/ProcessMaker/Core/System.php +++ b/workflow/engine/src/ProcessMaker/Core/System.php @@ -73,7 +73,8 @@ class System 'files_white_list' => '', 'delay' => '0', 'tries' => '10', - 'retry_after' => '90' + 'retry_after' => '90', + 'mobile_offline_tables_download_interval' => 24 ]; /** @@ -1203,6 +1204,15 @@ class System $config['proxy_pass'] = G::decrypt($config['proxy_pass'], 'proxy_pass'); } + /** + * Here if you validate if the type of data obtained from the configuration + * files are valid, otherwise the default value is used. + */ + $value = (string) $config['mobile_offline_tables_download_interval']; + if (!is_numeric($value)) { + $config['mobile_offline_tables_download_interval'] = self::$defaultConfig['mobile_offline_tables_download_interval']; + } + return $config; }