Unverified Commit cdbbae06 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #455724 Deprecate check_markup()

By: greggles
By: cburschka
By: heine
By: catch
By: sun
By: agentrickard
By: claudiu.cristea
By: nicxvan
By: berdir
By: alexpott
(cherry picked from commit a69f1e95)
parent 91e90273
Loading
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -155,8 +155,7 @@ public function tokens($type, $tokens, array $data, array $options, BubbleableMe
            break;

          case 'body':
            // "processed" returns a \Drupal\Component\Render\MarkupInterface
            // via check_markup().
            // "processed" returns a \Drupal\Component\Render\MarkupInterface.
            $replacements[$original] = $comment->comment_body->processed;
            break;

+6 −0
Original line number Diff line number Diff line
@@ -205,11 +205,17 @@ function filter_fallback_format() {
 * @return \Drupal\Component\Render\MarkupInterface
 *   The filtered text.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. There is no
 *   direct replacement. It's recommended to always return a renderable array
 *   without flattening as markup to pass the cacheability metadata.
 *
 * @see https://www.drupal.org/node/3588040
 * @see \Drupal\filter\Plugin\FilterInterface::process()
 *
 * @ingroup sanitization
 */
function check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = []) {
  @trigger_error(__FUNCTION__ . "() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. There is no direct replacement. It's recommended to always return a renderable array without flattening as markup to pass the cacheability metadata. See https://www.drupal.org/node/3588040", E_USER_DEPRECATED);
  $build = [
    '#type' => 'processed_text',
    '#text' => $text,
+0 −1
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@
 * convert smileys into images, etc.
 *
 * @see \Drupal\filter\Plugin\FilterInterface::process()
 * @see check_markup()
 *
 * Typically, only text processing is applied, but in more advanced use cases,
 * filters may also:
+60 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\filter\Kernel;

use Drupal\filter\Entity\FilterFormat;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests deprecation of check_markup().
 */
#[CoversFunction('check_markup')]
#[Group('filter')]
#[IgnoreDeprecations]
#[RunTestsInSeparateProcesses]
class CheckMarkupDeprecationTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['filter'];

  /**
   * Test deprecation of check_markup().
   */
  public function testCheckMarkup(): void {
    FilterFormat::create([
      'format' => 'foo',
      'name' => 'Foo',
      'filters' => [
        'filter_html' => [
          'settings' => [
            'allowed_html' => '<p>',
          ],
          'status' => TRUE,
        ],
        'filter_url' => [
          'status' => TRUE,
        ],
      ],
    ])->save();

    $this->expectUserDeprecationMessage("check_markup() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. There is no direct replacement. It's recommended to always return a renderable array without flattening as markup to pass the cacheability metadata. See https://www.drupal.org/node/3588040");
    $formatted = (string) check_markup(
      text: '<p>Visit https://example.com',
      format_id: 'foo',
      filter_types_to_skip: [FilterInterface::TYPE_MARKUP_LANGUAGE],
    );

    // The filter_html filter was applied, and filter_url skipped.
    $this->assertSame('<p>Visit https://example.com</p>', $formatted);
  }

}
+16 −18
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
use Drupal\filter\Plugin\DataType\FilterFormat as FilterFormatDataType;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\filter\Traits\ProcessedTextTestTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Symfony\Component\Validator\ConstraintViolationListInterface;
@@ -23,6 +24,8 @@
#[RunTestsInSeparateProcesses]
class FilterAPITest extends EntityKernelTestBase {

  use ProcessedTextTestTrait;

  /**
   * {@inheritdoc}
   */
@@ -40,7 +43,7 @@ protected function setUp(): void {
  /**
   * Tests that the filter order is respected.
   */
  public function testCheckMarkupFilterOrder(): void {
  public function testFormatFilterOrder(): void {
    // Create crazy HTML format.
    $crazy_format = FilterFormat::create([
      'format' => 'crazy',
@@ -65,30 +68,27 @@ public function testCheckMarkupFilterOrder(): void {
    $text = "<p>Llamas are <not> awesome!</p>";
    $expected_filtered_text = "&lt;p&gt;Llamas are  awesome!&lt;/p&gt;";

    $this->assertEquals($expected_filtered_text, check_markup($text, 'crazy'), 'Filters applied in correct order.');
    $this->assertEquals($expected_filtered_text, $this->processText($text, 'crazy'), 'Filters applied in correct order.');
  }

  /**
   * Tests the ability to apply only a subset of filters.
   */
  public function testCheckMarkupFilterSubset(): void {
  public function testFormatFilterSubset(): void {
    $text = "Text with <marquee>evil content and</marquee> a URL: https://www.drupal.org!";
    $expected_filtered_text = "Text with evil content and a URL: <a href=\"https://www.drupal.org\">https://www.drupal.org</a>!";
    $expected_filter_text_without_html_generators = "Text with evil content and a URL: https://www.drupal.org!";

    $actual_filtered_text = check_markup($text, 'filtered_html', '', []);
    $actual_filtered_text = $this->processText($text, 'filtered_html');
    $this->assertSame($expected_filtered_text, (string) $actual_filtered_text, 'Expected filter result.');
    $actual_filtered_text_without_html_generators = check_markup($text, 'filtered_html', '', [FilterInterface::TYPE_MARKUP_LANGUAGE]);

    $actual_filtered_text_without_html_generators = $this->processText($text, 'filtered_html', filterTypesToSkip: [FilterInterface::TYPE_MARKUP_LANGUAGE]);
    $this->assertSame($expected_filter_text_without_html_generators, (string) $actual_filtered_text_without_html_generators, 'Expected filter result when skipping FilterInterface::TYPE_MARKUP_LANGUAGE filters.');
    // Related to @see FilterSecurityTest.php/testSkipSecurityFilters(), but
    // this check focuses on the ability to filter multiple filter types at
    // once. Drupal core only ships with these two types of filters, so this is
    // the most extensive test possible.
    $actual_filtered_text_without_html_generators = check_markup(
      $text,
      'filtered_html',
      '',
      [
    $actual_filtered_text_without_html_generators = $this->processText($text, 'filtered_html', filterTypesToSkip: [
      FilterInterface::TYPE_HTML_RESTRICTOR,
      FilterInterface::TYPE_MARKUP_LANGUAGE,
    ]);
@@ -254,11 +254,9 @@ public function testFilterFormatAPI(): void {
  /**
   * Tests the 'processed_text' element.
   *
   * Function check_markup() is a wrapper for the 'processed_text' element, for
   * use in simple scenarios; the 'processed_text' element has more advanced
   * features: it lets filters attach assets, associate cache tags and define
   * #lazy_builder callbacks.
   * This test focuses solely on those advanced features.
   * The 'processed_text' element has advanced features: it lets filters attach
   * assets, associate cache tags, and define #lazy_builder callbacks. This test
   * focuses solely on those advanced features.
   */
  public function testProcessedTextElement(): void {
    FilterFormat::create([
Loading