aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/UsersRelationshipContactsRemoveTest.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/UsersRelationshipContactsRemoveTest.php
current code from svn, revision 62608
Diffstat (limited to 'tests/jsonapi/UsersRelationshipContactsRemoveTest.php')
-rw-r--r--tests/jsonapi/UsersRelationshipContactsRemoveTest.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/jsonapi/UsersRelationshipContactsRemoveTest.php b/tests/jsonapi/UsersRelationshipContactsRemoveTest.php
new file mode 100644
index 0000000..aeb9bdc
--- /dev/null
+++ b/tests/jsonapi/UsersRelationshipContactsRemoveTest.php
@@ -0,0 +1,76 @@
+<?php
+
+
+use JsonApi\Routes\Users\Rel\Contacts;
+use JsonApi\Schemas\User as UserSchema;
+
+class UsersRelationshipContactsRemoveTest extends \Codeception\Test\Unit
+{
+ /**
+ * @var \UnitTester
+ */
+ protected $tester;
+
+ protected function _before()
+ {
+ \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
+ }
+
+ protected function _after()
+ {
+ }
+
+ // tests
+ public function testShouldRemoveContacts()
+ {
+ $credentials = $this->tester->getCredentialsForTestAutor();
+ $credentials2 = $this->tester->getCredentialsForTestDozent();
+ $credentials3 = $this->tester->getCredentialsForRoot();
+
+ \Contact::create(
+ [
+ 'owner_id' => $credentials['id'],
+ 'user_id' => $credentials2['id'],
+ ]
+ );
+ \Contact::create(
+ [
+ 'owner_id' => $credentials['id'],
+ 'user_id' => $credentials3['id'],
+ ]
+ );
+
+ $response = $this->removeContactsOfUser($credentials, [$credentials2['id']]);
+
+ $this->tester->assertSame(204, $response->getStatusCode());
+ $this->tester->assertCount(1, \User::find($credentials['id'])->contacts);
+ }
+
+ // **** helper functions ****
+ private function removeContactsOfUser($credentials, array $contactIds)
+ {
+ return $this->tester->sendMockRequest(
+ $this->tester->createApp($credentials, 'delete', '/users/{id}/relationships/contacts', Contacts::class),
+ $this->tester->createRequestBuilder($credentials)
+ ->setUri('/users/'.$credentials['id'].'/relationships/contacts')
+ ->setJsonApiBody($this->prepareValidBody($contactIds))
+ ->delete()
+ ->getRequest()
+ );
+ }
+
+ private function prepareValidBody(array $contactIds)
+ {
+ return [
+ 'data' => array_map(
+ function ($contactId) {
+ return [
+ 'type' => UserSchema::TYPE,
+ 'id' => $contactId,
+ ];
+ },
+ $contactIds
+ ),
+ ];
+ }
+}