From 6cfaebfbcfb843807e808b0a67d38624ac88a2b9 Mon Sep 17 00:00:00 2001 From: Anne-Cath Date: Tue, 1 Apr 2025 16:41:56 +0200 Subject: [PATCH] Add tests --- .../unitary-tests/webservices/RestTest.php | 159 +++++++++++++++++- .../webservices/RestUtilsTest.php | 110 ++++++++++++ 2 files changed, 266 insertions(+), 3 deletions(-) create mode 100644 tests/php-unit-tests/unitary-tests/webservices/RestUtilsTest.php diff --git a/tests/php-unit-tests/unitary-tests/webservices/RestTest.php b/tests/php-unit-tests/unitary-tests/webservices/RestTest.php index 6b1c6848a..f96545ab9 100644 --- a/tests/php-unit-tests/unitary-tests/webservices/RestTest.php +++ b/tests/php-unit-tests/unitary-tests/webservices/RestTest.php @@ -142,6 +142,158 @@ JSON; $this->assertJsonStringEqualsJsonString($sExpectedJsonOuput, $sJSONOutput); } + + public function testCoreApiGet_Select2SubClasses(){ + // Create ticket + $description = date('dmY H:i:s'); + $iIdCaller = $this->CreatePerson(1)->GetKey(); + $oUserRequest = $this->CreateSampleTicket($description, 'UserRequest', $iIdCaller); + $oChange = $this->CreateSampleTicket($description, 'Change', $iIdCaller); + $iIdUserRequest = $oUserRequest->GetKey(); + $iIdChange = $oChange->GetKey(); + + $sJSONOutput = $this->CallCoreRestApi_Internally(<<$description

", + "id": "$iIdUserRequest" + }, + "key": "$iIdUserRequest", + "message": "" + }, + "Change::$iIdChange": { + "class": "Change", + "code": 0, + "fields": { + "description": "

$description

", + "id": "$iIdChange", + "outage": "no" + }, + "key": "$iIdChange", + "message": "" + } + } +} +JSON; + $this->assertJsonStringEqualsJsonString($sExpectedJsonOuput, $sJSONOutput); + } + + + public function testCoreApiGet_SelectTicketAndPerson(){ + // Create ticket + $description = date('dmY H:i:s'); + $iIdCaller = $this->CreatePerson(1)->GetKey(); + $oUserRequest = $this->CreateSampleTicket($description, 'UserRequest', $iIdCaller); + $iIdUserRequest = $oUserRequest->GetKey(); + + $sJSONOutput = $this->CallCoreRestApi_Internally(<<$description

", + "id": "$iIdUserRequest", + "title": "Houston, got a problem" + }, + "key": "$iIdUserRequest", + "message": "" + }, + "P": { + "class": "Person", + "code": 0, + "fields": { + "email": "", + "id": "$iIdCaller", + "name": "Person_1" + }, + "key": "$iIdCaller", + "message": "" + } + }] +} +JSON; + $this->assertJsonStringEqualsJsonString($sExpectedJsonOuput, $sJSONOutput); + } + + public function testCoreApiGetWithUnionAndDifferentOutputFields(){ + // Create ticket + $description = date('dmY H:i:s'); + $oUserRequest = $this->CreateSampleTicket($description); + $oChange = $this->CreateSampleTicket($description, 'Change'); + $iUserRequestId = $oUserRequest->GetKey(); + $sUserRequestRef = $oUserRequest->Get('ref'); + $iChangeId = $oChange->GetKey(); + $sChangeRef = $oChange->Get('ref'); + + $sJSONOutput = $this->CallCoreRestApi_Internally(<<assertJsonStringEqualsJsonString($sExpectedJsonOuput, $sJSONOutput); + } public function testCoreApiCreate() { // Create ticket @@ -253,12 +405,13 @@ JSON; // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private function CreateSampleTicket($description) + private function CreateSampleTicket($description, $sType = 'UserRequest', $iIdCaller = null) { - $oTicket = $this->createObject('UserRequest', [ + $oTicket = $this->createObject($sType, [ 'org_id' => $this->getTestOrgId(), "title" => "Houston, got a problem", - "description" => $description + "description" => $description, + "caller_id" => $iIdCaller, ]); return $oTicket; } diff --git a/tests/php-unit-tests/unitary-tests/webservices/RestUtilsTest.php b/tests/php-unit-tests/unitary-tests/webservices/RestUtilsTest.php new file mode 100644 index 000000000..779c93819 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/webservices/RestUtilsTest.php @@ -0,0 +1,110 @@ + 'ref,start_date,end_date'], 'output_fields'); + $this->assertSame([Ticket::class => ['ref', 'start_date', 'end_date']], $aList); + } + + public function testGetFieldListForSingleClassWithInvalidFieldNameFails(): void + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('output_fields: invalid attribute code \'something\' for class \'Ticket\''); + $aList = RestUtils::GetFieldList(Ticket::class, (object) ['output_fields' => 'ref,something'], 'output_fields'); + $this->assertSame([Ticket::class => ['ref', 'start_date', 'end_date']], $aList); + } + + public function testGetFieldListWithAsteriskOnParentClass(): void + { + $aList = RestUtils::GetFieldList(Ticket::class, (object) ['output_fields' => '*'], 'output_fields'); + $this->assertArrayHasKey(Ticket::class, $aList); + $this->assertContains('operational_status', $aList[Ticket::class]); + $this->assertNotContains('status', $aList[Ticket::class], 'Representation of Class Ticket should not contain status, since it is defined by children'); + } + + public function testGetFieldListWithAsteriskPlusOnParentClass(): void + { + $aList = RestUtils::GetFieldList(Ticket::class, (object) ['output_fields' => '*+'], 'output_fields'); + $this->assertArrayHasKey(Ticket::class, $aList); + $this->assertArrayHasKey(UserRequest::class, $aList); + $this->assertContains('operational_status', $aList[Ticket::class]); + $this->assertContains('status', $aList[UserRequest::class]); + } + + public function testGetFieldListForMultipleClasses(): void + { + $aList = RestUtils::GetFieldList(Ticket::class, (object) ['output_fields' => 'Ticket:ref,start_date,end_date;UserRequest:ref,status'], 'output_fields'); + $this->assertArrayHasKey(Ticket::class, $aList); + $this->assertArrayHasKey(UserRequest::class, $aList); + $this->assertContains('ref', $aList[Ticket::class]); + $this->assertContains('end_date', $aList[Ticket::class]); + $this->assertNotContains('status', $aList[Ticket::class]); + $this->assertContains('status', $aList[UserRequest::class]); + $this->assertNotContains('end_date', $aList[UserRequest::class]); + } + + public function testGetFieldListForMultipleClassesWithInvalidFieldNameFails(): void + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('output_fields: invalid attribute code \'something\''); + RestUtils::GetFieldList(Ticket::class, (object) ['output_fields' => 'Ticket:ref;UserRequest:ref,something'], 'output_fields'); + } + + + public function testGetFieldListForMultipleClassesWithInvalidFieldName(): void + { + $aList = RestUtils::GetFieldList(Ticket::class, (object) ['output_fields' => 'ref, something'], 'output_fields', false); + $this->assertContains('ref', $aList[Ticket::class]); + $this->assertNotContains('something', $aList[Ticket::class]); + } + + + /** + * @dataProvider extendedOutputDataProvider + */ + public function testIsExtendedOutputRequest(bool $bExpected, string $sFields): void + { + $this->assertSame($bExpected, RestUtils::HasRequestedExtendedOutput($sFields)); + } + + /** + * @dataProvider allFieldsOutputDataProvider + */ + public function testIsAllFieldsOutputRequest(bool $bExpected, string $sFields): void + { + $this->assertSame($bExpected, RestUtils::HasRequestedAllOutputFields($sFields)); + } + + public function extendedOutputDataProvider(): array + { + return [ + [false, 'ref,start_date,end_date'], + [false, '*'], + [true, '*+'], + [false, 'Ticket:ref'], + [true, 'Ticket:ref;UserRequest:ref'], + ]; + } + + public function allFieldsOutputDataProvider(): array + { + return [ + [false, 'ref,start_date,end_date'], + [true, '*'], + [true, '*+'], + [false, 'Ticket:ref'], + [false, 'Ticket:ref;UserRequest:ref'], + ]; + } +} \ No newline at end of file