You are here

class HtmlFilterTest in Search API 8

Tests the "HTML filter" processor.

@group search_api

Hierarchy

Expanded class hierarchy of HtmlFilterTest

See also

\Drupal\search_api\Plugin\search_api\processor\HtmlFilter

File

tests/src/Unit/Processor/HtmlFilterTest.php, line 19

Namespace

Drupal\Tests\search_api\Unit\Processor
View source
class HtmlFilterTest extends UnitTestCase {
  use ProcessorTestTrait;
  use TestItemsTrait;

  /**
   * Creates a new processor object for use in the tests.
   */
  public function setUp() {
    parent::setUp();
    $this
      ->setUpMockContainer();
    $this->processor = new HtmlFilter([], 'html_filter', []);
  }

  /**
   * Tests preprocessing field values with "title" settings.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
   * @param string $expected_value
   *   The expected processed value.
   * @param bool $title_config
   *   The value to set for the processor's "title" setting.
   *
   * @dataProvider titleConfigurationDataProvider
   */
  public function testTitleConfiguration($passed_value, $expected_value, $title_config) {
    $configuration = [
      'tags' => [],
      'title' => $title_config,
      'alt' => FALSE,
    ];
    $this->processor
      ->setConfiguration($configuration);
    $type = 'text';
    $this
      ->invokeMethod('processFieldValue', [
      &$passed_value,
      $type,
    ]);
    $this
      ->assertEquals($expected_value, $passed_value);
  }

  /**
   * Data provider for testTitleConfiguration().
   *
   * @return array
   *   An array of argument arrays for testTitleConfiguration().
   */
  public function titleConfigurationDataProvider() {
    return [
      [
        'word',
        'word',
        FALSE,
      ],
      [
        'word',
        'word',
        TRUE,
      ],
      [
        '<div>word</div>',
        'word',
        TRUE,
      ],
      [
        '<div title="TITLE">word</div>',
        'TITLE word',
        TRUE,
      ],
      [
        '<div title="TITLE">word</div>',
        'word',
        FALSE,
      ],
      [
        '<div data-title="TITLE">word</div>',
        'word',
        TRUE,
      ],
      [
        '<div title="TITLE">word</a>',
        'TITLE word',
        TRUE,
      ],
    ];
  }

  /**
   * Tests preprocessing field values with "alt" settings.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
   * @param mixed $expected_value
   *   The expected processed value.
   * @param bool $alt_config
   *   The value to set for the processor's "alt" setting.
   *
   * @dataProvider altConfigurationDataProvider
   */
  public function testAltConfiguration($passed_value, $expected_value, $alt_config) {
    $configuration = [
      'tags' => [
        'img' => '2',
      ],
      'title' => FALSE,
      'alt' => $alt_config,
    ];
    $this->processor
      ->setConfiguration($configuration);
    $type = 'text';
    $this
      ->invokeMethod('processFieldValue', [
      &$passed_value,
      $type,
    ]);
    $this
      ->assertEquals($expected_value, $passed_value);
  }

  /**
   * Data provider method for testAltConfiguration().
   *
   * @return array
   *   An array of argument arrays for testAltConfiguration().
   */
  public function altConfigurationDataProvider() {
    return [
      [
        'word',
        [
          Utility::createTextToken('word'),
        ],
        FALSE,
      ],
      [
        'word',
        [
          Utility::createTextToken('word'),
        ],
        TRUE,
      ],
      [
        '<img src="href" />word',
        [
          Utility::createTextToken('word'),
        ],
        TRUE,
      ],
      [
        '<img alt="ALT"/> word',
        [
          Utility::createTextToken('ALT', 2),
          Utility::createTextToken('word'),
        ],
        TRUE,
      ],
      [
        '<img alt="ALT" /> word',
        [
          Utility::createTextToken('word'),
        ],
        FALSE,
      ],
      [
        '<img data-alt="ALT"/> word',
        [
          Utility::createTextToken('word'),
        ],
        TRUE,
      ],
      [
        '<img src="href" alt="ALT" title="Bar" /> word </a>',
        [
          Utility::createTextToken('ALT', 2),
          Utility::createTextToken('word'),
        ],
        TRUE,
      ],
      // Test fault tolerance.
      [
        'a < b',
        [
          Utility::createTextToken('a < b'),
        ],
        TRUE,
      ],
    ];
  }

  /**
   * Tests preprocessing field values with "alt" settings.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
   * @param mixed $expected_value
   *   The expected processed value.
   * @param float[] $tags_config
   *   The value to set for the processor's "tags" setting.
   *
   * @dataProvider tagConfigurationDataProvider
   */
  public function testTagConfiguration($passed_value, $expected_value, array $tags_config) {
    $configuration = [
      'tags' => $tags_config,
      'title' => TRUE,
      'alt' => TRUE,
    ];
    $this->processor
      ->setConfiguration($configuration);
    $type = 'text';
    $this
      ->invokeMethod('processFieldValue', [
      &$passed_value,
      $type,
    ]);
    $this
      ->assertEquals($expected_value, $passed_value);
  }

  /**
   * Data provider method for testTagConfiguration().
   *
   * @return array
   *   An array of argument arrays for testTagConfiguration().
   */
  public function tagConfigurationDataProvider() {
    $complex_test = [
      '<h2>Foo Bar <em>Baz</em></h2>

<p>Bla Bla Bla. <strong title="Foobar">Important:</strong> Bla.</p>
<img src="/foo.png" alt="Some picture" />
<span>This is hidden</span>',
      [
        Utility::createTextToken('Foo Bar', 3.0),
        Utility::createTextToken('Baz', 4.5),
        Utility::createTextToken('Bla Bla Bla.', 1.0),
        Utility::createTextToken('Foobar Important:', 2.0),
        Utility::createTextToken('Bla.', 1.0),
        Utility::createTextToken('Some picture', 0.5),
      ],
      [
        'em' => 1.5,
        'strong' => 2.0,
        'h2' => 3.0,
        'img' => 0.5,
        'span' => 0,
      ],
    ];
    $tags_config = [
      'h2' => '2',
    ];
    return [
      [
        'h2word',
        'h2word',
        [],
      ],
      [
        'h2word',
        [
          Utility::createTextToken('h2word'),
        ],
        $tags_config,
      ],
      [
        'foo bar <h2> h2word </h2>',
        [
          Utility::createTextToken('foo bar'),
          Utility::createTextToken('h2word', 2.0),
        ],
        $tags_config,
      ],
      [
        'foo bar <h2>h2word</h2>',
        [
          Utility::createTextToken('foo bar'),
          Utility::createTextToken('h2word', 2.0),
        ],
        $tags_config,
      ],
      [
        '<div>word</div>',
        [
          Utility::createTextToken('word', 2),
        ],
        [
          'div' => 2,
        ],
      ],
      $complex_test,
    ];
  }

  /**
   * Tests whether strings are correctly handled.
   *
   * String field handling should be completely independent of configuration.
   *
   * @param array $config
   *   The configuration to set on the processor.
   *
   * @dataProvider stringProcessingDataProvider
   */
  public function testStringProcessing(array $config) {
    $this->processor
      ->setConfiguration($config);
    $passed_value = '<h2>Foo Bar <em>Baz</em></h2>

<p>Bla Bla Bla. <strong title="Foobar">Important:</strong> Bla.</p>
<img src="/foo.png" alt="Some picture" />
<span>This is hidden</span>';
    $expected_value = preg_replace('/\\s+/', ' ', strip_tags($passed_value));
    $type = 'string';
    $this
      ->invokeMethod('processFieldValue', [
      &$passed_value,
      $type,
    ]);
    $this
      ->assertEquals($expected_value, $passed_value);
  }

  /**
   * Provides a few sets of HTML filter configuration.
   *
   * @return array
   *   An array of argument arrays for testStringProcessing(), where each array
   *   contains a HTML filter configuration as the only value.
   */
  public function stringProcessingDataProvider() {
    $configs = [];
    $configs[] = [
      [],
    ];
    $config['tags'] = [
      'h2' => 2.0,
      'span' => 4.0,
      'strong' => 1.5,
      'p' => 0,
    ];
    $configs[] = [
      $config,
    ];
    $config['title'] = TRUE;
    $configs[] = [
      $config,
    ];
    $config['alt'] = TRUE;
    $configs[] = [
      $config,
    ];
    unset($config['tags']);
    $configs[] = [
      $config,
    ];
    return $configs;
  }

  /**
   * Tests whether "IS NULL" conditions are correctly kept.
   *
   * @see https://www.drupal.org/project/search_api/issues/3212925
   */
  public function testIsNullConditions() {
    $index = $this
      ->createMock(IndexInterface::class);
    $index
      ->method('getFields')
      ->willReturn([
      'field' => (new Field($index, 'field'))
        ->setType('string'),
    ]);
    $this->processor
      ->setIndex($index);
    $passed_value = NULL;
    $this
      ->invokeMethod('processConditionValue', [
      &$passed_value,
    ]);
    $this
      ->assertSame(NULL, $passed_value);
    $condition = new Condition('field', NULL);
    $conditions = [
      $condition,
    ];
    $this
      ->invokeMethod('processConditions', [
      &$conditions,
    ]);
    $this
      ->assertSame([
      $condition,
    ], $conditions);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlFilterTest::altConfigurationDataProvider public function Data provider method for testAltConfiguration().
HtmlFilterTest::setUp public function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
HtmlFilterTest::stringProcessingDataProvider public function Provides a few sets of HTML filter configuration.
HtmlFilterTest::tagConfigurationDataProvider public function Data provider method for testTagConfiguration().
HtmlFilterTest::testAltConfiguration public function Tests preprocessing field values with "alt" settings.
HtmlFilterTest::testIsNullConditions public function Tests whether "IS NULL" conditions are correctly kept.
HtmlFilterTest::testStringProcessing public function Tests whether strings are correctly handled.
HtmlFilterTest::testTagConfiguration public function Tests preprocessing field values with "alt" settings.
HtmlFilterTest::testTitleConfiguration public function Tests preprocessing field values with "title" settings.
HtmlFilterTest::titleConfigurationDataProvider public function Data provider for testTitleConfiguration().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
ProcessorTestTrait::$processor protected property The tested processor.
ProcessorTestTrait::invokeMethod protected function Invokes a method on the processor.
TestItemsTrait::$container protected property The class container.
TestItemsTrait::$itemIds protected property The used item IDs for test items.
TestItemsTrait::createItems public function Creates a certain number of test items.
TestItemsTrait::createSingleFieldItem public function Creates an array with a single item which has the given field.
TestItemsTrait::setUpMockContainer protected function Adds a container with several mock services commonly needed by our tests.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.