View source
<?php
namespace Drupal\Tests\facets_summary\Functional;
use Drupal\Tests\facets\Functional\FacetsTestBase;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\views\Views;
class IntegrationTest extends FacetsTestBase {
public static $modules = [
'facets_summary',
];
public function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this
->setUpExampleStructure();
$this
->insertExampleContent();
$this
->assertEquals(5, $this
->indexItems($this->indexId), '5 items were indexed.');
$this->blocks = NULL;
}
public function testFramework() {
$this
->drupalGet('admin/config/search/facets');
$this
->assertSession()
->pageTextNotContains('Facets Summary');
$values = [
'name' => 'Owl',
'id' => 'owl',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$this
->drupalPostForm(NULL, [], 'Save');
$this
->drupalGet('admin/config/search/facets');
$this
->assertSession()
->pageTextContains('Facets Summary');
$this
->assertSession()
->pageTextContains('Owl');
$this
->drupalGet('admin/config/search/facets/facet-summary/owl/edit');
$this
->assertSession()
->pageTextContains('No facets found.');
$this
->createFacet('Llama', 'llama');
$this
->drupalGet('admin/config/search/facets');
$this
->assertSession()
->pageTextContains('Llama');
$this
->drupalGet('admin/config/search/facets/facet-summary/owl/edit');
$this
->assertSession()
->pageTextNotContains('No facets found.');
$this
->assertSession()
->pageTextContains('Llama');
$this
->assertSession()
->checkboxNotChecked('edit-facets-llama-checked');
$this
->drupalPostForm(NULL, [], 'Save');
$this
->assertSession()
->checkboxNotChecked('edit-facets-llama-checked');
$this
->drupalPostForm(NULL, [
'facets[llama][checked]' => TRUE,
], 'Save');
$this
->assertSession()
->checkboxChecked('edit-facets-llama-checked');
$this
->configureShowCountProcessor();
$this
->configureResetFacetsProcessor();
}
public function testMultipleFacets() {
$this
->createFacet('Giraffe', 'giraffe', 'keywords');
$this
->resetAll();
$this
->createFacet('Llama', 'llama');
$values = [
'name' => 'Owlß',
'id' => 'owl',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$this
->drupalPostForm(NULL, [], 'Save');
$summaries = [
'facets[giraffe][checked]' => TRUE,
'facets[giraffe][label]' => 'Summary giraffe',
];
$this
->drupalPostForm('admin/config/search/facets/facet-summary/owl/edit', $summaries, 'Save');
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', 'owl'),
'weight' => 50,
];
$block = $this
->drupalPlaceBlock('facets_summary_block:owl', $block);
$this
->drupalGet('search-api-test-fulltext');
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$this
->assertSession()
->pageTextContains($block
->label());
$this
->assertFacetBlocksAppear();
$this
->clickLink('apple');
$list_items = $this
->getSession()
->getPage()
->findById('block-' . $block
->id())
->findAll('css', 'li');
$this
->assertCount(1, $list_items);
$this
->clickLink('item');
$list_items = $this
->getSession()
->getPage()
->findById('block-' . $block
->id())
->findAll('css', 'li');
$this
->assertCount(1, $list_items);
$summaries = [
'facets[giraffe][checked]' => TRUE,
'facets[giraffe][label]' => 'Summary giraffe',
'facets[llama][checked]' => TRUE,
'facets[llama][label]' => 'Summary llama',
];
$this
->drupalPostForm('admin/config/search/facets/facet-summary/owl/edit', $summaries, 'Save');
$this
->drupalGet('search-api-test-fulltext');
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$this
->assertSession()
->pageTextContains($block
->label());
$this
->assertFacetBlocksAppear();
$this
->clickLink('apple');
$list_items = $this
->getSession()
->getPage()
->findById('block-' . $block
->id())
->findAll('css', 'li');
$this
->assertCount(1, $list_items);
$this
->clickLink('item');
$list_items = $this
->getSession()
->getPage()
->findById('block-' . $block
->id())
->findAll('css', 'li');
$this
->assertCount(2, $list_items);
$this
->checkShowCountProcessor();
$this
->checkResetFacetsProcessor();
}
public function testShowSummary() {
$this
->createFacet('Giraffe', 'giraffe', 'keywords');
$this
->resetAll();
$this
->createFacet('Llama', 'llama');
$values = [
'name' => 'Owlß',
'id' => 'owl',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$summaries = [
'facets[giraffe][checked]' => TRUE,
'facets[giraffe][label]' => 'Summary giraffe',
'facets[llama][checked]' => TRUE,
'facets[llama][label]' => 'Summary llama',
'facets_summary_settings[show_summary][status]' => TRUE,
];
$this
->drupalPostForm(NULL, $summaries, 'Save');
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', 'owl'),
'weight' => 50,
];
$block = $this
->drupalPlaceBlock('facets_summary_block:owl', $block);
$this
->drupalGet('search-api-test-fulltext');
$this
->assertText('Displaying 5 search results');
$this
->clickLink('item');
$list_items = $this
->getSession()
->getPage()
->findById('block-' . $block
->id())
->findAll('css', 'li');
$this
->assertCount(2, $list_items);
$this
->assertEquals('Summary llama: item', $list_items[0]
->getText());
$this
->assertEquals('(-) item', $list_items[1]
->getText());
}
public function testViewsCacheDisable() {
$view = Views::getView('search_api_test_view');
$view
->setDisplay('page_1');
$current_cache = $view->display_handler
->getOption('cache');
$this
->assertEquals('none', $current_cache['type']);
$view->display_handler
->setOption('cache', [
'type' => 'tag',
]);
$view
->save();
$current_cache = $view->display_handler
->getOption('cache');
$this
->assertEquals('tag', $current_cache['type']);
$id = "western_screech_owl";
$name = "Western screech owl";
$values = [
'name' => $name,
'id' => $id,
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$this
->assertSession()
->pageTextContains('Caching of view Search API Test Fulltext search view has been disabled.');
$view = Views::getView('search_api_test_view');
$view
->setDisplay('page_1');
$current_cache = $view->display_handler
->getOption('cache');
$this
->assertEquals('none', $current_cache['type']);
}
public function testCount() {
$this
->createFacet('Otter', 'otter', 'keywords');
$this
->resetAll();
$this
->createFacet('Wolverine', 'wolverine');
$edit = [
'widget' => 'links',
'widget_config[show_numbers]' => '1',
];
$this
->drupalPostForm('admin/config/search/facets/otter/edit', $edit, 'Save');
$this
->drupalPostForm('admin/config/search/facets/wolverine/edit', $edit, 'Save');
$values = [
'name' => 'Mustelidae',
'id' => 'mustelidae',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$summaries = [
'facets[otter][checked]' => TRUE,
'facets[otter][label]' => 'Summary giraffe',
'facets[otter][show_count]' => FALSE,
'facets[wolverine][checked]' => TRUE,
'facets[wolverine][label]' => 'Summary llama',
'facets[wolverine][show_count]' => FALSE,
];
$this
->drupalPostForm(NULL, $summaries, 'Save');
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', 'owl'),
'weight' => 50,
];
$summary_block = $this
->drupalPlaceBlock('facets_summary_block:mustelidae', $block);
$this
->drupalGet('search-api-test-fulltext');
$webAssert = $this
->assertSession();
$webAssert
->pageTextContains('Displaying 5 search results');
$this
->assertFacetBlocksAppear();
$webAssert
->pageTextContains($summary_block
->label());
$this
->assertFacetLabel('article (2)');
$this
->assertFacetLabel('apple (2)');
$summaries = [
'facets[otter][show_count]' => TRUE,
'facets[wolverine][show_count]' => TRUE,
];
$this
->drupalPostForm('admin/config/search/facets/facet-summary/mustelidae/edit', $summaries, 'Save');
$this
->drupalGet('search-api-test-fulltext');
$webAssert = $this
->assertSession();
$webAssert
->pageTextContains('Displaying 5 search results');
$this
->assertFacetBlocksAppear();
$webAssert
->pageTextContains($summary_block
->label());
$this
->assertFacetLabel('article (2)');
$this
->assertFacetLabel('apple (2)');
}
public function testBlockDelete() {
$name = 'Owl';
$id = 'owl';
$values = [
'name' => $name,
'id' => $id,
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$this
->drupalPostForm(NULL, [], 'Save');
$block_settings = [
'region' => 'footer',
'id' => $id,
];
$block = $this
->drupalPlaceBlock('facets_summary_block:' . $id, $block_settings);
$this
->drupalGet('admin/structure/block');
$this
->assertSession()
->pageTextContains($block
->label());
$this
->drupalGet('admin/structure/block/library/classy');
$this
->assertSession()
->pageTextContains($name);
$this
->drupalGet('admin/config/search/facets/facet-summary/' . $id . '/delete');
$this
->assertSession()
->pageTextContains('The listed configuration will be deleted.');
$this
->assertSession()
->pageTextContains($block
->label());
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->drupalGet('admin/structure/block/library/classy');
$this
->assertSession()
->pageTextNotContains($name);
}
protected function configureShowCountProcessor() {
$this
->assertSession()
->checkboxNotChecked('edit-facets-summary-settings-show-count-status');
$this
->drupalPostForm(NULL, [
'facets_summary_settings[show_count][status]' => TRUE,
], 'Save');
$this
->assertSession()
->checkboxChecked('edit-facets-summary-settings-show-count-status');
$this
->assertSession()
->pageTextContains(t('Facets Summary Owl has been updated.'));
}
protected function configureResetFacetsProcessor() {
$this
->assertSession()
->checkboxNotChecked('edit-facets-summary-settings-reset-facets-status');
$this
->drupalPostForm(NULL, [
'facets_summary_settings[reset_facets][status]' => TRUE,
], 'Save');
$this
->assertSession()
->checkboxChecked('edit-facets-summary-settings-reset-facets-status');
$this
->assertSession()
->pageTextContains(t('Facets Summary Owl has been updated.'));
$this
->assertSession()
->fieldExists('facets_summary_settings[reset_facets][settings][link_text]');
$this
->drupalPostForm(NULL, [
'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset facets',
], 'Save');
$this
->assertSession()
->pageTextContains(t('Facets Summary Owl has been updated.'));
$this
->assertSession()
->fieldValueEquals('facets_summary_settings[reset_facets][settings][link_text]', 'Reset facets');
}
protected function checkShowCountProcessor() {
FacetsSummary::create([
'id' => 'show_count',
'name' => 'Show count summary',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
'facets' => [
'giraffe' => [
'checked' => 1,
'label' => 'Giraffe',
'separator' => ',',
'weight' => 0,
'show_count' => 0,
],
'llama' => [
'checked' => 1,
'label' => 'Llama',
'separator' => ',',
'weight' => 0,
'show_count' => 0,
],
],
'processor_configs' => [
'show_count' => [
'processor_id' => 'show_count',
'weights' => [
'build' => -10,
],
],
],
])
->save();
$this
->resetAll();
$blockConfig = [
'region' => 'footer',
'id' => 'show-count',
'label' => 'show-count-block',
];
$this
->drupalPlaceBlock('facets_summary_block:show_count', $blockConfig);
$this
->drupalGet('search-api-test-fulltext');
$this
->assertSession()
->pageTextContains('5 results found');
$this
->clickLink('apple');
$this
->assertSession()
->pageTextContains('2 results found');
$this
->clickLink('item');
$this
->assertSession()
->pageTextContains('1 result found');
}
protected function checkResetFacetsProcessor() {
FacetsSummary::create([
'id' => 'reset_facets',
'name' => t('Reset facets summary'),
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
'facets' => [
'giraffe' => [
'checked' => 1,
'label' => 'Giraffe',
'separator' => ',',
'weight' => 0,
'show_count' => 0,
],
'llama' => [
'checked' => 1,
'label' => 'Llama',
'separator' => ',',
'weight' => 0,
'show_count' => 0,
],
],
'processor_configs' => [
'reset_facets' => [
'processor_id' => 'reset_facets',
'weights' => [
'build' => -10,
],
'settings' => [
'link_text' => 'Reset facets',
],
],
],
])
->save();
$this
->resetAll();
$blockConfig = [
'label' => 'Reset block',
'region' => 'footer',
'id' => 'reset-facets',
];
$this
->drupalPlaceBlock('facets_summary_block:reset_facets', $blockConfig);
$this
->drupalGet('search-api-test-fulltext');
$this
->assertSession()
->addressEquals('/search-api-test-fulltext');
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$this
->assertSession()
->pageTextNotContains('Reset facets');
$this
->clickLink('apple');
$this
->assertSession()
->pageTextContains('Displaying 2 search results');
$this
->assertSession()
->pageTextContains('Reset facets');
$this
->clickLink('Reset facets');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/search-api-test-fulltext');
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$this
->assertSession()
->pageTextNotContains('Reset facets');
}
public function testEmptyFacetLinks() {
$this
->createFacet('Kepler-442b', 'category', 'category');
$this
->createFacet('Kepler-438b', 'keywords', 'keywords');
$this
->resetAll();
$entity_test_storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
$this->entities[] = $entity_test_storage
->create([
'name' => 'Test with no category',
'body' => 'test test',
'type' => 'item',
'keywords' => [
'rotten orange',
],
])
->save();
$this
->indexItems($this->indexId);
$values = [
'name' => 'Kepler planets',
'id' => 'kepler',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$block = [
'region' => 'footer',
'id' => 'kplanets',
'weight' => -10,
];
$summary_block = $this
->drupalPlaceBlock('facets_summary_block:kepler', $block);
$summaries = [
'facets[category][checked]' => TRUE,
'facets[category][weight]' => 0,
'facets[keywords][checked]' => TRUE,
'facets[keywords][weight]' => 1,
'facets_summary_settings[reset_facets][status]' => 1,
'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset',
];
$this
->drupalPostForm('admin/config/search/facets/facet-summary/kepler/edit', $summaries, 'Save');
$this
->drupalGet('search-api-test-fulltext');
$web_assert = $this
->assertSession();
$web_assert
->pageTextContains('Displaying 6 search results');
$this
->assertFacetBlocksAppear();
$web_assert
->pageTextContains($summary_block
->label());
$this
->clickLink('rotten orange');
$web_assert
->pageTextContains('Test with no category');
}
public function testResetFacetLink() {
$this
->createFacet('Brasserie d\'Orval', 'orval', 'category');
$this
->resetAll();
$values = [
'name' => 'Trappist beers',
'id' => 'trappist',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this
->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$block = [
'region' => 'footer',
'id' => 'trappist',
'weight' => -10,
];
$summary_block = $this
->drupalPlaceBlock('facets_summary_block:trappist', $block);
$summaries = [
'facets[orval][checked]' => TRUE,
'facets[orval][weight]' => 0,
'facets_summary_settings[reset_facets][status]' => 1,
'facets_summary_settings[reset_facets][settings][link_text]' => 'Reset',
];
$this
->drupalPostForm('admin/config/search/facets/facet-summary/trappist/edit', $summaries, 'Save');
$this
->drupalGet('search-api-test-fulltext');
$web_assert = $this
->assertSession();
$web_assert
->pageTextContains('Displaying 5 search results');
$this
->assertFacetBlocksAppear();
$web_assert
->pageTextContains($summary_block
->label());
$links = $this
->xpath('//a[normalize-space(text())=:label]', [
':label' => 'Reset',
]);
$this
->assertEmpty($links);
$this
->clickLink('article_category');
$links = $this
->xpath('//a[normalize-space(text())=:label]', [
':label' => 'Reset',
]);
$this
->assertNotEmpty($links);
}
}