class UserMailRequiredValidatorTest
Same name in other branches
- 9 core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest
- 8.9.x core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest
- 10 core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest
@coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\UserMailRequiredValidator @group user
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of UserMailRequiredValidatorTest
File
-
core/
modules/ user/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ UserMailRequiredValidatorTest.php, line 24
Namespace
Drupal\Tests\user\Unit\Plugin\Validation\ConstraintView source
class UserMailRequiredValidatorTest extends UnitTestCase {
/**
* Creates a validator instance.
*
* @param bool $is_admin
* Whether or not the current user is an administrator.
*
* @return \Drupal\user\Plugin\Validation\Constraint\UserMailRequiredValidator
* The validator instance.
*/
protected function createValidator($is_admin) {
// Setup mocks that don't need to change.
$unchanged_account = $this->prophesize(UserInterface::class);
$unchanged_account->getEmail()
->willReturn(NULL);
$user_storage = $this->prophesize(UserStorageInterface::class);
$user_storage->loadUnchanged(3)
->willReturn($unchanged_account->reveal());
$entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager->getStorage('user')
->willReturn($user_storage->reveal());
$current_user = $this->prophesize(AccountInterface::class);
$current_user->id()
->willReturn(3);
$current_user->hasPermission("administer users")
->willReturn($is_admin);
$container = new ContainerBuilder();
$container->set('entity_type.manager', $entity_type_manager->reveal());
$container->set('current_user', $current_user->reveal());
\Drupal::setContainer($container);
return new UserMailRequiredValidator();
}
/**
* @covers ::validate
*
* @dataProvider providerTestValidate
*/
public function testValidate($items, $expected_violation, $is_admin = FALSE) : void {
$constraint = new UserMailRequired();
// If a violation is expected, then the context's addViolation method will
// be called, otherwise it should not be called.
$context = $this->prophesize(ExecutionContextInterface::class);
if ($expected_violation) {
$context->addViolation('@name field is required.', [
'@name' => 'Email',
])
->shouldBeCalledTimes(1);
}
else {
$context->addViolation()
->shouldNotBeCalled();
}
$validator = $this->createValidator($is_admin);
$validator->initialize($context->reveal());
$validator->validate($items, $constraint);
}
/**
* Data provider for ::testValidate().
*/
public static function providerTestValidate() {
$prophet = new Prophet();
$cases = [];
// Case 1: Empty user should be ignored.
$items = $prophet->prophesize(FieldItemListInterface::class);
$items->getEntity()
->willReturn(NULL)
->shouldBeCalledTimes(1);
$cases['Empty user should be ignored'] = [
$items->reveal(),
FALSE,
];
// Case 2: New users without an email should add a violation.
$items = $prophet->prophesize(FieldItemListInterface::class);
$account = $prophet->prophesize(UserInterface::class);
$account->isNew()
->willReturn(TRUE);
$account->id()
->shouldNotBeCalled();
$field_definition = $prophet->prophesize(FieldDefinitionInterface::class);
$field_definition->getLabel()
->willReturn('Email');
$account->getFieldDefinition("mail")
->willReturn($field_definition->reveal())
->shouldBeCalledTimes(1);
$items->getEntity()
->willReturn($account->reveal())
->shouldBeCalledTimes(1);
$items->isEmpty()
->willReturn(TRUE);
$cases['New users without an email should add a violation'] = [
$items->reveal(),
TRUE,
];
// Case 3: Existing users without an email should add a violation.
$items = $prophet->prophesize(FieldItemListInterface::class);
$account = $prophet->prophesize(UserInterface::class);
$account->isNew()
->willReturn(FALSE);
$account->id()
->willReturn(3);
$field_definition = $prophet->prophesize(FieldDefinitionInterface::class);
$field_definition->getLabel()
->willReturn('Email');
$account->getFieldDefinition("mail")
->willReturn($field_definition->reveal())
->shouldBeCalledTimes(1);
$items->getEntity()
->willReturn($account->reveal())
->shouldBeCalledTimes(1);
$items->isEmpty()
->willReturn(TRUE);
$cases['Existing users without an email should add a violation'] = [
$items->reveal(),
TRUE,
];
// Case 4: New user with an email is valid.
$items = $prophet->prophesize(FieldItemListInterface::class);
$account = $prophet->prophesize(UserInterface::class);
$account->isNew()
->willReturn(TRUE);
$account->id()
->shouldNotBeCalled();
$field_definition = $prophet->prophesize(FieldDefinitionInterface::class);
$field_definition->getLabel()
->willReturn('Email');
$account->getFieldDefinition("mail")
->willReturn($field_definition->reveal())
->shouldBeCalledTimes(1);
$items->getEntity()
->willReturn($account->reveal())
->shouldBeCalledTimes(1);
$items->isEmpty()
->willReturn(FALSE);
$cases['New user with an email is valid'] = [
$items->reveal(),
FALSE,
];
// Case 5: Existing users with an email should be ignored.
$items = $prophet->prophesize(FieldItemListInterface::class);
$account = $prophet->prophesize(UserInterface::class);
$account->isNew()
->willReturn(FALSE);
$account->id()
->willReturn(3);
$field_definition = $prophet->prophesize(FieldDefinitionInterface::class);
$field_definition->getLabel()
->willReturn('Email');
$account->getFieldDefinition("mail")
->willReturn($field_definition->reveal())
->shouldBeCalledTimes(1);
$items->getEntity()
->willReturn($account->reveal())
->shouldBeCalledTimes(1);
$items->isEmpty()
->willReturn(FALSE);
$cases['Existing users with an email should be ignored'] = [
$items->reveal(),
FALSE,
];
// Case 6: Existing users without an email should be ignored if the current
// user is an administrator.
$items = $prophet->prophesize(FieldItemListInterface::class);
$account = $prophet->prophesize(UserInterface::class);
$account->isNew()
->willReturn(FALSE);
$account->id()
->willReturn(3);
$field_definition = $prophet->prophesize(FieldDefinitionInterface::class);
$field_definition->getLabel()
->willReturn('Email');
$account->getFieldDefinition("mail")
->willReturn($field_definition->reveal())
->shouldBeCalledTimes(1);
$items->getEntity()
->willReturn($account->reveal())
->shouldBeCalledTimes(1);
$items->isEmpty()
->willReturn(TRUE);
$cases['Existing users without an email should be ignored if the current user is an administrator.'] = [
$items->reveal(),
FALSE,
TRUE,
];
return $cases;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
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::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |
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::setUp | protected | function | 367 | |
UnitTestCase::setUpBeforeClass | public static | function | ||
UserMailRequiredValidatorTest::createValidator | protected | function | Creates a validator instance. | |
UserMailRequiredValidatorTest::providerTestValidate | public static | function | Data provider for ::testValidate(). | |
UserMailRequiredValidatorTest::testValidate | public | function | @covers ::validate |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.