/** * Tests filtering happens for regex filter. * * @dataProvider provideRegexTests */ public function testRegexFilter($regex, $expected_results) { $facet = new Facet([], 'facet'); $facet->setResults($this->originalResults); $facet->addProcessor(['processor_id' => 'exclude_specified_items', 'weights' => [], 'settings' => ['exclude' => 'alpaca', 'regex' => 0]]); $this->processor->setConfiguration(['exclude' => $regex, 'regex' => 1]); $filtered_results = $this->processor->build($facet, $this->originalResults); $this->assertCount(count($expected_results), $filtered_results); foreach ($filtered_results as $res) { $this->assertTrue(in_array($res->getDisplayValue(), $expected_results)); } }
/** * Tests filtering of results. */ public function testFilterResults() { $results = $this->originalResults; $results[2]->setActiveState(TRUE); $facet = new Facet([], 'facet'); $facet->setResults($results); $filtered_results = $this->processor->build($facet, $results); $this->assertCount(2, $filtered_results); $this->assertEquals(10, $filtered_results[0]->getCount()); $this->assertEquals('llama', $filtered_results[0]->getDisplayValue()); $this->assertEquals(15, $filtered_results[1]->getCount()); $this->assertEquals('badger', $filtered_results[1]->getDisplayValue()); }
/** * Tests filtering of results. */ public function testFilterResults() { $results = $this->originalResults; $results[2]->setActiveState(TRUE); $facet = new Facet([], 'facet'); $facet->setResults($results); $filtered_results = $this->processor->build($facet, $results); $this->assertCount(2, $filtered_results); // Llama is shown because it narrows results. $this->assertEquals(10, $filtered_results[0]->getCount()); $this->assertEquals('llama', $filtered_results[0]->getDisplayValue()); // Duck is shown because it's already active. $this->assertEquals(15, $filtered_results[2]->getCount()); $this->assertEquals('duck', $filtered_results[2]->getDisplayValue()); }
/** * Tests that results were correctly changed. */ public function testResultsChanged() { $original_results = [new Result(1, 1, 5)]; $facet = new Facet([], 'facet'); $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 string query type without executing the query with an "OR" operator. */ public function testQueryTypeOr() { $query = new SearchApiQuery([], 'search_api_query', []); $facet = new Facet(['query_operator' => 'OR'], 'facets_facet'); $facet->setFieldIdentifier('field_animal'); $original_results = [['count' => 3, 'filter' => 'badger'], ['count' => 5, 'filter' => 'mushroom'], ['count' => 7, 'filter' => 'narwhal'], ['count' => 9, 'filter' => 'unicorn']]; $query_type = new SearchApiString(['facet' => $facet, 'query' => $query, 'results' => $original_results], 'search_api_string', []); $built_facet = $query_type->build(); $this->assertInstanceOf('\\Drupal\\facets\\FacetInterface', $built_facet); $results = $built_facet->getResults(); $this->assertInternalType('array', $results); foreach ($original_results as $k => $result) { $this->assertInstanceOf('\\Drupal\\facets\\Result\\ResultInterface', $results[$k]); $this->assertEquals($result['count'], $results[$k]->getCount()); $this->assertEquals($result['filter'], $results[$k]->getDisplayValue()); } }
/** * Tests that results were correctly changed. */ public function testResultsChanged() { /** @var \Drupal\facets\Result\ResultInterface[] $original_results */ $original_results = [new Result(1, 1, 5)]; $facet = new Facet([], 'facet'); $facet->setResults($original_results); $expected_results = [['tid' => 1, 'name' => 'Burrowing owl']]; foreach ($expected_results as $key => $expected) { $this->assertEquals($expected['tid'], $original_results[$key]->getRawValue()); $this->assertEquals($expected['tid'], $original_results[$key]->getDisplayValue()); } $filtered_results = $this->processor->build($facet, $original_results); foreach ($expected_results as $key => $expected) { $this->assertEquals($expected['tid'], $filtered_results[$key]->getRawValue()); $this->assertEquals($expected['name'], $filtered_results[$key]->getDisplayValue()); } }
/** * Tests widget without filters. */ public function testNoFilterResults() { $facet = new Facet([], 'facet'); $facet->setResults($this->originalResults); $facet->setWidgetConfigs(['show_numbers' => 1]); $output = $this->widget->build($facet); $this->assertInternalType('array', $output); $this->assertCount(4, $output['#items']); $this->assertEquals(['js-facets-checkbox-links'], $output['#attributes']['class']); $expected_links = [$this->buildLinkAssertion('Llama', 10), $this->buildLinkAssertion('Badger', 20), $this->buildLinkAssertion('Duck', 15), $this->buildLinkAssertion('Alpaca', 9)]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); $this->assertEquals($value, $output['#items'][$index]['#title']); $this->assertInstanceOf(FormattableMarkup::class, $output['#items'][$index]['#title']); $this->assertEquals('link', $output['#items'][$index]['#type']); $this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']); } }
/** * Tests facet build with base props. */ public function testFacetBasepropBuild() { $config_manager = $this->getMockBuilder(ConfigManager::class)->disableOriginalConstructor()->getMock(); $base_field = $this->getMockBuilder(BaseFieldDefinition::class)->disableOriginalConstructor()->getMock(); $base_field->expects($this->any())->method('getSetting')->willReturnMap([['allowed_values_function', ''], ['allowed_values', [1 => 'blue whale', 2 => 'lynx', 3 => 'dog-wolf-lion']]]); $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]); $processor_id = 'list_item'; $processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager); // Base prop facet. $base_prop_facet = new Facet([], 'facet'); $base_prop_facet->setFieldIdentifier('test_facet_baseprop'); $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('blue whale', $base_prop_results[0]->getDisplayValue()); $this->assertEquals('lynx', $base_prop_results[1]->getDisplayValue()); $this->assertEquals('dog-wolf-lion', $base_prop_results[2]->getDisplayValue()); }
/** * Tests various url integration things. */ public function testUrlIntegration() { $id = 'facet'; $name = '&^Facet@#1'; $this->createFacet($name, $id); $url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'facet:item']]); $this->checkClickedFacetUrl($url); /** @var \Drupal\facets\FacetInterface $facet */ $facet = Facet::load($id); $this->assertTrue($facet instanceof FacetInterface); $config = $facet->getFacetSourceConfig(); $this->assertTrue($config instanceof FacetSourceInterface); $this->assertEqual(NULL, $config->getFilterKey()); $facet = NULL; $config = NULL; // Go to the only enabled facet source's config and change the filter key. $this->drupalGet('admin/config/search/facets'); $this->clickLink($this->t('Configure'), 1); $edit = ['filter_key' => 'y', 'url_processor' => 'query_string']; $this->drupalPostForm(NULL, $edit, $this->t('Save')); /** @var \Drupal\facets\FacetInterface $facet */ $facet = Facet::load($id); $config = $facet->getFacetSourceConfig(); $this->assertTrue($config instanceof FacetSourceInterface); $this->assertEqual('y', $config->getFilterKey()); $facet = NULL; $config = NULL; $url_2 = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['y[0]' => 'facet:item']]); $this->checkClickedFacetUrl($url_2); // Go to the only enabled facet source's config and change the url // processor. $this->drupalGet('admin/config/search/facets'); $this->clickLink($this->t('Configure'), 1); $edit = ['filter_key' => 'y', 'url_processor' => 'dummy_query']; $this->drupalPostForm(NULL, $edit, $this->t('Save')); /** @var \Drupal\facets\FacetInterface $facet */ $facet = Facet::load($id); $config = $facet->getFacetSourceConfig(); $this->assertTrue($config instanceof FacetSourceInterface); $this->assertEqual('y', $config->getFilterKey()); $facet = NULL; $config = NULL; $url_3 = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['y[0]' => 'facet||item']]); $this->checkClickedFacetUrl($url_3); }
/** * Tests for links widget with children. */ public function testChildren() { $original_results = $this->originalResults; $child = new Result('snake', 'Snake', 5); $original_results[1]->setActiveState(TRUE); $original_results[1]->setChildren($child); $facet = new Facet([], 'facet'); $facet->setResults($original_results); $facet->setWidgetConfigs(['show_numbers' => 1]); $output = $this->widget->build($facet); $this->assertInternalType('array', $output); $this->assertCount(4, $output['#items']); $expected_links = [$this->buildLinkAssertion('Llama', 10), $this->buildLinkAssertion('Badger', 20, TRUE), $this->buildLinkAssertion('Duck', 15), $this->buildLinkAssertion('Alpaca', 9)]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); $this->assertEquals($value, $output['#items'][$index]['#title']); $this->assertEquals('link', $output['#items'][$index]['#type']); if ($index === 1) { $this->assertEquals('active-trail', $output['#items'][$index]['#attributes']['class']); $this->assertEquals(['facet-item', 'expanded'], $output['#items'][$index]['#wrapper_attributes']['class']); } else { $this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']); } } }
/** * Tests that the facet source configuration filter key override works. */ public function testFacetSourceFilterKeyOverride() { $facet_source = new FacetSource(['filter_key' => 'ab'], 'facets_facet_source'); // Override the container with the new facet source. $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->once())->method('load')->willReturn($facet_source); $em = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')->disableOriginalConstructor()->getMock(); $em->expects($this->any())->method('getStorage')->willReturn($storage); $container = \Drupal::getContainer(); $container->set('entity_type.manager', $em); \Drupal::setContainer($container); $facet = new Facet([], 'facet'); $facet->setFieldIdentifier('test'); $facet->setFacetSourceId('facet_source__dummy'); $facet->setUrlAlias('test'); $request = new Request(); $request->query->set('ab', []); $this->processor = new QueryString(['facet' => $facet], 'query_string', [], $request); $results = $this->processor->buildUrls($facet, $this->originalResults); /** @var \Drupal\facets\Result\ResultInterface $r */ foreach ($results as $r) { $this->assertInstanceOf('\\Drupal\\facets\\Result\\ResultInterface', $r); $this->assertEquals('route:test?ab[0]=test%3A' . $r->getRawValue(), $r->getUrl()->toUriString()); } }
/** * Tests facet weights. */ public function testWeight() { $facet_name = "Forest owlet"; $id = "forest_owlet"; $this->createFacet($facet_name, $id); /** @var \Drupal\facets\FacetInterface $facet */ $facet = Facet::load($id); $facet->setWeight(10); $this->assertEqual(10, $facet->getWeight()); }
/** * Tests filtering of results. */ public function testFilterResults() { $facet = new Facet([], 'facet'); $facet->setResults($this->originalResults); $facet->addProcessor(['processor_id' => 'count_limit', 'weights' => [], 'settings' => ['minimum_items' => 8]]); $this->processor->setConfiguration(['minimum_items' => 8]); $sorted_results = $this->processor->build($facet, $this->originalResults); $this->assertCount(2, $sorted_results); $this->assertEquals('llama', $sorted_results[0]->getDisplayValue()); $this->assertEquals('duck', $sorted_results[2]->getDisplayValue()); }
/** * Tests the return value of the plugin for an inactive facet value. */ public function testInactiveFacetValue() { $facet = new Facet([], 'facets_facet'); /** @var \Drupal\facets\Result\ResultInterface[] $results */ $results = [new Result('llama', 'Llama', 1), new Result('kitten', 'Kitten', 5), new Result('puppy', 'Puppy', 3)]; $results[1]->setActiveState(TRUE); $facet->setResults($results); $block = $this->getMockBuilder('\\Drupal\\facets\\Plugin\\Block\\FacetBlock')->disableOriginalConstructor()->getMock(); $block->expects($this->exactly(0))->method('access'); $block->expects($this->exactly(1))->method('getPluginId')->willReturn('block:id'); $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->exactly(1))->method('load')->with('id')->willReturn($facet); $manager = $this->getMockBuilder('\\Drupal\\Core\\Block\\BlockManager')->disableOriginalConstructor()->getMock(); $manager->expects($this->exactly(1))->method('createInstance')->willReturn($block); $user = $this->getMock('\\Drupal\\Core\\Session\\AccountProxyInterface'); $facet_manager = $this->getMockBuilder('\\Drupal\\facets\\FacetManager\\DefaultFacetManager')->disableOriginalConstructor()->getMock(); $facet_manager->expects($this->exactly(1))->method('returnProcessedFacet')->with($facet)->willReturn($facet); $configuration = ['facets' => 'test', 'facet_value' => 'llama']; $sut = new OtherFacet($storage, $manager, $user, $facet_manager, $configuration, 'other_facet', ''); $evaluation = $sut->evaluate(); $this->assertFalse($evaluation); }