You are here

class CasRouteEnhancerTest in CAS 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Routing/CasRouteEnhancerTest.php \Drupal\Tests\cas\Unit\Routing\CasRouteEnhancerTest

CasRouteEnhancer unit tests.

@group cas

@coversDefaultClass \Drupal\cas\Routing\CasRouteEnhancer

Hierarchy

Expanded class hierarchy of CasRouteEnhancerTest

File

tests/src/Unit/Routing/CasRouteEnhancerTest.php, line 16

Namespace

Drupal\Tests\cas\Unit\Routing
View source
class CasRouteEnhancerTest extends UnitTestCase {

  /**
   * The mocked CasHelper.
   *
   * @var \Drupal\cas\Service\CasHelper|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $casHelper;

  /**
   * The mocked Request.
   *
   * @var \Symfony\Component\HttpFoundation\Request|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $request;

  /**
   * The mocked Route.
   *
   * @var \Symfony\Component\Routing\Route|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $route;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->casHelper = $this
      ->getMockBuilder('\\Drupal\\cas\\Service\\CasHelper')
      ->disableOriginalConstructor()
      ->getMock();
    $this->request = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Request')
      ->disableOriginalConstructor()
      ->getMock();
    $this->route = $this
      ->getMockBuilder('\\Symfony\\Component\\Routing\\Route')
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Test the constructor.
   *
   * @covers ::__construct
   */
  public function testConstruct() {
    $this
      ->assertInstanceOf('\\Drupal\\cas\\Routing\\CasRouteEnhancer', new CasRouteEnhancer($this
      ->getConfigFactoryStub()));
  }

  /**
   * Tests the enhance() method.
   *
   * @covers ::enhance
   *
   * @dataProvider enhanceDataProvider
   */
  public function testEnhance($path, $cas_logout_enabled, $is_cas_user) {
    $session = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Session')
      ->disableOriginalConstructor()
      ->setMethods([
      'get',
    ])
      ->getMock();
    $session
      ->expects($this
      ->any())
      ->method('get')
      ->with('is_cas_user')
      ->willReturn($is_cas_user);
    $this->request
      ->expects($this
      ->any())
      ->method('getSession')
      ->willReturn($session);
    $this->route
      ->expects($this
      ->any())
      ->method('getPath')
      ->willReturn($path);
    $enhancer = new CasRouteEnhancer($this
      ->getConfigFactoryStub([
      'cas.settings' => [
        'logout.cas_logout' => $cas_logout_enabled,
      ],
    ]));
    $originalDefaults = [
      '_route_object' => $this->route,
    ];
    $newDefaults = $enhancer
      ->enhance($originalDefaults, $this->request);

    // The controller should only be changed to our custom logout controller
    // if CAS logout is enabled AND the currently logged in user logged in
    // via CAS AND we're on the correct path.
    if ($path == '/user/logout' && $cas_logout_enabled && $is_cas_user) {
      $this
        ->assertArrayHasKey('_controller', $newDefaults, '$newDefaults array does not contain "_controller" key.');
      $this
        ->assertNotEmpty($newDefaults['_controller']);
      $this
        ->assertEquals($newDefaults['_controller'], '\\Drupal\\cas\\Controller\\LogoutController::logout');
    }
    else {
      $this
        ->assertEquals($originalDefaults, $newDefaults);
    }
  }

  /**
   * Provides configuration values for testEnhance()
   *
   * @return array
   *   Parameters.
   *
   * @see \Drupal\Tests\cas\Unit\Routing\CasRouteEnhancerTest::testEnhance
   */
  public function enhanceDataProvider() {
    $params = [
      [
        '/user/logout',
        FALSE,
        FALSE,
      ],
      [
        '/user/logout',
        TRUE,
        FALSE,
      ],
      [
        '/user/logout',
        FALSE,
        TRUE,
      ],
      [
        '/user/logout',
        TRUE,
        TRUE,
      ],
      [
        '/foobar',
        TRUE,
        TRUE,
      ],
    ];
    return $params;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CasRouteEnhancerTest::$casHelper protected property The mocked CasHelper.
CasRouteEnhancerTest::$request protected property The mocked Request.
CasRouteEnhancerTest::$route protected property The mocked Route.
CasRouteEnhancerTest::enhanceDataProvider public function Provides configuration values for testEnhance()
CasRouteEnhancerTest::setUp protected function Overrides UnitTestCase::setUp
CasRouteEnhancerTest::testConstruct public function Test the constructor.
CasRouteEnhancerTest::testEnhance public function Tests the enhance() method.
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.