View source
<?php
namespace Drupal\Tests\simple_fb_connect\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\simple_fb_connect\SimpleFbConnectPostLoginManager;
class SimpleFbConnectPostLoginManagerTest extends UnitTestCase {
protected $configFactory;
protected $requestContext;
protected $pathValidator;
protected $persistentDataHandler;
protected $postLoginManager;
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);
}
public function testGetPostLoginPathFromRequest($input, $expected) {
$this->requestContext
->expects($this
->once())
->method('getQueryString')
->willReturn($input);
$this
->assertSame($expected, $this->postLoginManager
->getPostLoginPathFromRequest());
}
public function getPostLoginPathFromRequestDataProvider() {
return [
[
'postLoginPath=<front>',
'<front>',
],
[
'postLoginPath=node',
'node',
],
[
'',
FALSE,
],
[
NULL,
FALSE,
],
[
'something=else',
FALSE,
],
];
}
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());
}
public function testGetPostLoginPathWithInvalidQueryParameter() {
$query_path = 'http://www.example.com';
$this->persistentDataHandler
->expects($this
->once())
->method('get')
->with('post_login_path')
->willReturn($query_path);
$query_url = FALSE;
$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());
}
public function testPostLoginPathWithInvalidModulePath() {
$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);
$module_url = FALSE;
$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());
}
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));
}
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;
}
}