MenuLinkSecurityTest.php in Drupal 9        
                          
                  
                        
  
  
  
  
File
  core/modules/system/tests/src/Functional/Menu/MenuLinkSecurityTest.php
  
    View source  
  <?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Tests\BrowserTestBase;
class MenuLinkSecurityTest extends BrowserTestBase {
  
  protected static $modules = [
    'menu_link_content',
    'block',
    'menu_test',
  ];
  
  protected $defaultTheme = 'stark';
  
  public function testMenuLink() {
    $menu_link_content = MenuLinkContent::create([
      'title' => '<script>alert("Wild animals")</script>',
      'menu_name' => 'tools',
      'link' => [
        'uri' => 'route:<front>',
      ],
    ]);
    $menu_link_content
      ->save();
    $this
      ->drupalPlaceBlock('system_menu_block:tools');
    $this
      ->drupalGet('<front>');
    $this
      ->assertSession()
      ->responseNotContains('<script>alert("Wild animals")</script>');
    $this
      ->assertSession()
      ->responseNotContains('<script>alert("Even more wild animals")</script>');
    $this
      ->assertSession()
      ->assertEscaped('<script>alert("Wild animals")</script>');
    $this
      ->assertSession()
      ->assertEscaped('<script>alert("Even more wild animals")</script>');
  }
}