From 5c52bf0c5370cbadb60904e39a100bf2725d27fd Mon Sep 17 00:00:00 2001 From: Bernard Hamlin Date: Fri, 9 Mar 2018 12:00:56 +1300 Subject: [PATCH] Alias unit tests --- src/DataFormatter.php | 2 +- src/RestfulServer.php | 1 - tests/unit/RestfulServerTest.php | 99 ++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/DataFormatter.php b/src/DataFormatter.php index 6939137..eec05ae 100644 --- a/src/DataFormatter.php +++ b/src/DataFormatter.php @@ -366,7 +366,7 @@ abstract class DataFormatter */ public function getRealFields($className, $fields) { - $apiMapping = Config::inst()->get($className, 'api_field_mapping'); + $apiMapping = $this->getApiMapping($className); if (is_array($apiMapping) && is_array($fields)) { $mappedFields = []; foreach ($fields as $field) { diff --git a/src/RestfulServer.php b/src/RestfulServer.php index 9bc2bd7..512b99e 100644 --- a/src/RestfulServer.php +++ b/src/RestfulServer.php @@ -13,7 +13,6 @@ use SilverStripe\ORM\SS_List; use SilverStripe\Security\Member; use SilverStripe\Security\Security; use SilverStripe\CMS\Model\SiteTree; -use TractorCow\Fluent\Task\ConvertTranslatableTask\Exception; /** * Generic RESTful server, which handles webservice access to arbitrary DataObjects. diff --git a/tests/unit/RestfulServerTest.php b/tests/unit/RestfulServerTest.php index 39463ca..5ef8f7b 100644 --- a/tests/unit/RestfulServerTest.php +++ b/tests/unit/RestfulServerTest.php @@ -16,6 +16,7 @@ use SilverStripe\ORM\DataObject; use SilverStripe\Dev\SapphireTest; use SilverStripe\RestfulServer\DataFormatter\JSONDataFormatter; use Page; +use SilverStripe\Core\Config\Config; /** * @@ -112,6 +113,18 @@ class RestfulServerTest extends SapphireTest unset($_SERVER['PHP_AUTH_PW']); } + public function testGETWithFieldAlias() + { + Config::inst()->set(RestfulServerTestAuthorRating::class, 'api_field_mapping', ['rate' => 'Rating']); + $rating1 = $this->objFromFixture(RestfulServerTestAuthorRating::class, 'rating1'); + + $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); + $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID; + $response = Director::test($url, null, null, 'GET'); + $responseArr = Convert::xml2array($response->getBody()); + $this->assertEquals(3, $responseArr['rate']); + } + public function testAuthenticatedPUT() { $comment1 = $this->objFromFixture(RestfulServerTestComment::class, 'comment1'); @@ -159,6 +172,28 @@ class RestfulServerTest extends SapphireTest $this->assertContains($rating2->ID, $ratingIDs); } + public function testGETRelationshipsWithAlias() + { + // Alias do not currently work with Relationships + Config::inst()->set(RestfulServerTestAuthor::class, 'api_field_mapping', ['stars' => 'Ratings']); + $author1 = $this->objFromFixture(RestfulServerTestAuthor::class, 'author1'); + $rating1 = $this->objFromFixture(RestfulServerTestAuthorRating::class, 'rating1'); + + // @todo should be set up by fixtures, doesn't work for some reason... + $author1->Ratings()->add($rating1); + + $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class); + $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID . '?add_fields=stars'; + $response = Director::test($url, null, null, 'GET'); + $this->assertEquals(200, $response->getStatusCode()); + + $responseArr = Convert::xml2array($response->getBody()); + $xmlTagSafeClassName = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); + + $this->assertTrue(array_key_exists('Ratings', $responseArr)); + $this->assertFalse(array_key_exists('stars', $responseArr)); + } + public function testGETManyManyRelationshipsXML() { // author4 has related authors author2 and author3 @@ -394,6 +429,17 @@ class RestfulServerTest extends SapphireTest $this->assertContains('' . $rating1->Rating . '', $response->getBody()); } + public function testXMLValueFormattingWithFieldAlias() + { + Config::inst()->set(RestfulServerTestAuthorRating::class, 'api_field_mapping', ['rate' => 'Rating']); + $rating1 = $this->objFromFixture(RestfulServerTestAuthorRating::class, 'rating1'); + + $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); + $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID; + $response = Director::test($url, null, null, 'GET'); + $this->assertContains('' . $rating1->Rating . '', $response->getBody()); + } + public function testApiAccessFieldRestrictions() { $author1 = $this->objFromFixture(RestfulServerTestAuthor::class, 'author1'); @@ -499,6 +545,23 @@ class RestfulServerTest extends SapphireTest $this->assertNotEquals('haxx0red', $responseArr['WriteProtectedField']); } + public function testFieldAliasWithPUT() + { + Config::inst()->set(RestfulServerTestAuthorRating::class, 'api_field_mapping', ['rate' => 'Rating']); + $rating1 = $this->objFromFixture(RestfulServerTestAuthorRating::class, 'rating1'); + $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); + $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID; + // Test input with original fieldname + $data = array( + 'Rating' => '42', + ); + $response = Director::test($url, $data, null, 'PUT'); + // Assumption: XML is default output + $responseArr = Convert::xml2array($response->getBody()); + // should output with aliased name + $this->assertEquals(42, $responseArr['rate']); + } + public function testJSONDataFormatter() { $formatter = new JSONDataFormatter(); @@ -526,6 +589,29 @@ class RestfulServerTest extends SapphireTest ); } + public function testJSONDataFormatterWithFieldAlias() + { + Config::inst()->set(Member::class, 'api_field_mapping', ['MyName' => 'FirstName']); + $formatter = new JSONDataFormatter(); + $editor = $this->objFromFixture(Member::class, 'editor'); + $user = $this->objFromFixture(Member::class, 'user'); + + // The DataFormatter performs canView calls + // these are `Member`s so we need to be ADMIN types + $this->logInWithPermission('ADMIN'); + + $set = Member::get() + ->filter('ID', [$editor->ID, $user->ID]) + ->sort('"Email" ASC'); // for sorting for postgres + + $this->assertEquals( + '{"totalSize":null,"items":[{"MyName":"Editor","Email":"editor@test.com"},' . + '{"MyName":"User","Email":"user@test.com"}]}', + $formatter->convertDataObjectSet($set, ["FirstName", "Email"]), + "Correct JSON formatting with field alias" + ); + } + public function testApiAccessWithPOST() { $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); @@ -541,6 +627,19 @@ class RestfulServerTest extends SapphireTest $this->assertNotEquals('haxx0red', $responseArr['WriteProtectedField']); } + public function testFieldAliasWithPOST() + { + Config::inst()->set(RestfulServerTestAuthorRating::class, 'api_field_mapping', ['rate' => 'Rating']); + $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); + $url = "{$this->baseURI}/api/v1/$urlSafeClassname/"; + $data = [ + 'rate' => '42', + ]; + $response = Director::test($url, $data, null, 'POST'); + $responseArr = Convert::xml2array($response->getBody()); + $this->assertEquals(42, $responseArr['rate']); + } + public function testCanViewRespectedInList() { // Default content type