You are here

public function IntegrationTest::testFacetDependencies in Facets 8

Tests facet dependencies.

File

tests/src/Functional/IntegrationTest.php, line 223

Class

IntegrationTest
Tests the overall functionality of the Facets admin UI.

Namespace

Drupal\Tests\facets\Functional

Code

public function testFacetDependencies() {
  $facet_name = "DependableFacet";
  $facet_id = 'dependablefacet';
  $depending_facet_name = "DependingFacet";
  $depending_facet_id = "dependingfacet";
  $this
    ->addFacet($facet_name);
  $this
    ->addFacet($depending_facet_name, 'keywords');

  // Create both facets as blocks and add them on the page.
  $this->blocks[$facet_id] = $this
    ->createBlock($facet_id);
  $this->blocks[$depending_facet_id] = $this
    ->createBlock($depending_facet_id);

  // Go to the view and test that both facets are shown. Item and article
  // come from the DependableFacet, orange and grape come from DependingFacet.
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertFacetLabel('grape');
  $this
    ->assertFacetLabel('orange');
  $this
    ->assertFacetLabel('item');
  $this
    ->assertFacetLabel('article');
  $this
    ->assertFacetBlocksAppear();

  // Change the visiblity settings of the DependingFacet.
  $this
    ->drupalGet('admin/config/search/facets/' . $depending_facet_id . '/edit');
  $edit = [
    'facet_settings[dependent_processor][status]' => TRUE,
    'facet_settings[dependent_processor][settings][' . $facet_id . '][enable]' => TRUE,
    'facet_settings[dependent_processor][settings][' . $facet_id . '][condition]' => 'values',
    'facet_settings[dependent_processor][settings][' . $facet_id . '][values]' => 'item',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');

  // Go to the view and test that only the types are shown.
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertSession()
    ->linkNotExists('grape');
  $this
    ->assertSession()
    ->linkNotExists('orange');
  $this
    ->assertFacetLabel('item');
  $this
    ->assertFacetLabel('article');

  // Click on the item, and test that this shows the keywords.
  $this
    ->clickLink('item');
  $this
    ->assertFacetLabel('grape');
  $this
    ->assertFacetLabel('orange');

  // Go back to the view, click on article and test that the keywords are
  // hidden.
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->clickLink('article');
  $this
    ->assertSession()
    ->linkNotExists('grape');
  $this
    ->assertSession()
    ->linkNotExists('orange');

  // Change the visibility settings to negate the previous settings.
  $this
    ->drupalGet('admin/config/search/facets/' . $depending_facet_id . '/edit');
  $edit = [
    'facet_settings[dependent_processor][status]' => TRUE,
    'facet_settings[dependent_processor][settings][' . $facet_id . '][enable]' => TRUE,
    'facet_settings[dependent_processor][settings][' . $facet_id . '][condition]' => 'values',
    'facet_settings[dependent_processor][settings][' . $facet_id . '][values]' => 'item',
    'facet_settings[dependent_processor][settings][' . $facet_id . '][negate]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');

  // Go to the view and test only the type facet is shown.
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertFacetLabel('item');
  $this
    ->assertFacetLabel('article');
  $this
    ->assertFacetLabel('grape');
  $this
    ->assertFacetLabel('orange');

  // Click on the article, and test that this shows the keywords.
  $this
    ->clickLink('article');
  $this
    ->assertFacetLabel('grape');
  $this
    ->assertFacetLabel('orange');

  // Go back to the view, click on item and test that the keywords are
  // hidden.
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->clickLink('item');
  $this
    ->assertSession()
    ->linkNotExists('grape');
  $this
    ->assertSession()
    ->linkNotExists('orange');
}