You are here

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

Expanded class hierarchy of OwnOrAdminConstraintValidatorTest

File

tests/src/Unit/Plugin/Validation/Constraint/OwnOrAdminConstraintValidatorTest.php, line 20

Namespace

Drupal\Tests\simple_oauth\Unit\Plugin\Validation\Constraint
View 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

Namesort descending Modifiers Type Description Overrides
OwnOrAdminConstraintValidatorTest::testValidate public function @covers ::validate @dataProvider validateProvider
OwnOrAdminConstraintValidatorTest::validateProvider public function
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340