diff --git a/status.inc.php b/sources/application/status/status.inc.php similarity index 81% rename from status.inc.php rename to sources/application/status/status.inc.php index ddc996bbc..27838dbea 100644 --- a/status.inc.php +++ b/sources/application/status/status.inc.php @@ -1,5 +1,7 @@ STATUS_RUNNING, 'code' => \RestResult::OK, 'message' => ''); } catch (\Exception $e) diff --git a/test/status/StatusIncTest.php b/test/status/StatusIncTest.php index c5336ad57..49fe073a2 100644 --- a/test/status/StatusIncTest.php +++ b/test/status/StatusIncTest.php @@ -11,6 +11,10 @@ namespace Combodo\iTop\Test\UnitTest\Status; * User: Guy Couronné (guy.couronne@gmail.com) * Date: 25/01/2019 */ + +use function Combodo\iTop\Application\Status\StatusCheckConfigFile; +use function Combodo\iTop\Application\Status\StatusGetAppRoot; +use function Combodo\iTop\Application\Status\StatusStartup; use PHPUnit\Framework\TestCase; if (!defined('DEBUG_UNIT_TEST')) { @@ -36,7 +40,7 @@ class StatusIncTest extends TestCase { protected function setUp() { //AppRoot is the directory containing the directory //Assume getcwd() is runned inside APPROOT/test - $this->sAppRoot = dirname(getcwd()); + $this->sAppRoot = __DIR__ . '/../../sources/application/status'; } /** @@ -89,7 +93,7 @@ class StatusIncTest extends TestCase { public function testStatusStartupWrongDbPwd() { include_once($this->sAppRoot . '/status.inc.php'); - \StatusCheckConfigFile(); + StatusCheckConfigFile(); require_once(APPROOT . '/core/cmdbobject.class.inc.php'); require_once(APPROOT . '/application/utils.inc.php'); require_once(APPROOT . '/core/contexttag.class.inc.php'); diff --git a/test/status/StatusTest.php b/test/status/StatusTest.php index d38f200ba..f607d9ff3 100644 --- a/test/status/StatusTest.php +++ b/test/status/StatusTest.php @@ -7,184 +7,48 @@ namespace Combodo\iTop\Test\UnitTest\Status; -use Combodo\iTop\Test\UnitTest\ItopDataTestCase; +use Combodo\iTop\Test\UnitTest\ItopTestCase; /** * */ -class StatusTest extends ItopDataTestCase { +class StatusTest extends ItopTestCase { /** * */ public function testStatusWrongUrl() { - $sUrl = \utils::GetAbsoluteUrlAppRoot() . 'status_wrong.php'; + $sPath = APPROOT . 'status_wrong.php'; - if (function_exists('curl_init')) { - // If cURL is available, let's use it, since it provides a greater control over the various HTTP/SSL options - // For instance fopen does not allow to work around the bug: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112 - // by setting the SSLVERSION to 3 as done below. - // Default options, can be overloaded/extended with the 4th parameter of this method, see above $aCurlOptions - $aOptions = array( - CURLOPT_RETURNTRANSFER => true, // return the content of the request - CURLOPT_HEADER => false, // don't return the headers in the output - CURLOPT_FOLLOWLOCATION => true, // follow redirects - CURLOPT_ENCODING => "", // handle all encodings - CURLOPT_USERAGENT => "spider", // who am i - CURLOPT_AUTOREFERER => true, // set referer on redirect - CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect - CURLOPT_TIMEOUT => 120, // timeout on response - CURLOPT_MAXREDIRS => 10, // stop after 10 redirects - CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks - // SSLV3 (CURL_SSLVERSION_SSLv3 = 3) is now considered as obsolete/dangerous: http://disablessl3.com/#why - // but it used to be a MUST to prevent a strange SSL error: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112 - // CURLOPT_SSLVERSION - CURLOPT_CUSTOMREQUEST => 'HEAD', //Get only HTTP Code as this page should only return a HTTP Code - ); + exec("php $sPath", $aOutput, $iRet); - $ch = curl_init($sUrl); - curl_setopt_array($ch, $aOptions); - curl_exec($ch); - $sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - } else { - // cURL is not available let's try with streams and fopen... - // By default get_headers uses a GET request to fetch the headers. If you - // want to send a HEAD request instead, you can do so using a stream context: - stream_context_set_default( - array( - 'http' => array( - 'method' => 'HEAD' - ) - ) - ); - $headers = get_headers($sUrl); - //Undo overriding default context - stream_context_set_default( - array( - 'http' => array( - 'method' => 'GET' - ) - ) - ); - $sHttpCode = (int) substr($headers[0], 9, 3); - } - - $this->assertNotEquals(200, $sHttpCode, "Problem opening URL: $sUrl, $sHttpCode"); + $this->assertNotEquals(0, $iRet, "Problem executing status page: $sPath, $iRet"); } /** * */ public function testStatusGood() { - $sUrl = \utils::GetAbsoluteUrlAppRoot() . 'status.php'; + $sPath = APPROOT . 'status.php'; - if (function_exists('curl_init')) { - // If cURL is available, let's use it, since it provides a greater control over the various HTTP/SSL options - // For instance fopen does not allow to work around the bug: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112 - // by setting the SSLVERSION to 3 as done below. - // Default options, can be overloaded/extended with the 4th parameter of this method, see above $aCurlOptions - $aOptions = array( - CURLOPT_RETURNTRANSFER => true, // return the content of the request - CURLOPT_HEADER => false, // don't return the headers in the output - CURLOPT_FOLLOWLOCATION => true, // follow redirects - CURLOPT_ENCODING => "", // handle all encodings - CURLOPT_USERAGENT => "spider", // who am i - CURLOPT_AUTOREFERER => true, // set referer on redirect - CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect - CURLOPT_TIMEOUT => 120, // timeout on response - CURLOPT_MAXREDIRS => 10, // stop after 10 redirects - CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks - // SSLV3 (CURL_SSLVERSION_SSLv3 = 3) is now considered as obsolete/dangerous: http://disablessl3.com/#why - // but it used to be a MUST to prevent a strange SSL error: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112 - // CURLOPT_SSLVERSION - CURLOPT_CUSTOMREQUEST => 'HEAD', //Get only HTTP Code as this page should only return a HTTP Code - ); + exec("php $sPath", $aOutput, $iRet); - $ch = curl_init($sUrl); - curl_setopt_array($ch, $aOptions); - curl_exec($ch); - $iErr = curl_errno($ch); - $sErrMsg = curl_error($ch); - $sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - $this->assertEquals(0, $iErr, "Problem opening URL: $sUrl, $sErrMsg"); - } else { - // cURL is not available let's try with streams and fopen... - // By default get_headers uses a GET request to fetch the headers. If you - // want to send a HEAD request instead, you can do so using a stream context: - stream_context_set_default( - array( - 'http' => array( - 'method' => 'HEAD' - ) - ) - ); - $headers = get_headers($sUrl); - //Undo overriding default context - stream_context_set_default( - array( - 'http' => array( - 'method' => 'GET' - ) - ) - ); - $sHttpCode = (int) substr($headers[0], 9, 3); - } - - $this->assertEquals(200, $sHttpCode, "Problem opening URL: $sUrl, $sHttpCode"); + $this->assertEquals(0, $iRet, "Problem executing status page: $sPath, $iRet"); } /** * */ public function testStatusGoodWithJson() { - $sUrl = \utils::GetAbsoluteUrlAppRoot() . 'status.php'; + $sPath = APPROOT . 'status.php'; - if (function_exists('curl_init')) { - // If cURL is available, let's use it, since it provides a greater control over the various HTTP/SSL options - // For instance fopen does not allow to work around the bug: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112 - // by setting the SSLVERSION to 3 as done below. - // Default options, can be overloaded/extended with the 4th parameter of this method, see above $aCurlOptions - $aOptions = array( - CURLOPT_RETURNTRANSFER => true, // return the content of the request - CURLOPT_HEADER => false, // don't return the headers in the output - CURLOPT_FOLLOWLOCATION => true, // follow redirects - CURLOPT_ENCODING => "", // handle all encodings - CURLOPT_USERAGENT => "spider", // who am i - CURLOPT_AUTOREFERER => true, // set referer on redirect - CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect - CURLOPT_TIMEOUT => 120, // timeout on response - CURLOPT_MAXREDIRS => 10, // stop after 10 redirects - CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks - // SSLV3 (CURL_SSLVERSION_SSLv3 = 3) is now considered as obsolete/dangerous: http://disablessl3.com/#why - // but it used to be a MUST to prevent a strange SSL error: http://stackoverflow.com/questions/18191672/php-curl-ssl-routinesssl23-get-server-helloreason1112 - // CURLOPT_SSLVERSION - ); - - $ch = curl_init($sUrl); - curl_setopt_array($ch, $aOptions); - $response = curl_exec($ch); - $iErr = curl_errno($ch); - $sErrMsg = curl_error($ch); - $sHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - $this->assertEquals(0, $iErr, "Problem opening URL: $sUrl, $sErrMsg"); - $this->assertEquals(200, $sHttpCode, "Problem opening URL: $sUrl, $sHttpCode"); - } else { - // cURL is not available let's try with file_get_contents - $response = file_get_contents($sUrl); - - $this->assertNotFalse($response, "Problem opening URL: $sUrl"); - } + exec("php $sPath", $aOutput, $iRet); //Check response - $this->assertNotEmpty($response, 'Empty response'); - $this->assertJson($response, 'Not a JSON'); + $this->assertNotEmpty($aOutput[0], 'Empty response'); + $this->assertJson($aOutput[0], 'Not a JSON'); - $aResponseDecoded = json_decode($response, true); + $aResponseDecoded = json_decode($aOutput[0], true); //Check status $this->assertArrayHasKey('status', $aResponseDecoded, 'JSON does not have a status\' field');