mirror of
https://github.com/silverstripe/silverstripe-restfulserver
synced 2024-10-22 14:05:58 +02:00
Merge pull request #96 from creative-commoners/pulls/2/sapphire-test-nine
API phpunit 9 support
This commit is contained in:
commit
e84a66dad3
@ -19,10 +19,11 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"silverstripe/framework": "^4"
|
"php": "^7.3 || ^8.0",
|
||||||
|
"silverstripe/framework": "^4.10"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"sminnee/phpunit": "^5.7",
|
"phpunit/phpunit": "^9.5",
|
||||||
"squizlabs/php_codesniffer": "^3.0",
|
"squizlabs/php_codesniffer": "^3.0",
|
||||||
"silverstripe/versioned": "^1"
|
"silverstripe/versioned": "^1"
|
||||||
},
|
},
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
|
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
|
||||||
|
<testsuites>
|
||||||
<testsuite name="Default">
|
<testsuite name="Default">
|
||||||
<directory>tests/</directory>
|
<directory>tests/</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||||
<directory suffix=".php">src/</directory>
|
<directory suffix=".php">src/</directory>
|
||||||
|
@ -27,20 +27,24 @@ class JSONDataFormatterTest extends SapphireTest
|
|||||||
$formatter = new JSONDataFormatter();
|
$formatter = new JSONDataFormatter();
|
||||||
$parent = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'parent');
|
$parent = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'parent');
|
||||||
$json = $formatter->convertDataObject($parent);
|
$json = $formatter->convertDataObject($parent);
|
||||||
$this->assertRegexp('/"ID":\d+/', $json, 'PK casted to integer');
|
$this->assertMatchesRegularExpression('/"ID":\d+/', $json, 'PK casted to integer');
|
||||||
$this->assertRegexp('/"Created":"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"/', $json, 'Datetime casted to string');
|
$this->assertMatchesRegularExpression(
|
||||||
$this->assertContains('"Name":"Parent"', $json, 'String casted to string');
|
'/"Created":"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"/',
|
||||||
$this->assertContains('"Active":true', $json, 'Boolean casted to boolean');
|
$json,
|
||||||
$this->assertContains('"Sort":17', $json, 'Integer casted to integer');
|
'Datetime casted to string'
|
||||||
$this->assertContains('"Average":1.2345', $json, 'Float casted to float');
|
);
|
||||||
$this->assertContains('"ParentID":0', $json, 'Empty FK is 0');
|
$this->assertStringContainsString('"Name":"Parent"', $json, 'String casted to string');
|
||||||
|
$this->assertStringContainsString('"Active":true', $json, 'Boolean casted to boolean');
|
||||||
|
$this->assertStringContainsString('"Sort":17', $json, 'Integer casted to integer');
|
||||||
|
$this->assertStringContainsString('"Average":1.2345', $json, 'Float casted to float');
|
||||||
|
$this->assertStringContainsString('"ParentID":0', $json, 'Empty FK is 0');
|
||||||
|
|
||||||
$child3 = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'child3');
|
$child3 = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'child3');
|
||||||
$json = $formatter->convertDataObject($child3);
|
$json = $formatter->convertDataObject($child3);
|
||||||
$this->assertContains('"Name":null', $json, 'Empty string is null');
|
$this->assertStringContainsString('"Name":null', $json, 'Empty string is null');
|
||||||
$this->assertContains('"Active":false', $json, 'Empty boolean is false');
|
$this->assertStringContainsString('"Active":false', $json, 'Empty boolean is false');
|
||||||
$this->assertContains('"Sort":0', $json, 'Empty integer is 0');
|
$this->assertStringContainsString('"Sort":0', $json, 'Empty integer is 0');
|
||||||
$this->assertContains('"Average":0', $json, 'Empty float is 0');
|
$this->assertStringContainsString('"Average":0', $json, 'Empty float is 0');
|
||||||
$this->assertRegexp('/"ParentID":\d+/', $json, 'FK casted to integer');
|
$this->assertMatchesRegularExpression('/"ParentID":\d+/', $json, 'FK casted to integer');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ class RestfulServerTest extends SapphireTest
|
|||||||
return str_replace('\\', '-', $classname);
|
return str_replace('\\', '-', $classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
Director::config()->set('alternate_base_url', $this->baseURI);
|
Director::config()->set('alternate_base_url', $this->baseURI);
|
||||||
@ -88,11 +88,11 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestComment::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestComment::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $comment1->ID;
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $comment1->ID;
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertContains('<ID>', $response->getBody());
|
$this->assertStringContainsString('<ID>', $response->getBody());
|
||||||
$this->assertContains('<Name>', $response->getBody());
|
$this->assertStringContainsString('<Name>', $response->getBody());
|
||||||
$this->assertContains('<Comment>', $response->getBody());
|
$this->assertStringContainsString('<Comment>', $response->getBody());
|
||||||
$this->assertContains('<Page', $response->getBody());
|
$this->assertStringContainsString('<Page', $response->getBody());
|
||||||
$this->assertContains('<Author', $response->getBody());
|
$this->assertStringContainsString('<Author', $response->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAuthenticatedGET()
|
public function testAuthenticatedGET()
|
||||||
@ -430,8 +430,8 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID;
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID;
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertContains('<ID>' . $rating1->ID . '</ID>', $response->getBody());
|
$this->assertStringContainsString('<ID>' . $rating1->ID . '</ID>', $response->getBody());
|
||||||
$this->assertContains('<Rating>' . $rating1->Rating . '</Rating>', $response->getBody());
|
$this->assertStringContainsString('<Rating>' . $rating1->Rating . '</Rating>', $response->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testXMLValueFormattingWithFieldAlias()
|
public function testXMLValueFormattingWithFieldAlias()
|
||||||
@ -442,7 +442,7 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID;
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID;
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertContains('<rate>' . $rating1->Rating . '</rate>', $response->getBody());
|
$this->assertStringContainsString('<rate>' . $rating1->Rating . '</rate>', $response->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testApiAccessFieldRestrictions()
|
public function testApiAccessFieldRestrictions()
|
||||||
@ -453,21 +453,21 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID;
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID;
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertContains('<ID>', $response->getBody());
|
$this->assertStringContainsString('<ID>', $response->getBody());
|
||||||
$this->assertContains('<Rating>', $response->getBody());
|
$this->assertStringContainsString('<Rating>', $response->getBody());
|
||||||
$this->assertContains('<Author', $response->getBody());
|
$this->assertStringContainsString('<Author', $response->getBody());
|
||||||
$this->assertNotContains('<SecretField>', $response->getBody());
|
$this->assertStringNotContainsString('<SecretField>', $response->getBody());
|
||||||
$this->assertNotContains('<SecretRelation>', $response->getBody());
|
$this->assertStringNotContainsString('<SecretRelation>', $response->getBody());
|
||||||
|
|
||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID . '?add_fields=SecretField,SecretRelation';
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID . '?add_fields=SecretField,SecretRelation';
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertNotContains(
|
$this->assertStringNotContainsString(
|
||||||
'<SecretField>',
|
'<SecretField>',
|
||||||
$response->getBody(),
|
$response->getBody(),
|
||||||
'"add_fields" URL parameter filters out disallowed fields from $api_access'
|
'"add_fields" URL parameter filters out disallowed fields from $api_access'
|
||||||
);
|
);
|
||||||
$this->assertNotContains(
|
$this->assertStringNotContainsString(
|
||||||
'<SecretRelation>',
|
'<SecretRelation>',
|
||||||
$response->getBody(),
|
$response->getBody(),
|
||||||
'"add_fields" URL parameter filters out disallowed relations from $api_access'
|
'"add_fields" URL parameter filters out disallowed relations from $api_access'
|
||||||
@ -476,12 +476,12 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID . '?fields=SecretField,SecretRelation';
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID . '?fields=SecretField,SecretRelation';
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertNotContains(
|
$this->assertStringNotContainsString(
|
||||||
'<SecretField>',
|
'<SecretField>',
|
||||||
$response->getBody(),
|
$response->getBody(),
|
||||||
'"fields" URL parameter filters out disallowed fields from $api_access'
|
'"fields" URL parameter filters out disallowed fields from $api_access'
|
||||||
);
|
);
|
||||||
$this->assertNotContains(
|
$this->assertStringNotContainsString(
|
||||||
'<SecretRelation>',
|
'<SecretRelation>',
|
||||||
$response->getBody(),
|
$response->getBody(),
|
||||||
'"fields" URL parameter filters out disallowed relations from $api_access'
|
'"fields" URL parameter filters out disallowed relations from $api_access'
|
||||||
@ -490,12 +490,12 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID . '/Ratings';
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID . '/Ratings';
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertContains(
|
$this->assertStringContainsString(
|
||||||
'<Rating>',
|
'<Rating>',
|
||||||
$response->getBody(),
|
$response->getBody(),
|
||||||
'Relation viewer shows fields allowed through $api_access'
|
'Relation viewer shows fields allowed through $api_access'
|
||||||
);
|
);
|
||||||
$this->assertNotContains(
|
$this->assertStringNotContainsString(
|
||||||
'<SecretField>',
|
'<SecretField>',
|
||||||
$response->getBody(),
|
$response->getBody(),
|
||||||
'Relation viewer on has-many filters out disallowed fields from $api_access'
|
'Relation viewer on has-many filters out disallowed fields from $api_access'
|
||||||
@ -509,8 +509,16 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class);
|
$urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class);
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID;
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID;
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertNotContains('<RelatedPages', $response->getBody(), 'Restricts many-many with api_access=false');
|
$this->assertStringNotContainsString(
|
||||||
$this->assertNotContains('<PublishedPages', $response->getBody(), 'Restricts has-many with api_access=false');
|
'<RelatedPages',
|
||||||
|
$response->getBody(),
|
||||||
|
'Restricts many-many with api_access=false'
|
||||||
|
);
|
||||||
|
$this->assertStringNotContainsString(
|
||||||
|
'<PublishedPages',
|
||||||
|
$response->getBody(),
|
||||||
|
'Restricts has-many with api_access=false'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testApiAccessRelationRestrictionsOnEndpoint()
|
public function testApiAccessRelationRestrictionsOnEndpoint()
|
||||||
@ -695,13 +703,13 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/";
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/";
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$this->assertNotContains('Unspeakable', $response->getBody());
|
$this->assertStringNotContainsString('Unspeakable', $response->getBody());
|
||||||
|
|
||||||
// JSON content type
|
// JSON content type
|
||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname.json";
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname.json";
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$this->assertNotContains('Unspeakable', $response->getBody());
|
$this->assertStringNotContainsString('Unspeakable', $response->getBody());
|
||||||
$responseArray = json_decode($response->getBody(), true);
|
$responseArray = json_decode($response->getBody(), true);
|
||||||
$this->assertSame(0, $responseArray['totalSize']);
|
$this->assertSame(0, $responseArray['totalSize']);
|
||||||
|
|
||||||
@ -712,7 +720,7 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/";
|
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/";
|
||||||
$response = Director::test($url, null, null, 'GET');
|
$response = Director::test($url, null, null, 'GET');
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$this->assertContains('Unspeakable', $response->getBody());
|
$this->assertStringContainsString('Unspeakable', $response->getBody());
|
||||||
// Assumption: default formatter is XML
|
// Assumption: default formatter is XML
|
||||||
$responseArray = Convert::xml2array($response->getBody());
|
$responseArray = Convert::xml2array($response->getBody());
|
||||||
$this->assertEquals(1, $responseArray['@attributes']['totalSize']);
|
$this->assertEquals(1, $responseArray['@attributes']['totalSize']);
|
||||||
|
Loading…
Reference in New Issue
Block a user