diff --git a/src/Tests/FacetSourceTest.php b/src/Tests/FacetSourceTest.php index 92d179e..fd853e6 100644 --- a/src/Tests/FacetSourceTest.php +++ b/src/Tests/FacetSourceTest.php @@ -17,9 +17,22 @@ use \Drupal\facets\Tests\WebTestBase as FacetWebTestBase; class FacetSourceTest extends FacetWebTestBase { /** + * {@inheritdoc} + */ + public static $modules = [ + 'views', + 'search_api', + 'search_api_test_backend', + 'facets', + 'search_api_test_views', + 'facets_search_api_dependency', + 'facets_query_processor', + ]; + + /** * Test the facet source editing. */ - public function testFacetSource() { + public function testEditFilterKey() { // Make sure we're logged in with a user that has sufficient permissions. $this->drupalLogin($this->adminUser); @@ -31,16 +44,44 @@ class FacetSourceTest extends FacetWebTestBase { // Test the edit page. $edit = array( 'filterKey' => 'fq', - 'urlProcessor' => 'query_string', ); $this->assertField('filterKey'); $this->assertField('urlProcessor'); $this->drupalPostForm(NULL, $edit, $this->t('Save')); + $this->assertResponse(200); - // Test that saving worked. + // Test that saving worked filterkey has the new value $this->assertField('filterKey'); $this->assertField('urlProcessor'); $this->assertRaw('fq'); } + /** + * Tests editing the url processor. + */ + public function testEditUrlProcessor() { + // Make sure we're logged in with a user that has sufficient permissions. + $this->drupalLogin($this->adminUser); + + // Test the overview. + $this->drupalGet('admin/config/search/facets'); + $this->assertLink($this->t('Configure')); + $this->clickLink($this->t('Configure')); + + // Test the edit page. + $edit = array( + 'urlProcessor' => 'dummy_query', + ); + $this->assertField('filterKey'); + $this->assertField('urlProcessor'); + $this->drupalPostForm(NULL, $edit, $this->t('Save')); + $this->assertResponse(200); + + // Test that saving worked and that the url processor has the new value. + $this->assertField('filterKey'); + $this->assertField('urlProcessor'); + $elements = $this->xpath('//input[@id=:id]', [':id' => 'edit-urlprocessor-dummy-query']); + $this->assertEqual('dummy_query', $elements[0]['value']); + } + } diff --git a/src/Tests/UrlIntegrationTest.php b/src/Tests/UrlIntegrationTest.php index bfb7614..f79ea48 100644 --- a/src/Tests/UrlIntegrationTest.php +++ b/src/Tests/UrlIntegrationTest.php @@ -7,6 +7,9 @@ namespace Drupal\facets\Tests; +use Drupal\Core\Url; +use Drupal\facets\Entity\Facet; +use Drupal\facets\FacetSourceInterface; use \Drupal\facets\Tests\WebTestBase as FacetWebTestBase; /** @@ -65,25 +68,80 @@ class UrlIntegrationTest extends FacetWebTestBase { $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_view:page_1'], $this->t('Configure facet source')); $this->drupalPostForm(NULL, $form_values, $this->t('Save')); - // Go to the only enabled facet source's config. + $block_values = [ + 'plugin_id' => 'facet_block:' . $id, + 'settings' => [ + 'region' => 'footer', + 'id' => str_replace('_', '-', $id), + ] + ]; + $this->drupalPlaceBlock($block_values['plugin_id'], $block_values['settings']); + + $url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'facet:item']]); + $this->checkClickedFacetUrl($url); + + /** @var \Drupal\facets\FacetInterface $facet */ + $facet = Facet::load($id); + $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')); $edit = [ 'filterKey' => 'y', - 'urlProcessor' => 'dummy_query', + 'urlProcessor' => 'query_string', ]; $this->drupalPostForm(NULL, $edit, $this->t('Save')); - $block_values = [ - 'plugin_id' => 'facet_block:' . $id, - 'settings' => [ - 'region' => 'footer', - 'id' => str_replace('_', '-', $id), - ] + /** @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')); + + $edit = [ + 'filterKey' => 'y', + 'urlProcessor' => 'dummy_query', ]; - $this->drupalPlaceBlock($block_values['plugin_id'], $block_values['settings']); + $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); + } + + /** + * Checks that the url after clicking a facet is as expected. + * + * @param \Drupal\Core\Url $url + * The expected url we end on. + */ + protected function checkClickedFacetUrl(Url $url) { $this->drupalGet('search-api-test-fulltext'); $this->assertResponse(200); $this->assertLink('item'); @@ -94,7 +152,7 @@ class UrlIntegrationTest extends FacetWebTestBase { $this->assertResponse(200); $this->assertLink('(-) item'); $this->assertNoLink('article'); - $this->assertUrl('search-api-test-fulltext', ['query' => ['y[0]' => 'facet||item']]); + $this->assertUrl($url); } }