View source
<?php
namespace Drupal\Tests\search_api\Unit\Processor;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\Field;
use Drupal\search_api\Plugin\search_api\processor\HtmlFilter;
use Drupal\search_api\Query\Condition;
use Drupal\search_api\Utility\Utility;
use Drupal\Tests\UnitTestCase;
class HtmlFilterTest extends UnitTestCase {
use ProcessorTestTrait;
use TestItemsTrait;
public function setUp() {
parent::setUp();
$this
->setUpMockContainer();
$this->processor = new HtmlFilter([], 'html_filter', []);
}
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);
}
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,
],
];
}
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);
}
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,
],
[
'a < b',
[
Utility::createTextToken('a < b'),
],
TRUE,
],
];
}
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);
}
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,
];
}
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);
}
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;
}
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);
}
}