class HtmlFilterTest in Search API 8
Tests the "HTML filter" processor.
@group search_api
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api\Unit\Processor\HtmlFilterTest uses ProcessorTestTrait, TestItemsTrait
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\ProcessorView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HtmlFilterTest:: |
public | function | Data provider method for testAltConfiguration(). | |
HtmlFilterTest:: |
public | function |
Creates a new processor object for use in the tests. Overrides UnitTestCase:: |
|
HtmlFilterTest:: |
public | function | Provides a few sets of HTML filter configuration. | |
HtmlFilterTest:: |
public | function | Data provider method for testTagConfiguration(). | |
HtmlFilterTest:: |
public | function | Tests preprocessing field values with "alt" settings. | |
HtmlFilterTest:: |
public | function | Tests whether "IS NULL" conditions are correctly kept. | |
HtmlFilterTest:: |
public | function | Tests whether strings are correctly handled. | |
HtmlFilterTest:: |
public | function | Tests preprocessing field values with "alt" settings. | |
HtmlFilterTest:: |
public | function | Tests preprocessing field values with "title" settings. | |
HtmlFilterTest:: |
public | function | Data provider for testTitleConfiguration(). | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ProcessorTestTrait:: |
protected | property | The tested processor. | |
ProcessorTestTrait:: |
protected | function | Invokes a method on the processor. | |
TestItemsTrait:: |
protected | property | The class container. | |
TestItemsTrait:: |
protected | property | The used item IDs for test items. | |
TestItemsTrait:: |
public | function | Creates a certain number of test items. | |
TestItemsTrait:: |
public | function | Creates an array with a single item which has the given field. | |
TestItemsTrait:: |
protected | function | Adds a container with several mock services commonly needed by our tests. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |