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