class OwnOrAdminConstraintValidatorTest in Simple OAuth (OAuth2) & OpenID Connect 8
Class OwnOrAdminConstraintValidatorTest.
@package Drupal\Tests\simple_oauth\Unit\Plugin\Validation\Constraint
@coversDefaultClass \Drupal\simple_oauth\Plugin\Validation\Constraint\OwnOrAdminConstraintValidator @group simple_oauth
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\simple_oauth\Unit\Plugin\Validation\Constraint\OwnOrAdminConstraintValidatorTest
Expanded class hierarchy of OwnOrAdminConstraintValidatorTest
File
- tests/
src/ Unit/ Plugin/ Validation/ Constraint/ OwnOrAdminConstraintValidatorTest.php, line 20
Namespace
Drupal\Tests\simple_oauth\Unit\Plugin\Validation\ConstraintView source
class OwnOrAdminConstraintValidatorTest extends UnitTestCase {
/**
* @covers ::validate
* @dataProvider validateProvider
*/
public function testValidate($uid, $has_permission, $success) {
$constraint = $this
->prophesize(OwnOrAdminConstraint::class);
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn(6);
$account
->hasPermission('lorem ipsum dolor')
->willReturn($has_permission);
$constraint
->getAccount()
->willReturn($account
->reveal());
$constraint
->getPermission()
->willReturn('lorem ipsum dolor');
$context = $this
->prophesize(ExecutionContext::class);
$constraint_validator = new OwnOrAdminConstraintValidator();
$constraint_validator
->initialize($context
->reveal());
$revealed_constraint = $constraint
->reveal();
$revealed_constraint->message = 'Foo';
$constraint_validator
->validate($uid, $revealed_constraint);
$method = $success ? 'shouldNotHaveBeenCalled' : 'shouldHaveBeenCalled';
$context
->addViolation(Argument::type('string'), Argument::type('array'))
->{$method}();
}
public function validateProvider() {
return [
// 1. Success: same user id.
[
6,
TRUE,
TRUE,
],
// 2. Success: different user ID but has permission.
[
2,
TRUE,
TRUE,
],
// 3. Fail.
[
2,
FALSE,
FALSE,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OwnOrAdminConstraintValidatorTest:: |
public | function | @covers ::validate @dataProvider validateProvider | |
OwnOrAdminConstraintValidatorTest:: |
public | function | ||
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |