You are here

class LTIToolProviderTest in LTI Tool Provider 8

Same name and namespace in other branches
  1. 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

Expanded class hierarchy of LTIToolProviderTest

File

tests/src/Unit/LTIToolProviderTest.php, line 34

Namespace

Drupal\Tests\lti_tool_provider\Unit
View 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

Namesort descending Modifiers Type Description Overrides
LTIToolProviderTest::$configFactory protected property The mocked configuration factory.
LTIToolProviderTest::$entityTypeManager protected property The mocked Entity Manager.
LTIToolProviderTest::$eventDispatcher protected property The mocked module handler.
LTIToolProviderTest::$loggerFactory protected property A mocked logger instance.
LTIToolProviderTest::$provider protected property The mocked PECL OauthProvider class.
LTIToolProviderTest::appliesProvider public function Request Provider.
LTIToolProviderTest::getNonceSpecificLtiToolProvider public function Generate a entity type manager for testing timestampNonceHandler.
LTIToolProviderTest::setUp protected function Overrides UnitTestCase::setUp
LTIToolProviderTest::testApplies public function Test the applies() method.
LTIToolProviderTest::testTimestampDuplicateNonce public function Tests duplicate nonces.
LTIToolProviderTest::testTimestampNonceHandlerAlmostTooNew public function Tests a nonce timestamp that is almost too new.
LTIToolProviderTest::testTimestampNonceHandlerAlmostTooOld public function Tests a nonce timestamp that is almost too old.
LTIToolProviderTest::testTimestampNonceHandlerCurrent public function Tests a nonce timestamp that is current.
LTIToolProviderTest::testTimestampNonceHandlerMissingNonceConsumer public function Test the timestampNonceHandler() method.
LTIToolProviderTest::testTimestampNonceHandlerMissingTimestamp public function Test the timestampNonceHandler() method.
LTIToolProviderTest::testTimestampNonceHandlerOld public function Tests a nonce timestamp that is too old.
LTIToolProviderTest::testTimestampNonceHandlerTooNew public function Tests a nonce timestamp that is too new.
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.
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.