adebf28ddd312de2786cba205c9abefb14fe7510
[yaffs-website] / web / core / modules / system / tests / modules / router_test_directory / src / TestControllers.php
1 <?php
2
3 namespace Drupal\router_test;
4
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;
12
13 /**
14  * Controller routines for testing the routing system.
15  */
16 class TestControllers {
17
18   public function test() {
19     return new Response('test');
20   }
21
22   public function test1() {
23     return new Response('test1');
24   }
25
26   public function test2() {
27     return ['#markup' => "test2"];
28   }
29
30   public function test3($value) {
31     return ['#markup' => $value];
32   }
33
34   public function test4($value) {
35     return ['#markup' => $value];
36   }
37
38   public function test5() {
39     return ['#markup' => "test5"];
40   }
41
42   public function test6() {
43     return new Response('test6');
44   }
45
46   public function test7() {
47     return new Response('test7text');
48   }
49
50   public function test8() {
51     return new Response('test8');
52   }
53
54   public function test9($uid) {
55     $text = 'Route not matched.';
56     try {
57       $match = \Drupal::service('router.no_access_checks')->match('/user/' . $uid);
58       if (isset($match['user']) && $match['user'] instanceof UserInterface) {
59         $text = sprintf('User route "%s" was matched.', $match[RouteObjectInterface::ROUTE_NAME]);
60       }
61     }
62     catch (ParamNotConvertedException $e) {
63     }
64     return new Response($text);
65   }
66
67   /**
68    * Test controller for ExceptionHandlingTest::testBacktraceEscaping().
69    *
70    * Passes unsafe HTML as an argument to a method which throws an exception.
71    * This can be used to test if the generated backtrace is properly escaped.
72    */
73   public function test10() {
74     $this->removeExceptionLogger();
75     $this->throwException('<script>alert(\'xss\')</script>');
76   }
77
78   public function test18() {
79     return [
80       '#cache' => [
81         'contexts' => ['url'],
82         'tags' => ['foo'],
83         'max-age' => 60,
84       ],
85       'content' => [
86         '#markup' => 'test18',
87       ],
88     ];
89   }
90
91   public function test21() {
92     return new CacheableResponse('test21');
93   }
94
95   public function test23() {
96     return new HtmlResponse('test23');
97   }
98
99   public function test24() {
100     $this->removeExceptionLogger();
101     throw new \Exception('Escaped content: <p> <br> <h3>');
102   }
103
104   public function test25() {
105     return [
106       '#cache' => [
107         'url',
108       ],
109       '#markup' => \Drupal::requestStack()->getCurrentRequest()->getUri(),
110     ];
111   }
112
113   public function testRouteName(Request $request) {
114     return [
115       '#markup' => $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
116     ];
117   }
118
119   /**
120    * Throws an exception.
121    *
122    * @param string $message
123    *   The message to use in the exception.
124    *
125    * @throws \Exception
126    *   Always thrown.
127    */
128   protected function throwException($message) {
129     throw new \Exception($message);
130   }
131
132   protected function removeExceptionLogger() {
133     // Remove the exception logger from the event dispatcher. We are going to
134     // throw an exception to check if it is properly escaped when rendered as a
135     // backtrace. The exception logger does a call to error_log() which is not
136     // handled by the Simpletest error handler and would cause a test failure.
137     $event_dispatcher = \Drupal::service('event_dispatcher');
138     $exception_logger = \Drupal::service('exception.logger');
139     $event_dispatcher->removeSubscriber($exception_logger);
140   }
141
142 }