class CasRouteEnhancerTest in CAS 8
Same name and namespace in other branches
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\cas\Unit\Routing\CasRouteEnhancerTest
 
Expanded class hierarchy of CasRouteEnhancerTest
File
- tests/src/ Unit/ Routing/ CasRouteEnhancerTest.php, line 16 
Namespace
Drupal\Tests\cas\Unit\RoutingView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| CasRouteEnhancerTest:: | protected | property | The mocked CasHelper. | |
| CasRouteEnhancerTest:: | protected | property | The mocked Request. | |
| CasRouteEnhancerTest:: | protected | property | The mocked Route. | |
| CasRouteEnhancerTest:: | public | function | Provides configuration values for testEnhance() | |
| CasRouteEnhancerTest:: | protected | function | Overrides UnitTestCase:: | |
| CasRouteEnhancerTest:: | public | function | Test the constructor. | |
| CasRouteEnhancerTest:: | public | function | Tests the enhance() method. | |
| 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. | 
