You are here

class FacetsCustomLabelProcessorTest in Facets Custom Label 8

Tests the custom label processor.

Hierarchy

Expanded class hierarchy of FacetsCustomLabelProcessorTest

File

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

Namespace

Drupal\Tests\facets_custom_label\Unit
View source
class FacetsCustomLabelProcessorTest extends UnitTestCase {

  /**
   * The processor to be tested.
   *
   * @var \Drupal\facets_custom_label\Plugin\facets\processor\FacetsCustomLabelProcessor
   */
  protected $processor;

  /**
   * An array containing the results before the processor has ran.
   *
   * @var \Drupal\facets\Result\Result[]
   */
  protected $originalResults;

  /**
   * Prepare the processor object for testing.
   */
  protected function setUp() {
    parent::setUp();
    $facet = new Facet([], 'facets_facet');
    $this->originalResults = [
      new Result($facet, 0, 'Numerical raw value 0', 5),
      new Result($facet, 'literal raw value 0', 'Literal raw value 0', 199),
      new Result($facet, 'literal raw value 1', 'Literal raw value 1', 2),
      new Result($facet, 1234, 'Numerical raw value 1234', 55),
    ];
    $this->processor = new FacetsCustomLabelProcessor([
      'replacement_values' => '',
    ], 'facets_custom_label_processor', []);
  }

  /**
   * Tests defaultConfiguration().
   */
  public function testDefaultConfiguration() {
    $config = $this->processor
      ->defaultConfiguration();
    $this
      ->assertEquals([
      'replacement_values' => '',
    ], $config);
  }

  /**
   * Tests build().
   */
  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());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetsCustomLabelProcessorTest::$originalResults protected property An array containing the results before the processor has ran.
FacetsCustomLabelProcessorTest::$processor protected property The processor to be tested.
FacetsCustomLabelProcessorTest::setUp protected function Prepare the processor object for testing. Overrides UnitTestCase::setUp
FacetsCustomLabelProcessorTest::testBuild public function Tests build().
FacetsCustomLabelProcessorTest::testDefaultConfiguration public function Tests defaultConfiguration().
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.
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.