class LTIToolProviderControllerTest in LTI Tool Provider 8
Same name and namespace in other branches
- 2.x tests/src/Unit/LTIToolProviderControllerTest.php \Drupal\Tests\lti_tool_provider\Unit\LTIToolProviderControllerTest
LTIToolProviderController unit tests.
@group lti_tool_provider
@coversDefaultClass \Drupal\lti_tool_provider\Controller\LTIToolProviderController
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\lti_tool_provider\Unit\LTIToolProviderControllerTest
Expanded class hierarchy of LTIToolProviderControllerTest
File
- tests/
src/ Unit/ LTIToolProviderControllerTest.php, line 29
Namespace
Drupal\Tests\lti_tool_provider\UnitView source
class LTIToolProviderControllerTest extends UnitTestCase {
/**
* @var ConfigFactoryInterface|MockObject
*/
protected $configFactory;
/**
* @var LoggerChannelFactoryInterface|MockObject
*/
protected $loggerFactory;
/**
* @var EventDispatcherInterface|MockObject
*/
protected $eventDispatcher;
/**
* @var ResponsePolicyInterface|MockObject
*/
protected $killSwitch;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->configFactory = $this
->createMock('\\Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->eventDispatcher = $this
->getMockBuilder('\\Symfony\\Component\\EventDispatcher\\EventDispatcher')
->onlyMethods([
'__construct',
])
->disableOriginalConstructor()
->getMock();
$this->killSwitch = $this
->getMockBuilder('\\Drupal\\Core\\PageCache\\ResponsePolicy\\KillSwitch')
->addMethods([
'__construct',
])
->disableOriginalConstructor()
->getMock();
$this->loggerFactory = $this
->getMockBuilder('\\Drupal\\Core\\Logger\\LoggerChannelFactory')
->addMethods([
'__construct',
])
->disableOriginalConstructor()
->getMock();
}
/**
* @dataProvider accessDataProvider
* @covers ::access
* @covers ::__construct
* @param $expected
* @param Request $request
* @param SessionInterface $session
* @param mixed $context
* @param string | null $destination
*/
public function testAccess($expected, Request $request, SessionInterface $session, $context, ?string $destination) {
$controller = new LTIToolProviderController($this->configFactory, $this->loggerFactory, $this->eventDispatcher, $this->killSwitch, $request, $session, $context, $destination);
$actual = $controller
->access();
$this
->assertInstanceOf(AccessResult::class, $actual);
$this
->assertEquals($expected, $actual);
}
/**
* @return array
*/
public function accessDataProvider() : array {
return [
'no context' => [
AccessResult::neutral(),
Request::create('/lti'),
new Session(),
null,
null,
],
'with context' => [
AccessResult::allowed(),
Request::create('/lti'),
new Session(),
[
1,
],
null,
],
];
}
/**
* @covers ::ltiLaunch
* @covers ::__construct
*/
public function testLtiLaunchNoContext() {
$controller = new LTIToolProviderController($this->configFactory, $this->loggerFactory, $this->eventDispatcher, $this->killSwitch, new Request(), new Session(), null, '/');
$this
->expectException(InvalidArgumentException::class);
$controller
->ltiLaunch();
}
/**
* @dataProvider ltiLaunchWithContextDataProvider
* @covers ::ltiLaunch
* @covers ::__construct
* @param RedirectResponse $expected
* @param Request $request
* @param SessionInterface $session
* @param mixed $context
* @param string | null $destination
*/
public function testLtiLaunchWithContext(RedirectResponse $expected, Request $request, SessionInterface $session, $context, ?string $destination) {
$controller = new LTIToolProviderController($this->configFactory, $this->loggerFactory, $this->eventDispatcher, $this->killSwitch, $request, $session, $context, $destination);
$actual = $controller
->ltiLaunch();
$this
->assertInstanceOf(RedirectResponse::class, $actual);
$this
->assertEquals($expected
->getTargetUrl(), $actual
->getTargetUrl());
}
/**
* @return array
*/
public function ltiLaunchWithContextDataProvider() : array {
return [
'destination from settings' => [
new RedirectResponse('/'),
Request::create('/lti'),
new Session(),
[
'no destination url',
],
'/',
],
'destination from context' => [
new RedirectResponse('/'),
Request::create('/lti'),
new Session(),
[
'custom_destination' => '/',
],
null,
],
];
}
/**
* @covers ::ltiReturn
* @covers ::__construct
*/
public function testLtiReturnNoContext() {
$controller = new LTIToolProviderController($this->configFactory, $this->loggerFactory, $this->eventDispatcher, $this->killSwitch, new Request(), new Session(), null, '/');
$this
->expectException(InvalidArgumentException::class);
$controller
->ltiReturn();
}
/**
* @dataProvider ltiReturnWithContextDataProvider
* @covers ::ltiReturn
* @covers ::__construct
* @param TrustedRedirectResponse $expected
* @param Request $request
* @param SessionInterface $session
* @param mixed $context
* @param string | null $destination
*/
public function testLtiReturnWithContext(TrustedRedirectResponse $expected, Request $request, SessionInterface $session, $context, ?string $destination) {
/* @var $controller LTIToolProviderController */
$controller = $this
->getMockBuilder('Drupal\\lti_tool_provider\\Controller\\LTIToolProviderController')
->setConstructorArgs([
$this->configFactory,
$this->loggerFactory,
$this->eventDispatcher,
$this->killSwitch,
$request,
$session,
$context,
$destination,
])
->onlyMethods([
'userLogout',
])
->getMock();
$actual = $controller
->ltiReturn();
$this
->assertInstanceOf(TrustedRedirectResponse::class, $actual);
$this
->assertEquals($expected
->getTargetUrl(), $actual
->getTargetUrl());
}
/**
* @return array
*/
public function ltiReturnWithContextDataProvider() : array {
return [
'destination from settings' => [
new TrustedRedirectResponse('/home'),
Request::create('/lti'),
new Session(),
[
'no destination url',
],
'/home',
],
'destination from context' => [
new TrustedRedirectResponse('/home'),
Request::create('/lti'),
new Session(),
[
'launch_presentation_return_url' => '/home',
],
null,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LTIToolProviderControllerTest:: |
protected | property | ||
LTIToolProviderControllerTest:: |
protected | property | ||
LTIToolProviderControllerTest:: |
protected | property | ||
LTIToolProviderControllerTest:: |
protected | property | ||
LTIToolProviderControllerTest:: |
public | function | ||
LTIToolProviderControllerTest:: |
public | function | ||
LTIToolProviderControllerTest:: |
public | function | ||
LTIToolProviderControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
LTIToolProviderControllerTest:: |
public | function | @dataProvider accessDataProvider @covers ::access @covers ::__construct | |
LTIToolProviderControllerTest:: |
public | function | @covers ::ltiLaunch @covers ::__construct | |
LTIToolProviderControllerTest:: |
public | function | @dataProvider ltiLaunchWithContextDataProvider @covers ::ltiLaunch @covers ::__construct | |
LTIToolProviderControllerTest:: |
public | function | @covers ::ltiReturn @covers ::__construct | |
LTIToolProviderControllerTest:: |
public | function | @dataProvider ltiReturnWithContextDataProvider @covers ::ltiReturn @covers ::__construct | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |