Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\user\Unit; | |
4 | |
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; | |
13 | |
14 /** | |
15 * Only administrators can create user accounts. | |
16 */ | |
17 if (!defined('USER_REGISTER_ADMINISTRATORS_ONLY')) { | |
18 define('USER_REGISTER_ADMINISTRATORS_ONLY', 'admin_only'); | |
19 } | |
20 | |
21 /** | |
22 * Visitors can create their own accounts. | |
23 */ | |
24 if (!defined('USER_REGISTER_VISITORS')) { | |
25 define('USER_REGISTER_VISITORS', 'visitors'); | |
26 } | |
27 | |
28 /** | |
29 * Visitors can create accounts, but they don't become active without | |
30 * administrative approval. | |
31 */ | |
32 if (!defined('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL')) { | |
33 define('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL', 'visitors_admin_approval'); | |
34 } | |
35 | |
36 /** | |
37 * Tests User Registration REST resource. | |
38 * | |
39 * @coversDefaultClass \Drupal\user\Plugin\rest\resource\UserRegistrationResource | |
40 * @group user | |
41 */ | |
42 class UserRegistrationResourceTest extends UnitTestCase { | |
43 | |
44 const ERROR_MESSAGE = "Unprocessable Entity: validation failed.\nproperty_path: message\nproperty_path_2: message_2\n"; | |
45 | |
46 /** | |
47 * Class to be tested. | |
48 * | |
49 * @var \Drupal\user\Plugin\rest\resource\UserRegistrationResource | |
50 */ | |
51 protected $testClass; | |
52 | |
53 /** | |
54 * A reflection of self::$testClass. | |
55 * | |
56 * @var \ReflectionClass | |
57 */ | |
58 protected $reflection; | |
59 | |
60 /** | |
61 * A user settings config instance. | |
62 * | |
63 * @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit_Framework_MockObject_MockObject | |
64 */ | |
65 protected $userSettings; | |
66 | |
67 /** | |
68 * Logger service. | |
69 * | |
70 * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject | |
71 */ | |
72 protected $logger; | |
73 | |
74 /** | |
75 * The current user. | |
76 * | |
77 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject | |
78 */ | |
79 protected $currentUser; | |
80 | |
81 /** | |
82 * {@inheritdoc} | |
83 */ | |
84 protected function setUp() { | |
85 parent::setUp(); | |
86 | |
87 $this->logger = $this->prophesize(LoggerInterface::class)->reveal(); | |
88 | |
89 $this->userSettings = $this->prophesize(ImmutableConfig::class); | |
90 | |
91 $this->currentUser = $this->prophesize(AccountInterface::class); | |
92 | |
93 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); | |
94 $this->reflection = new \ReflectionClass($this->testClass); | |
95 } | |
96 | |
97 /** | |
98 * Tests that an exception is thrown when no data provided for the account. | |
99 */ | |
100 public function testEmptyPost() { | |
101 $this->setExpectedException(BadRequestHttpException::class); | |
102 $this->testClass->post(NULL); | |
103 } | |
104 | |
105 /** | |
106 * Tests that only new user accounts can be registered. | |
107 */ | |
108 public function testExistedEntityPost() { | |
109 $entity = $this->prophesize(User::class); | |
110 $entity->isNew()->willReturn(FALSE); | |
111 $this->setExpectedException(BadRequestHttpException::class); | |
112 | |
113 $this->testClass->post($entity->reveal()); | |
114 } | |
115 | |
116 /** | |
117 * Tests that admin permissions are required to register a user account. | |
118 */ | |
119 public function testRegistrationAdminOnlyPost() { | |
120 | |
121 $this->userSettings->get('register')->willReturn(USER_REGISTER_ADMINISTRATORS_ONLY); | |
122 | |
123 $this->currentUser->isAnonymous()->willReturn(TRUE); | |
124 | |
125 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); | |
126 | |
127 $entity = $this->prophesize(User::class); | |
128 $entity->isNew()->willReturn(TRUE); | |
129 | |
130 $this->setExpectedException(AccessDeniedHttpException::class); | |
131 | |
132 $this->testClass->post($entity->reveal()); | |
133 } | |
134 | |
135 /** | |
136 * Tests that only anonymous users can register users. | |
137 */ | |
138 public function testRegistrationAnonymousOnlyPost() { | |
139 $this->currentUser->isAnonymous()->willReturn(FALSE); | |
140 | |
141 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); | |
142 | |
143 $entity = $this->prophesize(User::class); | |
144 $entity->isNew()->willReturn(TRUE); | |
145 | |
146 $this->setExpectedException(AccessDeniedHttpException::class); | |
147 | |
148 $this->testClass->post($entity->reveal()); | |
149 } | |
150 | |
151 } |