From e54a2e75e6844ad2f27baf0d7950c8a33a981006 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 8 Jul 2009 08:28:17 +0000 Subject: [PATCH] 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 --- api/RestfulServer.php | 2 ++ tests/api/RestfulServerTest.php | 40 ++++++++++++++++++++++++--------- tests/api/RestfulServerTest.yml | 7 ++++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/api/RestfulServer.php b/api/RestfulServer.php index 844f04706..c259b018d 100644 --- a/api/RestfulServer.php +++ b/api/RestfulServer.php @@ -508,6 +508,8 @@ class RestfulServer extends Controller { $query = $obj->{"{$relationName}Query"}(null, $sort, null, $limit); $relationClass = $obj->{"{$relationName}Class"}(); } elseif($relationClass = $obj->many_many($relationName)) { + // many_many() returns different notation + $relationClass = $relationClass[1]; $query = $obj->getManyManyComponentsQuery($relationName); } elseif($relationClass = $obj->has_many($relationName)) { $query = $obj->getComponentsQuery($relationName); diff --git a/tests/api/RestfulServerTest.php b/tests/api/RestfulServerTest.php index 4a210942a..6b5f4ab1b 100644 --- a/tests/api/RestfulServerTest.php +++ b/tests/api/RestfulServerTest.php @@ -54,7 +54,7 @@ class RestfulServerTest extends SapphireTest { unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); } - + public function testAuthenticatedPUT() { $url = "/api/v1/RestfulServerTest_Comment/1"; $data = array('Comment' => 'created'); @@ -83,18 +83,35 @@ class RestfulServerTest extends SapphireTest { $url = "/api/v1/RestfulServerTest_Author/" . $author1->ID; $response = Director::test($url, null, null, 'GET'); $this->assertEquals($response->getStatusCode(), 200); - + $responseArr = Convert::xml2array($response->getBody()); $ratingsArr = $responseArr['Ratings']['RestfulServerTest_AuthorRating']; $this->assertEquals(count($ratingsArr), 2); $this->assertEquals($ratingsArr[0]['@attributes']['id'], $rating1->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() { $_SERVER['PHP_AUTH_USER'] = 'editor@test.com'; $_SERVER['PHP_AUTH_PW'] = 'editor'; - + $url = "/api/v1/RestfulServerTest_Comment/1"; $body = 'Name=Updated Comment&Comment=updated'; $headers = array( @@ -107,15 +124,15 @@ class RestfulServerTest extends SapphireTest { $this->assertEquals($responseArr['ID'], 1); $this->assertEquals($responseArr['Comment'], 'updated'); $this->assertEquals($responseArr['Name'], 'Updated Comment'); - + unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); } - + public function testPOSTWithFormEncoded() { $_SERVER['PHP_AUTH_USER'] = 'editor@test.com'; $_SERVER['PHP_AUTH_PW'] = 'editor'; - + $url = "/api/v1/RestfulServerTest_Comment"; $body = 'Name=New Comment&Comment=created'; $headers = array( @@ -128,7 +145,7 @@ class RestfulServerTest extends SapphireTest { $this->assertEquals($responseArr['ID'], 2); $this->assertEquals($responseArr['Comment'], 'created'); $this->assertEquals($responseArr['Name'], 'New Comment'); - + unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); } @@ -145,7 +162,7 @@ class RestfulServerTest extends SapphireTest { $obj = Convert::json2obj($response->getBody()); $this->assertEquals($obj->ID, 1); $this->assertEquals($obj->Comment, 'updated'); - + // by extension $url = "/api/v1/RestfulServerTest_Comment/1.json"; $body = '{"Comment":"updated"}'; @@ -171,7 +188,7 @@ class RestfulServerTest extends SapphireTest { $obj = Convert::xml2array($response->getBody()); $this->assertEquals($obj['ID'], 1); $this->assertEquals($obj['Comment'], 'updated'); - + // by extension $url = "/api/v1/RestfulServerTest_Comment/1.xml"; $body = 'updated'; @@ -195,7 +212,7 @@ class RestfulServerTest extends SapphireTest { $this->assertEquals($obj->ID, 1); $this->assertEquals($response->getHeader('Content-Type'), 'application/json'); } - + public function testNotFound(){ $_SERVER['PHP_AUTH_USER'] = 'user@test.com'; $_SERVER['PHP_AUTH_PW'] = 'user'; @@ -225,7 +242,7 @@ class RestfulServerTest extends SapphireTest { public function testUnsupportedMediaType() { $_SERVER['PHP_AUTH_USER'] = 'user@test.com'; $_SERVER['PHP_AUTH_PW'] = 'user'; - + $url = "/api/v1/RestfulServerTest_Comment"; $data = "Comment||\/||updated"; // weird format $headers = array('Content-Type' => 'text/weirdformat'); @@ -410,6 +427,7 @@ class RestfulServerTest_Author extends DataObject implements TestOnly { static $many_many = array( 'RelatedPages' => 'RestfulServerTest_Page', + 'RelatedAuthors' => 'RestfulServerTest_Author', ); static $has_many = array( diff --git a/tests/api/RestfulServerTest.yml b/tests/api/RestfulServerTest.yml index 1b7a0b242..74d39fc11 100644 --- a/tests/api/RestfulServerTest.yml +++ b/tests/api/RestfulServerTest.yml @@ -43,6 +43,13 @@ RestfulServerTest_Comment: RestfulServerTest_Author: author1: FirstName: Author 1 + author2: + FirstName: Author 2 + author3: + Firstname: Author 3 + author4: + FirstName: Author 4 + RelatedAuthors: =>RestfulServerTest_Author.author2,=>RestfulServerTest_Author.author3 RestfulServerTest_AuthorRating: rating1: Rating: 3