From cf1b61392327d4c49a3878b2182da07d1fc72e2d Mon Sep 17 00:00:00 2001 From: odain Date: Sun, 6 Dec 2020 23:54:27 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B03464=20REST=20comment=20field=20not=20wo?= =?UTF-8?q?rking=20anymore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/applicationextension.inc.php | 5 + test/webservices/RestTest.php | 245 +++++++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 test/webservices/RestTest.php diff --git a/application/applicationextension.inc.php b/application/applicationextension.inc.php index 7b1baa97d8..d408700652 100644 --- a/application/applicationextension.inc.php +++ b/application/applicationextension.inc.php @@ -1345,6 +1345,11 @@ class RestUtils { $sComment = self::GetMandatoryParam($oData, 'comment'); CMDBObject::SetTrackInfo($sComment); + + /** @var \CMDBChange $oChange */ + $oChange = CMDBObject::GetCurrentChange(); + $oChange->Set("userinfo", $sComment); + $oChange->DBUpdate(); } /** diff --git a/test/webservices/RestTest.php b/test/webservices/RestTest.php new file mode 100644 index 0000000000..0b3d9c9b47 --- /dev/null +++ b/test/webservices/RestTest.php @@ -0,0 +1,245 @@ +sTmpFile)){ + unlink($this->sTmpFile); + } + } + + /** + * @dataProvider BasicProvider + * @param bool $bPassJsonDataAsFile + */ + public function testCreateApi($bPassJsonDataAsFile) + { + $this->bPassJsonDataAsFile = $bPassJsonDataAsFile; + + //create ticket + $description = date('dmY H:i:s'); + $sOuputJson = $this->CreateTicketViaApi($description); + $aJson = json_decode($sOuputJson, true); + $this->assertContains("0", "".$aJson['code']); + $sUserRequestKey = array_key_first($aJson['objects']); + $this->assertContains('UserRequest::', $sUserRequestKey); + $iId = $aJson['objects'][$sUserRequestKey]['key']; + $sExpectedJsonOuput=<<assertEquals($sExpectedJsonOuput, $sOuputJson); + + $sExpectedJsonOuput=<<$description<\/p>"}}},"code":0,"message":"Found: 1"} +JSON; + $this->assertEquals($sExpectedJsonOuput, $this->GetTicketViaRest($iId)); + + $aCmdbChangeUserInfo = $this->GetCmdbChangeUserInfo($iId); + $this->assertEquals(['CMDBChangeOpCreate' => 'test'], $aCmdbChangeUserInfo); + + //delete ticket + $this->DeleteTicketFromApi($iId); + } + + /** + * @dataProvider BasicProvider + * @param bool $bPassJsonDataAsFile + */ + public function testUpdateApi($bPassJsonDataAsFile) + { + $this->bPassJsonDataAsFile = $bPassJsonDataAsFile; + + //create ticket + $description = date('dmY H:i:s'); + $sOuputJson = $this->CreateTicketViaApi($description); + $aJson = json_decode($sOuputJson, true); + $this->assertContains("0", "".$aJson['code']); + $sUserRequestKey = array_key_first($aJson['objects']); + $this->assertContains('UserRequest::', $sUserRequestKey); + $iId = $aJson['objects'][$sUserRequestKey]['key']; + + //update ticket + $description = date('Ymd H:i:s'); + $sExpectedJsonOuput=<<$description<\/p>"}}},"code":0,"message":null} +JSON; + $this->assertEquals($sExpectedJsonOuput, $this->UpdateTicketViaApi($iId, $description)); + + $aCmdbChangeUserInfo = $this->GetCmdbChangeUserInfo($iId); + $this->assertEquals(['CMDBChangeOpCreate' => 'test', 'CMDBChangeOpSetAttributeHTML' => 'test'], $aCmdbChangeUserInfo); + + + //delete ticket + $this->DeleteTicketFromApi($iId); + } + /** + * @dataProvider BasicProvider + * @param bool $bPassJsonDataAsFile + */ + public function testDeleteApi($bPassJsonDataAsFile) + { + $this->bPassJsonDataAsFile = $bPassJsonDataAsFile; + + //create ticket + $description = date('dmY H:i:s'); + + $sOuputJson = $this->CreateTicketViaApi($description); + $aJson = json_decode($sOuputJson, true); + $this->assertContains("0", "".$aJson['code']); + $sUserRequestKey = array_key_first($aJson['objects']); + $this->assertContains('UserRequest::', $sUserRequestKey); + $iId = $aJson['objects'][$sUserRequestKey]['key']; + + //delete ticket + $sExpectedJsonOuput=<<assertContains($sExpectedJsonOuput, $this->DeleteTicketFromApi($iId)); + + $sExpectedJsonOuput=<<assertEquals($sExpectedJsonOuput, $this->GetTicketViaRest($iId)); + } + + private function GetTicketViaRest($iId){ + $sJsonGetContent = <<CallRestApi($sJsonGetContent); + } + + public function BasicProvider(){ + return [ + 'call rest call' => [ 'bCallApiViaFile' => false], + //'pass json_data as file' => [ 'bCallApiViaFile' => true] + ]; + } + + private function UpdateTicketViaApi($iId, $description){ + $sJsonUpdateContent = <<CallRestApi($sJsonUpdateContent); + } + + private function CreateTicketViaApi($description){ + $sJsonCreateContent = <<CallRestApi($sJsonCreateContent); + } + + private function DeleteTicketFromApi($iId){ + $sJson = <<CallRestApi($sJson); + + } + + private function CallRestApi($sJsonDataContent){ + $ch = curl_init(); + $aPostFields = [ + 'version' => '1.3', + 'auth_user' => 'admin', + 'auth_pwd' => 'admin', + ]; + + if ($this->bPassJsonDataAsFile){ + $this->sTmpFile = tempnam(sys_get_temp_dir(), 'jsondata_'); + file_put_contents($this->sTmpFile, $sJsonDataContent); + + $oCurlFile = curl_file_create($this->sTmpFile); + $aPostFields['json_data'] = $oCurlFile; + }else{ + $aPostFields['json_data'] = $sJsonDataContent; + } + + curl_setopt($ch, CURLOPT_URL, "http://localhost/iTop/webservices/rest.php"); + curl_setopt($ch, CURLOPT_POST, 1);// set post data to true + curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $sJson = curl_exec($ch); + curl_close ($ch); + + return $sJson; + } + + /** + * @param $iId + * Get CMDBChangeOp info to test + * @return array + */ + private function GetCmdbChangeUserInfo($iId){ + $sJsonGetContent = <<CallRestApi($sJsonGetContent); + $aJson = json_decode($sOutput, true); + if (is_array($aJson) && array_key_exists('objects', $aJson)){ + $aObjects = $aJson['objects']; + if (!empty($aObjects)){ + foreach ($aObjects as $aObject){ + $sClass = $aObject['class']; + $sUserInfo = $aObject['fields']['userinfo']; + $aUserInfo[$sClass] = $sUserInfo; + } + } + } + return $aUserInfo; + } +}