TestControllers.php 3.32 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Contains \Drupal\router_test\TestControllers.
6 7
 */

8 9
namespace Drupal\router_test;

10
use Drupal\Core\Cache\CacheableResponse;
11 12 13
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\user\UserInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
14
use Symfony\Component\HttpFoundation\Response;
15 16
use Zend\Diactoros\Response\HtmlResponse;

17 18

/**
19
 * Controller routines for testing the routing system.
20 21 22
 */
class TestControllers {

23 24 25 26
  public function test() {
    return new Response('test');
  }

27 28 29 30
  public function test1() {
    return new Response('test1');
  }

31
  public function test2() {
32
    return ['#markup' => "test2"];
33 34
  }

35
  public function test3($value) {
36
    return ['#markup' => $value];
37
  }
38

39
  public function test4($value) {
40
    return ['#markup' => $value];
41 42
  }

43
  public function test5() {
44
    return ['#markup' => "test5"];
45 46
  }

47 48 49 50 51 52 53 54 55 56 57 58
  public function test6() {
    return new Response('test6');
  }

  public function test7() {
    return new Response('test7text');
  }

  public function test8() {
    return new Response('test8');
  }

59 60 61
  public function test9($uid) {
    $text = 'Route not matched.';
    try {
62
      $match = \Drupal::service('router.no_access_checks')->match('/user/' . $uid);
63 64 65 66 67 68 69 70 71
      if (isset($match['user']) && $match['user'] instanceof UserInterface) {
        $text = sprintf('User route "%s" was matched.', $match[RouteObjectInterface::ROUTE_NAME]);
      }
    }
    catch (ParamNotConvertedException $e) {
    }
    return new Response($text);
  }

72 73 74 75 76 77 78
  /**
   * Test controller for ExceptionHandlingTest::testBacktraceEscaping().
   *
   * Passes unsafe HTML as an argument to a method which throws an exception.
   * This can be used to test if the generated backtrace is properly escaped.
   */
  public function test10() {
79
    $this->removeExceptionLogger();
80 81 82
    $this->throwException('<script>alert(\'xss\')</script>');
  }

83 84 85 86 87 88 89 90 91 92 93 94 95
  public function test18() {
    return [
      '#cache' => [
        'contexts' => ['url'],
        'tags' => ['foo'],
        'max-age' => 60,
      ],
      'content' => [
        '#markup' => 'test18',
      ],
    ];
  }

96 97 98 99
  public function test21() {
    return new CacheableResponse('test21');
  }

100 101 102 103
  public function test23() {
    return new HtmlResponse('test23');
  }

104 105 106 107 108
  public function test24() {
    $this->removeExceptionLogger();
    throw new \Exception('Escaped content: <p> <br> <h3>');
  }

109 110 111 112 113 114 115 116 117
  public function test25() {
    return [
      '#cache' => [
        'url',
      ],
      '#markup' => \Drupal::requestStack()->getCurrentRequest()->getUri(),
    ];
  }

118 119 120 121 122 123 124 125 126 127 128 129 130
  /**
   * Throws an exception.
   *
   * @param string $message
   *   The message to use in the exception.
   *
   * @throws \Exception
   *   Always thrown.
   */
  protected function throwException($message) {
    throw new \Exception($message);
  }

131 132 133 134 135 136 137 138 139 140
  protected function removeExceptionLogger() {
    // Remove the exception logger from the event dispatcher. We are going to
    // throw an exception to check if it is properly escaped when rendered as a
    // backtrace. The exception logger does a call to error_log() which is not
    // handled by the Simpletest error handler and would cause a test failure.
    $event_dispatcher = \Drupal::service('event_dispatcher');
    $exception_logger = \Drupal::service('exception.logger');
    $event_dispatcher->removeSubscriber($exception_logger);
  }

141
}