1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @todo Test Relation getters |
5
|
|
|
* @todo Test filter and limit through GET params |
6
|
|
|
* @todo Test DELETE verb |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class RestfulServerTest extends SapphireTest { |
|
|
|
|
10
|
|
|
|
11
|
|
|
static $fixture_file = 'RestfulServerTest.yml'; |
|
|
|
|
12
|
|
|
|
13
|
|
|
protected $extraDataObjects = array( |
14
|
|
|
'RestfulServerTest_Comment', |
15
|
|
|
'RestfulServerTest_SecretThing', |
16
|
|
|
'RestfulServerTest_Page', |
17
|
|
|
'RestfulServerTest_Author', |
18
|
|
|
'RestfulServerTest_AuthorRating', |
19
|
|
|
); |
20
|
|
|
|
21
|
|
View Code Duplication |
public function testApiAccess() { |
|
|
|
|
22
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
23
|
|
|
$page1 = $this->objFromFixture('RestfulServerTest_Page', 'page1'); |
24
|
|
|
|
25
|
|
|
// normal GET should succeed with $api_access enabled |
26
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
27
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
28
|
|
|
$this->assertEquals($response->getStatusCode(), 200); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
31
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user'; |
32
|
|
|
|
33
|
|
|
// even with logged in user a GET with $api_access disabled should fail |
34
|
|
|
$url = "/api/v1/RestfulServerTest_Page/" . $page1->ID; |
35
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
36
|
|
|
$this->assertEquals($response->getStatusCode(), 401); |
|
|
|
|
37
|
|
|
|
38
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
39
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testApiAccessBoolean() { |
43
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
44
|
|
|
|
45
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
46
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
47
|
|
|
$this->assertContains('<ID>', $response->getBody()); |
48
|
|
|
$this->assertContains('<Name>', $response->getBody()); |
49
|
|
|
$this->assertContains('<Comment>', $response->getBody()); |
50
|
|
|
$this->assertContains('<Page', $response->getBody()); |
51
|
|
|
$this->assertContains('<Author', $response->getBody()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testAuthenticatedGET() { |
|
|
|
|
55
|
|
|
$thing1 = $this->objFromFixture('RestfulServerTest_SecretThing', 'thing1'); |
56
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
57
|
|
|
|
58
|
|
|
// @todo create additional mock object with authenticated VIEW permissions |
59
|
|
|
$url = "/api/v1/RestfulServerTest_SecretThing/" . $thing1->ID; |
60
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
61
|
|
|
$this->assertEquals($response->getStatusCode(), 401); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
64
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user'; |
65
|
|
|
|
66
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
67
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
68
|
|
|
$this->assertEquals($response->getStatusCode(), 200); |
|
|
|
|
69
|
|
|
|
70
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
71
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testAuthenticatedPUT() { |
|
|
|
|
75
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
76
|
|
|
|
77
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
78
|
|
|
$data = array('Comment' => 'created'); |
79
|
|
|
|
80
|
|
|
$response = Director::test($url, $data, null, 'PUT'); |
|
|
|
|
81
|
|
|
$this->assertEquals($response->getStatusCode(), 401); // Permission failure |
|
|
|
|
82
|
|
|
|
83
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
84
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor'; |
85
|
|
|
$response = Director::test($url, $data, null, 'PUT'); |
|
|
|
|
86
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Success |
|
|
|
|
87
|
|
|
|
88
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
89
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGETRelationshipsXML() { |
93
|
|
|
$author1 = $this->objFromFixture('RestfulServerTest_Author', 'author1'); |
94
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating', 'rating1'); |
95
|
|
|
$rating2 = $this->objFromFixture('RestfulServerTest_AuthorRating', 'rating2'); |
96
|
|
|
|
97
|
|
|
// @todo should be set up by fixtures, doesn't work for some reason... |
98
|
|
|
$author1->Ratings()->add($rating1); |
99
|
|
|
$author1->Ratings()->add($rating2); |
100
|
|
|
|
101
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID; |
102
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
103
|
|
|
$this->assertEquals($response->getStatusCode(), 200); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$responseArr = Convert::xml2array($response->getBody()); |
106
|
|
|
$ratingsArr = $responseArr['Ratings']['RestfulServerTest_AuthorRating']; |
107
|
|
|
$this->assertEquals(count($ratingsArr), 2); |
|
|
|
|
108
|
|
|
$ratingIDs = array( |
109
|
|
|
(int)$ratingsArr[0]['@attributes']['id'], |
110
|
|
|
(int)$ratingsArr[1]['@attributes']['id'] |
111
|
|
|
); |
112
|
|
|
$this->assertContains($rating1->ID, $ratingIDs); |
113
|
|
|
$this->assertContains($rating2->ID, $ratingIDs); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testGETManyManyRelationshipsXML() { |
117
|
|
|
// author4 has related authors author2 and author3 |
118
|
|
|
$author2 = $this->objFromFixture('RestfulServerTest_Author', 'author2'); |
119
|
|
|
$author3 = $this->objFromFixture('RestfulServerTest_Author', 'author3'); |
120
|
|
|
$author4 = $this->objFromFixture('RestfulServerTest_Author', 'author4'); |
121
|
|
|
|
122
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author4->ID . '/RelatedAuthors'; |
123
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
124
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
|
|
|
125
|
|
|
$arr = Convert::xml2array($response->getBody()); |
126
|
|
|
$authorsArr = $arr['RestfulServerTest_Author']; |
127
|
|
|
|
128
|
|
|
$this->assertEquals(count($authorsArr), 2); |
|
|
|
|
129
|
|
|
$ratingIDs = array( |
130
|
|
|
(int)$authorsArr[0]['ID'], |
131
|
|
|
(int)$authorsArr[1]['ID'] |
132
|
|
|
); |
133
|
|
|
$this->assertContains($author2->ID, $ratingIDs); |
134
|
|
|
$this->assertContains($author3->ID, $ratingIDs); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testPUTWithFormEncoded() { |
|
|
|
|
138
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
139
|
|
|
|
140
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
141
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor'; |
142
|
|
|
|
143
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
144
|
|
|
$body = 'Name=Updated Comment&Comment=updated'; |
145
|
|
|
$headers = array( |
146
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' |
147
|
|
|
); |
148
|
|
|
$response = Director::test($url, null, null, 'PUT', $body, $headers); |
|
|
|
|
149
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Success |
|
|
|
|
150
|
|
|
// Assumption: XML is default output |
151
|
|
|
$responseArr = Convert::xml2array($response->getBody()); |
152
|
|
|
$this->assertEquals($responseArr['ID'], $comment1->ID); |
|
|
|
|
153
|
|
|
$this->assertEquals($responseArr['Comment'], 'updated'); |
|
|
|
|
154
|
|
|
$this->assertEquals($responseArr['Name'], 'Updated Comment'); |
|
|
|
|
155
|
|
|
|
156
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
157
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testPOSTWithFormEncoded() { |
|
|
|
|
161
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
162
|
|
|
|
163
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
164
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor'; |
165
|
|
|
|
166
|
|
|
$url = "/api/v1/RestfulServerTest_Comment"; |
167
|
|
|
$body = 'Name=New Comment&Comment=created'; |
168
|
|
|
$headers = array( |
169
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' |
170
|
|
|
); |
171
|
|
|
$response = Director::test($url, null, null, 'POST', $body, $headers); |
|
|
|
|
172
|
|
|
$this->assertEquals($response->getStatusCode(), 201); // Created |
|
|
|
|
173
|
|
|
// Assumption: XML is default output |
174
|
|
|
$responseArr = Convert::xml2array($response->getBody()); |
175
|
|
|
$this->assertTrue($responseArr['ID'] > 0); |
|
|
|
|
176
|
|
|
$this->assertNotEquals($responseArr['ID'], $comment1->ID); |
|
|
|
|
177
|
|
|
$this->assertEquals($responseArr['Comment'], 'created'); |
|
|
|
|
178
|
|
|
$this->assertEquals($responseArr['Name'], 'New Comment'); |
|
|
|
|
179
|
|
|
$this->assertEquals( |
|
|
|
|
180
|
|
|
$response->getHeader('Location'), |
181
|
|
|
Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID']) |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
185
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testPUTwithJSON() { |
|
|
|
|
189
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
190
|
|
|
|
191
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
192
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor'; |
193
|
|
|
|
194
|
|
|
// by mimetype |
195
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
196
|
|
|
$body = '{"Comment":"updated"}'; |
197
|
|
|
$response = Director::test($url, null, null, 'PUT', $body, array('Content-Type'=>'application/json')); |
|
|
|
|
198
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated |
|
|
|
|
199
|
|
|
$obj = Convert::json2obj($response->getBody()); |
200
|
|
|
$this->assertEquals($obj->ID, $comment1->ID); |
|
|
|
|
201
|
|
|
$this->assertEquals($obj->Comment, 'updated'); |
|
|
|
|
202
|
|
|
|
203
|
|
|
// by extension |
204
|
|
|
$url = sprintf("/api/v1/RestfulServerTest_Comment/%d.json", $comment1->ID); |
205
|
|
|
$body = '{"Comment":"updated"}'; |
206
|
|
|
$response = Director::test($url, null, null, 'PUT', $body); |
|
|
|
|
207
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated |
|
|
|
|
208
|
|
|
$this->assertEquals( |
|
|
|
|
209
|
|
|
$response->getHeader('Location'), |
210
|
|
|
Controller::join_links(Director::absoluteBaseURL(), $url) |
211
|
|
|
); |
212
|
|
|
$obj = Convert::json2obj($response->getBody()); |
213
|
|
|
$this->assertEquals($obj->ID, $comment1->ID); |
|
|
|
|
214
|
|
|
$this->assertEquals($obj->Comment, 'updated'); |
|
|
|
|
215
|
|
|
|
216
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
217
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testPUTwithXML() { |
|
|
|
|
221
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
222
|
|
|
|
223
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
224
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor'; |
225
|
|
|
|
226
|
|
|
// by mimetype |
227
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
228
|
|
|
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>'; |
229
|
|
|
$response = Director::test($url, null, null, 'PUT', $body, array('Content-Type'=>'text/xml')); |
|
|
|
|
230
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated |
|
|
|
|
231
|
|
|
$obj = Convert::xml2array($response->getBody()); |
232
|
|
|
$this->assertEquals($obj['ID'], $comment1->ID); |
|
|
|
|
233
|
|
|
$this->assertEquals($obj['Comment'], 'updated'); |
|
|
|
|
234
|
|
|
|
235
|
|
|
// by extension |
236
|
|
|
$url = sprintf("/api/v1/RestfulServerTest_Comment/%d.xml", $comment1->ID); |
237
|
|
|
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>'; |
238
|
|
|
$response = Director::test($url, null, null, 'PUT', $body); |
|
|
|
|
239
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated |
|
|
|
|
240
|
|
|
$this->assertEquals( |
|
|
|
|
241
|
|
|
$response->getHeader('Location'), |
242
|
|
|
Controller::join_links(Director::absoluteBaseURL(), $url) |
243
|
|
|
); |
244
|
|
|
$obj = Convert::xml2array($response->getBody()); |
245
|
|
|
$this->assertEquals($obj['ID'], $comment1->ID); |
|
|
|
|
246
|
|
|
$this->assertEquals($obj['Comment'], 'updated'); |
|
|
|
|
247
|
|
|
|
248
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
249
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testHTTPAcceptAndContentType() { |
253
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
254
|
|
|
|
255
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
256
|
|
|
|
257
|
|
|
$headers = array('Accept' => 'application/json'); |
258
|
|
|
$response = Director::test($url, null, null, 'GET', null, $headers); |
|
|
|
|
259
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Success |
|
|
|
|
260
|
|
|
$obj = Convert::json2obj($response->getBody()); |
261
|
|
|
$this->assertEquals($obj->ID, $comment1->ID); |
|
|
|
|
262
|
|
|
$this->assertEquals($response->getHeader('Content-Type'), 'application/json'); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function testNotFound(){ |
|
|
|
|
266
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
267
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user'; |
268
|
|
|
|
269
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/99"; |
270
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
271
|
|
|
$this->assertEquals($response->getStatusCode(), 404); |
|
|
|
|
272
|
|
|
|
273
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
274
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
View Code Duplication |
public function testMethodNotAllowed() { |
|
|
|
|
278
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); |
279
|
|
|
|
280
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; |
281
|
|
|
$response = Director::test($url, null, null, 'UNKNOWNHTTPMETHOD'); |
|
|
|
|
282
|
|
|
$this->assertEquals($response->getStatusCode(), 405); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
View Code Duplication |
public function testConflictOnExistingResourceWhenUsingPost() { |
|
|
|
|
286
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating', 'rating1'); |
287
|
|
|
|
288
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID; |
289
|
|
|
$response = Director::test($url, null, null, 'POST'); |
|
|
|
|
290
|
|
|
$this->assertEquals($response->getStatusCode(), 409); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testUnsupportedMediaType() { |
|
|
|
|
294
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
295
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user'; |
296
|
|
|
|
297
|
|
|
$url = "/api/v1/RestfulServerTest_Comment"; |
298
|
|
|
$data = "Comment||\/||updated"; // weird format |
299
|
|
|
$headers = array('Content-Type' => 'text/weirdformat'); |
300
|
|
|
$response = Director::test($url, null, null, 'POST', $data, $headers); |
|
|
|
|
301
|
|
|
$this->assertEquals($response->getStatusCode(), 415); |
|
|
|
|
302
|
|
|
|
303
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
304
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testXMLValueFormatting() { |
308
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating','rating1'); |
309
|
|
|
|
310
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID; |
311
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
312
|
|
|
$this->assertContains('<ID>' . $rating1->ID . '</ID>', $response->getBody()); |
313
|
|
|
$this->assertContains('<Rating>' . $rating1->Rating . '</Rating>', $response->getBody()); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function testApiAccessFieldRestrictions() { |
317
|
|
|
$author1 = $this->objFromFixture('RestfulServerTest_Author','author1'); |
318
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating','rating1'); |
319
|
|
|
|
320
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID; |
321
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
322
|
|
|
$this->assertContains('<ID>', $response->getBody()); |
323
|
|
|
$this->assertContains('<Rating>', $response->getBody()); |
324
|
|
|
$this->assertContains('<Author', $response->getBody()); |
325
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody()); |
326
|
|
|
$this->assertNotContains('<SecretRelation>', $response->getBody()); |
327
|
|
|
|
328
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID . '?add_fields=SecretField,SecretRelation'; |
329
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
330
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody(), |
331
|
|
|
'"add_fields" URL parameter filters out disallowed fields from $api_access' |
332
|
|
|
); |
333
|
|
|
$this->assertNotContains('<SecretRelation>', $response->getBody(), |
334
|
|
|
'"add_fields" URL parameter filters out disallowed relations from $api_access' |
335
|
|
|
); |
336
|
|
|
|
337
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID . '?fields=SecretField,SecretRelation'; |
338
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
339
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody(), |
340
|
|
|
'"fields" URL parameter filters out disallowed fields from $api_access' |
341
|
|
|
); |
342
|
|
|
$this->assertNotContains('<SecretRelation>', $response->getBody(), |
343
|
|
|
'"fields" URL parameter filters out disallowed relations from $api_access' |
344
|
|
|
); |
345
|
|
|
|
346
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID . '/Ratings'; |
347
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
348
|
|
|
$this->assertContains('<Rating>', $response->getBody(), |
349
|
|
|
'Relation viewer shows fields allowed through $api_access' |
350
|
|
|
); |
351
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody(), |
352
|
|
|
'Relation viewer on has-many filters out disallowed fields from $api_access' |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function testApiAccessRelationRestrictionsInline() { |
357
|
|
|
$author1 = $this->objFromFixture('RestfulServerTest_Author','author1'); |
358
|
|
|
|
359
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID; |
360
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
361
|
|
|
$this->assertNotContains('<RelatedPages', $response->getBody(), 'Restricts many-many with api_access=false'); |
362
|
|
|
$this->assertNotContains('<PublishedPages', $response->getBody(), 'Restricts has-many with api_access=false'); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function testApiAccessRelationRestrictionsOnEndpoint() { |
366
|
|
|
$author1 = $this->objFromFixture('RestfulServerTest_Author','author1'); |
367
|
|
|
|
368
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID . "/ProfilePage"; |
369
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
370
|
|
|
$this->assertEquals(404, $response->getStatusCode(), 'Restricts has-one with api_access=false'); |
|
|
|
|
371
|
|
|
|
372
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID . "/RelatedPages"; |
373
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
374
|
|
|
$this->assertEquals(404, $response->getStatusCode(), 'Restricts many-many with api_access=false'); |
|
|
|
|
375
|
|
|
|
376
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID . "/PublishedPages"; |
377
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
378
|
|
|
$this->assertEquals(404, $response->getStatusCode(), 'Restricts has-many with api_access=false'); |
|
|
|
|
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
public function testApiAccessWithPUT() { |
382
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating','rating1'); |
383
|
|
|
|
384
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID; |
385
|
|
|
$data = array( |
386
|
|
|
'Rating' => '42', |
387
|
|
|
'WriteProtectedField' => 'haxx0red' |
388
|
|
|
); |
389
|
|
|
$response = Director::test($url, $data, null, 'PUT'); |
|
|
|
|
390
|
|
|
// Assumption: XML is default output |
391
|
|
|
$responseArr = Convert::xml2array($response->getBody()); |
392
|
|
|
$this->assertEquals($responseArr['Rating'], 42); |
|
|
|
|
393
|
|
|
$this->assertNotEquals($responseArr['WriteProtectedField'], 'haxx0red'); |
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
public function testJSONDataFormatter() { |
397
|
|
|
$formatter = new JSONDataFormatter(); |
398
|
|
|
$editor = $this->objFromFixture('Member', 'editor'); |
399
|
|
|
$user = $this->objFromFixture('Member', 'user'); |
400
|
|
|
|
401
|
|
|
$this->assertEquals( |
|
|
|
|
402
|
|
|
$formatter->convertDataObject($editor, array("FirstName", "Email")), |
|
|
|
|
403
|
|
|
'{"FirstName":"Editor","Email":"[email protected]"}', |
404
|
|
|
"Correct JSON formatting with field subset"); |
405
|
|
|
|
406
|
|
|
$set = DataObject::get( |
407
|
|
|
"Member", |
408
|
|
|
sprintf('"Member"."ID" IN (%s)', implode(',', array($editor->ID, $user->ID))), |
409
|
|
|
'"Email" ASC' // for sorting for postgres |
410
|
|
|
); |
411
|
|
|
$this->assertEquals( |
|
|
|
|
412
|
|
|
$formatter->convertDataObjectSet($set, array("FirstName", "Email")), |
413
|
|
|
'{"totalSize":null,"items":[{"FirstName":"Editor","Email":"[email protected]"},' . |
414
|
|
|
'{"FirstName":"User","Email":"[email protected]"}]}', |
415
|
|
|
"Correct JSON formatting on a dataobjectset with field filter"); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
public function testApiAccessWithPOST() { |
419
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating"; |
420
|
|
|
$data = array( |
421
|
|
|
'Rating' => '42', |
422
|
|
|
'WriteProtectedField' => 'haxx0red' |
423
|
|
|
); |
424
|
|
|
$response = Director::test($url, $data, null, 'POST'); |
|
|
|
|
425
|
|
|
// Assumption: XML is default output |
426
|
|
|
$responseArr = Convert::xml2array($response->getBody()); |
427
|
|
|
$this->assertEquals($responseArr['Rating'], 42); |
|
|
|
|
428
|
|
|
$this->assertNotEquals($responseArr['WriteProtectedField'], 'haxx0red'); |
|
|
|
|
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function testCanViewRespectedInList() { |
|
|
|
|
432
|
|
|
// Default content type |
433
|
|
|
$url = "/api/v1/RestfulServerTest_SecretThing/"; |
434
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
435
|
|
|
$this->assertEquals($response->getStatusCode(), 200); |
|
|
|
|
436
|
|
|
$this->assertNotContains('Unspeakable', $response->getBody()); |
437
|
|
|
|
438
|
|
|
// JSON content type |
439
|
|
|
$url = "/api/v1/RestfulServerTest_SecretThing.json"; |
440
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
441
|
|
|
$this->assertEquals($response->getStatusCode(), 200); |
|
|
|
|
442
|
|
|
$this->assertNotContains('Unspeakable', $response->getBody()); |
443
|
|
|
|
444
|
|
|
// With authentication |
445
|
|
|
$_SERVER['PHP_AUTH_USER'] = '[email protected]'; |
446
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor'; |
447
|
|
|
$url = "/api/v1/RestfulServerTest_SecretThing/"; |
448
|
|
|
$response = Director::test($url, null, null, 'GET'); |
|
|
|
|
449
|
|
|
$this->assertEquals($response->getStatusCode(), 200); |
|
|
|
|
450
|
|
|
$this->assertContains('Unspeakable', $response->getBody()); |
451
|
|
|
unset($_SERVER['PHP_AUTH_USER']); |
452
|
|
|
unset($_SERVER['PHP_AUTH_PW']); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
} |
|
|
|
|
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Everybody can view comments, logged in members in the "users" group can create comments, |
459
|
|
|
* but only "editors" can edit or delete them. |
460
|
|
|
* |
461
|
|
|
*/ |
462
|
|
|
class RestfulServerTest_Comment extends DataObject implements PermissionProvider,TestOnly { |
|
|
|
|
463
|
|
|
|
464
|
|
|
static $api_access = true; |
|
|
|
|
465
|
|
|
|
466
|
|
|
static $db = array( |
|
|
|
|
467
|
|
|
"Name" => "Varchar(255)", |
468
|
|
|
"Comment" => "Text" |
469
|
|
|
); |
470
|
|
|
|
471
|
|
|
static $has_one = array( |
|
|
|
|
472
|
|
|
'Page' => 'RestfulServerTest_Page', |
473
|
|
|
'Author' => 'RestfulServerTest_Author', |
474
|
|
|
); |
475
|
|
|
|
476
|
|
|
public function providePermissions(){ |
477
|
|
|
return array( |
478
|
|
|
'EDIT_Comment' => 'Edit Comment Objects', |
479
|
|
|
'CREATE_Comment' => 'Create Comment Objects', |
480
|
|
|
'DELETE_Comment' => 'Delete Comment Objects', |
481
|
|
|
); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
public function canView($member = null) { |
485
|
|
|
return true; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
public function canEdit($member = null) { |
489
|
|
|
return Permission::checkMember($member, 'EDIT_Comment'); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
public function canDelete($member = null) { |
493
|
|
|
return Permission::checkMember($member, 'DELETE_Comment'); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
public function canCreate($member = null) { |
497
|
|
|
return Permission::checkMember($member, 'CREATE_Comment'); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
} |
|
|
|
|
501
|
|
|
|
502
|
|
|
class RestfulServerTest_SecretThing extends DataObject implements TestOnly,PermissionProvider{ |
|
|
|
|
503
|
|
|
static $api_access = true; |
|
|
|
|
504
|
|
|
|
505
|
|
|
static $db = array( |
|
|
|
|
506
|
|
|
"Name" => "Varchar(255)", |
507
|
|
|
); |
508
|
|
|
|
509
|
|
|
public function canView($member = null) { |
510
|
|
|
return Permission::checkMember($member, 'VIEW_SecretThing'); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
public function providePermissions(){ |
514
|
|
|
return array( |
515
|
|
|
'VIEW_SecretThing' => 'View Secret Things', |
516
|
|
|
); |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
class RestfulServerTest_Page extends DataObject implements TestOnly { |
|
|
|
|
521
|
|
|
|
522
|
|
|
static $api_access = false; |
|
|
|
|
523
|
|
|
|
524
|
|
|
static $db = array( |
|
|
|
|
525
|
|
|
'Title' => 'Text', |
526
|
|
|
'Content' => 'HTMLText', |
527
|
|
|
); |
528
|
|
|
|
529
|
|
|
static $has_one = array( |
|
|
|
|
530
|
|
|
'Author' => 'RestfulServerTest_Author', |
531
|
|
|
); |
532
|
|
|
|
533
|
|
|
static $has_many = array( |
|
|
|
|
534
|
|
|
'TestComments' => 'RestfulServerTest_Comment' |
535
|
|
|
); |
536
|
|
|
|
537
|
|
|
static $belongs_many_many = array( |
|
|
|
|
538
|
|
|
'RelatedAuthors' => 'RestfulServerTest_Author', |
539
|
|
|
); |
540
|
|
|
|
541
|
|
|
} |
|
|
|
|
542
|
|
|
|
543
|
|
|
class RestfulServerTest_Author extends DataObject implements TestOnly { |
|
|
|
|
544
|
|
|
|
545
|
|
|
static $api_access = true; |
|
|
|
|
546
|
|
|
|
547
|
|
|
static $db = array( |
|
|
|
|
548
|
|
|
'Name' => 'Text', |
549
|
|
|
); |
550
|
|
|
|
551
|
|
|
static $many_many = array( |
|
|
|
|
552
|
|
|
'RelatedPages' => 'RestfulServerTest_Page', |
553
|
|
|
'RelatedAuthors' => 'RestfulServerTest_Author', |
554
|
|
|
); |
555
|
|
|
|
556
|
|
|
static $has_many = array( |
|
|
|
|
557
|
|
|
'PublishedPages' => 'RestfulServerTest_Page', |
558
|
|
|
'Ratings' => 'RestfulServerTest_AuthorRating', |
559
|
|
|
); |
560
|
|
|
|
561
|
|
|
public function canView($member = null) { |
562
|
|
|
return true; |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
class RestfulServerTest_AuthorRating extends DataObject implements TestOnly { |
|
|
|
|
567
|
|
|
static $api_access = array( |
|
|
|
|
568
|
|
|
'view' => array( |
569
|
|
|
'Rating', |
570
|
|
|
'WriteProtectedField', |
571
|
|
|
'Author' |
572
|
|
|
), |
573
|
|
|
'edit' => array( |
574
|
|
|
'Rating' |
575
|
|
|
) |
576
|
|
|
); |
577
|
|
|
|
578
|
|
|
static $db = array( |
|
|
|
|
579
|
|
|
'Rating' => 'Int', |
580
|
|
|
'SecretField' => 'Text', |
581
|
|
|
'WriteProtectedField' => 'Text', |
582
|
|
|
); |
583
|
|
|
|
584
|
|
|
static $has_one = array( |
|
|
|
|
585
|
|
|
'Author' => 'RestfulServerTest_Author', |
586
|
|
|
'SecretRelation' => 'RestfulServerTest_Author', |
587
|
|
|
); |
588
|
|
|
|
589
|
|
|
public function canView($member = null) { |
590
|
|
|
return true; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
public function canEdit($member = null) { |
594
|
|
|
return true; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
public function canCreate($member = null) { |
598
|
|
|
return true; |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.