Commit ef16fa1c authored by catch's avatar catch

Issue #2775553 by Wim Leers, naveenvalecha: Convert web tests to browser tests for REST module

parent 42c492cd
<?php <?php
namespace Drupal\rest\Tests; namespace Drupal\Tests\rest\Functional;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\rest\Entity\RestResourceConfig;
use Drupal\rest\RestResourceConfigInterface; use Drupal\rest\RestResourceConfigInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface; use Drupal\user\RoleInterface;
use GuzzleHttp\RequestOptions;
/** /**
* Tests the structure of a REST resource. * Tests the structure of a REST resource.
* *
* @group rest * @group rest
*/ */
class ResourceTest extends RESTTestBase { class ResourceTest extends BrowserTestBase {
/** /**
* Modules to install. * Modules to install.
...@@ -34,7 +38,14 @@ class ResourceTest extends RESTTestBase { ...@@ -34,7 +38,14 @@ class ResourceTest extends RESTTestBase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
// Create an entity programmatic. // Create an entity programmatic.
$this->entity = $this->entityCreate('entity_test'); $this->entity = EntityTest::create([
'name' => $this->randomMachineName(),
'user_id' => 1,
'field_test_text' => [0 => [
'value' => $this->randomString(),
'format' => 'plain_text',
]],
]);
$this->entity->save(); $this->entity->save();
Role::load(AccountInterface::ANONYMOUS_ROLE) Role::load(AccountInterface::ANONYMOUS_ROLE)
...@@ -46,7 +57,7 @@ protected function setUp() { ...@@ -46,7 +57,7 @@ protected function setUp() {
* Tests that a resource without formats cannot be enabled. * Tests that a resource without formats cannot be enabled.
*/ */
public function testFormats() { public function testFormats() {
$this->resourceConfigStorage->create([ RestResourceConfig::create([
'id' => 'entity.entity_test', 'id' => 'entity.entity_test',
'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
'configuration' => [ 'configuration' => [
...@@ -59,21 +70,20 @@ public function testFormats() { ...@@ -59,21 +70,20 @@ public function testFormats() {
])->save(); ])->save();
// Verify that accessing the resource returns 406. // Verify that accessing the resource returns 406.
$response = $this->httpRequest($this->entity->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET'); $this->drupalGet($this->entity->urlInfo()->setRouteParameter('_format', 'hal_json'));
// \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical, // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
// non-REST route a match, but a lower quality one: no format restrictions // non-REST route a match, but a lower quality one: no format restrictions
// means there's always a match and hence when there is no matching REST // means there's always a match and hence when there is no matching REST
// route, the non-REST route is used, but can't render into // route, the non-REST route is used, but can't render into
// application/hal+json, so it returns a 406. // application/hal+json, so it returns a 406.
$this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.'); $this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
$this->curlClose();
} }
/** /**
* Tests that a resource without authentication cannot be enabled. * Tests that a resource without authentication cannot be enabled.
*/ */
public function testAuthentication() { public function testAuthentication() {
$this->resourceConfigStorage->create([ RestResourceConfig::create([
'id' => 'entity.entity_test', 'id' => 'entity.entity_test',
'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
'configuration' => [ 'configuration' => [
...@@ -86,37 +96,55 @@ public function testAuthentication() { ...@@ -86,37 +96,55 @@ public function testAuthentication() {
])->save(); ])->save();
// Verify that accessing the resource returns 401. // Verify that accessing the resource returns 401.
$response = $this->httpRequest($this->entity->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET'); $this->drupalGet($this->entity->urlInfo()->setRouteParameter('_format', 'hal_json'));
// \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical, // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
// non-REST route a match, but a lower quality one: no format restrictions // non-REST route a match, but a lower quality one: no format restrictions
// means there's always a match and hence when there is no matching REST // means there's always a match and hence when there is no matching REST
// route, the non-REST route is used, but can't render into // route, the non-REST route is used, but can't render into
// application/hal+json, so it returns a 406. // application/hal+json, so it returns a 406.
$this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.'); $this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
$this->curlClose();
} }
/** /**
* Tests that serialization_class is optional. * Tests that serialization_class is optional.
*/ */
public function testSerializationClassIsOptional() { public function testSerializationClassIsOptional() {
$this->enableService('serialization_test', 'POST', 'json'); 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) Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('restful post serialization_test') ->grantPermission('restful post serialization_test')
->save(); ->save();
$serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json'); $serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json');
$this->httpRequest('serialization_test', 'POST', $serialized, 'application/json'); $request_options = [
$this->assertResponse(200); RequestOptions::HEADERS => ['Content-Type' => 'application/json'],
$this->assertResponseBody('["foo","bar"]'); RequestOptions::BODY => $serialized,
];
/** @var \GuzzleHttp\ClientInterface $client */
$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());
} }
/** /**
* Tests that resource URI paths are formatted properly. * Tests that resource URI paths are formatted properly.
*/ */
public function testUriPaths() { public function testUriPaths() {
$this->enableService('entity:entity_test');
/** @var \Drupal\rest\Plugin\Type\ResourcePluginManager $manager */ /** @var \Drupal\rest\Plugin\Type\ResourcePluginManager $manager */
$manager = \Drupal::service('plugin.manager.rest'); $manager = \Drupal::service('plugin.manager.rest');
......
<?php <?php
namespace Drupal\rest\Tests\Views; namespace Drupal\Tests\rest\Functional\Views;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\views\Tests\ViewTestBase; use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData; use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views; use Drupal\views\Views;
...@@ -43,8 +43,8 @@ class ExcludedFieldTokenTest extends ViewTestBase { ...@@ -43,8 +43,8 @@ class ExcludedFieldTokenTest extends ViewTestBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function setUp() { protected function setUp($import_test_views = TRUE) {
parent::setUp(); parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), ['rest_test_views']); ViewTestData::createTestViews(get_class($this), ['rest_test_views']);
...@@ -66,7 +66,7 @@ protected function setUp() { ...@@ -66,7 +66,7 @@ protected function setUp() {
* Tests the display of an excluded title field when used as a token. * Tests the display of an excluded title field when used as a token.
*/ */
public function testExcludedTitleTokenDisplay() { public function testExcludedTitleTokenDisplay() {
$actual_json = $this->drupalGetWithFormat($this->view->getPath(), 'json'); $actual_json = $this->drupalGet($this->view->getPath(), ['query' => ['_format' => 'json']]);
$this->assertResponse(200); $this->assertResponse(200);
$expected = [ $expected = [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment