diff options
Diffstat (limited to 'tests/jsonapi')
| -rw-r--r-- | tests/jsonapi/BlubberThreadsCreateTest.php | 2 | ||||
| -rw-r--r-- | tests/jsonapi/BlubberThreadsIndexTest.php | 3 | ||||
| -rw-r--r-- | tests/jsonapi/BlubberThreadsShowTest.php | 2 | ||||
| -rw-r--r-- | tests/jsonapi/ClipboardRoutesTest.php | 167 | ||||
| -rw-r--r-- | tests/jsonapi/ConsultationHelper.php | 111 | ||||
| -rw-r--r-- | tests/jsonapi/JSONAPIHelperTrait.php | 117 | ||||
| -rw-r--r-- | tests/jsonapi/StructuralElementsShowTest.php | 2 | ||||
| -rw-r--r-- | tests/jsonapi/_bootstrap.php | 51 |
8 files changed, 294 insertions, 161 deletions
diff --git a/tests/jsonapi/BlubberThreadsCreateTest.php b/tests/jsonapi/BlubberThreadsCreateTest.php index 17846f8..d2bdaea 100644 --- a/tests/jsonapi/BlubberThreadsCreateTest.php +++ b/tests/jsonapi/BlubberThreadsCreateTest.php @@ -22,7 +22,7 @@ class BlubberThreadsCreateTest extends \Codeception\Test\Unit // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } } diff --git a/tests/jsonapi/BlubberThreadsIndexTest.php b/tests/jsonapi/BlubberThreadsIndexTest.php index ab6d200..14173d3 100644 --- a/tests/jsonapi/BlubberThreadsIndexTest.php +++ b/tests/jsonapi/BlubberThreadsIndexTest.php @@ -1,6 +1,5 @@ <?php -use JsonApi\Errors\RecordNotFoundException; use JsonApi\Routes\Blubber\ThreadsIndex; require_once 'BlubberTestHelper.php'; @@ -21,7 +20,7 @@ class BlubberThreadsIndexTest extends \Codeception\Test\Unit // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } } diff --git a/tests/jsonapi/BlubberThreadsShowTest.php b/tests/jsonapi/BlubberThreadsShowTest.php index 932b768..875fc8c 100644 --- a/tests/jsonapi/BlubberThreadsShowTest.php +++ b/tests/jsonapi/BlubberThreadsShowTest.php @@ -23,7 +23,7 @@ class BlubberThreadsShowTest extends \Codeception\Test\Unit // Create global template factory if neccessary $has_template_factory = isset($GLOBALS['template_factory']); if (!$has_template_factory) { - $GLOBALS['template_factory'] = new Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); + $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); } } 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 a46cb47..f84820b 100644 --- a/tests/jsonapi/ConsultationHelper.php +++ b/tests/jsonapi/ConsultationHelper.php @@ -1,22 +1,9 @@ <?php -use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse; -use WoohooLabs\Yang\JsonApi\Schema\Document; -use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject; - -// Required for consultation mailer -require_once 'vendor/flexi/flexi.php'; +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', @@ -91,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_TemplateFactory($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( @@ -125,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(); + } +} diff --git a/tests/jsonapi/StructuralElementsShowTest.php b/tests/jsonapi/StructuralElementsShowTest.php index 8cc168a..4873aae 100644 --- a/tests/jsonapi/StructuralElementsShowTest.php +++ b/tests/jsonapi/StructuralElementsShowTest.php @@ -52,7 +52,7 @@ class StructuralElementsShowTest extends \Codeception\Test\Unit $childIDs = $structuralElement->children->pluck('id'); $this->assertCount(count($childIDs), $includedResources); foreach ($includedResources as $included) { - $this->assertContains($included->id(), $childIDs); + $this->assertContainsEquals($included->id(), $childIDs); } } diff --git a/tests/jsonapi/_bootstrap.php b/tests/jsonapi/_bootstrap.php index 82ae54e..ea99293 100644 --- a/tests/jsonapi/_bootstrap.php +++ b/tests/jsonapi/_bootstrap.php @@ -27,59 +27,18 @@ $CACHING_ENABLE = false; date_default_timezone_set('Europe/Berlin'); +require_once __DIR__.'/../../composer/autoload.php'; + require 'config.inc.php'; +require 'lib/helpers.php'; require 'lib/functions.php'; require 'lib/language.inc.php'; require 'lib/visual.inc.php'; require 'lib/calendar_functions.inc.php'; require 'lib/dates.inc.php'; -// Setup autoloading -require 'lib/classes/StudipAutoloader.php'; -StudipAutoloader::register(); - -// General classes folders -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/models'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/models/calendar'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/models/resources'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes', 'Studip'); - -// Plugins -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/plugins/core'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/plugins/db'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/plugins/engine'); - -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/calendar'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/calendar', 'Studip\\Calendar'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/calendar/lib'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/exceptions'); - -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/filesystem'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/migrations'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/modules'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/navigation'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/phplib'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/raumzeit'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/resources'); - -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/activities', 'Studip\\Activity'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes/calendar'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes/globalsearch'); -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/lib/classes/visibility'); - -StudipAutoloader::addAutoloadPath($GLOBALS['STUDIP_BASE_PATH'].'/vendor/oauth-php/library'); - -// Messy file names -StudipAutoloader::addClassLookups( - array( - 'StudipPlugin' => $GLOBALS['STUDIP_BASE_PATH'].'/lib/plugins/core/StudIPPlugin.class.php', - 'messaging' => $GLOBALS['STUDIP_BASE_PATH'].'/lib/messaging.inc.php', - ) -); - -$GLOBALS['_fullname_sql'] = array(); +$GLOBALS['_fullname_sql'] = []; $GLOBALS['_fullname_sql']['full'] = "TRIM(CONCAT(title_front,' ',Vorname,' ',Nachname,IF(title_rear!='',CONCAT(', ',title_rear),'')))"; $GLOBALS['_fullname_sql']['full_rev'] = "TRIM(CONCAT(Nachname,', ',Vorname,IF(title_front!='',CONCAT(', ',title_front),''),IF(title_rear!='',CONCAT(', ',title_rear),'')))"; $GLOBALS['_fullname_sql']['no_title'] = "CONCAT(Vorname ,' ', Nachname)"; @@ -101,6 +60,4 @@ class DB_Seminar extends DB_Sql } } -require_once __DIR__.'/../../composer/autoload.php'; - session_id("test-session"); |
