diff --git a/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php b/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php index 25b895f..51bb3d4 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php @@ -20,7 +20,10 @@ * @RestResource( * id = "rest_user_registration", * label = @Translation("User Registration"), - * serialization_class = "Drupal\user\Entity\User" + * serialization_class = "Drupal\user\Entity\User", + * uri_paths = { + * "https://www.drupal.org/link-relations/create" = "/user/register", + * }, * ) */ class UserRegistrationResource extends ResourceBase { @@ -105,6 +108,7 @@ public function post(UserInterface $account = NULL) { throw new AccessDeniedHttpException('Only anonymous users can register users.'); } $approvalSettings = $this->userSettings->get('register'); + // Verify that the current user can register a user account. if ($approvalSettings == USER_REGISTER_ADMINISTRATORS_ONLY) { throw new AccessDeniedHttpException('Only administrators can register users.'); @@ -148,7 +152,7 @@ public function post(UserInterface $account = NULL) { _user_mail_notify('register_pending_approval', $account); } - $url = $account->urlInfo('canonical', ['absolute' => TRUE])->toString(TRUE); + $url = $account->toUrl('canonical', ['absolute' => TRUE])->toString(TRUE); $response = new ModifiedResourceResponse(NULL, 201, ['Location' => $url->getGeneratedUrl()]); return $response; @@ -160,7 +164,7 @@ public function post(UserInterface $account = NULL) { public function routes() { $collection = new RouteCollection(); - $route = $this->getBaseRoute('/entity/user/register', 'POST'); + $route = $this->getBaseRoute('/user/register', 'POST'); // Restrict the incoming HTTP Content-type header to the known serialization // formats. @@ -186,7 +190,7 @@ protected function validate(UserInterface $entity) { // changes. $violations->filterByFieldAccess(); - if (count($violations) > 0) { + if ($violations->count() > 0) { $message = "Unprocessable Entity: validation failed.\n"; foreach ($violations as $violation) { $message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\n"; diff --git a/core/modules/rest/src/Tests/RegisterUserTest.php b/core/modules/rest/src/Tests/RegisterUserTest.php index d2b5c4a..bda801c 100644 --- a/core/modules/rest/src/Tests/RegisterUserTest.php +++ b/core/modules/rest/src/Tests/RegisterUserTest.php @@ -5,7 +5,6 @@ use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; - /** * Tests User Registration. * @@ -51,7 +50,7 @@ public function setUp() { /** * Tests user registration from REST. */ - protected function testRegisterUser() { + public function testRegisterUser() { // New user info to be serialized. $data = [ diff --git a/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php b/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php index 28cfcbe..93fdfa1 100644 --- a/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php +++ b/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php @@ -85,20 +85,14 @@ class UserRegistrationResourceTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $this->logger = $this->getMock(LoggerInterface::class); + $this->logger = $this->prophesize(LoggerInterface::class)->reveal(); - $this->userSettings = $this->getMockBuilder(ImmutableConfig::class) - ->setMethods(['get']) - ->disableOriginalConstructor() - ->getMock(); + $this->userSettings = $this->prophesize(ImmutableConfig::class); - $this->currentUser = $this->getMockBuilder(AccountInterface::class) - ->disableOriginalConstructor() - ->getMock(); + $this->currentUser = $this->prophesize(AccountInterface::class); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); $this->reflection = new \ReflectionClass($this->testClass); - } /** @@ -120,27 +114,17 @@ public function getProtectedMethod($method) { * Tests that the user entity does not violate any validation constraints. */ public function testValidate() { - $violations = $this->getMockBuilder(EntityConstraintViolationList::class) - ->setMethods(['filterByFieldAccess']) - ->disableOriginalConstructor() - ->getMock(); + $violations = $this->prophesize(EntityConstraintViolationList::class); + $violations->filterByFieldAccess()->willReturn([]); + $violations->count()->willReturn(0); - $violations->expects($this->once()) - ->method('filterByFieldAccess') - ->will($this->returnValue(array())); - - $entity = $this->getMockBuilder(User::class) - ->disableOriginalConstructor() - ->getMock(); - - $entity->expects($this->once()) - ->method('validate') - ->will($this->returnValue($violations)); + $entity = $this->prophesize(User::class); + $entity->validate()->willReturn($violations->reveal()); $method = $this->getProtectedMethod('validate'); // No exception is thrown. - $method->invokeArgs($this->testClass, [$entity]); + $method->invokeArgs($this->testClass, [$entity->reveal()]); } /** @@ -150,32 +134,18 @@ public function testValidate() { * @expectedException UserRegistrationResourceTest::ERROR_MESSAGE */ public function testFailedValidate() { - $violation1 = $this->getMock(ConstraintViolationInterface::class); - - $violation1->expects($this->once()) - ->method('getPropertyPath') - ->will($this->returnValue('property_path')); + $violation1 = $this->prophesize(ConstraintViolationInterface::class); + $violation1->getPropertyPath()->willReturn('property_path'); + $violation1->getMessage()->willReturn('message'); - $violation1->expects($this->once()) - ->method('getMessage') - ->will($this->returnValue('message')); + $violation2 = $this->prophesize(ConstraintViolationInterface::class); + $violation2->getPropertyPath()->willReturn('property_path'); + $violation2->getMessage()->willReturn('message'); - $violation2 = $this->getMock(ConstraintViolationInterface::class); - - $violation2->expects($this->once()) - ->method('getPropertyPath') - ->will($this->returnValue('property_path_2')); - - $violation2->expects($this->once()) - ->method('getMessage') - ->will($this->returnValue('message_2')); - - $entity = $this->getMockBuilder(User::class) - ->disableOriginalConstructor() - ->getMock(); + $entity = $this->prophesize(User::class); $violations = $this->getMockBuilder(EntityConstraintViolationList::class) - ->setConstructorArgs([$entity, [$violation1, $violation2]]) + ->setConstructorArgs([$entity->reveal(), [$violation1->reveal(), $violation2->reveal()]]) ->setMethods(['filterByFieldAccess']) ->getMock(); @@ -183,13 +153,11 @@ public function testFailedValidate() { ->method('filterByFieldAccess') ->will($this->returnValue([])); - $entity->expects($this->once()) - ->method('validate') - ->will($this->returnValue($violations)); + $entity->validate()->willReturn($violations); $method = $this->getProtectedMethod('validate'); - $method->invoke($this->testClass, $entity); + $method->invoke($this->testClass, $entity->reveal()); } /** @@ -209,15 +177,10 @@ public function testEmptyPost() { * @expectedExceptionMessage An ID has been set and only new user accounts can be registered. */ public function testExistedEntityPost() { - $entity = $this->getMockBuilder(User::class) - ->disableOriginalConstructor() - ->getMock(); + $entity = $this->prophesize(User::class); + $entity->isNew()->willReturn(FALSE); - $entity->expects($this->once()) - ->method('isNew') - ->will($this->returnValue(FALSE)); - - $this->testClass->post($entity); + $this->testClass->post($entity->reveal()); } /** @@ -227,24 +190,17 @@ public function testExistedEntityPost() { * @expectedExceptionMessage Only administrators can register users. */ public function testRegistrationAdminOnlyPost() { - $this->currentUser->expects($this->once()) - ->method('isAnonymous') - ->will($this->returnValue(TRUE)); - $this->userSettings->expects($this->once()) - ->method('get') - ->will($this->returnValue(USER_REGISTER_ADMINISTRATORS_ONLY)); + $this->userSettings->get('register')->willReturn(USER_REGISTER_ADMINISTRATORS_ONLY); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); - $entity = $this->getMockBuilder(User::class) - ->disableOriginalConstructor() - ->getMock(); + $this->currentUser->isAnonymous()->willReturn(TRUE); - $entity->expects($this->once()) - ->method('isNew') - ->will($this->returnValue(TRUE)); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); - $this->testClass->post($entity); + $entity = $this->prophesize(User::class); + $entity->isNew()->willReturn(TRUE); + + $this->testClass->post($entity->reveal()); } /** @@ -254,21 +210,14 @@ public function testRegistrationAdminOnlyPost() { * @expectedExceptionMessage Only anonymous users can register users. */ public function testRegistrationAnonymousOnlyPost() { - $this->currentUser->expects($this->once()) - ->method('isAnonymous') - ->will($this->returnValue(FALSE)); + $this->currentUser->isAnonymous()->willReturn(FALSE); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); - $entity = $this->getMockBuilder(User::class) - ->disableOriginalConstructor() - ->getMock(); + $entity = $this->prophesize(User::class); + $entity->isNew()->willReturn(TRUE); - $entity->expects($this->once()) - ->method('isNew') - ->will($this->returnValue(TRUE)); - - $this->testClass->post($entity); + $this->testClass->post($entity->reveal()); } /** @@ -278,11 +227,9 @@ public function testRegistrationAnonymousOnlyPost() { * @expectedExceptionMessage Access denied on creating field 'test_field'. */ public function testFieldAccessValidation() { - $this->currentUser->expects($this->once()) - ->method('isAnonymous') - ->will($this->returnValue(TRUE)); + $this->currentUser->isAnonymous()->willReturn(TRUE); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor()