Completed
Push — feature/EVO-7166-no-cascading-... ( 84752e...92993c )
by Narcotic
73:27 queued 68:05
created

getExpectedErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
/**
3
 * test for RecordOriginConstraint
4
 */
5
6
namespace Graviton\SchemaBundle\Tests\ConstraintBuilder;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class RecordOriginConstraintTest extends RestTestCase
17
{
18
19
    /**
20
     * load fixtures
21
     *
22
     * @return void
23
     */
24
    public function setUp()
25
    {
26
        $this->loadFixtures(
27
            array(
28
                'GravitonDyn\CustomerBundle\DataFixtures\MongoDB\LoadCustomerData',
29
            ),
30
            null,
31
            'doctrine_mongodb'
32
        );
33
    }
34
35
    /**
36
     * test the validation of the RecordOriginConstraint
37
     *
38
     * @dataProvider createDataProvider
39
     *
40
     * @param object  $entity           The object to create
41
     * @param integer $expectedStatus   Header status code
42
     * @param string  $expectedResponse Post data result of post
43
     *
44
     * @return void
45
     */
46 View Code Duplication
    public function testRecordOriginHandlingOnCreate($entity, $expectedStatus, $expectedResponse)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $client = static::createRestClient();
49
        $client->post('/person/customer/', $entity);
50
51
        $response = $client->getResponse();
52
        $this->assertEquals($expectedStatus, $response->getStatusCode());
53
        $this->assertEquals($expectedResponse, $client->getResults());
54
    }
55
56
    /**
57
     * tests for the case if user doesn't provide an id in payload.. constraint
58
     * must take the id from the request in that case.
59
     *
60
     * @return void
61
     */
62
    public function testRecordOriginHandlingWithNoIdInPayload()
63
    {
64
        $record = (object) [
65
            //'id' => '' - no, no id.. that's the point ;-)
66
            'customerNumber' => 555,
67
            'name' => 'Muster Hans'
68
        ];
69
70
        $client = static::createRestClient();
71
        $client->put('/person/customer/100', $record);
72
73
        $this->assertEquals(
74
            $this->getExpectedErrorMessage('customerNumber, name')[0],
75
            $client->getResults()[0]
76
        );
77
78
        $this->assertEquals(1, count($client->getResults()));
79
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
80
    }
81
82
    /**
83
     * Test the validation of the RecordOriginConstraint
84
     *
85
     * @param array   $fieldsToSet      Fields to be modified
86
     * @param integer $expectedStatus   Header status code
87
     * @param string  $expectedResponse Result to be returned
88
     * @param boolean $checkSavedEntry  To check db for correct result
89
     *
90
     * @dataProvider updateDataProvider
91
     *
92
     * @return void
93
     */
94
    public function testRecordOriginHandlingOnUpdate(
95
        $fieldsToSet,
96
        $expectedStatus,
97
        $expectedResponse,
98
        $checkSavedEntry = true
99
    ) {
100
        $client = static::createRestClient();
101
        $client->request('GET', '/person/customer/100');
102
        $result = $client->getResults();
103
104
        // apply changes
105
        foreach ($fieldsToSet as $key => $val) {
106
            $result->{$key} = $val;
107
        }
108
109
        $expectedObject = $result;
110
111
        $client = static::createRestClient();
112
        $client->put('/person/customer/100', $result);
113
114
        $response = $client->getResponse();
115
        $this->assertEquals($expectedStatus, $response->getStatusCode());
116
        $this->assertEquals($expectedResponse, $client->getResults());
117
118
        if ($checkSavedEntry) {
119
            // fetch it again and compare
120
            $client = static::createRestClient();
121
            $client->request('GET', '/person/customer/100');
122
            $this->assertEquals($expectedObject, $client->getResults());
123
        }
124
    }
125
126
    /**
127
     * Test the validation of the RecordOriginConstraint
128
     *
129
     * @param array   $ops              PATCH operations
130
     * @param integer $expectedStatus   Header status code
131
     * @param string  $expectedResponse Result to be returned
132
     *
133
     * @dataProvider patchDataProvider
134
     *
135
     * @return void
136
     */
137
    public function testRecordOriginHandlingOnPatch(
138
        $ops,
139
        $expectedStatus,
140
        $expectedResponse
141
    ) {
142
        $client = static::createRestClient();
143
144
        $client->request('PATCH', '/person/customer/100', [], [], [], json_encode($ops));
145
146
        $response = $client->getResponse();
147
        $this->assertEquals($expectedStatus, $response->getStatusCode());
148
        $this->assertEquals($expectedResponse, $client->getResults());
149
    }
150
151
    /**
152
     * test to see if DELETE on a recordorigin: core is denied
153
     *
154
     * @return void
155
     */
156
    public function testDeleteHandling()
157
    {
158
        $client = static::createRestClient();
159
        $client->request('DELETE', '/person/customer/100');
160
        $response = $client->getResponse();
161
162
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
163
        $this->assertEquals(
164
            (object) [
165
                'propertyPath' => 'recordOrigin',
166
                'message' => 'Must not be one of the following keywords: core'
167
            ],
168
            $client->getResults()
169
        );
170
    }
171
172
    /**
173
     * Data provider for POST related stuff
174
     *
175
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,null|array>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
176
     */
177
    public function createDataProvider()
178
    {
179
        $baseObj = [
180
            'customerNumber' => 888,
181
            'name' => 'Muster Hans'
182
        ];
183
184
        return [
185
186
            /*** STUFF THAT SHOULD BE ALLOWED ***/
187
188
            'create-allowed-object' => [
189
                'entity' => (object) array_merge(
190
                    $baseObj,
191
                    [
192
                        'recordOrigin' => 'hans'
193
                    ]
194
                ),
195
                'httpStatusExpected' => Response::HTTP_CREATED,
196
                'expectedResponse' => null
197
            ],
198
199
            /*** STUFF THAT SHOULD BE DENIED ***/
200
201
            'create-recordorigin-core' => [
202
                'entity' => (object) array_merge(
203
                    $baseObj,
204
                    [
205
                        'recordOrigin' => 'core'
206
                    ]
207
                ),
208
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
209
                'expectedResponse' => [
210
                    (object) [
211
                        'propertyPath' => 'recordOrigin',
212
                        'message' => 'Creating documents with the recordOrigin field having a '.
213
                            'value of core is not permitted.'
214
                    ]
215
                ]
216
            ]
217
        ];
218
    }
219
220
    /**
221
     * Data provider for PUT related stuff
222
     *
223
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
224
     */
225
    public function updateDataProvider()
226
    {
227
        return [
228
229
            /*** STUFF THAT SHOULD BE ALLOWED ***/
230
            'create-allowed-object' => [
231
                'fieldsToSet' => [
232
                    'addedField' => (object) [
233
                        'some' => 'property',
234
                        'another' => 'one'
235
                    ]
236
                ],
237
                'httpStatusExpected' => Response::HTTP_NO_CONTENT,
238
                'expectedResponse' => null
239
            ],
240
            'subproperty-modification' => [
241
                'fieldsToSet' => [
242
                    'someObject' => (object) [
243
                        'oneField' => 'value',
244
                        'twoField' => 'twofield'
245
                    ]
246
                ],
247
                'httpStatusExpected' => Response::HTTP_NO_CONTENT,
248
                'expectedResponse' => null
249
            ],
250
251
            /*** STUFF THAT NEEDS TO BE DENIED ***/
252
            'denied-subproperty-modification' => [
253
                'fieldsToSet' => [
254
                    'someObject' => (object) [
255
                        'oneField' => 'changed-value'
256
                    ]
257
                ],
258
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
259
                'expectedResponse' => $this->getExpectedErrorMessage('someObject.oneField'),
260
                'checkSavedEntry' => false
261
            ],
262
            'denied-try-change-recordorigin' => [
263
                'fieldsToSet' => [
264
                    'recordOrigin' => 'hans'
265
                ],
266
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
267
                'expectedResponse' => $this->getExpectedErrorMessage('recordOrigin'),
268
                'checkSavedEntry' => false
269
            ],
270
        ];
271
    }
272
273
274
    /**
275
     * providing the conditional errorMessage Object
276
     *
277
     * @param string $changedFields the Field the user wants to change
278
     *
279
     * @return array
280
     */
281
    private function getExpectedErrorMessage($changedFields)
282
    {
283
        $expectedErrorOutput = [
284
            (object) [
285
                'propertyPath' => 'recordOrigin',
286
                'message' => 'Prohibited modification attempt on record with recordOrigin of core.'
287
                    .' You tried to change ('.$changedFields.'), but you can only change'
288
                    .' (addedField, someObject.twoField) by recordOriginException.'
289
            ]
290
        ];
291
        return $expectedErrorOutput;
292
    }
293
294
    /**
295
     * Data provider for PATCH related stuff
296
     *
297
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
298
     */
299
    public function patchDataProvider()
300
    {
301
        return [
302
303
            /*** STUFF THAT SHOULD BE ALLOWED ***/
304
305
            'patch-allowed-attribute' => [
306
                'ops' => [
307
                    [
308
                        'op' => 'add',
309
                        'path' => '/someObject/twoField',
310
                        'value' => 'myValue'
311
                    ]
312
                ],
313
                'httpStatusExpected' => Response::HTTP_OK,
314
                'expectedResponse' => null
315
            ],
316
            'patch-add-object-data' => [
317
                'ops' => [
318
                    [
319
                        'op' => 'add',
320
                        'path' => '/addedField',
321
                        'value' => [
322
                            'someProperty' => 'someValue',
323
                            'anotherOne' => 'oneMore'
324
                        ]
325
                    ]
326
                ],
327
                'httpStatusExpected' => Response::HTTP_OK,
328
                'expectedResponse' => null
329
            ],
330
331
            /*** STUFF THAT NEEDS TO BE DENIED ***/
332
            'patch-denied-subproperty' => [
333
                'ops' => [
334
                    [
335
                        'op' => 'add',
336
                        'path' => '/someObject/oneField',
337
                        'value' => 'myValue'
338
                    ]
339
                ],
340
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
341
                'expectedResponse' => $this->getExpectedErrorMessage('someObject.oneField')
342
            ],
343
            'patch-denied-recordorigin-change' => [
344
                'ops' => [
345
                    [
346
                        'op' => 'replace',
347
                        'path' => '/recordOrigin',
348
                        'value' => 'hans'
349
                    ]
350
                ],
351
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
352
                'expectedResponse' => $this->getExpectedErrorMessage('recordOrigin')
353
            ],
354
355
        ];
356
    }
357
}
358