aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/BlubberThreadsCreateTest.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:07:19 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:19:12 +0200
commita3da1483a9e689846179159355badfec8073dbec (patch)
tree770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /tests/jsonapi/BlubberThreadsCreateTest.php
current code from svn, revision 62608
Diffstat (limited to 'tests/jsonapi/BlubberThreadsCreateTest.php')
-rw-r--r--tests/jsonapi/BlubberThreadsCreateTest.php104
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/jsonapi/BlubberThreadsCreateTest.php b/tests/jsonapi/BlubberThreadsCreateTest.php
new file mode 100644
index 0000000..9bcbf41
--- /dev/null
+++ b/tests/jsonapi/BlubberThreadsCreateTest.php
@@ -0,0 +1,104 @@
+<?php
+
+use JsonApi\Routes\Blubber\ThreadsCreate;
+use JsonApi\Schemas\BlubberThread as Schema;
+use JsonApi\Schemas\User as UsersSchema;
+
+require_once 'BlubberTestHelper.php';
+
+class BlubberThreadsCreateTest extends \Codeception\Test\Unit
+{
+ use BlubberTestHelper;
+
+ /**
+ * @var \UnitTester
+ */
+ protected $tester;
+
+ protected function _before()
+ {
+ \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
+ }
+
+ protected function _after()
+ {
+ }
+
+ // tests
+ public function testCreatePrivateThreadSucessfully()
+ {
+ // given
+ $credentials = $this->tester->getCredentialsForTestAutor();
+
+ $response = $this->createThread($credentials, 'private');
+ $this->tester->assertTrue($response->isSuccessfulDocument([201]));
+
+ $document = $response->document();
+ $this->tester->assertTrue($document->isSingleResourceDocument());
+
+ $resourceObject = $document->primaryResource();
+
+ $this->tester->assertSame('private', $resourceObject->attribute('context-type'));
+ $this->tester->assertSame('', $resourceObject->attribute('content'));
+
+ // check predicates
+ $this->tester->assertTrue($resourceObject->attribute('is-commentable'));
+ $this->tester->assertTrue($resourceObject->attribute('is-readable'));
+ $this->tester->assertTrue($resourceObject->attribute('is-writable'));
+ $this->tester->assertTrue($resourceObject->attribute('is-visible-in-stream'));
+
+ // check author relationship
+ $authorRel = $resourceObject->relationship('author');
+ $this->tester->assertTrue($authorRel->isToOneRelationship());
+ $this->tester->assertCount(1, $links = $authorRel->resourceLinks());
+
+ $this->tester->assertSame(UsersSchema::TYPE, $links[0]['type']);
+ $this->tester->assertSame($credentials['id'], $links[0]['id']);
+
+ // check mentions relationship
+ $mentionsRel = $resourceObject->relationship('mentions');
+ $this->tester->assertTrue($mentionsRel->isToManyRelationship());
+ $this->tester->assertCount(1, $links = $mentionsRel->resourceLinks());
+
+ $this->tester->assertSame(UsersSchema::TYPE, $links[0]['type']);
+ $this->tester->assertSame($credentials['id'], $links[0]['id']);
+ }
+
+ public function testFailToCreateAnotherTypeOfThread()
+ {
+ // given
+ $credentials = $this->tester->getCredentialsForTestAutor();
+
+ $this->expectException(JsonApi\Errors\BadRequestException::class);
+ $this->createThread($credentials, 'course');
+
+ $this->expectException(JsonApi\Errors\BadRequestException::class);
+ $this->createThread($credentials, 'institute');
+
+ $this->expectException(JsonApi\Errors\BadRequestException::class);
+ $this->createThread($credentials, 'public');
+ }
+
+
+ private function createThread($credentials, $contextType = 'private')
+ {
+ $body = [
+ 'data' => [
+ 'type' => Schema::TYPE,
+ 'attributes' => [
+ 'context-type' => $contextType
+ ]
+ ]
+ ];
+
+ $app = $this->tester->createApp($credentials, 'post', '/blubber-threads', ThreadsCreate::class);
+
+ $requestBuilder = $this->tester->createRequestBuilder($credentials);
+ $requestBuilder
+ ->setUri('/blubber-threads')
+ ->create()
+ ->setJsonApiBody($body);
+
+ return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
+ }
+}