You are here

class UidToUserNameCallbackProcessorTest in Facets 8

Unit test for processor.

@group facets

Hierarchy

Expanded class hierarchy of UidToUserNameCallbackProcessorTest

File

tests/src/Unit/Plugin/processor/UidToUserNameCallbackProcessorTest.php, line 20

Namespace

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

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

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();
    $this->processor = new UidToUserNameCallbackProcessor([], 'uid_to_username_callback', []);
  }

  /**
   * Tests that results were correctly changed.
   */
  public function testResultsChanged() {
    $user_storage = $this
      ->createMock(EntityStorageInterface::class);
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_repository = $this
      ->createMock(EntityTypeRepositoryInterface::class);
    $entity_repository
      ->expects($this
      ->any())
      ->method('getEntityTypeFromClass')
      ->willReturn('user');
    $entity_type_manager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->willReturn($user_storage);
    $user1 = $this
      ->createMock(AccountInterface::class);
    $user1
      ->method('getDisplayName')
      ->willReturn('Admin');
    $user_storage
      ->method('load')
      ->willReturn($user1);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.repository', $entity_repository);
    $container
      ->set('entity_type.manager', $entity_type_manager);
    \Drupal::setContainer($container);
    $facet = new Facet([], 'facets_facet');
    $original_results = [
      new Result($facet, 1, 1, 5),
    ];
    $facet
      ->setResults($original_results);
    $expected_results = [
      [
        'uid' => 1,
        'name' => 'Admin',
      ],
    ];
    foreach ($expected_results as $key => $expected) {
      $this
        ->assertEquals($expected['uid'], $original_results[$key]
        ->getRawValue());
      $this
        ->assertEquals($expected['uid'], $original_results[$key]
        ->getDisplayValue());
    }
    $filtered_results = $this->processor
      ->build($facet, $original_results);
    foreach ($expected_results as $key => $expected) {
      $this
        ->assertEquals($expected['uid'], $filtered_results[$key]
        ->getRawValue());
      $this
        ->assertEquals($expected['name'], $filtered_results[$key]
        ->getDisplayValue());
    }
  }

  /**
   * Tests that deleted entity results were correctly handled.
   */
  public function testDeletedEntityResults() {
    $user_storage = $this
      ->createMock(EntityStorageInterface::class);
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_repository = $this
      ->createMock(EntityTypeRepositoryInterface::class);
    $entity_repository
      ->expects($this
      ->any())
      ->method('getEntityTypeFromClass')
      ->willReturn('user');
    $entity_type_manager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->willReturn($user_storage);
    $user_storage
      ->method('load')
      ->willReturn(NULL);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.repository', $entity_repository);
    $container
      ->set('entity_type.manager', $entity_type_manager);
    \Drupal::setContainer($container);
    $facet = new Facet([], 'facets_facet');
    $original_results = [
      new Result($facet, 1, 1, 5),
    ];
    $facet
      ->setResults($original_results);
    $filtered_results = $this->processor
      ->build($facet, $original_results);
    $this
      ->assertEmpty($filtered_results);
  }

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

}

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.
UidToUserNameCallbackProcessorTest::$processor protected property The processor to be tested.
UidToUserNameCallbackProcessorTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
UidToUserNameCallbackProcessorTest::testConfiguration public function Tests configuration.
UidToUserNameCallbackProcessorTest::testDeletedEntityResults public function Tests that deleted entity results were correctly handled.
UidToUserNameCallbackProcessorTest::testResultsChanged public function Tests that 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.