diff --git a/core/modules/rest/lib/Drupal/rest/Tests/NodeTest.php b/core/modules/rest/lib/Drupal/rest/Tests/NodeTest.php index 8e3644c..1e02c70 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/NodeTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/NodeTest.php @@ -29,23 +29,48 @@ public static function getInfo() { ); } - public function setUp() { - parent::setUp(); - $this->enableService('entity:node', 'GET'); - $permissions = $this->entityPermissions('node', 'view'); - $permissions[] = 'restful get entity:node'; + /** + * Enables node specific REST API configuration and authentication. + * + * @param string $method + * The HTTP method to be tested. + * @param string $operation + * The operation, one of 'view', 'create', 'update' or 'delete'. + */ + protected function enableNodeConfiguration($method, $operation) { + $this->enableService('entity:node', $method); + $permissions = $this->entityPermissions('node', $operation); + $permissions[] = 'restful ' . strtolower($method) . ' entity:node'; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); } /** - * Tests that the node resource works with comment module enabled. + * Performs various tests on nodes and their REST API. */ - public function testWithCommentModule() { + public function testNodes() { + // Tests that the node resource works with comment module enabled. \Drupal::moduleHandler()->enable(array('comment')); + $this->enableNodeConfiguration('GET', 'view'); + $node = $this->entityCreate('node'); $node->save(); $this->httpRequest('entity/node/' . $node->id(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200); + + // Check that a simple PATCH update to the node title works as expected. + $this->enableNodeConfiguration('PATCH', 'update'); + + // Create a PATCH request body that only updates the title field. + $new_title = $this->randomString(); + $serialized = '{"_links":{"type":{"href":"' + . url('rest/type/node/resttest', array('absolute' => TRUE)) + . '"}},"title":[{"value":"' . $new_title . '"}]}'; + $this->httpRequest('entity/node/' . $node->id(), 'PATCH', $serialized, $this->defaultMimeType); + $this->assertResponse(204); + + // Reload the node from the DB and check if the title was correctly updated. + $node = entity_load('node', $node->id(), TRUE); + $this->assertEqual($node->get('title')->get('value')->getValue(), $new_title); } } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index 66c7515..24352fc 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -278,6 +278,8 @@ protected function entityPermissions($entity_type, $operation) { return array('access content'); case 'create': return array('create resttest content'); + case 'update': + return array('edit any resttest content'); case 'delete': return array('delete any resttest content'); }