You are here

class TfaContextTest in Two-factor Authentication (TFA) 8

@coversDefaultClass \Drupal\tfa\TfaContext

@group tfa

Hierarchy

Expanded class hierarchy of TfaContextTest

File

tests/src/Unit/TfaContextTest.php, line 21

Namespace

Drupal\Tests\tfa\Unit
View source
class TfaContextTest extends UnitTestCase {

  /**
   * Validation plugin manager.
   *
   * @var \Drupal\tfa\TfaValidationPluginManager
   */
  protected $tfaValidationManager;

  /**
   * Login plugin manager.
   *
   * @var \Drupal\tfa\TfaLoginPluginManager
   */
  protected $tfaLoginManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Tfa settings config object.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $tfaSettings;

  /**
   * Entity for the user that is attempting to login.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;

  /**
   * User data service.
   *
   * @var \Drupal\user\UserDataInterface
   */
  protected $userData;

  /**
   * Current request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Setup default mocked services. These can be overridden by
    // re-instantiating them as needed prior to calling ::getFixture().
    $this->tfaValidationManager = $this
      ->prophesize(TfaValidationPluginManager::class)
      ->reveal();
    $this->tfaLoginManager = $this
      ->prophesize(TfaLoginPluginManager::class)
      ->reveal();
    $this->tfaSettings = $this
      ->prophesize(ImmutableConfig::class)
      ->reveal();
    $this->configFactory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $this->configFactory
      ->get('tfa.settings')
      ->willReturn($this->tfaSettings);
    $this->configFactory = $this->configFactory
      ->reveal();
    $this->user = $this
      ->prophesize(UserInterface::class);
    $this->user
      ->id()
      ->willReturn(3);
    $this->user = $this->user
      ->reveal();
    $this->userData = $this
      ->prophesize(UserDataInterface::class)
      ->reveal();
    $this->request = $this
      ->prophesize(Request::class)
      ->reveal();
  }

  /**
   * Helper method to instantiate the test fixture.
   *
   * @return \Drupal\tfa\TfaContext
   *   TFA context.
   */
  protected function getFixture() {
    return new TfaContext($this->tfaValidationManager, $this->tfaLoginManager, $this->configFactory, $this->user, $this->userData, $this->request);
  }

  /**
   * @covers ::getUser
   */
  public function testGetUser() {
    $fixture = $this
      ->getFixture();
    $this
      ->assertEquals(3, $fixture
      ->getUser()
      ->id());
  }

  /**
   * @covers ::isModuleSetup
   */
  public function testIsModuleSetup() {

    // Defaults to false with empty mocked services.
    $fixture = $this
      ->getFixture();
    $this
      ->assertFalse($fixture
      ->isModuleSetup());

    // Enable.
    $settings = $this
      ->prophesize(ImmutableConfig::class);
    $settings
      ->get('enabled')
      ->willReturn(TRUE);
    $settings
      ->get('default_validation_plugin')
      ->willReturn('foo');
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('tfa.settings')
      ->willReturn($settings
      ->reveal());
    $this->configFactory = $config_factory
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertTrue($fixture
      ->isModuleSetup());
  }

  /**
   * @covers ::isTfaRequired
   */
  public function testIsTfaRequired() {

    // User has setup TFA.
    $user_data = $this
      ->prophesize(UserDataInterface::class);
    $user_data
      ->get('tfa', 3, 'tfa_user_settings')
      ->willReturn([
      'status' => 1,
      'saved' => FALSE,
      'data' => [
        'plugins' => [
          'foo',
        ],
      ],
      'validation_skipped' => 1,
    ]);
    $this->userData = $user_data
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertTrue($fixture
      ->isTfaRequired());

    // Not setup, no required roles matching the user.
    $user_data
      ->get('tfa', 3, 'tfa_user_settings')
      ->willReturn([
      'status' => 0,
      'saved' => FALSE,
      'data' => [
        'plugins' => [
          'foo',
        ],
      ],
      'validation_skipped' => 1,
    ]);
    $this->userData = $user_data
      ->reveal();
    $settings = $this
      ->prophesize(ImmutableConfig::class);
    $settings
      ->get('default_validation_plugin')
      ->willReturn('foo');
    $settings
      ->get('required_roles')
      ->willReturn([
      'foo' => 'foo',
    ]);
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('tfa.settings')
      ->willReturn($settings
      ->reveal());
    $this->configFactory = $config_factory
      ->reveal();
    $user = $this
      ->prophesize(UserInterface::class);
    $user
      ->id()
      ->willReturn(3);
    $user
      ->getRoles()
      ->willReturn([
      'bar' => 'bar',
    ]);
    $this->user = $user
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertFalse($fixture
      ->isTfaRequired());

    // Setup, matching roles.
    $user_data
      ->get('tfa', 3, 'tfa_user_settings')
      ->willReturn([
      'status' => 1,
      'saved' => FALSE,
      'data' => [
        'plugins' => [
          'foo',
        ],
      ],
      'validation_skipped' => 1,
    ]);
    $this->userData = $user_data
      ->reveal();
    $user = $this
      ->prophesize(UserInterface::class);
    $user
      ->id()
      ->willReturn(3);
    $user
      ->getRoles()
      ->willReturn([
      'foo' => 'foo',
      'bar' => 'bar',
    ]);
    $this->user = $user
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertTrue($fixture
      ->isTfaRequired());
  }

  /**
   * @covers ::isReady
   */
  public function testIsReady() {

    // Not ready.
    $settings = $this
      ->prophesize(ImmutableConfig::class);
    $settings
      ->get('default_validation_plugin')
      ->willReturn(FALSE);
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('tfa.settings')
      ->willReturn($settings
      ->reveal());
    $this->configFactory = $config_factory
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertFalse($fixture
      ->isReady());

    // Is ready.
    $settings
      ->get('default_validation_plugin')
      ->willReturn('foo');
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('tfa.settings')
      ->willReturn($settings
      ->reveal());
    $this->configFactory = $config_factory
      ->reveal();
    $validator = $this
      ->prophesize(TfaValidationInterface::class);
    $validator
      ->ready()
      ->willReturn(TRUE);
    $manager = $this
      ->prophesize(TfaValidationPluginManager::class);
    $manager
      ->createInstance('foo', [
      'uid' => 3,
    ])
      ->willReturn($validator
      ->reveal());
    $this->tfaValidationManager = $manager
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertTrue($fixture
      ->isReady());

    // Plugin set, but not ready.
    $validator = $this
      ->prophesize(TfaValidationInterface::class);
    $validator
      ->ready()
      ->willReturn(FALSE);
    $manager = $this
      ->prophesize(TfaValidationPluginManager::class);
    $manager
      ->createInstance('foo', [
      'uid' => 3,
    ])
      ->willReturn($validator
      ->reveal());
    $this->tfaValidationManager = $manager
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertFalse($fixture
      ->isReady());
  }

  /**
   * @covers ::remainingSkips
   */
  public function testRemainingSkips() {

    // No allowed skips.
    $settings = $this
      ->prophesize(ImmutableConfig::class);
    $settings
      ->get('default_validation_plugin')
      ->willReturn(FALSE);
    $settings
      ->get('validation_skip')
      ->willReturn(0);
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('tfa.settings')
      ->willReturn($settings
      ->reveal());
    $this->configFactory = $config_factory
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertFalse($fixture
      ->remainingSkips());

    // 3 allowed skips, user hasn't skipped any.
    $settings
      ->get('validation_skip')
      ->willReturn(3);
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('tfa.settings')
      ->willReturn($settings
      ->reveal());
    $this->configFactory = $config_factory
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertEquals(3, $fixture
      ->remainingSkips());

    // 3 allowed skips, user has skipped 2.
    $user_data = $this
      ->prophesize(UserDataInterface::class);
    $user_data
      ->get('tfa', 3, 'tfa_user_settings')
      ->willReturn([
      'status' => 1,
      'saved' => FALSE,
      'data' => [
        'plugins' => [
          'foo',
        ],
      ],
      'validation_skipped' => 2,
    ]);
    $this->userData = $user_data
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertEquals(1, $fixture
      ->remainingSkips());

    // User has exceeded attempts, check for 0 return.
    $user_data
      ->get('tfa', 3, 'tfa_user_settings')
      ->willReturn([
      'status' => 1,
      'saved' => FALSE,
      'data' => [
        'plugins' => [
          'foo',
        ],
      ],
      'validation_skipped' => 9,
    ]);
    $this->userData = $user_data
      ->reveal();
    $fixture = $this
      ->getFixture();
    $this
      ->assertEquals(0, $fixture
      ->remainingSkips());
  }

}

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.
TfaContextTest::$configFactory protected property The config factory.
TfaContextTest::$request protected property Current request object.
TfaContextTest::$tfaLoginManager protected property Login plugin manager.
TfaContextTest::$tfaSettings protected property Tfa settings config object.
TfaContextTest::$tfaValidationManager protected property Validation plugin manager.
TfaContextTest::$user protected property Entity for the user that is attempting to login.
TfaContextTest::$userData protected property User data service.
TfaContextTest::getFixture protected function Helper method to instantiate the test fixture.
TfaContextTest::setUp protected function Overrides UnitTestCase::setUp
TfaContextTest::testGetUser public function @covers ::getUser
TfaContextTest::testIsModuleSetup public function @covers ::isModuleSetup
TfaContextTest::testIsReady public function @covers ::isReady
TfaContextTest::testIsTfaRequired public function @covers ::isTfaRequired
TfaContextTest::testRemainingSkips public function @covers ::remainingSkips
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.