You are here

class LinksWidgetTest in Facets 8

Unit test for widget.

@group facets

Hierarchy

Expanded class hierarchy of LinksWidgetTest

File

tests/src/Unit/Plugin/widget/LinksWidgetTest.php, line 23

Namespace

Drupal\Tests\facets\Unit\Plugin\widget
View source
class LinksWidgetTest extends WidgetTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->widget = new LinksWidget([], 'links_widget', []);
  }

  /**
   * Tests widget without filters.
   */
  public function testNoFilterResults() {
    $facet = $this->facet;
    $facet
      ->setResults($this->originalResults);
    $this->widget
      ->setConfiguration([
      'show_numbers' => TRUE,
    ]);
    $output = $this->widget
      ->build($facet);
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(4, $output['#items']);
    $expected_links = [
      $this
        ->buildLinkAssertion('Llama', 'llama', $facet, 10),
      $this
        ->buildLinkAssertion('Badger', 'badger', $facet, 20),
      $this
        ->buildLinkAssertion('Duck', 'duck', $facet, 15),
      $this
        ->buildLinkAssertion('Alpaca', 'alpaca', $facet, 9),
    ];
    foreach ($expected_links as $index => $value) {
      $this
        ->assertSame('array', gettype($output['#items'][$index]));
      $this
        ->assertEquals($value, $output['#items'][$index]['#title']);
      $this
        ->assertSame('array', gettype($output['#items'][$index]['#title']));
      $this
        ->assertEquals('link', $output['#items'][$index]['#type']);
      $this
        ->assertEquals([
        'facet-item',
      ], $output['#items'][$index]['#wrapper_attributes']['class']);
    }
  }

  /**
   * Test widget with 2 active items.
   */
  public function testActiveItems() {
    $original_results = $this->originalResults;
    $original_results[0]
      ->setActiveState(TRUE);
    $original_results[3]
      ->setActiveState(TRUE);
    $facet = $this->facet;
    $facet
      ->setResults($original_results);
    $this->widget
      ->setConfiguration([
      'show_numbers' => TRUE,
    ]);
    $output = $this->widget
      ->build($facet);
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(4, $output['#items']);
    $expected_links = [
      $this
        ->buildLinkAssertion('Llama', 'llama', $facet, 10, TRUE),
      $this
        ->buildLinkAssertion('Badger', 'badger', $facet, 20),
      $this
        ->buildLinkAssertion('Duck', 'duck', $facet, 15),
      $this
        ->buildLinkAssertion('Alpaca', 'alpaca', $facet, 9, TRUE),
    ];
    foreach ($expected_links as $index => $value) {
      $this
        ->assertSame('array', gettype($output['#items'][$index]));
      $this
        ->assertEquals($value, $output['#items'][$index]['#title']);
      $this
        ->assertEquals('link', $output['#items'][$index]['#type']);
      if ($index === 0 || $index === 3) {
        $this
          ->assertEquals([
          'is-active',
        ], $output['#items'][$index]['#attributes']['class']);
      }
      $this
        ->assertEquals([
        'facet-item',
      ], $output['#items'][$index]['#wrapper_attributes']['class']);
    }
  }

  /**
   * Tests widget, make sure hiding and showing numbers works.
   */
  public function testHideNumbers() {
    $original_results = $this->originalResults;
    $original_results[1]
      ->setActiveState(TRUE);
    $facet = $this->facet;
    $facet
      ->setResults($original_results);
    $this->widget
      ->setConfiguration([
      'show_numbers' => FALSE,
    ]);
    $output = $this->widget
      ->build($facet);
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(4, $output['#items']);
    $expected_links = [
      $this
        ->buildLinkAssertion('Llama', 'llama', $facet, 10, FALSE, FALSE),
      $this
        ->buildLinkAssertion('Badger', 'badger', $facet, 20, TRUE, FALSE),
      $this
        ->buildLinkAssertion('Duck', 'duck', $facet, 15, FALSE, FALSE),
      $this
        ->buildLinkAssertion('Alpaca', 'alpaca', $facet, 9, FALSE, FALSE),
    ];
    foreach ($expected_links as $index => $value) {
      $this
        ->assertSame('array', gettype($output['#items'][$index]));
      $this
        ->assertEquals($value, $output['#items'][$index]['#title']);
      $this
        ->assertEquals('link', $output['#items'][$index]['#type']);
      if ($index === 1) {
        $this
          ->assertEquals([
          'is-active',
        ], $output['#items'][$index]['#attributes']['class']);
      }
      $this
        ->assertEquals([
        'facet-item',
      ], $output['#items'][$index]['#wrapper_attributes']['class']);
    }

    // Enable the 'show_numbers' setting again to make sure that the switch
    // between those settings works.
    $this->widget
      ->setConfiguration([
      'show_numbers' => TRUE,
    ]);
    $output = $this->widget
      ->build($facet);
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(4, $output['#items']);
    $expected_links = [
      $this
        ->buildLinkAssertion('Llama', 'llama', $facet, 10),
      $this
        ->buildLinkAssertion('Badger', 'badger', $facet, 20, TRUE),
      $this
        ->buildLinkAssertion('Duck', 'duck', $facet, 15),
      $this
        ->buildLinkAssertion('Alpaca', 'alpaca', $facet, 9),
    ];
    foreach ($expected_links as $index => $value) {
      $this
        ->assertSame('array', gettype($output['#items'][$index]));
      $this
        ->assertEquals($value, $output['#items'][$index]['#title']);
      $this
        ->assertEquals('link', $output['#items'][$index]['#type']);
      if ($index === 1) {
        $this
          ->assertEquals([
          'is-active',
        ], $output['#items'][$index]['#attributes']['class']);
      }
      $this
        ->assertEquals([
        'facet-item',
      ], $output['#items'][$index]['#wrapper_attributes']['class']);
    }
  }

  /**
   * Tests for links widget with children.
   */
  public function testChildren() {
    $original_results = $this->originalResults;
    $facet = $this->facet;
    $child = new Result($facet, 'snake', 'Snake', 5);
    $original_results[1]
      ->setActiveState(TRUE);
    $original_results[1]
      ->setChildren([
      $child,
    ]);
    $facet
      ->setResults($original_results);
    $this->widget
      ->setConfiguration([
      'show_numbers' => TRUE,
    ]);
    $output = $this->widget
      ->build($facet);
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(4, $output['#items']);
    $expected_links = [
      $this
        ->buildLinkAssertion('Llama', 'llama', $facet, 10),
      $this
        ->buildLinkAssertion('Badger', 'badger', $facet, 20, TRUE),
      $this
        ->buildLinkAssertion('Duck', 'duck', $facet, 15),
      $this
        ->buildLinkAssertion('Alpaca', 'alpaca', $facet, 9),
    ];
    foreach ($expected_links as $index => $value) {
      $this
        ->assertSame('array', gettype($output['#items'][$index]));
      $this
        ->assertEquals($value, $output['#items'][$index]['#title']);
      $this
        ->assertEquals('link', $output['#items'][$index]['#type']);
      if ($index === 1) {
        $this
          ->assertEquals([
          'is-active',
        ], $output['#items'][$index]['#attributes']['class']);
        $this
          ->assertEquals([
          'facet-item',
          'facet-item--expanded',
        ], $output['#items'][$index]['#wrapper_attributes']['class']);
      }
      else {
        $this
          ->assertEquals([
          'facet-item',
        ], $output['#items'][$index]['#wrapper_attributes']['class']);
      }
    }
  }

  /**
   * Tests the rest link.
   */
  public function testResetLink() {
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setResults($this->originalResults);
    $output = $this->widget
      ->build($facet);
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(4, $output['#items']);
    $request = new Request();
    $request->query
      ->set('f', []);
    $request_stack = new RequestStack();
    $request_stack
      ->push($request);
    $this
      ->createContainer();
    $container = \Drupal::getContainer();
    $container
      ->set('request_stack', $request_stack);
    \Drupal::setContainer($container);

    // Enable the show reset link.
    $this->widget
      ->setConfiguration([
      'show_reset_link' => TRUE,
    ]);
    $output = $this->widget
      ->build($facet);

    // Check that we now have more results.
    $this
      ->assertSame('array', gettype($output));
    $this
      ->assertCount(5, $output['#items']);
  }

  /**
   * {@inheritdoc}
   */
  public function testDefaultConfiguration() {
    $default_config = $this->widget
      ->defaultConfiguration();
    $this
      ->assertArrayHasKey('show_numbers', $default_config);
    $this
      ->assertArrayHasKey('soft_limit', $default_config);
    $this
      ->assertArrayHasKey('show_reset_link', $default_config);
    $this
      ->assertArrayHasKey('reset_text', $default_config);
    $this
      ->assertArrayHasKey('soft_limit_settings', $default_config);
    $this
      ->assertArrayHasKey('show_less_label', $default_config['soft_limit_settings']);
    $this
      ->assertArrayHasKey('show_more_label', $default_config['soft_limit_settings']);
    $this
      ->assertEquals(FALSE, $default_config['show_numbers']);
    $this
      ->assertEquals(0, $default_config['soft_limit']);
    $this
      ->assertEquals(FALSE, $default_config['show_reset_link']);
  }

  /**
   * Sets up a container.
   */
  protected function createContainer() {
    $router = $this
      ->getMockBuilder(TestRouterInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $router
      ->expects($this
      ->any())
      ->method('matchRequest')
      ->willReturn([
      '_raw_variables' => new ParameterBag([]),
      '_route' => 'test',
    ]);
    $url_processor = $this
      ->getMockBuilder(UrlProcessorInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $manager = $this
      ->getMockBuilder(FacetSourcePluginManager::class)
      ->disableOriginalConstructor()
      ->getMock();
    $manager
      ->expects($this
      ->exactly(1))
      ->method('createInstance')
      ->willReturn($url_processor);
    $storage = $this
      ->createMock(EntityStorageInterface::class);
    $em = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $em
      ->expects($this
      ->exactly(1))
      ->method('getStorage')
      ->willReturn($storage);
    $container = new ContainerBuilder();
    $container
      ->set('router.no_access_checks', $router);
    $container
      ->set('entity_type.manager', $em);
    $container
      ->set('plugin.manager.facets.url_processor', $manager);
    \Drupal::setContainer($container);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinksWidgetTest::createContainer protected function Sets up a container.
LinksWidgetTest::setUp protected function Sets up the container and other variables used in all the tests. Overrides WidgetTestBase::setUp
LinksWidgetTest::testActiveItems public function Test widget with 2 active items.
LinksWidgetTest::testChildren public function Tests for links widget with children.
LinksWidgetTest::testDefaultConfiguration public function Tests default configuration. Overrides WidgetTestBase::testDefaultConfiguration
LinksWidgetTest::testHideNumbers public function Tests widget, make sure hiding and showing numbers works.
LinksWidgetTest::testNoFilterResults public function Tests widget without filters.
LinksWidgetTest::testResetLink public function Tests the rest link.
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.
WidgetTestBase::$facet protected property The facet used for the widget test.
WidgetTestBase::$originalResults protected property An array containing the results for the widget.
WidgetTestBase::$queryTypes protected property An array of possible query types.
WidgetTestBase::$widget protected property The widget to be tested.
WidgetTestBase::buildLinkAssertion protected function Build a formattable markup object to use as assertion.
WidgetTestBase::testGetQueryType public function Tests get query type. 1
WidgetTestBase::testIsPropertyRequired public function Tests default for required properties. 1