diff --git a/core/modules/rest/src/Tests/NodeTest.php b/core/modules/rest/src/Tests/NodeTest.php index ccbee60..02b5ae9 100644 --- a/core/modules/rest/src/Tests/NodeTest.php +++ b/core/modules/rest/src/Tests/NodeTest.php @@ -42,6 +42,28 @@ protected function enableNodeConfiguration($method, $operation) { $this->drupalLogin($account); } + protected function postNode($data) { + $this->enableNodeConfiguration('POST', 'create'); + $this->enableService('entity:node', 'POST', 'json'); + + // Create a JSON version of a simple node with the title. + $serialized = $this->container->get('serializer')->serialize($data, 'json'); + + // Post to the REST service to create the node. + return $this->httpRequest('/entity/node', 'POST', $serialized, 'application/json'); + } + + protected function assertNodeTitleMatch($data) { + /** @var \Drupal\node\Entity\Node $node */ + // Load the newly created node. + $node = Node::load(1); + + // Test that the title is the same as what we posted. + $this->assertEqual($node->title->value, $data['title'][0]['value']); + + return $node; + } + /** * Performs various tests on nodes and their REST API. */ @@ -95,35 +117,75 @@ public function testNodes() { * Test creating a node using json serialization. */ public function testCreate() { - $this->enableNodeConfiguration('POST', 'create'); - $this->enableService('entity:node', 'POST', 'json'); - - // Create a title. - $title = $this->randomString(); - // Data to be used for serialization. $data = [ 'type' => [['target_id' => 'resttest']], - 'title' => [['value' => $title ]], + 'title' => [['value' => $this->randomString() ]], ]; - // Create a JSON version of a simple node with the title. - $serialized = $this->container->get('serializer')->serialize($data, 'json'); - - // Post to the REST service to create the node. - $this->httpRequest('/entity/node', 'POST', $serialized, 'application/json'); + $this->postNode($data); // Make sure the response is "CREATED". $this->assertResponse(201); - /** @var \Drupal\node\Entity\Node $node */ - // Load the newly created node. - $node = Node::load(1); - - // Test that the title is the same as what we posted. - $this->assertEqual($node->title->value, $title); + // Make sure the node was created and the title matches. + $node = $this->assertNodeTitleMatch($data); // Make sure the request returned a redirect header to view the node. $this->assertHeader('Location', $node->url('canonical', ['absolute' => TRUE])); } + + /** + * Test bundle normalization when posting bundle as a simple string. + */ + public function testBundleNormalization() { + // Data to be used for serialization. + $data = [ + 'type' => 'resttest', + 'title' => [['value' => $this->randomString() ]], + ]; + + // testing + $this->postNode($data); + + // Make sure the response is "CREATED". + $this->assertResponse(201); + + // Make sure the node was created and the title matches. + $this->assertNodeTitleMatch($data); + } + + /** + * Test bundle normalization when posting using a simple string. + */ + public function testInvalidBundle() { + // Data to be used for serialization. + $data = [ + 'type' => 'bad_bundle_name', + 'title' => [['value' => $this->randomString() ]], + ]; + + $this->postNode($data); + + // Make sure the response is "Bad Request". + $this->assertResponse(400); + $this->assertResponseBody('{"error":"\"bad_bundle_name\" is not a valid bundle type for denormalization."}'); + } + + /** + * Test when the bundle is missing. + */ + public function testMissingBundle() { + // Data to be used for serialization. + $data = [ + 'title' => [['value' => $this->randomString() ]], + ]; + + // testing + $this->postNode($data); + + // Make sure the response is "Bad Request". + $this->assertResponse(400); + $this->assertResponseBody('{"error":"\"\" is not a valid bundle type for denormalization."}'); + } } diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index 46003af..b75a02a 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -7,11 +7,9 @@ namespace Drupal\rest\Tests; -use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Url; +use Drupal\Component\Utility\SafeMarkup; use Drupal\node\NodeInterface; use Drupal\simpletest\WebTestBase; -use Drupal\user\UserInterface; /** * Test helper class that provides a REST client method to send HTTP requests. @@ -46,6 +44,14 @@ */ protected $defaultAuth; + + /** + * The raw response body from http request operations. + * + * @var array + */ + protected $responseBody; + /** * Modules to install. * @@ -153,7 +159,7 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { break; } - $response = $this->curlExec($curl_options); + $this->responseBody = $this->curlExec($curl_options); // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); @@ -163,9 +169,9 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { $this->verbose($method . ' request to: ' . $url . '