aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2024-05-21 11:41:55 +0000
committerDavid Siegfried <david.siegfried@uni-vechta.de>2024-05-21 11:41:55 +0000
commitc21817bfe8bd8695f612e47c2a1d8de03148fb88 (patch)
tree791a588cd0c276f117e1bf8f70a4b56d9c83013f /tests
parent98ee46ee0cb82934ff111a72d5cb7a93b68219d8 (diff)
implement jsonapi for clipboard and clipboard items and replace old clipboards route with new ones, fixes #4198
Closes #4198 Merge request studip/studip!3029
Diffstat (limited to 'tests')
-rw-r--r--tests/_support/Helper/StudipDb.php8
-rw-r--r--tests/jsonapi/ClipboardRoutesTest.php167
-rw-r--r--tests/jsonapi/ConsultationHelper.php108
-rw-r--r--tests/jsonapi/JSONAPIHelperTrait.php117
4 files changed, 286 insertions, 114 deletions
diff --git a/tests/_support/Helper/StudipDb.php b/tests/_support/Helper/StudipDb.php
index c80338c..b95e703 100644
--- a/tests/_support/Helper/StudipDb.php
+++ b/tests/_support/Helper/StudipDb.php
@@ -11,19 +11,11 @@ class StudipDb extends \Codeception\Module
{
/**
* @api
- *
- * @var
*/
public ?\StudipPdo $dbh;
- /**
- * @var array
- */
protected array $config = [];
- /**
- * @var array
- */
protected array $requiredFields = ['dsn', 'user', 'password'];
/**
diff --git a/tests/jsonapi/ClipboardRoutesTest.php b/tests/jsonapi/ClipboardRoutesTest.php
new file mode 100644
index 0000000..71a851a
--- /dev/null
+++ b/tests/jsonapi/ClipboardRoutesTest.php
@@ -0,0 +1,167 @@
+<?php
+
+use JsonApi\Routes\Clipboards\ClipboardItemsCreate;
+use JsonApi\Routes\Clipboards\ClipboardItemsDelete;
+use JsonApi\Routes\Clipboards\ClipboardsCreate;
+use JsonApi\Routes\Clipboards\ClipboardsDelete;
+use JsonApi\Routes\Clipboards\ClipboardsUpdate;
+use JsonApi\Schemas\Clipboard as ClipboardSchema;
+use JsonApi\Schemas\ClipboardItem as ClipboardItemSchema;
+use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;
+use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;
+
+require_once __DIR__ . '/JSONAPIHelperTrait.php';
+
+class ClipboardRoutesTest extends Codeception\Test\Unit
+{
+ use JSONAPIHelperTrait;
+
+ public function testCreateClipboard(): void
+ {
+ $resource = $this->createClipboard(
+ $this->tester->getCredentialsForTestDozent()
+ );
+
+ $this->assertHasRelations($resource, 'user', 'clipboard-items');
+ $this->assertEquals(ClipboardSchema::TYPE, $resource->type());
+ $this->assertEquals('Test-Clipboard', $resource->attribute('name'));
+ }
+
+ public function testUpdateClipboard(): void
+ {
+ $credentials = $this->tester->getCredentialsForTestDozent();
+ $resource = $this->createClipboard($credentials);
+
+ $response = $this->sendMockRequest(
+ "/clipboards/{id}",
+ ClipboardsUpdate::class,
+ $credentials,
+ ['id' => $resource->id()],
+ [
+ 'considered_successful' => [200],
+ 'method' => 'PATCH',
+ 'json_body' => [
+ 'data' => [
+ 'attributes' => ['name' => 'Foo Bar'],
+ ],
+ ],
+ ],
+ );
+
+ $resource = $this->getResourceFromResponse($response);
+
+ $this->assertEquals('Foo Bar', $resource->attribute('name'));
+ }
+
+ public function testDeleteClipboard(): void
+ {
+ $credentials = $this->tester->getCredentialsForTestDozent();
+
+ $resource = $this->createClipboard($credentials);
+
+ $this->sendMockRequest(
+ "/clipboards/{id}",
+ ClipboardsDelete::class,
+ $credentials,
+ ['id' => $resource->id()],
+ [
+ 'considered_successful' => [204],
+ 'method' => 'DELETE',
+ ],
+ );
+ }
+
+ public function testAddItemToClipboard(): void
+ {
+ $credentials = $this->tester->getCredentialsForTestDozent();
+ $resource = $this->createClipboard($credentials);
+
+ $resource = $this->createClipboardItem(
+ $credentials,
+ $resource->id(),
+ 'abcd1234',
+ 'Room'
+ );
+
+ $this->assertHasRelations($resource, 'clipboard');
+ $this->assertEquals(ClipboardItemSchema::TYPE, $resource->type());
+ $this->assertEquals('abcd1234', $resource->attribute('range_id'));
+ $this->assertEquals('Room', $resource->attribute('range_type'));
+ }
+
+ public function testRemoveItemFromClipboard(): void
+ {
+ $credentials = $this->tester->getCredentialsForTestDozent();
+ $clipboard = $this->createClipboard($credentials);
+ $item = $this->createClipboardItem(
+ $credentials,
+ $clipboard->id(),
+ 'abcd1234',
+ 'Room'
+ );
+
+ $this->sendMockRequest(
+ "/clipboards/{id}/items/{itemId}",
+ ClipboardItemsDelete::class,
+ $credentials,
+ [
+ 'id' => $clipboard->id(),
+ 'itemId' => $item->id(),
+ ],
+ [
+ 'considered_successful' => [204],
+ 'method' => 'DELETE',
+ ],
+ );
+ }
+
+ protected function createClipboard(array $credentials, string $name = 'Test-Clipboard'): ResourceObject
+ {
+ $response = $this->sendMockRequest(
+ "/clipboards",
+ ClipboardsCreate::class,
+ $credentials,
+ [],
+ [
+ 'considered_successful' => [200],
+ 'method' => 'POST',
+ 'json_body' => [
+ 'data' => [
+ 'type' => ClipboardSchema::TYPE,
+ 'attributes' => ['name' => $name],
+ ],
+ ],
+ ],
+ );
+
+ return $this->getResourceFromResponse($response);
+ }
+
+ protected function createClipboardItem(
+ array $credentials,
+ string $clipboard_id,
+ string $range_id,
+ string $range_type
+ ): ResourceObject {
+ $response = $this->sendMockRequest(
+ "/clipboards/{id}/items",
+ ClipboardItemsCreate::class,
+ $credentials,
+ ['id' => $clipboard_id],
+ [
+ 'considered_successful' => [200],
+ 'method' => 'POST',
+ 'json_body' => [
+ 'data' => [
+ 'attributes' => [
+ 'range_id' => $range_id,
+ 'range_type' => $range_type,
+ ],
+ ],
+ ],
+ ],
+ );
+
+ return $this->getResourceFromResponse($response);
+ }
+}
diff --git a/tests/jsonapi/ConsultationHelper.php b/tests/jsonapi/ConsultationHelper.php
index 673174e..f84820b 100644
--- a/tests/jsonapi/ConsultationHelper.php
+++ b/tests/jsonapi/ConsultationHelper.php
@@ -1,19 +1,9 @@
<?php
-use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;
-use WoohooLabs\Yang\JsonApi\Schema\Document;
-use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;
+require_once __DIR__ . '/JSONAPIHelperTrait.php';
trait ConsultationHelper
{
- /**
- * @var \UnitTester
- */
- protected $tester;
-
- protected function _before()
- {
- \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
- }
+ use JSONAPIHelperTrait;
protected static $BLOCK_DATA = [
'room' => 'Testraum',
@@ -88,23 +78,6 @@ trait ConsultationHelper
return $block->slots->first();
}
- protected function withStudipEnv(array $credentials, callable $fn)
- {
- // Create global template factory if neccessary
- $has_template_factory = isset($GLOBALS['template_factory']);
- if (!$has_template_factory) {
- $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
- }
-
- $result = $this->tester->withPHPLib($credentials, $fn);
-
- if (!$has_template_factory) {
- unset($GLOBALS['template_factory']);
- }
-
- return $result;
- }
-
protected function createBookingForSlot(array $credentials, ConsultationSlot $slot, User $user): ConsultationBooking
{
return $this->withStudipEnv(
@@ -122,81 +95,4 @@ trait ConsultationHelper
}
);
}
-
- protected function sendMockRequest(string $route, string $handler, array $credentials, array $variables = [], array $options = []): JsonApiResponse
- {
- $options = array_merge([
- 'method' => 'GET',
- 'considered_successful' => [200],
- 'json_body' => null,
- ], $options);
-
- $app = $this->tester->createApp(
- $credentials,
- strtolower($options['method']),
- $route,
- $handler
- );
-
- $evaluated_route = preg_replace_callback(
- '/\{(.+?)(:[^}]+)?}/',
- function ($match) use ($variables) {
- $key = $match[1];
- if (!isset($variables[$key])) {
- throw new Exception("No variable '{$key}' defined");
- }
- return $variables[$key];
- },
- $route
- );
-
- $requestBuilder = $this->tester->createRequestBuilder($credentials);
- $requestBuilder->setUri($evaluated_route)->setMethod(strtoupper($options['method']));
-
- if (isset($options['json_body'])) {
- $requestBuilder->setJsonApiBody($options['json_body']);
-
- }
-
- /** @var JsonApiResponse $response */
- $response = $this->withStudipEnv($credentials, function () use ($app, $requestBuilder) {
- return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
- });
-
- if ($options['considered_successful']) {
- $this->assertTrue(
- $response->isSuccessful($options['considered_successful']),
- 'Actual status code is ' . $response->getStatusCode()
- );
- }
-
- return $response;
- }
-
- protected function getSingleResourceDocument(JsonApiResponse $response): Document
- {
- $this->assertTrue($response->hasDocument());
-
- $document = $response->document();
- $this->assertTrue($document->isSingleResourceDocument());
-
- return $document;
- }
-
- protected function getResourceCollectionDocument(JsonApiResponse $response): Document
- {
- $this->assertTrue($response->hasDocument());
-
- $document = $response->document();
- $this->assertTrue($document->isResourceCollectionDocument());
-
- return $document;
- }
-
- protected function assertHasRelations(ResourceObject $resource, ...$relations)
- {
- foreach ($relations as $relation) {
- $this->assertTrue($resource->hasRelationship($relation));
- }
- }
}
diff --git a/tests/jsonapi/JSONAPIHelperTrait.php b/tests/jsonapi/JSONAPIHelperTrait.php
new file mode 100644
index 0000000..666e198
--- /dev/null
+++ b/tests/jsonapi/JSONAPIHelperTrait.php
@@ -0,0 +1,117 @@
+<?php
+
+use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;
+use WoohooLabs\Yang\JsonApi\Schema\Document;
+use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;
+
+trait JSONAPIHelperTrait
+{
+ protected JSONAPITester $tester;
+
+ protected function _before()
+ {
+ DBManager::getInstance()->setConnection(
+ 'studip',
+ $this->getModule('\\Helper\\StudipDb')->dbh
+ );
+ }
+
+ protected function withStudipEnv(array $credentials, callable $fn)
+ {
+ // Create global template factory if neccessary
+ $has_template_factory = isset($GLOBALS['template_factory']);
+ if (!$has_template_factory) {
+ $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
+ }
+
+ $result = $this->tester->withPHPLib($credentials, $fn);
+
+ if (!$has_template_factory) {
+ unset($GLOBALS['template_factory']);
+ }
+
+ return $result;
+ }
+
+ protected function sendMockRequest(string $route, string $handler, array $credentials, array $variables = [], array $options = []): JsonApiResponse
+ {
+ $options = array_merge([
+ 'method' => 'GET',
+ 'considered_successful' => [200],
+ 'json_body' => null,
+ ], $options);
+
+ $app = $this->tester->createApp(
+ $credentials,
+ strtolower($options['method']),
+ $route,
+ $handler
+ );
+
+ $evaluated_route = preg_replace_callback(
+ '/\{(.+?)(:[^}]+)?}/',
+ function ($match) use ($variables) {
+ $key = $match[1];
+ if (!isset($variables[$key])) {
+ throw new Exception("No variable '{$key}' defined");
+ }
+ return $variables[$key];
+ },
+ $route
+ );
+
+ $requestBuilder = $this->tester->createRequestBuilder($credentials);
+ $requestBuilder->setUri($evaluated_route)->setMethod(strtoupper($options['method']));
+
+ if (isset($options['json_body'])) {
+ $requestBuilder->setJsonApiBody($options['json_body']);
+
+ }
+
+ /** @var JsonApiResponse $response */
+ $response = $this->withStudipEnv($credentials, function () use ($app, $requestBuilder) {
+ return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
+ });
+
+ if ($options['considered_successful']) {
+ $this->assertTrue(
+ $response->isSuccessful($options['considered_successful']),
+ 'Actual status code is ' . $response->getStatusCode()
+ );
+ }
+
+ return $response;
+ }
+
+ protected function getSingleResourceDocument(JsonApiResponse $response): Document
+ {
+ $this->assertTrue($response->hasDocument());
+
+ $document = $response->document();
+ $this->assertTrue($document->isSingleResourceDocument());
+
+ return $document;
+ }
+
+ protected function getResourceCollectionDocument(JsonApiResponse $response): Document
+ {
+ $this->assertTrue($response->hasDocument());
+
+ $document = $response->document();
+ $this->assertTrue($document->isResourceCollectionDocument());
+
+ return $document;
+ }
+
+ protected function assertHasRelations(ResourceObject $resource, ...$relations)
+ {
+ foreach ($relations as $relation) {
+ $this->assertTrue($resource->hasRelationship($relation));
+ }
+ }
+
+ protected function getResourceFromResponse(JsonApiResponse $response): ResourceObject
+ {
+ return $this->getSingleResourceDocument($response)->primaryResource();
+ }
+}