class UserRegistrationResourceTest
Tests User Registration REST resource.
Attributes
#[CoversClass(UserRegistrationResource::class)]
#[Group('user')]
  Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\user\Unit\UserRegistrationResourceTest extends \Drupal\Tests\UnitTestCase
 
 
Expanded class hierarchy of UserRegistrationResourceTest
File
- 
              core/
modules/ user/ tests/ src/ Unit/ UserRegistrationResourceTest.php, line 23  
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;
  
  /**
   * The password generator.
   *
   * @var \Drupal\Core\Password\PasswordGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $passwordGenerator;
  
  /**
   * {@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->passwordGenerator = $this->prophesize(PasswordGeneratorInterface::class)
      ->reveal();
    $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings
      ->reveal(), $this->currentUser
      ->reveal(), $this->passwordGenerator);
    $this->reflection = new \ReflectionClass($this->testClass);
  }
  
  /**
   * Tests that an exception is thrown when no data provided for the account.
   */
  public function testEmptyPost() : void {
    $this->expectException(BadRequestHttpException::class);
    $this->testClass
      ->post(NULL);
  }
  
  /**
   * Tests that only new user accounts can be registered.
   */
  public function testExistedEntityPost() : void {
    $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() : void {
    $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(), $this->passwordGenerator);
    $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() : void {
    $this->currentUser
      ->isAnonymous()
      ->willReturn(FALSE);
    $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings
      ->reveal(), $this->currentUser
      ->reveal(), $this->passwordGenerator);
    $entity = $this->prophesize(User::class);
    $entity->isNew()
      ->willReturn(TRUE);
    $this->expectException(AccessDeniedHttpException::class);
    $this->testClass
      ->post($entity->reveal());
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. | |
| UserRegistrationResourceTest::$currentUser | protected | property | The current user. | |
| UserRegistrationResourceTest::$logger | protected | property | Logger service. | |
| UserRegistrationResourceTest::$passwordGenerator | protected | property | The password generator. | |
| UserRegistrationResourceTest::$reflection | protected | property | A reflection of self::$testClass. | |
| UserRegistrationResourceTest::$testClass | protected | property | Class to be tested. | |
| UserRegistrationResourceTest::$userSettings | protected | property | A user settings config instance. | |
| UserRegistrationResourceTest::ERROR_MESSAGE | constant | |||
| UserRegistrationResourceTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
| UserRegistrationResourceTest::testEmptyPost | public | function | Tests that an exception is thrown when no data provided for the account. | |
| UserRegistrationResourceTest::testExistedEntityPost | public | function | Tests that only new user accounts can be registered. | |
| UserRegistrationResourceTest::testRegistrationAdminOnlyPost | public | function | Tests that admin permissions are required to register a user account. | |
| UserRegistrationResourceTest::testRegistrationAnonymousOnlyPost | public | function | Tests that only anonymous users can register users. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.