You are here

class SimpleFbConnectPostLoginManagerTest in Simple FB Connect 8.3

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/SimpleFbConnectPostLoginManagerTest.php \Drupal\Tests\simple_fb_connect\Unit\SimpleFbConnectPostLoginManagerTest

@coversDefaultClass Drupal\simple_fb_connect\SimpleFbConnectPostLoginManager @group simple_fb_connect

Hierarchy

Expanded class hierarchy of SimpleFbConnectPostLoginManagerTest

File

tests/src/Unit/SimpleFbConnectPostLoginManagerTest.php, line 12

Namespace

Drupal\Tests\simple_fb_connect\Unit
View source
class SimpleFbConnectPostLoginManagerTest extends UnitTestCase {
  protected $configFactory;
  protected $requestContext;
  protected $pathValidator;
  protected $persistentDataHandler;
  protected $postLoginManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->configFactory = $this
      ->getConfigFactoryStub([
      'simple_fb_connect.settings' => [
        'post_login_path' => '<front>',
      ],
    ]);
    $this->requestContext = $this
      ->getMock('Drupal\\Core\\Routing\\RequestContext');
    $this->pathValidator = $this
      ->getMockBuilder('Drupal\\Core\\Path\\PathValidatorInterface')
      ->disableOriginalConstructor()
      ->setMethods([
      'getUrlIfValid',
      'toString',
    ])
      ->getMockForAbstractClass();
    $this->persistentDataHandler = $this
      ->getMockBuilder('Drupal\\simple_fb_connect\\SimpleFbConnectPersistentDataHandler')
      ->disableOriginalConstructor()
      ->getMock();
    $this->postLoginManager = new SimpleFbConnectPostLoginManager($this->configFactory, $this->requestContext, $this->pathValidator, $this->persistentDataHandler);
  }

  /**
   * Tests getPostLoginPathFromRequest when value is set.
   *
   * @covers ::getPostLoginPathFromRequest
   *
   * @dataProvider getPostLoginPathFromRequestDataProvider
   */
  public function testGetPostLoginPathFromRequest($input, $expected) {
    $this->requestContext
      ->expects($this
      ->once())
      ->method('getQueryString')
      ->willReturn($input);
    $this
      ->assertSame($expected, $this->postLoginManager
      ->getPostLoginPathFromRequest());
  }

  /**
   * Data provider for testPostLoginPathFromRequestWithValue().
   *
   * @return array
   *   Nested arrays of values to check
   *
   * @see ::testGetPostLoginPathFromRequest()
   */
  public function getPostLoginPathFromRequestDataProvider() {
    return [
      [
        'postLoginPath=<front>',
        '<front>',
      ],
      [
        'postLoginPath=node',
        'node',
      ],
      [
        '',
        FALSE,
      ],
      [
        NULL,
        FALSE,
      ],
      [
        'something=else',
        FALSE,
      ],
    ];
  }

  /**
   * Tests getPostLoginPath method with valid query parameter.
   *
   * @covers ::getPostLoginPath
   * @covers ::validateInternalPath
   */
  public function testGetPostLoginPathWithValidQueryParameter() {
    $query_path = 'node/1';
    $query_url = $this
      ->generateStubUrl(FALSE, $query_path);
    $this->persistentDataHandler
      ->expects($this
      ->once())
      ->method('get')
      ->with('post_login_path')
      ->willReturn($query_path);
    $this->pathValidator
      ->expects($this
      ->once())
      ->method('getUrlIfValid')
      ->willReturn($query_url);
    $this
      ->assertEquals($query_path, $this->postLoginManager
      ->getPostLoginPath());
  }

  /**
   * Tests getPostLoginPath method with invalid query parameter.
   *
   * In this situation we are expected to use the path found in module settings.
   *
   * @covers ::getPostLoginPath
   * @covers ::validateInternalPath
   */
  public function testGetPostLoginPathWithInvalidQueryParameter() {

    // 1. Path from query parameter.
    $query_path = 'http://www.example.com';
    $this->persistentDataHandler
      ->expects($this
      ->once())
      ->method('get')
      ->with('post_login_path')
      ->willReturn($query_path);
    $query_url = FALSE;

    // 2. Path from module settings.
    $module_path = $this->configFactory
      ->get('simple_fb_connect.settings')
      ->get('post_login_path');
    $module_url = $this
      ->generateStubUrl(FALSE, $module_path);
    $this->pathValidator
      ->expects($this
      ->any())
      ->method('getUrlIfValid')
      ->will($this
      ->onConsecutiveCalls($query_url, $module_url));
    $this
      ->assertEquals($module_path, $this->postLoginManager
      ->getPostLoginPath());
  }

  /**
   * Tests getPostLoginPath method with fallback to 'user'.
   *
   * @covers ::getPostLoginPath
   * @covers ::validateInternalPath
   */
  public function testPostLoginPathWithInvalidModulePath() {

    // 1. Path from query parameter.
    $query_path = 'http://www.example.com';
    $this->persistentDataHandler
      ->expects($this
      ->once())
      ->method('get')
      ->with('post_login_path')
      ->willReturn($query_path);
    $query_url = $this
      ->generateStubUrl(TRUE, $query_path);

    // 2. Module settings has invalid path so pathValidator will return FALSE
    // instead of an URL object.
    $module_url = FALSE;

    // 3. Fallback to 'user'.
    $fallback_path = 'user';
    $fallback_url = $this
      ->generateStubUrl(FALSE, $fallback_path);
    $this->pathValidator
      ->expects($this
      ->any())
      ->method('getUrlIfValid')
      ->will($this
      ->onConsecutiveCalls($query_url, $module_url, $fallback_url));
    $this
      ->assertEquals($fallback_path, $this->postLoginManager
      ->getPostLoginPath());
  }

  /**
   * Tests the getPathToUserForm() method.
   *
   * @covers ::getPathToUserForm
   */
  public function testGetPathToUserForm() {
    $user = $this
      ->getMockBuilder('Drupal\\user\\Entity\\User')
      ->disableOriginalConstructor()
      ->getMock();
    $user
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('1');
    $this->pathValidator
      ->expects($this
      ->once())
      ->method('getUrlIfValid')
      ->willReturn($this->pathValidator);
    $this->pathValidator
      ->expects($this
      ->once())
      ->method('toString')
      ->willReturn('user/1/edit');
    $this
      ->assertEquals('user/1/edit', $this->postLoginManager
      ->getPathToUserForm($user));
  }

  /**
   * Helper function to generate stub Url objects.
   *
   * @param bool $external
   *   Value to be returned from 'isExternal' method.
   * @param string $path
   *   Value to be returned from 'toString' method.
   */
  protected function generateStubUrl($external, $path) {
    $url = $this
      ->getMockBuilder('Drupal\\Core\\Url')
      ->disableOriginalConstructor()
      ->setMethods([
      'isExternal',
      'toString',
    ])
      ->getMock();
    $url
      ->expects($this
      ->any())
      ->method('isExternal')
      ->willReturn($external);
    $url
      ->expects($this
      ->any())
      ->method('toString')
      ->willReturn($path);
    return $url;
  }

}

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.
SimpleFbConnectPostLoginManagerTest::$configFactory protected property
SimpleFbConnectPostLoginManagerTest::$pathValidator protected property
SimpleFbConnectPostLoginManagerTest::$persistentDataHandler protected property
SimpleFbConnectPostLoginManagerTest::$postLoginManager protected property
SimpleFbConnectPostLoginManagerTest::$requestContext protected property
SimpleFbConnectPostLoginManagerTest::generateStubUrl protected function Helper function to generate stub Url objects.
SimpleFbConnectPostLoginManagerTest::getPostLoginPathFromRequestDataProvider public function Data provider for testPostLoginPathFromRequestWithValue().
SimpleFbConnectPostLoginManagerTest::setUp protected function Overrides UnitTestCase::setUp
SimpleFbConnectPostLoginManagerTest::testGetPathToUserForm public function Tests the getPathToUserForm() method.
SimpleFbConnectPostLoginManagerTest::testGetPostLoginPathFromRequest public function Tests getPostLoginPathFromRequest when value is set.
SimpleFbConnectPostLoginManagerTest::testGetPostLoginPathWithInvalidQueryParameter public function Tests getPostLoginPath method with invalid query parameter.
SimpleFbConnectPostLoginManagerTest::testGetPostLoginPathWithValidQueryParameter public function Tests getPostLoginPath method with valid query parameter.
SimpleFbConnectPostLoginManagerTest::testPostLoginPathWithInvalidModulePath public function Tests getPostLoginPath method with fallback to 'user'.
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.