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;
14 * Controller routines for testing the routing system.
16 class TestControllers {
18 public function test() {
19 return new Response('test');
22 public function test1() {
23 return new Response('test1');
26 public function test2() {
27 return ['#markup' => "test2"];
30 public function test3($value) {
31 return ['#markup' => $value];
34 public function test4($value) {
35 return ['#markup' => $value];
38 public function test5() {
39 return ['#markup' => "test5"];
42 public function test6() {
43 return new Response('test6');
46 public function test7() {
47 return new Response('test7text');
50 public function test8() {
51 return new Response('test8');
54 public function test9($uid) {
55 $text = 'Route not matched.';
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]);
62 catch (ParamNotConvertedException $e) {
64 return new Response($text);
68 * Test controller for ExceptionHandlingTest::testBacktraceEscaping().
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.
73 public function test10() {
74 $this->removeExceptionLogger();
75 $this->throwException('<script>alert(\'xss\')</script>');
78 public function test18() {
81 'contexts' => ['url'],
86 '#markup' => 'test18',
91 public function test21() {
92 return new CacheableResponse('test21');
95 public function test23() {
96 return new HtmlResponse('test23');
99 public function test24() {
100 $this->removeExceptionLogger();
101 throw new \Exception('Escaped content: <p> <br> <h3>');
104 public function test25() {
109 '#markup' => \Drupal::requestStack()->getCurrentRequest()->getUri(),
113 public function testRouteName(Request $request) {
115 '#markup' => $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
120 * Throws an exception.
122 * @param string $message
123 * The message to use in the exception.
128 protected function throwException($message) {
129 throw new \Exception($message);
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);