diff --git a/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php b/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php index 552e56c..4ee9df2 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserRegistrationResource.php @@ -45,7 +45,7 @@ class UserRegistrationResource extends ResourceBase { protected $currentUser; /** - * Constructs a new RestPermissions instance. + * Constructs a new UserRegistrationResource instance. * * @param array $configuration * A configuration array containing information about the plugin instance. diff --git a/core/modules/rest/src/Tests/RegisterUserTest.php b/core/modules/rest/src/Tests/RegisterUserTest.php index 09ccdf2..a5e2d58 100644 --- a/core/modules/rest/src/Tests/RegisterUserTest.php +++ b/core/modules/rest/src/Tests/RegisterUserTest.php @@ -25,16 +25,16 @@ class RegisterUserTest extends RESTTestBase { public static $modules = ['hal']; /** - * Enables user registration specific REST API configuration and authenticate. + * {@inheritdoc} */ - protected function enableUserRegistrationConfiguration() { + public function setUp() { + parent::setUp(); // Enabling services for the user registration and GET the new user entity // created. $config = $this->config('rest.settings'); $settings = []; $resources = [ ['type' => 'rest_user_registration', 'method' => 'POST'], - ['type' => 'entity:user', 'method' => 'GET'] ]; $format = 'hal_json'; $auth = $this->defaultAuth; @@ -50,7 +50,6 @@ protected function enableUserRegistrationConfiguration() { Role::load(RoleInterface::ANONYMOUS_ID) ->grantPermission('restful post rest_user_registration') ->save(); - } /** @@ -58,7 +57,6 @@ protected function enableUserRegistrationConfiguration() { */ protected function testRegisterUser() { - $this->enableUserRegistrationConfiguration(); // New user info to be serialized. $data = [ "_links" => @@ -77,7 +75,7 @@ protected function testRegisterUser() { ], "mail" => [ [ - "value" => "druplicon@superpoweredbydrupal.com" + "value" => "druplicon@example.com" ] ], "pass" => [ @@ -88,7 +86,7 @@ protected function testRegisterUser() { ]; // Create a JSON version for the user entity we want to create. - $serialized = \Drupal::service('serializer')->serialize($data, 'hal_json'); + $serialized = $this->container->get('serializer')->serialize($data, 'hal_json'); // Post to the REST service to register the user. $this->httpRequest('entity/user/register', 'POST', $serialized, 'application/hal+json'); @@ -99,7 +97,8 @@ protected function testRegisterUser() { $id = end($url_parts); $this->assertHeader('Location', $GLOBALS['base_url'] . '/user/' . $id); - $this->assertTrue((bool) \Drupal::entityQuery('user') + $this->assertTrue((bool) $this->container->get('entity.query') + ->get('user') ->condition('name', 'Druplicon') ->range(0, 1) ->count() diff --git a/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php b/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php index c3f3f2c..778b534 100644 --- a/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php +++ b/core/modules/rest/tests/src/Unit/UserRegistrationResourceTest.php @@ -7,41 +7,82 @@ namespace Drupal\Tests\rest\Unit; +use Drupal\Core\Config\ImmutableConfig; +use Drupal\Core\Field\FieldItemList; +use Drupal\Core\Session\AccountInterface; use Drupal\rest\Plugin\rest\resource\UserRegistrationResource; use Drupal\Tests\UnitTestCase; +use Drupal\user\Entity\User; +use Psr\Log\LoggerInterface; +use Drupal\Core\Entity\EntityConstraintViolationList; +use Symfony\Component\Validator\ConstraintViolationInterface; /** * Only administrators can create user accounts. */ -define('USER_REGISTER_ADMINISTRATORS_ONLY', 'admin_only'); +if (!defined('USER_REGISTER_ADMINISTRATORS_ONLY')) { + define('USER_REGISTER_ADMINISTRATORS_ONLY', 'admin_only'); +} /** * Visitors can create their own accounts. */ -define('USER_REGISTER_VISITORS', 'visitors'); +if (!defined('USER_REGISTER_VISITORS')) { + define('USER_REGISTER_VISITORS', 'visitors'); +} /** * Visitors can create accounts, but they don't become active without * administrative approval. */ -define('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL', 'visitors_admin_approval'); - +if (!defined('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL')) { + define('USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL', 'visitors_admin_approval'); +} /** * Tests User Registration REST resource. * + * @coversDefaultClass \Drupal\Tests\rest\Unit\UserRegistrationResourceTest * @group rest */ 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\rest\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; - protected $renderer; /** * {@inheritdoc} @@ -49,14 +90,14 @@ class UserRegistrationResourceTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $this->logger = $this->getMock('Psr\Log\LoggerInterface'); + $this->logger = $this->getMock(LoggerInterface::class); - $this->userSettings = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig') + $this->userSettings = $this->getMockBuilder(ImmutableConfig::class) ->setMethods(['get']) ->disableOriginalConstructor() ->getMock(); - $this->currentUser = $this->getMockBuilder('Drupal\Core\Session\AccountInterface') + $this->currentUser = $this->getMockBuilder(AccountInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -65,6 +106,14 @@ protected function setUp() { } + /** + * Helper method to set a protected method as accessible. + * + * @param string $method + * The protected method. + * + * @return \ReflectionMethod + */ public function getProtectedMethod($method) { $method = $this->reflection->getMethod($method); $method->setAccessible(TRUE); @@ -72,9 +121,11 @@ public function getProtectedMethod($method) { return $method; } + /** + * Tests that the user entity does not violate any validation constraints. + */ public function testValidate() { - - $violations = $this->getMockBuilder('Drupal\Core\Entity\EntityConstraintViolationList') + $violations = $this->getMockBuilder(EntityConstraintViolationList::class) ->setMethods(['filterByFieldAccess']) ->disableOriginalConstructor() ->getMock(); @@ -83,14 +134,16 @@ public function testValidate() { ->method('filterByFieldAccess') ->will($this->returnValue(array())); - $entity = $this->getMockBuilder('Drupal\user\Entity\User') + $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); + $entity->expects($this->once()) ->method('validate') ->will($this->returnValue($violations)); $method = $this->getProtectedMethod('validate'); + // No exception is thrown. $method->invokeArgs($this->testClass, array($entity)); } @@ -100,25 +153,31 @@ public function testValidate() { * @expectedException UserRegistrationResourceTest::ERROR_MESSAGE */ public function testFailedValidate() { - $violation1 = $this->getMock('Symfony\Component\Validator\ConstraintViolationInterface'); + $violation1 = $this->getMock(ConstraintViolationInterface::class); + $violation1->expects($this->once()) ->method('getPropertyPath') ->will($this->returnValue('property_path')); + $violation1->expects($this->once()) ->method('getMessage') ->will($this->returnValue('message')); - $violation2 = $this->getMock('Symfony\Component\Validator\ConstraintViolationInterface'); + + $violation2 = $this->getMock(ConstraintViolationInterface::class); + $violation2->expects($this->once()) ->method('getPropertyPath') ->will($this->returnValue('property_path_2')); + $violation2->expects($this->once()) ->method('getMessage') ->will($this->returnValue('message_2')); - $entity = $this->getMockBuilder('Drupal\user\Entity\User') + + $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); - $violations = $this->getMockBuilder('Drupal\Core\Entity\EntityConstraintViolationList') + $violations = $this->getMockBuilder(EntityConstraintViolationList::class) ->setConstructorArgs([$entity, [$violation1, $violation2]]) ->setMethods(['filterByFieldAccess']) ->getMock(); @@ -149,7 +208,7 @@ public function testEmptyPost() { * @expectedExceptionMessage Only new user accounts can be registered. */ public function testExistedEntityPost() { - $entity = $this->getMockBuilder('Drupal\user\Entity\User') + $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); @@ -174,7 +233,7 @@ public function testRegistrationAdminOnlyPost() { ->will($this->returnValue(USER_REGISTER_ADMINISTRATORS_ONLY)); $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); - $entity = $this->getMockBuilder('Drupal\user\Entity\User') + $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); @@ -195,9 +254,11 @@ public function testRegistrationAnonymousOnlyPost() { ->will($this->returnValue(FALSE)); $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); - $entity = $this->getMockBuilder('Drupal\user\Entity\User') + + $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); + $entity->expects($this->once()) ->method('isNew') ->will($this->returnValue(TRUE)); @@ -215,16 +276,16 @@ public function testFieldAccessValidation() { ->will($this->returnValue(TRUE)); $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings, $this->currentUser); - $entity = $this->getMockBuilder('Drupal\user\Entity\User') + + $entity = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); - $entity->expects($this->once()) ->method('isNew') ->will($this->returnValue(TRUE)); - $field = $this->getMockBuilder('\Drupal\Core\Field\FieldItemList') + $field = $this->getMockBuilder(FieldItemList::class) ->setMethods(['access']) ->disableOriginalConstructor() ->getMock();