You are here

class Test in Drupal 9

Same name in this branch
  1. 9 core/modules/system/tests/modules/test_page_test/src/Controller/Test.php \Drupal\test_page_test\Controller\Test
  2. 9 core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php \Drupal\media_test_source\Plugin\media\Source\Test
Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/test_page_test/src/Controller/Test.php \Drupal\test_page_test\Controller\Test
  2. 10 core/modules/system/tests/modules/test_page_test/src/Controller/Test.php \Drupal\test_page_test\Controller\Test

Defines a test controller for page titles.

Hierarchy

  • class \Drupal\test_page_test\Controller\Test

Expanded class hierarchy of Test

85 string references to 'Test'
CacheableExceptionTest::testCacheableHttpException in core/tests/Drupal/Tests/Core/Http/CacheableExceptionTest.php
@covers \Drupal\Core\Http\Exception\CacheableHttpException
config_translation_test.content.yml in core/modules/config_translation/tests/modules/config_translation_test/config/install/config_translation_test.content.yml
core/modules/config_translation/tests/modules/config_translation_test/config/install/config_translation_test.content.yml
ContentModerationWorkflowTypeTest::testNewWorkflow in core/modules/content_moderation/tests/src/Functional/ContentModerationWorkflowTypeTest.php
Tests creating a new workflow using the content moderation plugin.
ContentTranslationContextualLinksTest::testContentTranslationContextualLinks in core/modules/content_translation/tests/src/FunctionalJavascript/ContentTranslationContextualLinksTest.php
Tests that a contextual link is available for translating a node.
ContentTranslationHandlerTest::providerTestEntityFormSharedElements in core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php
Returns test cases for ::testEntityFormSharedElements().

... See full list

File

core/modules/system/tests/modules/test_page_test/src/Controller/Test.php, line 14

Namespace

Drupal\test_page_test\Controller
View source
class Test {

  /**
   * Renders a page with a title.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function renderTitle() {
    $build = [];
    $build['#markup'] = 'Hello Drupal';
    $build['#title'] = 'Foo';
    return $build;
  }

  /**
   * Renders a page.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function staticTitle() {
    $build = [];
    $build['#markup'] = 'Hello Drupal';
    return $build;
  }

  /**
   * Returns a 'dynamic' title for the '_title_callback' route option.
   *
   * @return string
   *   The page title.
   */
  public function dynamicTitle() {
    return 'Dynamic title';
  }

  /**
   * Defines a controller with a cached render array.
   *
   * @return array
   *   A render array
   */
  public function controllerWithCache() {
    $build = [];
    $build['#title'] = '<span>Cached title</span>';
    $build['#cache']['keys'] = [
      'test_controller',
      'with_title',
    ];
    return $build;
  }

  /**
   * Returns a generic page render array for title tests.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function renderPage() {
    return [
      '#markup' => 'Content',
    ];
  }

  /**
   * Throws a HTTP exception.
   *
   * @param int $code
   *   The status code.
   */
  public function httpResponseException($code) {
    throw new HttpException($code);
  }
  public function error() {
    trigger_error('foo', E_USER_NOTICE);
    return [
      '#markup' => 'Content',
    ];
  }

  /**
   * Sets an HTTP header.
   *
   * @param string $name
   *   The header name.
   * @param string $value
   *   (optional) The header value ot set.
   */
  public function setHeader($name, $value = NULL) {
    $response = new Response();
    $response->headers
      ->set($name, $value);
    return $response;
  }

  /**
   * Renders a page with encoded markup.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function renderEncodedMarkup() {
    return [
      '#plain_text' => 'Bad html <script>alert(123);</script>',
    ];
  }

  /**
   * Renders a page with pipe character in link test.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function renderPipeInLink() {
    return [
      '#markup' => '<a href="http://example.com">foo|bar|baz</a>',
    ];
  }
  public function escapedCharacters() {
    return [
      '#prefix' => '<div class="escaped">',
      '#plain_text' => 'Escaped: <"\'&>',
      '#suffix' => '</div>',
    ];
  }
  public function escapedScript() {
    return [
      '#prefix' => '<div class="escaped">',
      // We use #plain_text because #markup would be filtered and that is not
      // being tested here.
      '#plain_text' => "<script>alert('XSS');alert(\"XSS\");</script>",
      '#suffix' => '</div>',
    ];
  }
  public function unEscapedScript() {
    return [
      '#prefix' => '<div class="unescaped">',
      '#markup' => Markup::create("<script>alert('Marked safe');alert(\"Marked safe\");</script>"),
      '#suffix' => '</div>',
    ];
  }

  /**
   * Loads a page that does a redirect.
   *
   * Drupal uses Symfony's RedirectResponse for generating redirects. That class
   * uses a lower-case 'http-equiv="refresh"'.
   *
   * @see \Symfony\Component\HttpFoundation\RedirectResponse
   */
  public function metaRefresh() {
    return new RedirectResponse(Url::fromRoute('test_page_test.test_page', [], [
      'absolute' => TRUE,
    ])
      ->toString(), 302);
  }

  /**
   * Returns a page render array with 2 elements with the same HTML IDs.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function renderPageWithDuplicateIds() {
    return [
      '#type' => 'container',
      'title' => [
        '#type' => 'html_tag',
        '#tag' => 'h1',
        '#value' => 'Hello',
        '#attributes' => [
          'id' => 'page-element',
        ],
      ],
      'description' => [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => 'World',
        '#attributes' => [
          'id' => 'page-element',
        ],
      ],
    ];
  }

  /**
   * Returns a page render array with 2 elements with the unique HTML IDs.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function renderPageWithoutDuplicateIds() {
    return [
      '#type' => 'container',
      'title' => [
        '#type' => 'html_tag',
        '#tag' => 'h1',
        '#value' => 'Hello',
        '#attributes' => [
          'id' => 'page-element-title',
        ],
      ],
      'description' => [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => 'World',
        '#attributes' => [
          'id' => 'page-element-description',
        ],
      ],
    ];
  }

  /**
   * Returns a page while triggering deprecation notices.
   */
  public function deprecations() {

    // Create 2 identical deprecation messages. This should only trigger a
    // single response header.
    @trigger_error('Test deprecation message', E_USER_DEPRECATED);
    @trigger_error('Test deprecation message', E_USER_DEPRECATED);
    return [
      '#markup' => 'Content that triggers deprecation messages',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Test::controllerWithCache public function Defines a controller with a cached render array.
Test::deprecations public function Returns a page while triggering deprecation notices.
Test::dynamicTitle public function Returns a 'dynamic' title for the '_title_callback' route option.
Test::error public function
Test::escapedCharacters public function
Test::escapedScript public function
Test::httpResponseException public function Throws a HTTP exception.
Test::metaRefresh public function Loads a page that does a redirect.
Test::renderEncodedMarkup public function Renders a page with encoded markup.
Test::renderPage public function Returns a generic page render array for title tests.
Test::renderPageWithDuplicateIds public function Returns a page render array with 2 elements with the same HTML IDs.
Test::renderPageWithoutDuplicateIds public function Returns a page render array with 2 elements with the unique HTML IDs.
Test::renderPipeInLink public function Renders a page with pipe character in link test.
Test::renderTitle public function Renders a page with a title.
Test::setHeader public function Sets an HTTP header.
Test::staticTitle public function Renders a page.
Test::unEscapedScript public function