You are here

class RequiredByRoleTest in Required by role 8

Tests the requird_by_role plugin.

@group Required API

Hierarchy

Expanded class hierarchy of RequiredByRoleTest

See also

\Drupal\required_by_role\Plugin\RequiredByRoleTest

File

src/Tests/Plugin/Required/RequiredByRoleTest.php, line 22
Contains \Drupal\required_by_role\Tests\Plugin\Required\RequiredByRoleTest.

Namespace

Drupal\required_by_role\Tests\Plugin\Required
View source
class RequiredByRoleTest extends UnitTestCase {

  /**
   * The required plugin.
   *
   * @var \Drupal\required_by_role\Plugin\Required\RequiredByRole
   */
  protected $plugin;

  /**
   * Method getInfo.
   *
   * @return array
   *   Information regarding to the tests.
   */
  public static function getInfo() {
    return [
      'name' => 'Required by role plugin',
      'description' => 'Test the required by role logic.',
      'group' => 'Required API',
    ];
  }

  /**
   * Caching the plugin instance in the $plugin property.
   */
  public function setUp() {
    $this->plugin = $this
      ->getMockBuilder('Drupal\\required_by_role\\Plugin\\Required\\RequiredByRole')
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();
  }

  /**
   * Tests the required by role behavior.
   *
   * @dataProvider getRequiredCases
   */
  public function testRequiredByRole($result, $user_roles, $required_roles) {
    $required = $this->plugin
      ->getMatches($user_roles, $required_roles);
    $this
      ->assertEquals($result, $required);
  }

  /**
   * Provides a cases to test.
   */
  public function getRequiredCases() {

    // array(bool $result, array $user_roles, array $required_roles)
    return [
      // User with matching roles.
      [
        TRUE,
        [
          AccountInterface::AUTHENTICATED_ROLE,
          'administrator',
        ],
        [
          'administrator',
        ],
      ],
      // User with no matching roles.
      [
        FALSE,
        [
          AccountInterface::AUTHENTICATED_ROLE,
          'administrator',
        ],
        [
          AccountInterface::ANONYMOUS_ROLE,
        ],
      ],
      // No required roles set.
      [
        FALSE,
        [
          AccountInterface::AUTHENTICATED_ROLE,
          'administrator',
        ],
        [],
      ],
      // Required roles is not an array.
      [
        FALSE,
        [
          AccountInterface::AUTHENTICATED_ROLE,
          'administrator',
        ],
        NULL,
      ],
      // The user has no roles.
      [
        FALSE,
        NULL,
        [
          AccountInterface::AUTHENTICATED_ROLE,
          'administrator',
        ],
      ],
      // The user has no roles and there is no required roles.
      [
        FALSE,
        NULL,
        NULL,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
RequiredByRoleTest::$plugin protected property The required plugin.
RequiredByRoleTest::getInfo public static function Method getInfo.
RequiredByRoleTest::getRequiredCases public function Provides a cases to test.
RequiredByRoleTest::setUp public function Caching the plugin instance in the $plugin property. Overrides UnitTestCase::setUp
RequiredByRoleTest::testRequiredByRole public function Tests the required by role behavior.
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.