Commit d1e190d4 authored by Thomas Seiber's avatar Thomas Seiber
Browse files

Issue #3257550 by drunken monkey, lpeidro: Fixed wrong handling of empty values in the HTML filter.

parent 7d29cf6e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
Search API 1.x, dev (xxxx-xx-xx):
---------------------------------
- #3257550 by drunken monkey, lpeidro: Fixed wrong handling of empty values in
  the HTML filter.
- #3029582 by drunken monkey, RichardDavies: Fixed handling of Views contextual
  filters in combination with filter groups.
- #3247840 by drunken monkey: Added a hidden datasource option to disable the
+7 −4
Original line number Diff line number Diff line
@@ -326,8 +326,6 @@ abstract class FieldsProcessorPluginBase extends ProcessorPluginBase implements
    $type = $field->getType();

    foreach ($values as $i => &$value) {
      // We restore the field's type for each run of the loop since we need the
      // unchanged one as long as the current field value hasn't been updated.
      if ($value instanceof TextValueInterface) {
        $tokens = $value->getTokens();
        if ($tokens !== NULL) {
@@ -352,13 +350,18 @@ abstract class FieldsProcessorPluginBase extends ProcessorPluginBase implements
              }
            }
          }
          if ($new_tokens === []) {
            unset($values[$i]);
          }
          else {
            $value->setTokens($new_tokens);
          }
        }
        else {
          $text = $value->getText();
          if ($text !== '') {
            $this->processFieldValue($text, $type);
            if ($text === '') {
            if ($text === '' || $text === []) {
              unset($values[$i]);
            }
            elseif (is_scalar($text)) {
@@ -373,7 +376,7 @@ abstract class FieldsProcessorPluginBase extends ProcessorPluginBase implements
      elseif ($value !== '') {
        $this->processFieldValue($value, $type);

        if ($value === '') {
        if ($value === '' || $value === []) {
          unset($values[$i]);
        }
      }
+41 −3
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ 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\data_type\value\TextToken;
use Drupal\search_api\Plugin\search_api\data_type\value\TextValue;
use Drupal\search_api\Plugin\search_api\processor\HtmlFilter;
use Drupal\search_api\Query\Condition;
use Drupal\search_api\Utility\Utility;
@@ -33,7 +35,7 @@ class HtmlFilterTest extends UnitTestCase {
  }

  /**
   * Tests preprocessing field values with "title" settings.
   * Tests preprocessing field values with different "title" settings.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
@@ -75,7 +77,7 @@ class HtmlFilterTest extends UnitTestCase {
  }

  /**
   * Tests preprocessing field values with "alt" settings.
   * Tests preprocessing field values with different "alt" settings.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
@@ -151,7 +153,7 @@ class HtmlFilterTest extends UnitTestCase {
  }

  /**
   * Tests preprocessing field values with "alt" settings.
   * Tests preprocessing field values with different "tags" settings.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
@@ -305,4 +307,40 @@ class HtmlFilterTest extends UnitTestCase {
    $this->assertSame([$condition], $conditions);
  }

  /**
   * Tests empty values handling.
   *
   * @see https://www.drupal.org/project/search_api/issues/3212925
   */
  public function testEmptyValueHandling() {
    $index = $this->createMock(IndexInterface::class);
    $field = (new Field($index, 'field'))
      ->setType('text');
    $index->method('getFields')->willReturn([
      'field' => $field,
    ]);

    $field->setValues([new TextValue('<p></p>')]);
    $this->invokeMethod('processField', [$field]);
    $this->assertEquals([], $field->getValues());

    $value = new TextValue('<p></p>');
    $value->setTokens([new TextToken('<p></p>')]);
    $field->setValues([$value]);
    $this->invokeMethod('processField', [$field]);
    $this->assertEquals([], $field->getValues());

    // In theory, setting the value of a "text" field to a string instead of a
    // TextValue object is not allowed, but when it happens we might as well
    // handle it graciously (though other parts of the framework might not).
    $field->setValues(['<p></p>']);
    $this->invokeMethod('processField', [$field]);
    $this->assertEquals([], $field->getValues());

    $field->setType('string');
    $field->setValues(['<p></p>']);
    $this->invokeMethod('processField', [$field]);
    $this->assertEquals([], $field->getValues());
  }

}