3 namespace Drupal\router_test;
5 use Drupal\Core\Cache\CacheableResponse;
6 use Drupal\Core\ParamConverter\ParamNotConvertedException;
7 use Drupal\user\UserInterface;
8 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpFoundation\Response;
11 use Zend\Diactoros\Response\HtmlResponse;
15 * Controller routines for testing the routing system.
17 class TestControllers {
19 public function test() {
20 return new Response('test');
23 public function test1() {
24 return new Response('test1');
27 public function test2() {
28 return ['#markup' => "test2"];
31 public function test3($value) {
32 return ['#markup' => $value];
35 public function test4($value) {
36 return ['#markup' => $value];
39 public function test5() {
40 return ['#markup' => "test5"];
43 public function test6() {
44 return new Response('test6');
47 public function test7() {
48 return new Response('test7text');
51 public function test8() {
52 return new Response('test8');
55 public function test9($uid) {
56 $text = 'Route not matched.';
58 $match = \Drupal::service('router.no_access_checks')->match('/user/' . $uid);
59 if (isset($match['user']) && $match['user'] instanceof UserInterface) {
60 $text = sprintf('User route "%s" was matched.', $match[RouteObjectInterface::ROUTE_NAME]);
63 catch (ParamNotConvertedException $e) {
65 return new Response($text);
69 * Test controller for ExceptionHandlingTest::testBacktraceEscaping().
71 * Passes unsafe HTML as an argument to a method which throws an exception.
72 * This can be used to test if the generated backtrace is properly escaped.
74 public function test10() {
75 $this->removeExceptionLogger();
76 $this->throwException('<script>alert(\'xss\')</script>');
79 public function test18() {
82 'contexts' => ['url'],
87 '#markup' => 'test18',
92 public function test21() {
93 return new CacheableResponse('test21');
96 public function test23() {
97 return new HtmlResponse('test23');
100 public function test24() {
101 $this->removeExceptionLogger();
102 throw new \Exception('Escaped content: <p> <br> <h3>');
105 public function test25() {
110 '#markup' => \Drupal::requestStack()->getCurrentRequest()->getUri(),
114 public function testRouteName(Request $request) {
116 '#markup' => $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
121 * Throws an exception.
123 * @param string $message
124 * The message to use in the exception.
129 protected function throwException($message) {
130 throw new \Exception($message);
133 protected function removeExceptionLogger() {
134 // Remove the exception logger from the event dispatcher. We are going to
135 // throw an exception to check if it is properly escaped when rendered as a
136 // backtrace. The exception logger does a call to error_log() which is not
137 // handled by the Simpletest error handler and would cause a test failure.
138 $event_dispatcher = \Drupal::service('event_dispatcher');
139 $exception_logger = \Drupal::service('exception.logger');
140 $event_dispatcher->removeSubscriber($exception_logger);