Version 1
[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 /**
15  * Controller routines for testing the routing system.
16  */
17 class TestControllers {
18
19   public function test() {
20     return new Response('test');
21   }
22
23   public function test1() {
24     return new Response('test1');
25   }
26
27   public function test2() {
28     return ['#markup' => "test2"];
29   }
30
31   public function test3($value) {
32     return ['#markup' => $value];
33   }
34
35   public function test4($value) {
36     return ['#markup' => $value];
37   }
38
39   public function test5() {
40     return ['#markup' => "test5"];
41   }
42
43   public function test6() {
44     return new Response('test6');
45   }
46
47   public function test7() {
48     return new Response('test7text');
49   }
50
51   public function test8() {
52     return new Response('test8');
53   }
54
55   public function test9($uid) {
56     $text = 'Route not matched.';
57     try {
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]);
61       }
62     }
63     catch (ParamNotConvertedException $e) {
64     }
65     return new Response($text);
66   }
67
68   /**
69    * Test controller for ExceptionHandlingTest::testBacktraceEscaping().
70    *
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.
73    */
74   public function test10() {
75     $this->removeExceptionLogger();
76     $this->throwException('<script>alert(\'xss\')</script>');
77   }
78
79   public function test18() {
80     return [
81       '#cache' => [
82         'contexts' => ['url'],
83         'tags' => ['foo'],
84         'max-age' => 60,
85       ],
86       'content' => [
87         '#markup' => 'test18',
88       ],
89     ];
90   }
91
92   public function test21() {
93     return new CacheableResponse('test21');
94   }
95
96   public function test23() {
97     return new HtmlResponse('test23');
98   }
99
100   public function test24() {
101     $this->removeExceptionLogger();
102     throw new \Exception('Escaped content: <p> <br> <h3>');
103   }
104
105   public function test25() {
106     return [
107       '#cache' => [
108         'url',
109       ],
110       '#markup' => \Drupal::requestStack()->getCurrentRequest()->getUri(),
111     ];
112   }
113
114   public function testRouteName(Request $request) {
115     return [
116       '#markup' => $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
117     ];
118   }
119
120   /**
121    * Throws an exception.
122    *
123    * @param string $message
124    *   The message to use in the exception.
125    *
126    * @throws \Exception
127    *   Always thrown.
128    */
129   protected function throwException($message) {
130     throw new \Exception($message);
131   }
132
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);
141   }
142
143 }