class ListItemProcessorTest in Facets 8
Unit test for processor.
@group facets
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\facets\Unit\Plugin\processor\ListItemProcessorTest
Expanded class hierarchy of ListItemProcessorTest
File
- tests/
src/ Unit/ Plugin/ processor/ ListItemProcessorTest.php, line 24
Namespace
Drupal\Tests\facets\Unit\Plugin\processorView source
class ListItemProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\processor\BuildProcessorInterface
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $results;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
$this->results = [
new Result($facet, 1, 1, 10),
new Result($facet, 2, 2, 5),
new Result($facet, 3, 3, 15),
];
$config_manager = $this
->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$entity_field_manager = $this
->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$entity_type_bundle_info = $this
->getMockBuilder(EntityTypeBundleInfo::class)
->disableOriginalConstructor()
->getMock();
// Create a search api based facet source and make the property definition
// return null.
$data_definition = $this
->createMock(ComplexDataDefinitionInterface::class);
$data_definition
->expects($this
->any())
->method('getPropertyDefinition')
->willReturn(NULL);
$facet_source = $this
->getMockBuilder(FacetSourcePluginInterface::class)
->disableOriginalConstructor()
->getMock();
$facet_source
->expects($this
->any())
->method('getDataDefinition')
->willReturn($data_definition);
// Add the plugin manager.
$pluginManager = $this
->getMockBuilder(FacetSourcePluginManager::class)
->disableOriginalConstructor()
->getMock();
$pluginManager
->expects($this
->any())
->method('hasDefinition')
->willReturn(TRUE);
$pluginManager
->expects($this
->any())
->method('createInstance')
->willReturn($facet_source);
$this->processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
$container = new ContainerBuilder();
$container
->set('plugin.manager.facets.facet_source', $pluginManager);
\Drupal::setContainer($container);
}
/**
* Tests facet build with field.module field.
*/
public function testBuildConfigurableField() {
$module_field = $this
->getMockBuilder(FieldStorageConfig::class)
->disableOriginalConstructor()
->getMock();
// Make sure that when the processor calls loadConfigEntityByName the field
// we created here is called.
$config_manager = $this
->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$config_manager
->expects($this
->exactly(2))
->method('loadConfigEntityByName')
->willReturn($module_field);
$entity_field_manager = $this
->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$entity_type_bundle_info = $this
->getMockBuilder(EntityTypeBundleInfo::class)
->disableOriginalConstructor()
->getMock();
$processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
// Config entity field facet.
$module_field_facet = new Facet([], 'facets_facet');
$module_field_facet
->setFieldIdentifier('test_facet');
$module_field_facet
->setFacetSourceId('llama_source');
$module_field_facet
->setResults($this->results);
$module_field_facet
->addProcessor([
'processor_id' => 'list_item',
'weights' => [],
'settings' => [],
]);
/* @var \Drupal\facets\Result\Result[] $module_field_facet- */
$module_field_results = $processor
->build($module_field_facet, $this->results);
$this
->assertCount(3, $module_field_results);
$this
->assertEquals('llama', $module_field_results[0]
->getDisplayValue());
$this
->assertEquals('badger', $module_field_results[1]
->getDisplayValue());
$this
->assertEquals('kitten', $module_field_results[2]
->getDisplayValue());
}
/**
* Tests facet build with field.module field.
*/
public function testBuildBundle() {
$module_field = $this
->getMockBuilder(FieldStorageConfig::class)
->disableOriginalConstructor()
->getMock();
$config_manager = $this
->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$config_manager
->expects($this
->exactly(2))
->method('loadConfigEntityByName')
->willReturn($module_field);
$entity_field_manager = $this
->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$entity_type_bundle_info = $this
->getMockBuilder(EntityTypeBundleInfo::class)
->disableOriginalConstructor()
->getMock();
$processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
// Config entity field facet.
$module_field_facet = new Facet([], 'facets_facet');
$module_field_facet
->setFieldIdentifier('test_facet');
$module_field_facet
->setFacetSourceId('llama_source');
$module_field_facet
->setResults($this->results);
$module_field_facet
->addProcessor([
'processor_id' => 'list_item',
'weights' => [],
'settings' => [],
]);
/* @var \Drupal\facets\Result\Result[] $module_field_facet- */
$module_field_results = $processor
->build($module_field_facet, $this->results);
$this
->assertCount(3, $module_field_results);
$this
->assertEquals('llama', $module_field_results[0]
->getDisplayValue());
$this
->assertEquals('badger', $module_field_results[1]
->getDisplayValue());
$this
->assertEquals('kitten', $module_field_results[2]
->getDisplayValue());
}
/**
* Tests facet build with base props.
*/
public function testBuildBaseField() {
$config_manager = $this
->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$base_field = $this
->getMockBuilder(BaseFieldDefinition::class)
->disableOriginalConstructor()
->getMock();
$entity_field_manager = $this
->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$entity_field_manager
->expects($this
->any())
->method('getFieldDefinitions')
->with('node', '')
->willReturn([
'test_facet_baseprop' => $base_field,
]);
$entity_type_bundle_info = $this
->getMockBuilder(EntityTypeBundleInfo::class)
->disableOriginalConstructor()
->getMock();
$processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
// Base prop facet.
$base_prop_facet = new Facet([], 'facets_facet');
$base_prop_facet
->setFieldIdentifier('test_facet_baseprop');
$base_prop_facet
->setFacetSourceId('llama_source');
$base_prop_facet
->setResults($this->results);
$base_prop_facet
->addProcessor([
'processor_id' => 'list_item',
'weights' => [],
'settings' => [],
]);
/** @var \Drupal\facets\Result\Result[] $base_prop_results */
$base_prop_results = $processor
->build($base_prop_facet, $this->results);
$this
->assertCount(3, $base_prop_results);
$this
->assertEquals('llama', $base_prop_results[0]
->getDisplayValue());
$this
->assertEquals('badger', $base_prop_results[1]
->getDisplayValue());
$this
->assertEquals('kitten', $base_prop_results[2]
->getDisplayValue());
}
/**
* Tests configuration.
*/
public function testConfiguration() {
$config = $this->processor
->defaultConfiguration();
$this
->assertEquals([], $config);
}
/**
* Tests testDescription().
*/
public function testDescription() {
$this
->assertEquals('', $this->processor
->getDescription());
}
/**
* Tests isHidden().
*/
public function testIsHidden() {
$this
->assertEquals(FALSE, $this->processor
->isHidden());
}
/**
* Tests isLocked().
*/
public function testIsLocked() {
$this
->assertEquals(FALSE, $this->processor
->isLocked());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ListItemProcessorTest:: |
protected | property | The processor to be tested. | |
ListItemProcessorTest:: |
protected | property | An array containing the results before the processor has ran. | |
ListItemProcessorTest:: |
protected | function |
Creates a new processor object for use in the tests. Overrides UnitTestCase:: |
|
ListItemProcessorTest:: |
public | function | Tests facet build with base props. | |
ListItemProcessorTest:: |
public | function | Tests facet build with field.module field. | |
ListItemProcessorTest:: |
public | function | Tests facet build with field.module field. | |
ListItemProcessorTest:: |
public | function | Tests configuration. | |
ListItemProcessorTest:: |
public | function | Tests testDescription(). | |
ListItemProcessorTest:: |
public | function | Tests isHidden(). | |
ListItemProcessorTest:: |
public | function | Tests isLocked(). | |
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. |