class FacetsCustomLabelProcessorTest in Facets Custom Label 8
Tests the custom label processor.
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\facets_custom_label\Unit\FacetsCustomLabelProcessorTest
Expanded class hierarchy of FacetsCustomLabelProcessorTest
File
- tests/
src/ Unit/ Plugin/ processor/ FacetsCustomLabelProcessorTest.php, line 13
Namespace
Drupal\Tests\facets_custom_label\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FacetsCustomLabelProcessorTest:: |
protected | property | An array containing the results before the processor has ran. | |
FacetsCustomLabelProcessorTest:: |
protected | property | The processor to be tested. | |
FacetsCustomLabelProcessorTest:: |
protected | function |
Prepare the processor object for testing. Overrides UnitTestCase:: |
|
FacetsCustomLabelProcessorTest:: |
public | function | Tests build(). | |
FacetsCustomLabelProcessorTest:: |
public | function | Tests defaultConfiguration(). | |
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. | |
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. |