View source
<?php
namespace Drupal\Tests\rest\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\rest\Entity\RestResourceConfig;
use Drupal\rest\RestResourceConfigInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use GuzzleHttp\RequestOptions;
class ResourceTest extends BrowserTestBase {
public static $modules = [
'hal',
'rest',
'entity_test',
'rest_test',
];
protected $defaultTheme = 'stark';
protected $entity;
protected function setUp() {
parent::setUp();
$this->entity = EntityTest::create([
'name' => $this
->randomMachineName(),
'user_id' => 1,
'field_test_text' => [
0 => [
'value' => $this
->randomString(),
'format' => 'plain_text',
],
],
]);
$this->entity
->save();
Role::load(AccountInterface::ANONYMOUS_ROLE)
->grantPermission('view test entity')
->save();
}
public function testFormats() {
RestResourceConfig::create([
'id' => 'entity.entity_test',
'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
'configuration' => [
'GET' => [
'supported_auth' => [
'basic_auth',
],
],
],
])
->save();
$this
->drupalGet($this->entity
->toUrl()
->setRouteParameter('_format', 'hal_json'));
$this
->assertSession()
->statusCodeEquals(406);
}
public function testAuthentication() {
RestResourceConfig::create([
'id' => 'entity.entity_test',
'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
'configuration' => [
'GET' => [
'supported_formats' => [
'hal_json',
],
],
],
])
->save();
$this
->drupalGet($this->entity
->toUrl()
->setRouteParameter('_format', 'hal_json'));
$this
->assertSession()
->statusCodeEquals(406);
}
public function testSerializationClassIsOptional() {
RestResourceConfig::create([
'id' => 'serialization_test',
'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
'configuration' => [
'POST' => [
'supported_formats' => [
'json',
],
'supported_auth' => [
'cookie',
],
],
],
])
->save();
\Drupal::service('router.builder')
->rebuildIfNeeded();
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('restful post serialization_test')
->save();
$serialized = $this->container
->get('serializer')
->serialize([
'foo',
'bar',
], 'json');
$request_options = [
RequestOptions::HEADERS => [
'Content-Type' => 'application/json',
],
RequestOptions::BODY => $serialized,
];
$client = $this
->getSession()
->getDriver()
->getClient()
->getClient();
$response = $client
->request('POST', $this
->buildUrl('serialization_test', [
'query' => [
'_format' => 'json',
],
]), $request_options);
$this
->assertSame(200, $response
->getStatusCode());
$this
->assertSame('["foo","bar"]', (string) $response
->getBody());
}
public function testUriPaths() {
$manager = \Drupal::service('plugin.manager.rest');
foreach ($manager
->getDefinitions() as $resource => $definition) {
foreach ($definition['uri_paths'] as $key => $uri_path) {
$this
->assertStringNotContainsString('//', $uri_path, 'The resource URI path does not have duplicate slashes.');
}
}
}
}