mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Fixed many_many relations querying in RestfulServer
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@81346 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
c389c01f9c
commit
e54a2e75e6
@ -508,6 +508,8 @@ class RestfulServer extends Controller {
|
|||||||
$query = $obj->{"{$relationName}Query"}(null, $sort, null, $limit);
|
$query = $obj->{"{$relationName}Query"}(null, $sort, null, $limit);
|
||||||
$relationClass = $obj->{"{$relationName}Class"}();
|
$relationClass = $obj->{"{$relationName}Class"}();
|
||||||
} elseif($relationClass = $obj->many_many($relationName)) {
|
} elseif($relationClass = $obj->many_many($relationName)) {
|
||||||
|
// many_many() returns different notation
|
||||||
|
$relationClass = $relationClass[1];
|
||||||
$query = $obj->getManyManyComponentsQuery($relationName);
|
$query = $obj->getManyManyComponentsQuery($relationName);
|
||||||
} elseif($relationClass = $obj->has_many($relationName)) {
|
} elseif($relationClass = $obj->has_many($relationName)) {
|
||||||
$query = $obj->getComponentsQuery($relationName);
|
$query = $obj->getComponentsQuery($relationName);
|
||||||
|
@ -54,7 +54,7 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAuthenticatedPUT() {
|
public function testAuthenticatedPUT() {
|
||||||
$url = "/api/v1/RestfulServerTest_Comment/1";
|
$url = "/api/v1/RestfulServerTest_Comment/1";
|
||||||
$data = array('Comment' => 'created');
|
$data = array('Comment' => 'created');
|
||||||
@ -83,18 +83,35 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID;
|
$url = "/api/v1/RestfulServerTest_Author/" . $author1->ID;
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertEquals($response->getStatusCode(), 200);
|
$this->assertEquals($response->getStatusCode(), 200);
|
||||||
|
|
||||||
$responseArr = Convert::xml2array($response->getBody());
|
$responseArr = Convert::xml2array($response->getBody());
|
||||||
$ratingsArr = $responseArr['Ratings']['RestfulServerTest_AuthorRating'];
|
$ratingsArr = $responseArr['Ratings']['RestfulServerTest_AuthorRating'];
|
||||||
$this->assertEquals(count($ratingsArr), 2);
|
$this->assertEquals(count($ratingsArr), 2);
|
||||||
$this->assertEquals($ratingsArr[0]['@attributes']['id'], $rating1->ID);
|
$this->assertEquals($ratingsArr[0]['@attributes']['id'], $rating1->ID);
|
||||||
$this->assertEquals($ratingsArr[1]['@attributes']['id'], $rating2->ID);
|
$this->assertEquals($ratingsArr[1]['@attributes']['id'], $rating2->ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
$this->assertEquals($response->getStatusCode(), 200);
|
||||||
|
$arr = Convert::xml2array($response->getBody());
|
||||||
|
$authorsArr = $arr['RestfulServerTest_Author'];
|
||||||
|
|
||||||
|
$this->assertEquals(count($authorsArr), 2);
|
||||||
|
$this->assertEquals($authorsArr[0]['ID'], $author2->ID);
|
||||||
|
$this->assertEquals($authorsArr[1]['ID'], $author3->ID);
|
||||||
|
}
|
||||||
|
|
||||||
public function testPUTWithFormEncoded() {
|
public function testPUTWithFormEncoded() {
|
||||||
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
||||||
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
||||||
|
|
||||||
$url = "/api/v1/RestfulServerTest_Comment/1";
|
$url = "/api/v1/RestfulServerTest_Comment/1";
|
||||||
$body = 'Name=Updated Comment&Comment=updated';
|
$body = 'Name=Updated Comment&Comment=updated';
|
||||||
$headers = array(
|
$headers = array(
|
||||||
@ -107,15 +124,15 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$this->assertEquals($responseArr['ID'], 1);
|
$this->assertEquals($responseArr['ID'], 1);
|
||||||
$this->assertEquals($responseArr['Comment'], 'updated');
|
$this->assertEquals($responseArr['Comment'], 'updated');
|
||||||
$this->assertEquals($responseArr['Name'], 'Updated Comment');
|
$this->assertEquals($responseArr['Name'], 'Updated Comment');
|
||||||
|
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPOSTWithFormEncoded() {
|
public function testPOSTWithFormEncoded() {
|
||||||
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
||||||
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
||||||
|
|
||||||
$url = "/api/v1/RestfulServerTest_Comment";
|
$url = "/api/v1/RestfulServerTest_Comment";
|
||||||
$body = 'Name=New Comment&Comment=created';
|
$body = 'Name=New Comment&Comment=created';
|
||||||
$headers = array(
|
$headers = array(
|
||||||
@ -128,7 +145,7 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$this->assertEquals($responseArr['ID'], 2);
|
$this->assertEquals($responseArr['ID'], 2);
|
||||||
$this->assertEquals($responseArr['Comment'], 'created');
|
$this->assertEquals($responseArr['Comment'], 'created');
|
||||||
$this->assertEquals($responseArr['Name'], 'New Comment');
|
$this->assertEquals($responseArr['Name'], 'New Comment');
|
||||||
|
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
unset($_SERVER['PHP_AUTH_PW']);
|
||||||
}
|
}
|
||||||
@ -145,7 +162,7 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$obj = Convert::json2obj($response->getBody());
|
$obj = Convert::json2obj($response->getBody());
|
||||||
$this->assertEquals($obj->ID, 1);
|
$this->assertEquals($obj->ID, 1);
|
||||||
$this->assertEquals($obj->Comment, 'updated');
|
$this->assertEquals($obj->Comment, 'updated');
|
||||||
|
|
||||||
// by extension
|
// by extension
|
||||||
$url = "/api/v1/RestfulServerTest_Comment/1.json";
|
$url = "/api/v1/RestfulServerTest_Comment/1.json";
|
||||||
$body = '{"Comment":"updated"}';
|
$body = '{"Comment":"updated"}';
|
||||||
@ -171,7 +188,7 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$obj = Convert::xml2array($response->getBody());
|
$obj = Convert::xml2array($response->getBody());
|
||||||
$this->assertEquals($obj['ID'], 1);
|
$this->assertEquals($obj['ID'], 1);
|
||||||
$this->assertEquals($obj['Comment'], 'updated');
|
$this->assertEquals($obj['Comment'], 'updated');
|
||||||
|
|
||||||
// by extension
|
// by extension
|
||||||
$url = "/api/v1/RestfulServerTest_Comment/1.xml";
|
$url = "/api/v1/RestfulServerTest_Comment/1.xml";
|
||||||
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
||||||
@ -195,7 +212,7 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
$this->assertEquals($obj->ID, 1);
|
$this->assertEquals($obj->ID, 1);
|
||||||
$this->assertEquals($response->getHeader('Content-Type'), 'application/json');
|
$this->assertEquals($response->getHeader('Content-Type'), 'application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotFound(){
|
public function testNotFound(){
|
||||||
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
||||||
$_SERVER['PHP_AUTH_PW'] = 'user';
|
$_SERVER['PHP_AUTH_PW'] = 'user';
|
||||||
@ -225,7 +242,7 @@ class RestfulServerTest extends SapphireTest {
|
|||||||
public function testUnsupportedMediaType() {
|
public function testUnsupportedMediaType() {
|
||||||
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
$_SERVER['PHP_AUTH_USER'] = 'user@test.com';
|
||||||
$_SERVER['PHP_AUTH_PW'] = 'user';
|
$_SERVER['PHP_AUTH_PW'] = 'user';
|
||||||
|
|
||||||
$url = "/api/v1/RestfulServerTest_Comment";
|
$url = "/api/v1/RestfulServerTest_Comment";
|
||||||
$data = "Comment||\/||updated"; // weird format
|
$data = "Comment||\/||updated"; // weird format
|
||||||
$headers = array('Content-Type' => 'text/weirdformat');
|
$headers = array('Content-Type' => 'text/weirdformat');
|
||||||
@ -410,6 +427,7 @@ class RestfulServerTest_Author extends DataObject implements TestOnly {
|
|||||||
|
|
||||||
static $many_many = array(
|
static $many_many = array(
|
||||||
'RelatedPages' => 'RestfulServerTest_Page',
|
'RelatedPages' => 'RestfulServerTest_Page',
|
||||||
|
'RelatedAuthors' => 'RestfulServerTest_Author',
|
||||||
);
|
);
|
||||||
|
|
||||||
static $has_many = array(
|
static $has_many = array(
|
||||||
|
@ -43,6 +43,13 @@ RestfulServerTest_Comment:
|
|||||||
RestfulServerTest_Author:
|
RestfulServerTest_Author:
|
||||||
author1:
|
author1:
|
||||||
FirstName: Author 1
|
FirstName: Author 1
|
||||||
|
author2:
|
||||||
|
FirstName: Author 2
|
||||||
|
author3:
|
||||||
|
Firstname: Author 3
|
||||||
|
author4:
|
||||||
|
FirstName: Author 4
|
||||||
|
RelatedAuthors: =>RestfulServerTest_Author.author2,=>RestfulServerTest_Author.author3
|
||||||
RestfulServerTest_AuthorRating:
|
RestfulServerTest_AuthorRating:
|
||||||
rating1:
|
rating1:
|
||||||
Rating: 3
|
Rating: 3
|
||||||
|
Loading…
Reference in New Issue
Block a user