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\views\Entity\View;
class FacetsTest extends SearchApiBrowserTestBase {
use BlockTestTrait;
use ExampleContentTrait {
indexItems as doIndexItems;
}
use TestHelperTrait;
public static $modules = array(
'block',
'views',
'search_api_solr',
'search_api_solr_test',
'search_api_solr_test_facets',
'facets',
);
protected function tearDown() {
if ($this->indexId) {
Index::load($this->indexId)
->clear();
sleep(2);
}
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
->drupalPostForm('admin/config/search/facets/owl/edit', $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);
sleep(2);
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, array(
':label' => $label,
));
if (isset($urls[$index])) {
$url = $urls[$index];
$url_target = $this
->getAbsoluteUrl($url
->getAttribute('href'));
$this
->assertTrue(TRUE, new FormattableMarkup('Clicked link %label (@url_target) from @url_before', array(
'%label' => $label,
'@url_target' => $url_target,
'@url_before' => $url_before,
)));
return $this
->drupalGet($url_target);
}
$this
->assertTrue(FALSE, new FormattableMarkup('Link %label does not exist on @url_before', array(
'%label' => $label,
'@url_before' => $url_before,
)));
return FALSE;
}
}