View source
<?php
namespace Drupal\Tests\search_api_solr\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\facets\Functional\BlockTestTrait;
use Drupal\Tests\facets\Functional\ExampleContentTrait;
use Drupal\Tests\facets\Functional\TestHelperTrait;
use Drupal\search_api\Entity\Index;
use Drupal\Tests\search_api\Functional\SearchApiBrowserTestBase;
use Drupal\search_api_solr\Utility\SolrCommitTrait;
use Drupal\views\Entity\View;
class FacetsTest extends SearchApiBrowserTestBase {
use SolrCommitTrait;
use BlockTestTrait;
use ExampleContentTrait {
indexItems as doIndexItems;
}
use TestHelperTrait;
public static $modules = [
'block',
'views',
'search_api_solr',
'search_api_solr_test',
'search_api_solr_test_facets',
'facets',
];
protected function tearDown() : void {
if ($this->indexId) {
$index = Index::load($this->indexId);
$index
->clear();
$this
->ensureCommit($index);
}
parent::tearDown();
}
public function testFacets() {
$this->indexId = 'solr_search_index';
$view = View::load('search_api_test_view');
$this
->assertEquals('search_api_index_solr_search_index', $view
->get('base_table'));
$admin_user = $this
->drupalCreateUser([
'administer search_api',
'administer facets',
'access administration pages',
'administer blocks',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/search/search-api');
$this
->assertSession()
->pageTextContains('Test index');
$this
->setUpExampleStructure();
$this
->insertExampleContent();
$indexed_items = $this
->indexItems($this->indexId);
$this
->assertEquals(5, $indexed_items, 'Five items are indexed.');
$this
->createFacet('Owl', 'owl');
$edit = [
'widget' => 'links',
'widget_config[show_numbers]' => '1',
];
$this
->drupalGet('admin/config/search/facets/owl/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('search-api-test-fulltext');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertFacetLabel('item (3)');
$this
->assertFacetLabel('article (2)');
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$this
->clickLinkPartialName('item');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Displaying 3 search results');
}
protected function indexItems($index_id) {
$index_status = $this
->doindexItems($index_id);
$index = Index::load($this->indexId);
$this
->ensureCommit($index);
return $index_status;
}
protected function clickLinkPartialName($label, $index = 0) {
return $this
->clickLinkHelper($label, $index, '//a[starts-with(normalize-space(), :label)]');
}
protected function clickLinkHelper($label, $index, $pattern) {
$label = (string) $label;
$url_before = $this
->getUrl();
$urls = $this
->xpath($pattern, [
':label' => $label,
]);
if (isset($urls[$index])) {
$url = $urls[$index];
$url_target = $this
->getAbsoluteUrl($url
->getAttribute('href'));
$message = new FormattableMarkup('Clicked link %label (@url_target) from @url_before', [
'%label' => $label,
'@url_target' => $url_target,
'@url_before' => $url_before,
]);
$this
->assertTrue(TRUE, $message);
return $this
->drupalGet($url_target);
}
$this
->assertTrue(FALSE, new FormattableMarkup('Link %label does not exist on @url_before', [
'%label' => $label,
'@url_before' => $url_before,
]));
return FALSE;
}
}