annotate core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\user\Unit;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ImmutableConfig;
Chris@0 6 use Drupal\Core\Session\AccountInterface;
Chris@0 7 use Drupal\Tests\UnitTestCase;
Chris@0 8 use Drupal\user\Entity\User;
Chris@0 9 use Drupal\user\Plugin\rest\resource\UserRegistrationResource;
Chris@18 10 use Drupal\user\UserInterface;
Chris@0 11 use Psr\Log\LoggerInterface;
Chris@0 12 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
Chris@0 13 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Tests User Registration REST resource.
Chris@0 17 *
Chris@0 18 * @coversDefaultClass \Drupal\user\Plugin\rest\resource\UserRegistrationResource
Chris@0 19 * @group user
Chris@0 20 */
Chris@0 21 class UserRegistrationResourceTest extends UnitTestCase {
Chris@0 22
Chris@0 23 const ERROR_MESSAGE = "Unprocessable Entity: validation failed.\nproperty_path: message\nproperty_path_2: message_2\n";
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Class to be tested.
Chris@0 27 *
Chris@0 28 * @var \Drupal\user\Plugin\rest\resource\UserRegistrationResource
Chris@0 29 */
Chris@0 30 protected $testClass;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * A reflection of self::$testClass.
Chris@0 34 *
Chris@0 35 * @var \ReflectionClass
Chris@0 36 */
Chris@0 37 protected $reflection;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * A user settings config instance.
Chris@0 41 *
Chris@0 42 * @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit_Framework_MockObject_MockObject
Chris@0 43 */
Chris@0 44 protected $userSettings;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Logger service.
Chris@0 48 *
Chris@0 49 * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 50 */
Chris@0 51 protected $logger;
Chris@0 52
Chris@0 53 /**
Chris@0 54 * The current user.
Chris@0 55 *
Chris@0 56 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 57 */
Chris@0 58 protected $currentUser;
Chris@0 59
Chris@0 60 /**
Chris@0 61 * {@inheritdoc}
Chris@0 62 */
Chris@0 63 protected function setUp() {
Chris@0 64 parent::setUp();
Chris@0 65
Chris@0 66 $this->logger = $this->prophesize(LoggerInterface::class)->reveal();
Chris@0 67
Chris@0 68 $this->userSettings = $this->prophesize(ImmutableConfig::class);
Chris@0 69
Chris@0 70 $this->currentUser = $this->prophesize(AccountInterface::class);
Chris@0 71
Chris@0 72 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal());
Chris@0 73 $this->reflection = new \ReflectionClass($this->testClass);
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Tests that an exception is thrown when no data provided for the account.
Chris@0 78 */
Chris@0 79 public function testEmptyPost() {
Chris@0 80 $this->setExpectedException(BadRequestHttpException::class);
Chris@0 81 $this->testClass->post(NULL);
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Tests that only new user accounts can be registered.
Chris@0 86 */
Chris@0 87 public function testExistedEntityPost() {
Chris@0 88 $entity = $this->prophesize(User::class);
Chris@0 89 $entity->isNew()->willReturn(FALSE);
Chris@0 90 $this->setExpectedException(BadRequestHttpException::class);
Chris@0 91
Chris@0 92 $this->testClass->post($entity->reveal());
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Tests that admin permissions are required to register a user account.
Chris@0 97 */
Chris@0 98 public function testRegistrationAdminOnlyPost() {
Chris@0 99
Chris@18 100 $this->userSettings->get('register')->willReturn(UserInterface::REGISTER_ADMINISTRATORS_ONLY);
Chris@0 101
Chris@0 102 $this->currentUser->isAnonymous()->willReturn(TRUE);
Chris@0 103
Chris@0 104 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal());
Chris@0 105
Chris@0 106 $entity = $this->prophesize(User::class);
Chris@0 107 $entity->isNew()->willReturn(TRUE);
Chris@0 108
Chris@0 109 $this->setExpectedException(AccessDeniedHttpException::class);
Chris@0 110
Chris@0 111 $this->testClass->post($entity->reveal());
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Tests that only anonymous users can register users.
Chris@0 116 */
Chris@0 117 public function testRegistrationAnonymousOnlyPost() {
Chris@0 118 $this->currentUser->isAnonymous()->willReturn(FALSE);
Chris@0 119
Chris@0 120 $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal());
Chris@0 121
Chris@0 122 $entity = $this->prophesize(User::class);
Chris@0 123 $entity->isNew()->willReturn(TRUE);
Chris@0 124
Chris@0 125 $this->setExpectedException(AccessDeniedHttpException::class);
Chris@0 126
Chris@0 127 $this->testClass->post($entity->reveal());
Chris@0 128 }
Chris@0 129
Chris@0 130 }