You are here

public function FacetsCustomLabelProcessorTest::testBuild in Facets Custom Label 8

Tests build().

File

tests/src/Unit/Plugin/processor/FacetsCustomLabelProcessorTest.php, line 57

Class

FacetsCustomLabelProcessorTest
Tests the custom label processor.

Namespace

Drupal\Tests\facets_custom_label\Unit

Code

public function testBuild() {
  $facet = new Facet([], 'facets_facet');
  $facet
    ->setResults($this->originalResults);

  // Invalid configs.
  $replacementValues = FacetsCustomLabelProcessor::ORIGIN__RAW . FacetsCustomLabelProcessor::SEPARATOR . '0' . FacetsCustomLabelProcessor::SEPARATOR . 'too much arguments / must be ignored' . FacetsCustomLabelProcessor::SEPARATOR . '' . "\n";
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__RAW . FacetsCustomLabelProcessor::SEPARATOR . '0' . "\n";
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__RAW . "\n";
  $replacementValues .= "\n";

  // Valid configs.
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__RAW . FacetsCustomLabelProcessor::SEPARATOR . '0' . FacetsCustomLabelProcessor::SEPARATOR . '0 replaced using raw value' . "\n";
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__RAW . FacetsCustomLabelProcessor::SEPARATOR . 'literal raw value 0' . FacetsCustomLabelProcessor::SEPARATOR . 'literal raw value 0 replaced using raw value' . "\r\n";
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__DISPLAY . FacetsCustomLabelProcessor::SEPARATOR . 'Literal raw value 1' . FacetsCustomLabelProcessor::SEPARATOR . 'literal raw value 1 replaced using display value' . "\n";
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__RAW . FacetsCustomLabelProcessor::SEPARATOR . 'literal raw value 1' . FacetsCustomLabelProcessor::SEPARATOR . 'literal raw value 1 replaced using raw value' . "\n";
  $replacementValues .= FacetsCustomLabelProcessor::ORIGIN__DISPLAY . FacetsCustomLabelProcessor::SEPARATOR . 'Numerical raw value 1234' . FacetsCustomLabelProcessor::SEPARATOR . '1234 replaced using display value';
  $this->processor
    ->setConfiguration([
    'replacement_values' => $replacementValues,
  ]);
  $filteredResults = $this->processor
    ->build($facet, $this->originalResults);

  // Values replaced by raw values.
  $this
    ->assertEquals('0 replaced using raw value', $filteredResults[0]
    ->getDisplayValue());
  $this
    ->assertEquals('literal raw value 0 replaced using raw value', $filteredResults[1]
    ->getDisplayValue());

  // Regardless of order in the replacement values, the processor attempts
  // to replace using raw id before trying to replace using display value.
  // This is why this one is replaced by raw value and not by display value.
  $this
    ->assertEquals('literal raw value 1 replaced using raw value', $filteredResults[2]
    ->getDisplayValue());

  // Value replaced by display value.
  $this
    ->assertEquals('1234 replaced using display value', $filteredResults[3]
    ->getDisplayValue());
}