Completed
Push — develop ( 6bb9f8...c1e8ff )
by Narcotic
41:48 queued 41:41
created

ReadOnlyFieldTest::testUpdateEmptyReadOnlyField()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
/**
3
 * test a readOnly service
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Services;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Functional test
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class ReadOnlyFieldTest extends RestTestCase
19
{
20
    /**
21
     * load fixtures
22
     *
23
     * @return void
24
     */
25
    public function setUp()
26
    {
27
        $this->loadFixtures(
28
            array(
29
                'GravitonDyn\TestCaseReadOnlyFieldBundle\DataFixtures\MongoDB\LoadTestCaseReadOnlyFieldData',
30
            ),
31
            null,
32
            'doctrine_mongodb'
33
        );
34
    }
35
36
    /**
37
     * see if the readOnly fields are denied as they should
38
     *
39
     * @return void
40
     */
41
    public function testReadOnlyDenying()
42
    {
43
        $client = static::createRestClient();
44
        $client->request('GET', '/testcase/readonlyfield/101');
45
        $data = $client->getResults();
46
47
        $data->denied = 'cannot change';
48
        $data->deniedArray[0] = 'also no change';
49
        $data->deniedArray[1] = 'also no change';
50
        $data->deniedObject->denied = 'whatever you do, do not change this';
51
        $data->deniedObject->allowed = 'can do';
52
        $data->allowed = 'this can be changed';
53
54
        $client = static::createRestClient();
55
        $client->put('/testcase/readonlyfield/101', $data);
56
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
57
58
        $this->assertEquals(
59
            $client->getResults(),
60
            [
61
                (object) [
62
                    'propertyPath' => 'denied',
63
                    'message' => 'The value "this is a denied field" is read only.'
64
                ],
65
                (object) [
66
                    'propertyPath' => 'deniedArray',
67
                    'message' => 'The value ["this is denied","this also"] is read only.'
68
                ],
69
                (object) [
70
                    'propertyPath' => 'deniedObject.denied',
71
                    'message' => 'The value "this is denied" is read only.'
72
                ]
73
            ]
74
        );
75
    }
76
77
    /**
78
     * see if the allowed fields can be updated
79
     *
80
     * @return void
81
     */
82
    public function testReadOnlyChangeAllowedFields()
83
    {
84
        $client = static::createRestClient();
85
        $client->request('GET', '/testcase/readonlyfield/101');
86
        $data = $client->getResults();
87
88
        $data->allowed = 'this can be changed';
89
        $data->deniedObject->allowed = 'can do';
90
91
        $client = static::createRestClient();
92
        $client->put('/testcase/readonlyfield/101', $data);
93
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
94
    }
95
96
    /**
97
     * Check for readonly property that should be updated only if it was not there.
98
     *
99
     * @return void
100
     */
101
    public function testUpdateEmptyReadOnlyField()
102
    {
103
        $client = static::createRestClient();
104
        $data = new \stdClass();
105
        $data->id = '101_2';
106
        $data->allowed = 'can be edited';
107
108
        $client->put('/testcase/readonlyfield/101_2', $data);
109
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
110
111
        // But should be able to add it, as it was never there.
112
        $data->denied = 'can not be edited';
113
114
        $client->put('/testcase/readonlyfield/101_2', $data);
115
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
116
117
        $client->request('GET', '/testcase/readonlyfield/101_2');
118
        $savedData = $client->getResults();
119
120
        $data = (array) $data;
121
        $savedData = (array) $savedData;
122
        ksort($data);
123
        ksort($savedData);
124
125
        $this->assertEquals(json_encode($data), json_encode($savedData));
126
    }
127
}
128