class CustomPageExceptionHtmlSubscriberTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\CustomPageExceptionHtmlSubscriberTest
 
@coversDefaultClass \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber @group EventSubscriber
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\EventSubscriber\CustomPageExceptionHtmlSubscriberTest
 
 
Expanded class hierarchy of CustomPageExceptionHtmlSubscriberTest
File
- core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ CustomPageExceptionHtmlSubscriberTest.php, line 22  - Contains \Drupal\Tests\Core\EventSubscriber\CustomPageExceptionHtmlSubscriberTest.
 
Namespace
Drupal\Tests\Core\EventSubscriberView source
class CustomPageExceptionHtmlSubscriberTest extends UnitTestCase {
  /**
   * The mocked HTTP kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $kernel;
  /**
   * The mocked config factory
   *
   * @var  \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;
  /**
   * The mocked alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $aliasManager;
  /**
   * The mocked logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;
  /**
   * The PHP error log settings before the test.
   *
   * @var string
   */
  protected $errorLog;
  /**
   * The tested custom page exception subscriber.
   *
   * @var \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber|\Drupal\Tests\Core\EventSubscriber\TestCustomPageExceptionHtmlSubscriber
   */
  protected $customPageSubscriber;
  /**
   * The mocked redirect.destination service.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $redirectDestination;
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->configFactory = $this
      ->getConfigFactoryStub([
      'system.site' => [
        'page.403' => 'access-denied-page',
        'page.404' => 'not-found-page',
      ],
    ]);
    $this->aliasManager = $this
      ->getMock('Drupal\\Core\\Path\\AliasManagerInterface');
    $this->kernel = $this
      ->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $this->logger = $this
      ->getMock('Psr\\Log\\LoggerInterface');
    $this->redirectDestination = $this
      ->getMock('\\Drupal\\Core\\Routing\\RedirectDestinationInterface');
    $this->redirectDestination
      ->expects($this
      ->any())
      ->method('getAsArray')
      ->willReturn([
      'destination' => 'test',
    ]);
    $this->customPageSubscriber = new CustomPageExceptionHtmlSubscriber($this->configFactory, $this->aliasManager, $this->kernel, $this->logger, $this->redirectDestination);
    // You can't create an exception in PHP without throwing it. Store the
    // current error_log, and disable it temporarily.
    $this->errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
  }
  /**
   * {@inheritdoc}
   */
  protected function tearDown() {
    ini_set('error_log', $this->errorLog);
  }
  /**
   * Sets up an alias manager that does nothing.
   */
  protected function setupStubAliasManager() {
    $this->aliasManager
      ->expects($this
      ->any())
      ->method('getPathByAlias')
      ->willReturnArgument(0);
  }
  /**
   * Tests onHandleException with a POST request.
   */
  public function testHandleWithPostRequest() {
    $this
      ->setupStubAliasManager();
    $request = Request::create('/test', 'POST', array(
      'name' => 'druplicon',
      'pass' => '12345',
    ));
    $this->kernel
      ->expects($this
      ->once())
      ->method('handle')
      ->will($this
      ->returnCallback(function (Request $request) {
      return new Response($request
        ->getMethod());
    }));
    $event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', new NotFoundHttpException('foo'));
    $this->customPageSubscriber
      ->onException($event);
    $response = $event
      ->getResponse();
    $result = $response
      ->getContent() . " " . UrlHelper::buildQuery($request->request
      ->all());
    $this
      ->assertEquals('POST name=druplicon&pass=12345', $result);
  }
  /**
   * Tests onHandleException with a GET request.
   */
  public function testHandleWithGetRequest() {
    $this
      ->setupStubAliasManager();
    $request = Request::create('/test', 'GET', array(
      'name' => 'druplicon',
      'pass' => '12345',
    ));
    $this->kernel
      ->expects($this
      ->once())
      ->method('handle')
      ->will($this
      ->returnCallback(function (Request $request) {
      return new Response($request
        ->getMethod() . ' ' . UrlHelper::buildQuery($request->query
        ->all()));
    }));
    $event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', new NotFoundHttpException('foo'));
    $this->customPageSubscriber
      ->onException($event);
    $response = $event
      ->getResponse();
    $result = $response
      ->getContent() . " " . UrlHelper::buildQuery($request->request
      ->all());
    $this
      ->assertEquals('GET name=druplicon&pass=12345&destination=test&_exception_statuscode=404 ', $result);
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The mocked alias manager. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The mocked config factory | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The tested custom page exception subscriber. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The PHP error log settings before the test. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The mocked HTTP kernel. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The mocked logger. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | property | The mocked redirect.destination service. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | function | 
            Overrides UnitTestCase:: | 
                  |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | function | Sets up an alias manager that does nothing. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  protected | function | ||
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  public | function | Tests onHandleException with a GET request. | |
| 
            CustomPageExceptionHtmlSubscriberTest:: | 
                  public | function | Tests onHandleException with a POST request. | |
| 
            UnitTestCase:: | 
                  protected | property | The random generator. | |
| 
            UnitTestCase:: | 
                  protected | property | The app root. | |
| 
            UnitTestCase:: | 
                  protected | function | Asserts if two arrays are equal by sorting them first. | |
| 
            UnitTestCase:: | 
                  protected | function | Mocks a block with a block plugin. | |
| 
            UnitTestCase:: | 
                  protected | function | Returns a stub class resolver. | |
| 
            UnitTestCase:: | 
                  public | function | Returns a stub config factory that behaves according to the passed in 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. |