class TestControllers

Same name in this branch
  1. 9 core/modules/system/tests/modules/paramconverter_test/src/TestControllers.php \Drupal\paramconverter_test\TestControllers
  2. 9 core/modules/system/tests/modules/menu_test/src/TestControllers.php \Drupal\menu_test\TestControllers
Same name in other branches
  1. 8.9.x core/modules/system/tests/modules/paramconverter_test/src/TestControllers.php \Drupal\paramconverter_test\TestControllers
  2. 8.9.x core/modules/system/tests/modules/menu_test/src/TestControllers.php \Drupal\menu_test\TestControllers
  3. 8.9.x core/modules/system/tests/modules/router_test_directory/src/TestControllers.php \Drupal\router_test\TestControllers
  4. 10 core/modules/system/tests/modules/paramconverter_test/src/TestControllers.php \Drupal\paramconverter_test\TestControllers
  5. 10 core/modules/system/tests/modules/menu_test/src/TestControllers.php \Drupal\menu_test\TestControllers
  6. 10 core/modules/system/tests/modules/router_test_directory/src/TestControllers.php \Drupal\router_test\TestControllers
  7. 11.x core/modules/system/tests/modules/paramconverter_test/src/TestControllers.php \Drupal\paramconverter_test\TestControllers
  8. 11.x core/modules/system/tests/modules/menu_test/src/TestControllers.php \Drupal\menu_test\TestControllers
  9. 11.x core/modules/system/tests/modules/router_test_directory/src/TestControllers.php \Drupal\router_test\TestControllers

Controller routines for testing the routing system.

Hierarchy

Expanded class hierarchy of TestControllers

1 file declares its use of TestControllers
RouterTest.php in core/modules/system/tests/src/Functional/Routing/RouterTest.php

File

core/modules/system/tests/modules/router_test_directory/src/TestControllers.php, line 16

Namespace

Drupal\router_test
View source
class TestControllers {
    const LONG_TEXT = 'This is text long enough to trigger Apache mod_deflate to add a `vary: accept-encoding` header to the response.';
    public function test() {
        return new Response('test');
    }
    public function test1() {
        return new Response(self::LONG_TEXT);
    }
    public function test2() {
        return [
            '#markup' => "test2",
        ];
    }
    public function test3($value) {
        return [
            '#markup' => $value,
        ];
    }
    public function test4($value) {
        return [
            '#markup' => $value,
        ];
    }
    public function test5() {
        return [
            '#markup' => "test5",
        ];
    }
    public function test6() {
        return new Response('test6');
    }
    public function test7() {
        return new Response('test7text');
    }
    public function test8() {
        return new Response('test8');
    }
    public function test9($uid) {
        $text = 'Route not matched.';
        try {
            $match = \Drupal::service('router.no_access_checks')->match('/user/' . $uid);
            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);
    }
    
    /**
     * Tests 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() {
        $this->removeExceptionLogger();
        $this->throwException('<script>alert(\'xss\')</script>');
    }
    public function test18() {
        return [
            '#cache' => [
                'contexts' => [
                    'url',
                ],
                'tags' => [
                    'foo',
                ],
                'max-age' => 60,
            ],
            'content' => [
                '#markup' => 'test18',
            ],
        ];
    }
    public function test21() {
        return new CacheableResponse('test21');
    }
    public function test23() {
        return new HtmlResponse('test23');
    }
    public function test24() {
        $this->removeExceptionLogger();
        throw new \Exception('Escaped content: <p> <br> <h3>');
    }
    public function test25() {
        return [
            '#cache' => [
                'url',
            ],
            '#markup' => \Drupal::requestStack()->getCurrentRequest()
                ->getUri(),
        ];
    }
    public function testRouteName(Request $request) {
        return [
            '#markup' => $request->attributes
                ->get(RouteObjectInterface::ROUTE_NAME),
        ];
    }
    
    /**
     * 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);
    }
    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);
    }

}

Members

Title Sort descending Modifiers Object type Summary
TestControllers::LONG_TEXT constant
TestControllers::removeExceptionLogger protected function
TestControllers::test public function
TestControllers::test1 public function
TestControllers::test10 public function Tests controller for ExceptionHandlingTest::testBacktraceEscaping().
TestControllers::test18 public function
TestControllers::test2 public function
TestControllers::test21 public function
TestControllers::test23 public function
TestControllers::test24 public function
TestControllers::test25 public function
TestControllers::test3 public function
TestControllers::test4 public function
TestControllers::test5 public function
TestControllers::test6 public function
TestControllers::test7 public function
TestControllers::test8 public function
TestControllers::test9 public function
TestControllers::testRouteName public function
TestControllers::throwException protected function Throws an exception.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.