diff --git a/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php b/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php index 9836a5e..1d56933 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php @@ -143,7 +143,7 @@ public function post(User $account = NULL) { } }); - $response = new ResourceResponse(NULL, 201); + $response = new ResourceResponse(NULL, 201, ['Location' => 'user/' . $account->id()]); if (!$context->isEmpty()) { $response->addCacheableDependency($context->pop()); } diff --git a/core/modules/rest/src/Tests/RegisterUserTest.php b/core/modules/rest/src/Tests/RegisterUserTest.php new file mode 100644 index 0000000..8ca10b5 --- /dev/null +++ b/core/modules/rest/src/Tests/RegisterUserTest.php @@ -0,0 +1,109 @@ +config('rest.settings'); + $settings = array(); + $resources = [ + ['type' => 'rest_user_registration', 'method' => 'POST'], + ['type' => 'entity:user', 'method' => 'GET'] + ]; + $format = 'hal_json'; + $auth = $this->defaultAuth; + foreach ($resources as $resource) { + $settings[$resource['type']][$resource['method']]['supported_formats'][] = $format; + $settings[$resource['type']][$resource['method']]['supported_auth'] = $auth; + $config->set('resources', $settings); + $config->save(); + } + $this->rebuildCache(); + + // Permissions for the user registration. + $permissions[] = 'restful post rest_user_registration'; + // Permissions to GET the new user. + $permissions[] = 'restful get entity:user'; + + $account = $this->drupalCreateUser($permissions); + $this->drupalLogin($account); + + } + + /** + * Tests user registration from REST. + */ + protected function testRegisterUser() { + + $this->enableUserRegistrationConfiguration(); + // New user info to be serialized. + $data = [ + "_links" => + [ + "type" => ["href" => $GLOBALS['base_url'] . "/rest/type/user/user"] + ], + "langcode" => [ + [ + "value" => "en" + ] + ], + "name" => [ + [ + "value" => "Druplicon" + ] + ], + "mail" => [ + [ + "value" => "druplicon@superpoweredbydrupal.com" + ] + ], + "pass" => [ + [ + "value" => "SuperSecretPassword" + ] + ] + ]; + + // Create a JSON version for the user entity we want to create. + $serialized = \Drupal::service('serializer')->serialize($data, 'hal_json'); + + // Post to the REST service to register the user. + $this->httpRequest('entity/user/register', 'POST', $serialized, 'application/hal+json'); + $this->assertResponse('201', 'HTTP response code is correct.'); + + // Obtain the uid from the header. + $url_parts = explode('/', $this->drupalGetHeader('location')); + $id = end($url_parts); + $this->assertHeader('Location', 'user/' . $id); + + // Log in as admin and verify that the new user exists. + $this->drupalLogin($this->rootUser); + $this->httpRequest('user/' . $id, 'GET'); + } + +} diff --git a/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php b/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php index 2c1d2fc..c84c85c 100644 --- a/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php +++ b/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php @@ -27,7 +27,7 @@ define('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL', 'visitors_admin_approval'); /** - * Tests the REST export view plugin. + * Tests User Registration REST resource. * * @group rest */