You are here

class ListItemProcessorTest in Facets 8

Unit test for processor.

@group facets

Hierarchy

Expanded class hierarchy of ListItemProcessorTest

File

tests/src/Unit/Plugin/processor/ListItemProcessorTest.php, line 24

Namespace

Drupal\Tests\facets\Unit\Plugin\processor
View 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

Namesort descending Modifiers Type Description Overrides
ListItemProcessorTest::$processor protected property The processor to be tested.
ListItemProcessorTest::$results protected property An array containing the results before the processor has ran.
ListItemProcessorTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
ListItemProcessorTest::testBuildBaseField public function Tests facet build with base props.
ListItemProcessorTest::testBuildBundle public function Tests facet build with field.module field.
ListItemProcessorTest::testBuildConfigurableField public function Tests facet build with field.module field.
ListItemProcessorTest::testConfiguration public function Tests configuration.
ListItemProcessorTest::testDescription public function Tests testDescription().
ListItemProcessorTest::testIsHidden public function Tests isHidden().
ListItemProcessorTest::testIsLocked public function Tests isLocked().
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.