2008-10-16 19:37:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @todo Test Relation getters
|
|
|
|
* @todo Test filter and limit through GET params
|
|
|
|
* @todo Test DELETE verb
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class RestfulServerTest extends SapphireTest {
|
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'RestfulServerTest.yml';
|
2008-10-16 19:37:31 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'RestfulServerTest_Comment',
|
|
|
|
'RestfulServerTest_SecretThing',
|
|
|
|
'RestfulServerTest_Page',
|
|
|
|
'RestfulServerTest_Author',
|
|
|
|
'RestfulServerTest_AuthorRating',
|
|
|
|
);
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
public function testApiAccess() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
$page1 = $this->objFromFixture('RestfulServerTest_Page', 'page1');
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
// normal GET should succeed with $api_access enabled
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200);
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user';
|
|
|
|
|
|
|
|
// even with logged in user a GET with $api_access disabled should fail
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Page/" . $page1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$response = Director::test($url, null, null, 'GET');
|
2009-03-30 05:12:58 +02:00
|
|
|
$this->assertEquals($response->getStatusCode(), 401);
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiAccessBoolean() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertContains('<ID>', $response->getBody());
|
|
|
|
$this->assertContains('<Name>', $response->getBody());
|
|
|
|
$this->assertContains('<Comment>', $response->getBody());
|
|
|
|
$this->assertContains('<Page', $response->getBody());
|
|
|
|
$this->assertContains('<Author', $response->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticatedGET() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$thing1 = $this->objFromFixture('RestfulServerTest_SecretThing', 'thing1');
|
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
// @todo create additional mock object with authenticated VIEW permissions
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_SecretThing/" . $thing1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$response = Director::test($url, null, null, 'GET');
|
2009-03-30 05:12:58 +02:00
|
|
|
$this->assertEquals($response->getStatusCode(), 401);
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user';
|
|
|
|
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200);
|
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
public function testAuthenticatedPUT() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$data = array('Comment' => 'created');
|
|
|
|
|
|
|
|
$response = Director::test($url, $data, null, 'PUT');
|
2009-03-30 05:12:58 +02:00
|
|
|
$this->assertEquals($response->getStatusCode(), 401); // Permission failure
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
|
|
|
$response = Director::test($url, $data, null, 'PUT');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Success
|
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
2009-01-05 07:19:48 +01:00
|
|
|
|
|
|
|
public function testGETRelationshipsXML() {
|
|
|
|
$author1 = $this->objFromFixture('RestfulServerTest_Author', 'author1');
|
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating', 'rating1');
|
|
|
|
$rating2 = $this->objFromFixture('RestfulServerTest_AuthorRating', 'rating2');
|
|
|
|
|
|
|
|
// @todo should be set up by fixtures, doesn't work for some reason...
|
|
|
|
$author1->Ratings()->add($rating1);
|
|
|
|
$author1->Ratings()->add($rating2);
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID;
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200);
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$responseArr = Convert::xml2array($response->getBody());
|
|
|
|
$ratingsArr = $responseArr['Ratings']['RestfulServerTest_AuthorRating'];
|
|
|
|
$this->assertEquals(count($ratingsArr), 2);
|
2009-10-02 00:07:50 +02:00
|
|
|
$ratingIDs = array(
|
2010-10-13 03:23:07 +02:00
|
|
|
(int)$ratingsArr[0]['@attributes']['id'],
|
|
|
|
(int)$ratingsArr[1]['@attributes']['id']
|
2009-10-02 00:07:50 +02:00
|
|
|
);
|
|
|
|
$this->assertContains($rating1->ID, $ratingIDs);
|
|
|
|
$this->assertContains($rating2->ID, $ratingIDs);
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2009-07-08 10:28:17 +02:00
|
|
|
|
|
|
|
public function testGETManyManyRelationshipsXML() {
|
|
|
|
// author4 has related authors author2 and author3
|
|
|
|
$author2 = $this->objFromFixture('RestfulServerTest_Author', 'author2');
|
|
|
|
$author3 = $this->objFromFixture('RestfulServerTest_Author', 'author3');
|
|
|
|
$author4 = $this->objFromFixture('RestfulServerTest_Author', 'author4');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author4->ID . '/RelatedAuthors';
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2009-07-08 10:28:17 +02:00
|
|
|
$arr = Convert::xml2array($response->getBody());
|
|
|
|
$authorsArr = $arr['RestfulServerTest_Author'];
|
|
|
|
|
|
|
|
$this->assertEquals(count($authorsArr), 2);
|
2009-10-02 00:07:50 +02:00
|
|
|
$ratingIDs = array(
|
2010-10-13 03:23:07 +02:00
|
|
|
(int)$authorsArr[0]['ID'],
|
|
|
|
(int)$authorsArr[1]['ID']
|
2009-10-02 00:07:50 +02:00
|
|
|
);
|
|
|
|
$this->assertContains($author2->ID, $ratingIDs);
|
|
|
|
$this->assertContains($author3->ID, $ratingIDs);
|
2009-07-08 10:28:17 +02:00
|
|
|
}
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
public function testPUTWithFormEncoded() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2009-03-29 01:51:38 +01:00
|
|
|
$body = 'Name=Updated Comment&Comment=updated';
|
|
|
|
$headers = array(
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
);
|
|
|
|
$response = Director::test($url, null, null, 'PUT', $body, $headers);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Success
|
|
|
|
// Assumption: XML is default output
|
|
|
|
$responseArr = Convert::xml2array($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertEquals($responseArr['ID'], $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($responseArr['Comment'], 'updated');
|
2009-03-29 01:51:38 +01:00
|
|
|
$this->assertEquals($responseArr['Name'], 'Updated Comment');
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
public function testPOSTWithFormEncoded() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment";
|
2009-03-29 01:51:38 +01:00
|
|
|
$body = 'Name=New Comment&Comment=created';
|
|
|
|
$headers = array(
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
);
|
|
|
|
$response = Director::test($url, null, null, 'POST', $body, $headers);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($response->getStatusCode(), 201); // Created
|
|
|
|
// Assumption: XML is default output
|
|
|
|
$responseArr = Convert::xml2array($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertTrue($responseArr['ID'] > 0);
|
|
|
|
$this->assertNotEquals($responseArr['ID'], $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($responseArr['Comment'], 'created');
|
2009-03-29 01:51:38 +01:00
|
|
|
$this->assertEquals($responseArr['Name'], 'New Comment');
|
2010-10-19 02:32:18 +02:00
|
|
|
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID']));
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPUTwithJSON() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
|
|
|
|
|
|
|
// by mimetype
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$body = '{"Comment":"updated"}';
|
|
|
|
$response = Director::test($url, null, null, 'PUT', $body, array('Content-Type'=>'application/json'));
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
|
|
|
$obj = Convert::json2obj($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertEquals($obj->ID, $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($obj->Comment, 'updated');
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
// by extension
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = sprintf("/api/v1/RestfulServerTest_Comment/%d.json", $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$body = '{"Comment":"updated"}';
|
|
|
|
$response = Director::test($url, null, null, 'PUT', $body);
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
2010-10-19 02:32:18 +02:00
|
|
|
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url));
|
2008-10-16 19:37:31 +02:00
|
|
|
$obj = Convert::json2obj($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertEquals($obj->ID, $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($obj->Comment, 'updated');
|
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPUTwithXML() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
|
|
|
|
|
|
|
// by mimetype
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
|
|
|
$response = Director::test($url, null, null, 'PUT', $body, array('Content-Type'=>'text/xml'));
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
|
|
|
$obj = Convert::xml2array($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertEquals($obj['ID'], $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($obj['Comment'], 'updated');
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
// by extension
|
2009-10-02 00:07:50 +02:00
|
|
|
$url = sprintf("/api/v1/RestfulServerTest_Comment/%d.xml", $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
|
|
|
$response = Director::test($url, null, null, 'PUT', $body);
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
2010-10-19 02:32:18 +02:00
|
|
|
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url));
|
2008-10-16 19:37:31 +02:00
|
|
|
$obj = Convert::xml2array($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertEquals($obj['ID'], $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($obj['Comment'], 'updated');
|
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHTTPAcceptAndContentType() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
$headers = array('Accept' => 'application/json');
|
|
|
|
$response = Director::test($url, null, null, 'GET', null, $headers);
|
|
|
|
$this->assertEquals($response->getStatusCode(), 200); // Success
|
|
|
|
$obj = Convert::json2obj($response->getBody());
|
2009-10-02 00:07:50 +02:00
|
|
|
$this->assertEquals($obj->ID, $comment1->ID);
|
2008-10-16 19:37:31 +02:00
|
|
|
$this->assertEquals($response->getHeader('Content-Type'), 'application/json');
|
|
|
|
}
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
public function testNotFound(){
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user';
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/99";
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 404);
|
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMethodNotAllowed() {
|
2009-10-02 00:07:50 +02:00
|
|
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
2008-10-16 19:37:31 +02:00
|
|
|
$response = Director::test($url, null, null, 'UNKNOWNHTTPMETHOD');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 405);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConflictOnExistingResourceWhenUsingPost() {
|
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating', 'rating1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID;
|
|
|
|
$response = Director::test($url, null, null, 'POST');
|
|
|
|
$this->assertEquals($response->getStatusCode(), 409);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsupportedMediaType() {
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'user';
|
2009-07-08 10:28:17 +02:00
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
$url = "/api/v1/RestfulServerTest_Comment";
|
|
|
|
$data = "Comment||\/||updated"; // weird format
|
|
|
|
$headers = array('Content-Type' => 'text/weirdformat');
|
|
|
|
$response = Director::test($url, null, null, 'POST', $data, $headers);
|
|
|
|
$this->assertEquals($response->getStatusCode(), 415);
|
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testXMLValueFormatting() {
|
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating','rating1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID;
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertContains('<ID>' . $rating1->ID . '</ID>', $response->getBody());
|
|
|
|
$this->assertContains('<Rating>' . $rating1->Rating . '</Rating>', $response->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiAccessFieldRestrictions() {
|
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating','rating1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID;
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertContains('<ID>', $response->getBody());
|
|
|
|
$this->assertContains('<Rating>', $response->getBody());
|
|
|
|
$this->assertContains('<Author', $response->getBody());
|
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody());
|
|
|
|
$this->assertNotContains('<SecretRelation>', $response->getBody());
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID . '?add_fields=SecretField,SecretRelation';
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody(),
|
|
|
|
'"add_fields" URL parameter filters out disallowed fields from $api_access'
|
|
|
|
);
|
|
|
|
$this->assertNotContains('<SecretRelation>', $response->getBody(),
|
|
|
|
'"add_fields" URL parameter filters out disallowed relations from $api_access'
|
|
|
|
);
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID . '?fields=SecretField,SecretRelation';
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertNotContains('<SecretField>', $response->getBody(),
|
|
|
|
'"fields" URL parameter filters out disallowed fields from $api_access'
|
|
|
|
);
|
|
|
|
$this->assertNotContains('<SecretRelation>', $response->getBody(),
|
|
|
|
'"fields" URL parameter filters out disallowed relations from $api_access'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-05-29 03:04:40 +02:00
|
|
|
public function testApiAccessRelationRestrictions() {
|
|
|
|
$author1 = $this->objFromFixture('RestfulServerTest_Author','author1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID;
|
|
|
|
$response = Director::test($url, null, null, 'GET');
|
|
|
|
$this->assertNotContains('<RelatedPages', $response->getBody());
|
|
|
|
$this->assertNotContains('<PublishedPages', $response->getBody());
|
|
|
|
}
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
public function testApiAccessWithPUT() {
|
|
|
|
$rating1 = $this->objFromFixture('RestfulServerTest_AuthorRating','rating1');
|
|
|
|
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating/" . $rating1->ID;
|
|
|
|
$data = array(
|
|
|
|
'Rating' => '42',
|
|
|
|
'WriteProtectedField' => 'haxx0red'
|
|
|
|
);
|
|
|
|
$response = Director::test($url, $data, null, 'PUT');
|
|
|
|
// Assumption: XML is default output
|
|
|
|
$responseArr = Convert::xml2array($response->getBody());
|
|
|
|
$this->assertEquals($responseArr['Rating'], 42);
|
|
|
|
$this->assertNotEquals($responseArr['WriteProtectedField'], 'haxx0red');
|
|
|
|
}
|
2011-03-22 22:23:57 +01:00
|
|
|
|
|
|
|
public function testJSONDataFormatter() {
|
|
|
|
$formatter = new JSONDataFormatter();
|
|
|
|
$single_do = $this->objFromFixture('Member', 'editor');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$formatter->convertDataObject($single_do, array("FirstName", "Email")),
|
|
|
|
'{"FirstName":"Editor","Email":"editor@test.com"}',
|
|
|
|
"Correct JSON formatting with field subset");
|
|
|
|
|
|
|
|
$set = DataObject::get("Member");
|
|
|
|
$this->assertEquals(
|
|
|
|
$formatter->convertDataObjectSet($set, array("FirstName", "Email")),
|
|
|
|
'{"totalSize":null,"items":[{"FirstName":"Editor","Email":"editor@test.com"},{"FirstName":"User","Email":"user@test.com"},{"FirstName":"ADMIN","Email":"ADMIN@example.org"}]}',
|
|
|
|
"Correct JSON formatting on a dataobjectset with field filter");
|
|
|
|
}
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
public function testApiAccessWithPOST() {
|
|
|
|
$url = "/api/v1/RestfulServerTest_AuthorRating";
|
|
|
|
$data = array(
|
|
|
|
'Rating' => '42',
|
|
|
|
'WriteProtectedField' => 'haxx0red'
|
|
|
|
);
|
|
|
|
$response = Director::test($url, $data, null, 'POST');
|
|
|
|
// Assumption: XML is default output
|
|
|
|
$responseArr = Convert::xml2array($response->getBody());
|
|
|
|
$this->assertEquals($responseArr['Rating'], 42);
|
|
|
|
$this->assertNotEquals($responseArr['WriteProtectedField'], 'haxx0red');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Everybody can view comments, logged in members in the "users" group can create comments,
|
|
|
|
* but only "editors" can edit or delete them.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class RestfulServerTest_Comment extends DataObject implements PermissionProvider,TestOnly {
|
|
|
|
|
|
|
|
static $api_access = true;
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"Name" => "Varchar(255)",
|
|
|
|
"Comment" => "Text"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
'Page' => 'RestfulServerTest_Page',
|
|
|
|
'Author' => 'RestfulServerTest_Author',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function providePermissions(){
|
|
|
|
return array(
|
|
|
|
'EDIT_Comment' => 'Edit Comment Objects',
|
|
|
|
'CREATE_Comment' => 'Create Comment Objects',
|
|
|
|
'DELETE_Comment' => 'Delete Comment Objects',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canEdit($member = null) {
|
|
|
|
return Permission::checkMember($member, 'EDIT_Comment');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canDelete($member = null) {
|
|
|
|
return Permission::checkMember($member, 'DELETE_Comment');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canCreate($member = null) {
|
|
|
|
return Permission::checkMember($member, 'CREATE_Comment');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class RestfulServerTest_SecretThing extends DataObject implements TestOnly,PermissionProvider{
|
|
|
|
static $api_access = true;
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"Name" => "Varchar(255)",
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return Permission::checkMember($member, 'VIEW_SecretThing');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providePermissions(){
|
|
|
|
return array(
|
|
|
|
'VIEW_SecretThing' => 'View Secret Things',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RestfulServerTest_Page extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $api_access = false;
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Text',
|
|
|
|
'Content' => 'HTMLText',
|
|
|
|
);
|
|
|
|
|
2009-05-29 03:04:40 +02:00
|
|
|
static $has_one = array(
|
|
|
|
'Author' => 'RestfulServerTest_Author',
|
|
|
|
);
|
|
|
|
|
2008-10-16 19:37:31 +02:00
|
|
|
static $has_many = array(
|
|
|
|
'TestComments' => 'RestfulServerTest_Comment'
|
|
|
|
);
|
2009-05-29 03:04:40 +02:00
|
|
|
|
|
|
|
static $belongs_many_many = array(
|
|
|
|
'RelatedAuthors' => 'RestfulServerTest_Author',
|
|
|
|
);
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class RestfulServerTest_Author extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $api_access = true;
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Name' => 'Text',
|
|
|
|
);
|
2009-05-29 03:04:40 +02:00
|
|
|
|
|
|
|
static $many_many = array(
|
|
|
|
'RelatedPages' => 'RestfulServerTest_Page',
|
2009-07-08 10:28:17 +02:00
|
|
|
'RelatedAuthors' => 'RestfulServerTest_Author',
|
2009-05-29 03:04:40 +02:00
|
|
|
);
|
2008-10-16 19:37:31 +02:00
|
|
|
|
|
|
|
static $has_many = array(
|
2009-05-29 03:04:40 +02:00
|
|
|
'PublishedPages' => 'RestfulServerTest_Page',
|
2008-10-16 19:37:31 +02:00
|
|
|
'Ratings' => 'RestfulServerTest_AuthorRating',
|
|
|
|
);
|
2009-01-05 07:19:48 +01:00
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-16 19:37:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class RestfulServerTest_AuthorRating extends DataObject implements TestOnly {
|
|
|
|
static $api_access = array(
|
|
|
|
'view' => array(
|
|
|
|
'Rating',
|
|
|
|
'WriteProtectedField',
|
|
|
|
'Author'
|
|
|
|
),
|
|
|
|
'edit' => array(
|
|
|
|
'Rating'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Rating' => 'Int',
|
|
|
|
'SecretField' => 'Text',
|
|
|
|
'WriteProtectedField' => 'Text'
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
'Author' => 'RestfulServerTest_Author',
|
|
|
|
'SecretRelation' => 'RestfulServerTest_Author',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canEdit($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canCreate($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 02:32:18 +02:00
|
|
|
?>
|