From 9b651c245198882e4da614d457794ddb2c4c6c23 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Fri, 18 Oct 2024 17:23:46 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B07803=20:white=5Fcheck=5Fmark:=20Test=20i?= =?UTF-8?q?Top=20Hub=20move=20to=20production=20(compilation=20phase)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../itop-hub-connector/AjaxPageTest.php | 68 +++++++++++++++++++ .../src/BaseTestCase/ItopDataTestCase.php | 24 +++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/php-unit-tests/integration-tests/itop-hub-connector/AjaxPageTest.php diff --git a/tests/php-unit-tests/integration-tests/itop-hub-connector/AjaxPageTest.php b/tests/php-unit-tests/integration-tests/itop-hub-connector/AjaxPageTest.php new file mode 100644 index 000000000..e69150660 --- /dev/null +++ b/tests/php-unit-tests/integration-tests/itop-hub-connector/AjaxPageTest.php @@ -0,0 +1,68 @@ +GivenUserInDB(self::AUTHENTICATION_PASSWORD, ['Administrator']); + + $iLastCompilation = filemtime(APPROOT.'env-production'); + + // When + $sOutput = $this->CallItopUrl( + "/pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php", + [ + 'auth_user' => $sLogin, + 'auth_pwd' => self::AUTHENTICATION_PASSWORD, + 'operation' => "compile", + 'authent' => self::AUTHENTICATION_TOKEN, + ] + ); + + // Then + $aRes = json_decode($sOutput, true); + $this->assertNotNull($aRes, "Response should be a valid json, found instead:" . PHP_EOL . $sOutput); + $this->assertEquals( + [ + 'code' => 0, + 'message' => 'Ok', + 'fields' => [] + ], + $aRes + ); + + clearstatcache(); + $this->assertGreaterThan($iLastCompilation, filemtime(APPROOT.'env-production'), 'The env-production directory should have been rebuilt'); + } + + protected function CallItopUrl($sUri, ?array $aPostFields = null, bool $bXDebugEnabled = false) + { + $ch = curl_init(); + if ($bXDebugEnabled) { + curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=phpstorm'); + } + + $sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri"; + var_dump($sUrl); + curl_setopt($ch, CURLOPT_URL, $sUrl); + curl_setopt($ch, CURLOPT_POST, 1);// set post data to true + curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + $sOutput = curl_exec($ch); + //echo "$sUrl error code:".curl_error($ch); + curl_close($ch); + + return $sOutput; + } +} \ No newline at end of file diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php index 4c4f756e5..3d5aba722 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php @@ -1368,4 +1368,28 @@ abstract class ItopDataTestCase extends ItopTestCase ]); return $sLogin; } + + /** + * @param string $sPassword + * @param array $aProfiles Profile names Example: ['Administrator'] + * + * @return string The unique login + * @throws \Exception + */ + protected function GivenUserInDB(string $sPassword, array $aProfiles): string + { + $sLogin = 'demo_test_'.uniqid(__CLASS__, true); + + $aProfileList = array_map(function($sProfileId) { + return 'profileid:'.self::$aURP_Profiles[$sProfileId]; + }, $aProfiles); + + $iUser = $this->GivenObjectInDB('UserLocal', [ + 'login' => $sLogin, + 'password' => $sPassword, + 'language' => 'EN US', + 'profile_list' => $aProfileList, + ]); + return $sLogin; + } }