3 namespace Drupal\Tests\user\Unit;
5 use Drupal\Core\Config\ImmutableConfig;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\user\Entity\User;
9 use Drupal\user\Plugin\rest\resource\UserRegistrationResource;
10 use Psr\Log\LoggerInterface;
11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
12 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15 * Only administrators can create user accounts.
17 if (!defined('USER_REGISTER_ADMINISTRATORS_ONLY')) {
18 define('USER_REGISTER_ADMINISTRATORS_ONLY', 'admin_only');
22 * Visitors can create their own accounts.
24 if (!defined('USER_REGISTER_VISITORS')) {
25 define('USER_REGISTER_VISITORS', 'visitors');
29 * Visitors can create accounts, but they don't become active without
30 * administrative approval.
32 if (!defined('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL')) {
33 define('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL', 'visitors_admin_approval');
37 * Tests User Registration REST resource.
39 * @coversDefaultClass \Drupal\user\Plugin\rest\resource\UserRegistrationResource
42 class UserRegistrationResourceTest extends UnitTestCase {
44 const ERROR_MESSAGE = "Unprocessable Entity: validation failed.\nproperty_path: message\nproperty_path_2: message_2\n";
49 * @var \Drupal\user\Plugin\rest\resource\UserRegistrationResource
54 * A reflection of self::$testClass.
56 * @var \ReflectionClass
58 protected $reflection;
61 * A user settings config instance.
63 * @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit_Framework_MockObject_MockObject
65 protected $userSettings;
70 * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
77 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
79 protected $currentUser;
84 protected function setUp() {
87 $this->logger = $this->prophesize(LoggerInterface::class)->reveal();
89 $this->userSettings = $this->prophesize(ImmutableConfig::class);
91 $this->currentUser = $this->prophesize(AccountInterface::class);
93 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal());
94 $this->reflection = new \ReflectionClass($this->testClass);
98 * Tests that an exception is thrown when no data provided for the account.
100 public function testEmptyPost() {
101 $this->setExpectedException(BadRequestHttpException::class);
102 $this->testClass->post(NULL);
106 * Tests that only new user accounts can be registered.
108 public function testExistedEntityPost() {
109 $entity = $this->prophesize(User::class);
110 $entity->isNew()->willReturn(FALSE);
111 $this->setExpectedException(BadRequestHttpException::class);
113 $this->testClass->post($entity->reveal());
117 * Tests that admin permissions are required to register a user account.
119 public function testRegistrationAdminOnlyPost() {
121 $this->userSettings->get('register')->willReturn(USER_REGISTER_ADMINISTRATORS_ONLY);
123 $this->currentUser->isAnonymous()->willReturn(TRUE);
125 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal());
127 $entity = $this->prophesize(User::class);
128 $entity->isNew()->willReturn(TRUE);
130 $this->setExpectedException(AccessDeniedHttpException::class);
132 $this->testClass->post($entity->reveal());
136 * Tests that only anonymous users can register users.
138 public function testRegistrationAnonymousOnlyPost() {
139 $this->currentUser->isAnonymous()->willReturn(FALSE);
141 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal());
143 $entity = $this->prophesize(User::class);
144 $entity->isNew()->willReturn(TRUE);
146 $this->setExpectedException(AccessDeniedHttpException::class);
148 $this->testClass->post($entity->reveal());