aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/UsersRelationshipContactsRemoveTest.php
blob: aeb9bdc85082e901bdbfc4c7267f12542597e017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
            ),
        ];
    }
}