mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-20 00:58:48 +02:00
N°3455: clean code + test after review
This commit is contained in:
@@ -31,68 +31,6 @@ class RestTest extends ItopDataTestCase
|
||||
}
|
||||
}
|
||||
|
||||
private function GetTicketViaRest($iId){
|
||||
$sJsonGetContent = <<<JSON
|
||||
{
|
||||
"operation": "core/get",
|
||||
"class": "UserRequest",
|
||||
"key": "SELECT UserRequest WHERE id=$iId",
|
||||
"output_fields": "id, description"
|
||||
}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonGetContent);
|
||||
}
|
||||
|
||||
private function UpdateTicketViaApi($iId, $description){
|
||||
$sJsonUpdateContent = <<<JSON
|
||||
{"operation": "core/update","comment": "test","class": "UserRequest","key":"$iId","output_fields": "description","fields":{"description": "$description"}}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonUpdateContent);
|
||||
}
|
||||
|
||||
private function CreateTicketViaApi($description){
|
||||
$sJsonCreateContent = <<<JSON
|
||||
{
|
||||
"operation": "core/create",
|
||||
"comment": "test",
|
||||
"class": "UserRequest",
|
||||
"output_fields": "id",
|
||||
"fields":
|
||||
{
|
||||
"org_id": "SELECT Organization WHERE name = \"Demo\"",
|
||||
|
||||
"title": "Houston, got a problem",
|
||||
"description": "$description"
|
||||
}
|
||||
}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonCreateContent);
|
||||
}
|
||||
|
||||
private function DeleteTicketFromApi($iId){
|
||||
$sJson = <<<JSON
|
||||
{
|
||||
"operation": "core/delete",
|
||||
"comment": "Cleanup",
|
||||
"class": "UserRequest",
|
||||
"key":$iId,
|
||||
"simulate": false
|
||||
}
|
||||
JSON;
|
||||
return $this->CallRestApi($sJson);
|
||||
|
||||
}
|
||||
|
||||
public function BasicProvider(){
|
||||
return [
|
||||
'call rest call' => [ 'bCallApiViaFile' => false],
|
||||
'pass json_data as file' => [ 'bCallApiViaFile' => true]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider BasicProvider
|
||||
*/
|
||||
@@ -138,7 +76,69 @@ JSON;
|
||||
$this->assertEquals($sExpectedJsonOuput, $this->GetTicketViaRest($iId));
|
||||
}
|
||||
|
||||
private function CallRestApi($sJsonDataContent){
|
||||
private function GetTicketViaRest($iId){
|
||||
$sJsonGetContent = <<<JSON
|
||||
{
|
||||
"operation": "core/get",
|
||||
"class": "UserRequest",
|
||||
"key": "SELECT UserRequest WHERE id=$iId",
|
||||
"output_fields": "id, description"
|
||||
}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonGetContent);
|
||||
}
|
||||
|
||||
public function BasicProvider(){
|
||||
return [
|
||||
'call rest call' => [ 'bCallApiViaFile' => false],
|
||||
'pass json_data as file' => [ 'bCallApiViaFile' => true]
|
||||
];
|
||||
}
|
||||
|
||||
private function UpdateTicketViaApi($iId, $description){
|
||||
$sJsonUpdateContent = <<<JSON
|
||||
{"operation": "core/update","comment": "test","class": "UserRequest","key":"$iId","output_fields": "description","fields":{"description": "$description"}}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonUpdateContent);
|
||||
}
|
||||
|
||||
private function CreateTicketViaApi($description){
|
||||
$sJsonCreateContent = <<<JSON
|
||||
{
|
||||
"operation": "core/create",
|
||||
"comment": "test",
|
||||
"class": "UserRequest",
|
||||
"output_fields": "id",
|
||||
"fields":
|
||||
{
|
||||
"org_id": "SELECT Organization WHERE name = \"Demo\"",
|
||||
|
||||
"title": "Houston, got a problem",
|
||||
"description": "$description"
|
||||
}
|
||||
}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonCreateContent);
|
||||
}
|
||||
|
||||
private function DeleteTicketFromApi($iId){
|
||||
$sJson = <<<JSON
|
||||
{
|
||||
"operation": "core/delete",
|
||||
"comment": "Cleanup",
|
||||
"class": "UserRequest",
|
||||
"key":$iId,
|
||||
"simulate": false
|
||||
}
|
||||
JSON;
|
||||
return $this->CallRestApi($sJson);
|
||||
|
||||
}
|
||||
|
||||
private function CallRestApi($sJsonDataContent){
|
||||
$ch = curl_init();
|
||||
$aPostFields = [
|
||||
'version' => '1.3',
|
||||
|
||||
@@ -70,20 +70,21 @@ $oCtx = new ContextTag(ContextTag::TAG_REST);
|
||||
|
||||
$sVersion = utils::ReadParam('version', null, false, 'raw_data');
|
||||
$sOperation = utils::ReadParam('operation', null);
|
||||
if(isset($_FILES['json_data']['name']) && !empty($_FILES['json_data']['name']))
|
||||
{
|
||||
$sTmpFilePath = $_FILES['json_data']['tmp_name'];
|
||||
if (is_file($sTmpFilePath)){
|
||||
$sValue = file_get_contents($sTmpFilePath);
|
||||
unlink($sTmpFilePath);
|
||||
if (! empty($sValue)){
|
||||
$sJsonString = utils::Sanitize($sValue, null, 'raw_data');
|
||||
$sJsonString = utils::ReadParam('json_data', null, false, 'raw_data');
|
||||
|
||||
if (empty($sJsonString)){
|
||||
if(isset($_FILES['json_data']['tmp_name']))
|
||||
{
|
||||
$sTmpFilePath = $_FILES['json_data']['tmp_name'];
|
||||
if (is_file($sTmpFilePath)){
|
||||
$sValue = file_get_contents($sTmpFilePath);
|
||||
unlink($sTmpFilePath);
|
||||
if (! empty($sValue)){
|
||||
$sJsonString = utils::Sanitize($sValue, null, 'raw_data');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($sJsonString)){
|
||||
$sJsonString = utils::ReadParam('json_data', null, false, 'raw_data');
|
||||
}
|
||||
|
||||
$sProvider = '';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user