class LTIToolProviderTest in LTI Tool Provider 8
Same name and namespace in other branches
- 2.x tests/src/Unit/LTIToolProviderTest.php \Drupal\Tests\lti_tool_provider\Unit\LTIToolProviderTest
LTIToolProvider unit tests.
@group lti_tool_provider
@coversDefaultClass \Drupal\lti_tool_provider\Authentication\Provider\LTIToolProvider
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\lti_tool_provider\Unit\LTIToolProviderTest
Expanded class hierarchy of LTIToolProviderTest
File
- tests/
src/ Unit/ LTIToolProviderTest.php, line 34
Namespace
Drupal\Tests\lti_tool_provider\UnitView source
class LTIToolProviderTest extends UnitTestCase {
/**
* The mocked configuration factory.
*
* @var ConfigFactoryInterface|MockObject
*/
protected $configFactory;
/**
* The mocked Entity Manager.
*
* @var EntityTypeManagerInterface|MockObject
*/
protected $entityTypeManager;
/**
* A mocked logger instance.
*
* @var LoggerChannelFactoryInterface|MockObject
*/
protected $loggerFactory;
/**
* The mocked module handler.
*
* @var EventDispatcherInterface|MockObject
*/
protected $eventDispatcher;
/**
* The mocked PECL OauthProvider class.
*
* @var OauthProvider | mixed
*/
protected $provider;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->configFactory = $this
->createMock('\\Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->entityTypeManager = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->eventDispatcher = $this
->getMockBuilder('\\Symfony\\Component\\EventDispatcher\\EventDispatcher')
->onlyMethods([
'__construct',
])
->disableOriginalConstructor()
->getMock();
$this->loggerFactory = $this
->getMockBuilder('\\Drupal\\Core\\Logger\\LoggerChannelFactory')
->addMethods([
'__construct',
])
->disableOriginalConstructor()
->getMock();
$this->provider = $this
->getMockBuilder('\\OAuthProvider')
->onlyMethods([
'__construct',
'checkOAuthRequest',
])
->disableOriginalConstructor()
->getMock();
$this->provider
->expects($this
->any())
->method('checkOAuthRequest')
->will($this
->returnValue(true));
}
/**
* Test the applies() method.
*
* @dataProvider appliesProvider
* @covers ::applies
* @covers ::__construct
* @param $expected
* @param $request
*/
public function testApplies($expected, $request) {
$provider = new LTIToolProvider($this->configFactory, $this->entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
$actual = $provider
->applies($request);
$this
->assertEquals($expected, $actual);
}
/**
* Request Provider.
*/
public function appliesProvider() : array {
return [
'empty request' => [
false,
Request::create('/lti', 'POST', []),
],
'get request' => [
false,
Request::create('/lti', 'GET', [
'oauth_consumer_key' => 'oauth_consumer_key',
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p0',
'resource_link_id' => 'resource_link_id',
]),
],
'LTI-1p0 request' => [
true,
Request::create('/lti', 'POST', [
'oauth_consumer_key' => 'oauth_consumer_key',
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p0',
'resource_link_id' => 'resource_link_id',
]),
],
'LTI-1p2 request' => [
true,
Request::create('/lti', 'POST', [
'oauth_consumer_key' => 'oauth_consumer_key',
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p2',
'resource_link_id' => 'resource_link_id',
]),
],
'missing resource link request' => [
false,
Request::create('/lti', 'POST', [
'oauth_consumer_key' => 'oauth_consumer_key',
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p0',
]),
],
'empty resource link request' => [
false,
Request::create('/lti', 'POST', [
'oauth_consumer_key' => 'oauth_consumer_key',
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p0',
'resource_link_id' => '',
]),
],
'missing oauth consumer key request' => [
false,
Request::create('/lti', 'POST', [
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p0',
'resource_link_id' => 'resource_link_id',
]),
],
'empty oauth_consumer_key request' => [
false,
Request::create('/lti', 'POST', [
'oauth_consumer_key' => '',
'lti_message_type' => 'basic-lti-launch-request',
'lti_version' => 'LTI-1p0',
'resource_link_id' => 'resource_link_id',
]),
],
];
}
/**
* Test the timestampNonceHandler() method.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerMissingTimestamp() {
$provider = new LTIToolProvider($this->configFactory, $this->entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
$expected = OAUTH_BAD_TIMESTAMP;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Test the timestampNonceHandler() method.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerMissingNonceConsumer() {
$provider = new LTIToolProvider($this->configFactory, $this->entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
$this->provider->timestamp = time();
$expected = OAUTH_BAD_NONCE;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Tests a nonce timestamp that is too old.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerOld() {
$provider = new LTIToolProvider($this->configFactory, $this->entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
$this->provider->consumer_key = '';
$this->provider->nonce = uniqid();
$this->provider->timestamp = time() - LTI_TOOL_PROVIDER_NONCE_INTERVAL - 10;
$expected = OAUTH_BAD_TIMESTAMP;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Tests a nonce timestamp that is almost too old.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerAlmostTooOld() {
$provider = $this
->getNonceSpecificLtiToolProvider();
$this->provider->consumer_key = '';
$this->provider->nonce = uniqid();
$this->provider->timestamp = time() - LTI_TOOL_PROVIDER_NONCE_INTERVAL;
$expected = OAUTH_OK;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Tests a nonce timestamp that is current.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerCurrent() {
$provider = $this
->getNonceSpecificLtiToolProvider();
$this->provider->consumer_key = '';
$this->provider->nonce = uniqid();
$this->provider->timestamp = time();
$expected = OAUTH_OK;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Tests a nonce timestamp that is almost too new.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerAlmostTooNew() {
$provider = $this
->getNonceSpecificLtiToolProvider();
$this->provider->consumer_key = '';
$this->provider->nonce = uniqid();
$this->provider->timestamp = time() + LTI_TOOL_PROVIDER_NONCE_INTERVAL;
$expected = OAUTH_OK;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Tests a nonce timestamp that is too new.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampNonceHandlerTooNew() {
$provider = new LTIToolProvider($this->configFactory, $this->entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
$this->provider->consumer_key = '';
$this->provider->nonce = uniqid();
$this->provider->timestamp = time() + LTI_TOOL_PROVIDER_NONCE_INTERVAL + 10;
$expected = OAUTH_BAD_TIMESTAMP;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
/**
* Generate a entity type manager for testing timestampNonceHandler.
*/
public function getNonceSpecificLtiToolProvider() : LTIToolProvider {
$entityTypeManager = $this->entityTypeManager;
$query = $this
->createMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
$query
->expects($this
->once())
->method('condition')
->will($this
->returnValue($query));
$query
->expects($this
->once())
->method('execute')
->will($this
->returnValue([]));
$storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('getQuery')
->will($this
->returnValue($query));
$storage
->expects($this
->once())
->method('create')
->will($this
->returnValue($this
->createMock('Drupal\\Core\\Entity\\EntityInterface')));
$entityTypeManager
->expects($this
->once())
->method('getStorage')
->will($this
->returnValue($storage));
return new LTIToolProvider($this->configFactory, $entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
}
/**
* Tests duplicate nonces.
*
* @covers ::timestampNonceHandler
* @covers ::__construct
*/
public function testTimestampDuplicateNonce() {
$entityTypeManager = $this->entityTypeManager;
$this->provider->consumer_key = '';
$this->provider->nonce = uniqid();
$this->provider->timestamp = time();
$query = $this
->createMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
$query
->expects($this
->once())
->method('condition')
->will($this
->returnValue($query));
$query
->expects($this
->once())
->method('execute')
->will($this
->returnValue([
$this
->createMock('Drupal\\Core\\Entity\\EntityInterface'),
]));
$storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('getQuery')
->will($this
->returnValue($query));
$entityTypeManager
->expects($this
->once())
->method('getStorage')
->will($this
->returnValue($storage));
$provider = new LTIToolProvider($this->configFactory, $entityTypeManager, $this->loggerFactory, $this->eventDispatcher);
$expected = OAUTH_BAD_NONCE;
$actual = $provider
->timestampNonceHandler($this->provider);
$this
->assertEquals($expected, $actual);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LTIToolProviderTest:: |
protected | property | The mocked configuration factory. | |
LTIToolProviderTest:: |
protected | property | The mocked Entity Manager. | |
LTIToolProviderTest:: |
protected | property | The mocked module handler. | |
LTIToolProviderTest:: |
protected | property | A mocked logger instance. | |
LTIToolProviderTest:: |
protected | property | The mocked PECL OauthProvider class. | |
LTIToolProviderTest:: |
public | function | Request Provider. | |
LTIToolProviderTest:: |
public | function | Generate a entity type manager for testing timestampNonceHandler. | |
LTIToolProviderTest:: |
protected | function |
Overrides UnitTestCase:: |
|
LTIToolProviderTest:: |
public | function | Test the applies() method. | |
LTIToolProviderTest:: |
public | function | Tests duplicate nonces. | |
LTIToolProviderTest:: |
public | function | Tests a nonce timestamp that is almost too new. | |
LTIToolProviderTest:: |
public | function | Tests a nonce timestamp that is almost too old. | |
LTIToolProviderTest:: |
public | function | Tests a nonce timestamp that is current. | |
LTIToolProviderTest:: |
public | function | Test the timestampNonceHandler() method. | |
LTIToolProviderTest:: |
public | function | Test the timestampNonceHandler() method. | |
LTIToolProviderTest:: |
public | function | Tests a nonce timestamp that is too old. | |
LTIToolProviderTest:: |
public | function | Tests a nonce timestamp that is too new. | |
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. |