Problem/Motivation

#3586832: Allow kernel test classes to define their own routes added the ability to kernel test classes to define their own routes. The test for that functionality involves generating a random string value in the test class and assigning to a class property, then using that property to set the content in the route response. Since the random string generated is not sanitized, the sanitized rendered string will not match the original.

Steps to reproduce

Proposed resolution

Assert the escaped value string exists in the response.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3613115

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

godotislate created an issue. See original summary.

godotislate’s picture

Issue summary: View changes
godotislate’s picture

Status: Active » Needs review
godotislate’s picture

Title: KernelTestHttpRequestTest::testKernelTestRoute fails intermittently because random string is not sanitized » [random test failure] KernelTestHttpRequestTest::testKernelTestRoute fails intermittently because random string is not sanitized
Related issues: +#2829040: [meta] Known intermittent, random, and environment-specific test failures
godotislate’s picture

Issue summary: View changes
nitinkumar_7’s picture

I have tested and reviewed MR !16436 locally.

### Review Summary
- **Root Cause:** In `KernelTestHttpRequestTest::testKernelTestRoute()`, `$this->testMarkup` is populated using `$this->randomString()`, which can produce HTML special characters (`<`, `>`, `&`, `'`, `"`). Because Drupal automatically escapes raw text rendered in responses, `pageTextContains()` intermittently failed whenever special characters were present.
- **Verification:** Changing the assertion to `$this->assertSession()->assertEscaped($this->testMarkup)` correctly verifies that raw random strings with special characters are safely HTML-escaped in the output, resolving the random test failures.

### Local Test Output
Executed `KernelTestHttpRequestTest` locally against MR !16436:

```bash
$ SIMPLETEST_DB=sqlite://localhost/sites/default/files/.ht.sqlite ./vendor/bin/phpunit -c core core/tests/Drupal/KernelTests/KernelTestHttpRequestTest.php

nitinkumar_7’s picture

PHPUnit 11.5.55 by Sebastian Bergmann and contributors.

Runtime: PHP 8.3.6
Configuration: /home/gai/Documents/drupal/core/phpunit.xml.dist

... 3 / 3 (100%)

Time: 00:08.875, Memory: 14.00 MB

OK (3 tests, 17 assertions)

The test suite passed completely with 17 assertions. The proposed fix resolves the intermittent failure.

godotislate’s picture

@nitinkumar_7 if the MR passes review, please set to Reviewed & tested by the community. Otherwise, please set to Needs work and advise of what changes need to be made.

nitinkumar_7’s picture

Status: Needs review » Reviewed & tested by the community
longwave’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed e703c4740cd to main and 040541d150b to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • longwave committed 040541d1 on 11.x
    test: #3613115 [random test failure] KernelTestHttpRequestTest::...

  • longwave committed e703c474 on main
    test: #3613115 [random test failure] KernelTestHttpRequestTest::...
catch’s picture

Status: Fixed » Needs work