You are here

class TranslateEntityProcessorTest in Facets 8

Unit test for processor.

@group facets

Hierarchy

Expanded class hierarchy of TranslateEntityProcessorTest

File

tests/src/Unit/Plugin/processor/TranslateEntityProcessorTest.php, line 26

Namespace

Drupal\Tests\facets\Unit\Plugin\processor
View source
class TranslateEntityProcessorTest extends UnitTestCase {

  /**
   * The mocked language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $languageManager;

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Mock language manager.
    $this->languageManager = $this
      ->getMockBuilder(LanguageManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $language = new Language([
      'langcode' => 'en',
    ]);
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->will($this
      ->returnValue($language));

    // Mock entity type manager.
    $this->entityTypeManager = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();

    // Create and set a global container with the language manager and entity
    // type manager.
    $container = new ContainerBuilder();
    $container
      ->set('language_manager', $this->languageManager);
    $container
      ->set('entity_type.manager', $this->entityTypeManager);
    \Drupal::setContainer($container);
  }

  /**
   * Provides mock data for the tests in this class.
   *
   * We create a data definition for both entity reference and entity reference
   * revision field types so we can test with both the label tranformation.
   *
   * @return array
   *   The facet and results test data.
   */
  public function facetDataProvider() {
    $data = [];
    foreach ([
      'entity_reference',
      'entity_reference_revision',
    ] as $field_type) {

      // Mock the typed data chain.
      $target_field_definition = $this
        ->createMock(EntityDataDefinition::class);
      $target_field_definition
        ->expects($this
        ->once())
        ->method('getEntityTypeId')
        ->willReturn('entity_type');
      $property_definition = $this
        ->createMock(DataReferenceDefinitionInterface::class);
      $property_definition
        ->expects($this
        ->any())
        ->method('getTargetDefinition')
        ->willReturn($target_field_definition);
      $property_definition
        ->expects($this
        ->any())
        ->method('getDataType')
        ->willReturn($field_type);
      $data_definition = $this
        ->createMock(ComplexDataDefinitionInterface::class);
      $data_definition
        ->expects($this
        ->any())
        ->method('getPropertyDefinition')
        ->willReturn($property_definition);
      $data_definition
        ->expects($this
        ->any())
        ->method('getPropertyDefinitions')
        ->willReturn([
        $property_definition,
      ]);

      // Create the actual facet.
      $facet = $this
        ->getMockBuilder(Facet::class)
        ->disableOriginalConstructor()
        ->getMock();
      $facet
        ->expects($this
        ->any())
        ->method('getDataDefinition')
        ->willReturn($data_definition);

      // Add a field identifier.
      $facet
        ->expects($this
        ->any())
        ->method('getFieldIdentifier')
        ->willReturn('testfield');
      $results = [
        new Result($facet, 2, 2, 5),
      ];
      $facet
        ->setResults($results);
      $data[$field_type][] = $facet;
      $data[$field_type][] = $results;
    }
    return $data;
  }

  /**
   * Tests that node results were correctly changed.
   *
   * @dataProvider facetDataProvider
   *
   * @param \Drupal\facets\FacetInterface $facet
   *   A facet mock.
   * @param array $results
   *   The facet original results mock.
   */
  public function testNodeResultsChanged(FacetInterface $facet, array $results) {

    // Mock a node and add the label to it.
    $node = $this
      ->getMockBuilder(Node::class)
      ->disableOriginalConstructor()
      ->getMock();
    $node
      ->expects($this
      ->any())
      ->method('label')
      ->willReturn('shaken not stirred');
    $nodes = [
      2 => $node,
    ];
    $node_storage = $this
      ->createMock(EntityStorageInterface::class);
    $node_storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturn($nodes);
    $this->entityTypeManager
      ->expects($this
      ->exactly(1))
      ->method('getStorage')
      ->willReturn($node_storage);

    // Set expected results.
    $expected_results = [
      [
        'nid' => 2,
        'title' => 'shaken not stirred',
      ],
    ];

    // Without the processor we expect the id to display.
    foreach ($expected_results as $key => $expected) {
      $this
        ->assertEquals($expected['nid'], $results[$key]
        ->getRawValue());
      $this
        ->assertEquals($expected['nid'], $results[$key]
        ->getDisplayValue());
    }

    // With the processor we expect the title to display.

    /** @var \Drupal\facets\Result\ResultInterface[] $filtered_results */
    $processor = new TranslateEntityProcessor([], 'translate_entity', [], $this->languageManager, $this->entityTypeManager);
    $filtered_results = $processor
      ->build($facet, $results);
    foreach ($expected_results as $key => $expected) {
      $this
        ->assertEquals($expected['nid'], $filtered_results[$key]
        ->getRawValue());
      $this
        ->assertEquals($expected['title'], $filtered_results[$key]
        ->getDisplayValue());
    }
  }

  /**
   * Tests that term results were correctly changed.
   *
   * @dataProvider facetDataProvider
   *
   * @param \Drupal\facets\FacetInterface $facet
   *   A facet mock.
   * @param array $results
   *   The facet original results mock.
   */
  public function testTermResultsChanged(FacetInterface $facet, array $results) {

    // Mock term.
    $term = $this
      ->getMockBuilder(Term::class)
      ->disableOriginalConstructor()
      ->getMock();
    $term
      ->expects($this
      ->once())
      ->method('label')
      ->willReturn('Burrowing owl');
    $terms = [
      2 => $term,
    ];
    $term_storage = $this
      ->createMock(EntityStorageInterface::class);
    $term_storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturn($terms);
    $this->entityTypeManager
      ->expects($this
      ->exactly(1))
      ->method('getStorage')
      ->willReturn($term_storage);

    // Set expected results.
    $expected_results = [
      [
        'tid' => 2,
        'name' => 'Burrowing owl',
      ],
    ];

    // Without the processor we expect the id to display.
    foreach ($expected_results as $key => $expected) {
      $this
        ->assertEquals($expected['tid'], $results[$key]
        ->getRawValue());
      $this
        ->assertEquals($expected['tid'], $results[$key]
        ->getDisplayValue());
    }

    /** @var \Drupal\facets\Result\ResultInterface[] $filtered_results */
    $processor = new TranslateEntityProcessor([], 'translate_entity', [], $this->languageManager, $this->entityTypeManager);
    $filtered_results = $processor
      ->build($facet, $results);

    // With the processor we expect the title to display.
    foreach ($expected_results as $key => $expected) {
      $this
        ->assertEquals($expected['tid'], $filtered_results[$key]
        ->getRawValue());
      $this
        ->assertEquals($expected['name'], $filtered_results[$key]
        ->getDisplayValue());
    }
  }

  /**
   * Test that deleted entities still in index results doesn't display.
   *
   * @dataProvider facetDataProvider
   *
   * @param \Drupal\facets\FacetInterface $facet
   *   A facet mock.
   * @param array $results
   *   The facet original results mock.
   */
  public function testDeletedEntityResults(FacetInterface $facet, array $results) {

    // Set original results.
    $term_storage = $this
      ->createMock(EntityStorageInterface::class);
    $term_storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturn([]);
    $this->entityTypeManager
      ->expects($this
      ->exactly(1))
      ->method('getStorage')
      ->willReturn($term_storage);

    // Processor should return nothing (and not throw an exception).

    /** @var \Drupal\facets\Result\ResultInterface[] $filtered_results */
    $processor = new TranslateEntityProcessor([], 'translate_entity', [], $this->languageManager, $this->entityTypeManager);
    $filtered_results = $processor
      ->build($facet, $results);
    $this
      ->assertEmpty($filtered_results);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
TranslateEntityProcessorTest::$entityTypeManager protected property The mocked entity type manager.
TranslateEntityProcessorTest::$languageManager protected property The mocked language manager.
TranslateEntityProcessorTest::facetDataProvider public function Provides mock data for the tests in this class.
TranslateEntityProcessorTest::setUp protected function Overrides UnitTestCase::setUp
TranslateEntityProcessorTest::testDeletedEntityResults public function Test that deleted entities still in index results doesn't display.
TranslateEntityProcessorTest::testNodeResultsChanged public function Tests that node results were correctly changed.
TranslateEntityProcessorTest::testTermResultsChanged public function Tests that term results were correctly changed.
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.