diff --git a/setup/sequencers/DataAuditSequencer.php b/setup/sequencers/DataAuditSequencer.php index 718692f5e..da487fb10 100644 --- a/setup/sequencers/DataAuditSequencer.php +++ b/setup/sequencers/DataAuditSequencer.php @@ -50,7 +50,7 @@ class DataAuditSequencer extends StepSequencer return $this->GetNextStep('compile', 'Compiling the data model', 20, 'Copying...'); case 'compile': - $aSelectedModules = $this->oParams->Get('selected_modules'); + $aSelectedModules = $this->oParams->Get('selected_modules', []); $sSourceDir = $this->oParams->Get('source_dir', 'datamodels/latest'); $sExtensionDir = $this->oParams->Get('extensions_dir', 'extensions'); $aMiscOptions = $this->oParams->Get('options', []); diff --git a/tests/php-unit-tests/unitary-tests/setup/ApplicationInstallSequencerFake.php b/tests/php-unit-tests/unitary-tests/setup/ApplicationInstallSequencerFake.php deleted file mode 100644 index f0d5bf80e..000000000 --- a/tests/php-unit-tests/unitary-tests/setup/ApplicationInstallSequencerFake.php +++ /dev/null @@ -1,57 +0,0 @@ -RequireOnceItopFile('/setup/sequencers/ApplicationInstallSequencer.php'); - $this->RequireOnceItopFile('/setup/sequencers/DataAuditSequencer.php'); - $this->RequireOnceItopFile('/setup/parameters.class.inc.php'); - $this->RequireOnceItopFile('/setup/setuputils.class.inc.php'); - $this->RequireOnceItopFile('/setup/runtimeenv.class.inc.php'); - require_once __DIR__.'/ApplicationInstallSequencerFake.php'; - require_once __DIR__.'/DataAuditSequencerFake.php'; - } - - public function testApplicationInstallSequencer() - { - $oParams = new PHPParameters(); - $aParams = [ - 'mode' => 'install', - 'database' => [ - 'server' => 'server', - 'user' => 'user', - 'pwd' => 'pwd', - 'name' => 'name', - 'prefix' => 'prefix', - 'db_tls_enabled' => 'db_tls_enabled', - 'db_tls_ca' => 'db_tls_ca', - ], - 'application_path' => '', - 'language' => '', - 'graphviz_path' => '', - 'source_dir' => '', - ]; - $oParams->LoadFromHash($aParams); - - $oInstallSequencer = new ApplicationInstallSequencerFake($oParams); - $oInstallSequencer->ExecuteAllSteps(); - - $aStepSequence = [ - '', - 'copy', - 'compile', - 'db-schema', - 'after-db-create', - 'load-data', - 'create-config', - ]; - $this->AssertStepsHistoryIs($aStepSequence, $oInstallSequencer); - } - - public function testDataAuditSequencer() - { - $oParams = new PHPParameters(); - $oParams->LoadFromHash([]); - - $oInstallSequencer = new DataAuditSequencerFake($oParams); - $oInstallSequencer->ExecuteAllSteps(); - - $aStepSequence = [ - '', - 'compile', - 'write-config', - 'setup-audit', - 'cleanup', - ]; - $this->AssertStepsHistoryIs($aStepSequence, $oInstallSequencer); - } - - protected function AssertStepsHistoryIs($aExpectedStepSequence, $oSequencer) - { - - $aHistory = $oSequencer->GetHistory(); - $aStepSequence = []; - foreach ($aHistory as $aStep) { - $aStepSequence[] = $aStep['step']; - } - $this->assertEquals($aExpectedStepSequence, $aStepSequence, 'The step sequence should be '.implode(',', $aExpectedStepSequence)); - } -} diff --git a/tests/php-unit-tests/unitary-tests/setup/sequencers/ApplicationInstallerSequencerTest.php b/tests/php-unit-tests/unitary-tests/setup/sequencers/ApplicationInstallerSequencerTest.php new file mode 100644 index 000000000..4f904b55a --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/setup/sequencers/ApplicationInstallerSequencerTest.php @@ -0,0 +1,105 @@ +RequireOnceItopFile('/setup/sequencers/ApplicationInstallSequencer.php'); + $this->RequireOnceItopFile('/setup/sequencers/DataAuditSequencer.php'); + $this->RequireOnceItopFile('/setup/parameters.class.inc.php'); + $this->RequireOnceItopFile('/setup/setuputils.class.inc.php'); + $this->RequireOnceItopFile('/setup/runtimeenv.class.inc.php'); + } + + public function testFirstStep() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->exactly(2))->method('GetFinalEnv') + ->willReturn('production'); + $oSequencer = new ApplicationInstallSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep(); + $aExpected = [ + 'status' => 1, + 'message' => '', + 'next-step' => 'log-parameters', + 'next-step-label' => 'Log parameters', + 'percentage-completed' => 0, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testAnyFailure() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->exactly(2))->method('GetFinalEnv') + ->willReturn('production'); + $oRunTimeEnvironment->expects($this->once())->method('GetBuildEnv') + ->willThrowException(new \CoreException('SHADOK MSG')); + $oSequencer = new ApplicationInstallSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('db-schema'); + $aExpected = [ + 'status' => 2, + 'message' => 'SHADOK MSG', + 'next-step' => '', + 'next-step-label' => '', + 'percentage-completed' => 100, + 'error_code' => 0, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testUnknownStep() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->exactly(2))->method('GetFinalEnv') + ->willReturn('production'); + $oSequencer = new ApplicationInstallSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('gabuzomeu'); + $aExpected = [ + 'status' => 2, + 'message' => '', + 'next-step' => '', + 'next-step-label' => 'Unknown setup step \'gabuzomeu\'.', + 'percentage-completed' => 100, + ]; + $this->assertEquals($aExpected, $aRes); + } + + private function GivenParams(array $aAdditionalParams = []): PHPParameters + { + $oParams = new PHPParameters(); + $aParams = array_merge([ + 'mode' => 'install', + 'database' => [ + 'server' => 'server', + 'user' => 'user', + 'pwd' => 'pwd', + 'name' => 'name', + 'prefix' => 'prefix', + 'db_tls_enabled' => 'db_tls_enabled', + 'db_tls_ca' => 'db_tls_ca', + ], + 'application_path' => '', + 'language' => '', + 'graphviz_path' => '', + 'source_dir' => '', + ], $aAdditionalParams); + + $oParams->LoadFromHash($aParams); + return $oParams; + } +} diff --git a/tests/php-unit-tests/unitary-tests/setup/sequencers/DataAuditSequencerTest.php b/tests/php-unit-tests/unitary-tests/setup/sequencers/DataAuditSequencerTest.php new file mode 100644 index 000000000..be30c8ac0 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/setup/sequencers/DataAuditSequencerTest.php @@ -0,0 +1,200 @@ +RequireOnceItopFile('/setup/sequencers/ApplicationInstallSequencer.php'); + $this->RequireOnceItopFile('/setup/sequencers/DataAuditSequencer.php'); + $this->RequireOnceItopFile('/setup/parameters.class.inc.php'); + $this->RequireOnceItopFile('/setup/setuputils.class.inc.php'); + $this->RequireOnceItopFile('/setup/runtimeenv.class.inc.php'); + } + + public function testDataAuditFirstStep() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->never())->method($this->anything()); + $oSequencer = new DataAuditSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep(); + $aExpected = [ + 'status' => 1, + 'message' => '', + 'next-step' => 'copy', + 'next-step-label' => 'Copying data model files', + 'percentage-completed' => 5, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testDataUnknownStep() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->never())->method($this->anything()); + $oSequencer = new DataAuditSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('gabuzomeu'); + $aExpected = [ + 'status' => 2, + 'message' => '', + 'next-step' => '', + 'next-step-label' => 'Unknown setup step \'gabuzomeu\'.', + 'percentage-completed' => 100, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testCopy() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->once())->method('CopySetupFiles'); + $oSequencer = new DataAuditSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('copy'); + $aExpected = [ + 'status' => 1, + 'message' => 'Copying...', + 'next-step' => 'compile', + 'next-step-label' => 'Compiling the data model', + 'percentage-completed' => 20, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testCompile() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->once())->method('DoCompile'); + $oSequencer = new DataAuditSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('compile'); + $aExpected = [ + 'status' => 1, + 'message' => 'Using symbolic links instead of copying data model files (for developers only!)', + 'next-step' => 'setup-audit', + 'next-step-label' => 'Checking data consistency with the new data model', + 'percentage-completed' => 70, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testCompileFailure() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->once())->method('DoCompile') + ->willThrowException(new \CoreException('SHADOK MSG')); + $oSequencer = new DataAuditSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('compile'); + $aExpected = [ + 'status' => 2, + 'message' => 'SHADOK MSG', + 'next-step' => '', + 'next-step-label' => '', + 'percentage-completed' => 100, + 'error_code' => 0, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testAuditCalled() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->once())->method('DataToCleanupAudit'); + $oRunTimeEnvironment->expects($this->once())->method('GetFinalEnv') + ->willReturn('production'); + + $aAdditionalParams = [ + 'mode' => 'update', + ]; + $oSequencer = new DataAuditSequencer($this->GivenParams($aAdditionalParams), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('setup-audit'); + $aExpected = [ + 'status' => 1, + 'message' => '', + 'next-step' => '', + 'next-step-label' => 'Completed', + 'percentage-completed' => 100, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testNoAuditInUpgradeModeWhenNoRuntimeEnvironmentAvailable() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->never())->method('DataToCleanupAudit'); + $oRunTimeEnvironment->expects($this->once())->method('GetFinalEnv') + ->willReturn('gabuzomeu'); + + $aAdditionalParams = [ + 'mode' => 'update', + ]; + $oSequencer = new DataAuditSequencer($this->GivenParams($aAdditionalParams), $oRunTimeEnvironment); + $aRes = $oSequencer->ExecuteStep('setup-audit'); + $aExpected = [ + 'status' => 1, + 'message' => '', + 'next-step' => '', + 'next-step-label' => 'Completed', + 'percentage-completed' => 100, + ]; + $this->assertEquals($aExpected, $aRes); + } + + public function testNoAuditInFreshInstallMode() + { + $oRunTimeEnvironment = $this->createMock(\RunTimeEnvironment::class); + $oRunTimeEnvironment->expects($this->never())->method('GetFinalEnv') + ->willReturn('gabuzomeu'); + $oRunTimeEnvironment->expects($this->never())->method('DataToCleanupAudit'); + + $oSequencer = new DataAuditSequencer($this->GivenParams(), $oRunTimeEnvironment); + + $aRes = $oSequencer->ExecuteStep('setup-audit'); + $aExpected = [ + 'status' => 1, + 'message' => '', + 'next-step' => '', + 'next-step-label' => 'Completed', + 'percentage-completed' => 100, + ]; + $this->assertEquals($aExpected, $aRes); + } + + private function GivenParams(array $aAdditionalParams = []): PHPParameters + { + $oParams = new PHPParameters(); + $aParams = array_merge([ + 'mode' => 'install', + 'database' => [ + 'server' => 'server', + 'user' => 'user', + 'pwd' => 'pwd', + 'name' => 'name', + 'prefix' => 'prefix', + 'db_tls_enabled' => 'db_tls_enabled', + 'db_tls_ca' => 'db_tls_ca', + ], + 'application_path' => '', + 'language' => '', + 'graphviz_path' => '', + 'source_dir' => '', + ], $aAdditionalParams); + + $oParams->LoadFromHash($aParams); + return $oParams; + } +}