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 $session;
protected $pathValidator;
protected $postLoginManager;
protected function setUp() {
parent::setUp();
$this->configFactory = $this
->getConfigFactoryStub(array(
'simple_fb_connect.settings' => array(
'post_login_path' => '<front>',
),
));
$this->requestContext = $this
->getMock('Drupal\\Core\\Routing\\RequestContext');
$this->session = $this
->getMock('Symfony\\Component\\HttpFoundation\\Session\\SessionInterface');
$this->pathValidator = $this
->getMockBuilder('Drupal\\Core\\Path\\PathValidatorInterface')
->disableOriginalConstructor()
->setMethods(array(
'getUrlIfValid',
'toString',
))
->getMockForAbstractClass();
$this->postLoginManager = new SimpleFbConnectPostLoginManager($this->configFactory, $this->session, $this->requestContext, $this->pathValidator);
}
public function testGetPostLoginPathFromRequest($input, $expected) {
$this->requestContext
->expects($this
->once())
->method('getQueryString')
->willReturn($input);
$this
->assertSame($expected, $this->postLoginManager
->getPostLoginPathFromRequest());
}
public function getPostLoginPathFromRequestDataProvider() {
return array(
array(
'postLoginPath=<front>',
'<front>',
),
array(
'postLoginPath=node',
'node',
),
array(
'',
FALSE,
),
array(
NULL,
FALSE,
),
array(
'something=else',
FALSE,
),
);
}
public function testGetPostLoginPathWithValidQueryParameter() {
$query_path = 'node/1';
$query_url = $this
->generateStubUrl(FALSE, $query_path);
$this->session
->expects($this
->once())
->method('get')
->with('simple_fb_connect_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->session
->expects($this
->once())
->method('get')
->with('simple_fb_connect_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->session
->expects($this
->once())
->method('get')
->with('simple_fb_connect_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(array(
'isExternal',
'toString',
))
->getMock();
$url
->expects($this
->any())
->method('isExternal')
->willReturn($external);
$url
->expects($this
->any())
->method('toString')
->willReturn($path);
return $url;
}
}