class UserRegistrationResourceTest in Drupal 9
Same name and namespace in other branches
- 8 core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php \Drupal\Tests\user\Unit\UserRegistrationResourceTest
Tests User Registration REST resource.
@coversDefaultClass \Drupal\user\Plugin\rest\resource\UserRegistrationResource @group user
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\user\Unit\UserRegistrationResourceTest
Expanded class hierarchy of UserRegistrationResourceTest
File
- core/
modules/ user/ tests/ src/ Unit/ UserRegistrationResourceTest.php, line 21
Namespace
Drupal\Tests\user\UnitView source
class UserRegistrationResourceTest extends UnitTestCase {
const ERROR_MESSAGE = "Unprocessable Entity: validation failed.\nproperty_path: message\nproperty_path_2: message_2\n";
/**
* Class to be tested.
*
* @var \Drupal\user\Plugin\rest\resource\UserRegistrationResource
*/
protected $testClass;
/**
* A reflection of self::$testClass.
*
* @var \ReflectionClass
*/
protected $reflection;
/**
* A user settings config instance.
*
* @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit\Framework\MockObject\MockObject
*/
protected $userSettings;
/**
* Logger service.
*
* @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $logger;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->logger = $this
->prophesize(LoggerInterface::class)
->reveal();
$this->userSettings = $this
->prophesize(ImmutableConfig::class);
$this->currentUser = $this
->prophesize(AccountInterface::class);
$this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings
->reveal(), $this->currentUser
->reveal());
$this->reflection = new \ReflectionClass($this->testClass);
}
/**
* Tests that an exception is thrown when no data provided for the account.
*/
public function testEmptyPost() {
$this
->expectException(BadRequestHttpException::class);
$this->testClass
->post(NULL);
}
/**
* Tests that only new user accounts can be registered.
*/
public function testExistedEntityPost() {
$entity = $this
->prophesize(User::class);
$entity
->isNew()
->willReturn(FALSE);
$this
->expectException(BadRequestHttpException::class);
$this->testClass
->post($entity
->reveal());
}
/**
* Tests that admin permissions are required to register a user account.
*/
public function testRegistrationAdminOnlyPost() {
$this->userSettings
->get('register')
->willReturn(UserInterface::REGISTER_ADMINISTRATORS_ONLY);
$this->currentUser
->isAnonymous()
->willReturn(TRUE);
$this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings
->reveal(), $this->currentUser
->reveal());
$entity = $this
->prophesize(User::class);
$entity
->isNew()
->willReturn(TRUE);
$this
->expectException(AccessDeniedHttpException::class);
$this->testClass
->post($entity
->reveal());
}
/**
* Tests that only anonymous users can register users.
*/
public function testRegistrationAnonymousOnlyPost() {
$this->currentUser
->isAnonymous()
->willReturn(FALSE);
$this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings
->reveal(), $this->currentUser
->reveal());
$entity = $this
->prophesize(User::class);
$entity
->isNew()
->willReturn(TRUE);
$this
->expectException(AccessDeniedHttpException::class);
$this->testClass
->post($entity
->reveal());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function | ||
UserRegistrationResourceTest:: |
protected | property | The current user. | |
UserRegistrationResourceTest:: |
protected | property | Logger service. | |
UserRegistrationResourceTest:: |
protected | property | A reflection of self::$testClass. | |
UserRegistrationResourceTest:: |
protected | property | Class to be tested. | |
UserRegistrationResourceTest:: |
protected | property | A user settings config instance. | |
UserRegistrationResourceTest:: |
constant | |||
UserRegistrationResourceTest:: |
protected | function |
Overrides UnitTestCase:: |
|
UserRegistrationResourceTest:: |
public | function | Tests that an exception is thrown when no data provided for the account. | |
UserRegistrationResourceTest:: |
public | function | Tests that only new user accounts can be registered. | |
UserRegistrationResourceTest:: |
public | function | Tests that admin permissions are required to register a user account. | |
UserRegistrationResourceTest:: |
public | function | Tests that only anonymous users can register users. |