You are here

class LTIToolProviderControllerTest in LTI Tool Provider 8

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

Expanded class hierarchy of LTIToolProviderControllerTest

File

tests/src/Unit/LTIToolProviderControllerTest.php, line 29

Namespace

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

Namesort descending Modifiers Type Description Overrides
LTIToolProviderControllerTest::$configFactory protected property
LTIToolProviderControllerTest::$eventDispatcher protected property
LTIToolProviderControllerTest::$killSwitch protected property
LTIToolProviderControllerTest::$loggerFactory protected property
LTIToolProviderControllerTest::accessDataProvider public function
LTIToolProviderControllerTest::ltiLaunchWithContextDataProvider public function
LTIToolProviderControllerTest::ltiReturnWithContextDataProvider public function
LTIToolProviderControllerTest::setUp protected function Overrides UnitTestCase::setUp
LTIToolProviderControllerTest::testAccess public function @dataProvider accessDataProvider @covers ::access @covers ::__construct
LTIToolProviderControllerTest::testLtiLaunchNoContext public function @covers ::ltiLaunch @covers ::__construct
LTIToolProviderControllerTest::testLtiLaunchWithContext public function @dataProvider ltiLaunchWithContextDataProvider @covers ::ltiLaunch @covers ::__construct
LTIToolProviderControllerTest::testLtiReturnNoContext public function @covers ::ltiReturn @covers ::__construct
LTIToolProviderControllerTest::testLtiReturnWithContext public function @dataProvider ltiReturnWithContextDataProvider @covers ::ltiReturn @covers ::__construct
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.