You are here

protected function CoreViewsIntegrationTest::addFacet in Core Views Facets 8

Tests adding a facet trough the interface.

Parameters

string $facet_name: The name of the facet.

string $source_type: Either exposed or contextual.

string $facet_type: Facet type.

Throws

\Exception

1 call to CoreViewsIntegrationTest::addFacet()
CoreViewsIntegrationTest::testFramework in tests/src/Functional/CoreViewsIntegrationTest.php
Tests various operations via the Facets' admin UI.

File

tests/src/Functional/CoreViewsIntegrationTest.php, line 238

Class

CoreViewsIntegrationTest
Tests the overall functionality of the Facets admin UI.

Namespace

Drupal\Tests\core_views_facets\Functional

Code

protected function addFacet($facet_name, $source_type = 'exposed', $facet_type = 'type') {
  $facet_id = $this
    ->convertNameToMachineName($facet_name);
  switch ($source_type) {
    case 'contextual':
      $facet_source_id = $this->contextualFiltersFacetSourceId;
      break;
    case 'exposed':
    default:
      $facet_source_id = $this->exposedFiltersFacetSourceId;
      break;
  }
  $facet_source_edit_page = Url::fromRoute('entity.facets_facet_source.edit_form', [
    'facets_facet_source' => $facet_source_id,
  ]);
  $this
    ->drupalGet($facet_source_edit_page);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $url_processor_form_values = [
    'url_processor' => 'core_views_url_processor',
  ];
  $this
    ->drupalPostForm($facet_source_edit_page, $url_processor_form_values, 'Save');

  // Go to the Add facet page and make sure that returns a 200.
  $facet_add_page = Url::fromRoute('entity.facets_facet.add_form');
  $this
    ->drupalGet($facet_add_page);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $form_values = [
    'name' => '',
    'id' => $facet_id,
  ];

  // Try filling out the form, but without having filled in a name for the
  // facet to test for form errors.
  $this
    ->drupalPostForm($facet_add_page, $form_values, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Name field is required.');
  $this
    ->assertSession()
    ->pageTextContains('Facet source field is required.');

  // Make sure that when filling out the name, the form error disappears.
  $form_values['name'] = $facet_name;
  $this
    ->drupalPostForm(NULL, $form_values, 'Save');
  $this
    ->assertSession()
    ->pageTextNotContains('Name field is required.');

  // Configure the facet source by selecting the test view.
  $this
    ->drupalGet($facet_add_page);
  $this
    ->drupalPostForm(NULL, [
    'facet_source_id' => $facet_source_id,
  ], 'Configure facet source');

  // The field is still required.
  $this
    ->drupalPostForm(NULL, $form_values, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Facet field field is required.');

  // Fill in all fields and make sure the 'field is required' message is no
  // longer shown.
  $facet_source_form = [
    'facet_source_id' => $facet_source_id,
    'facet_source_configs[' . $facet_source_id . '][field_identifier]' => $facet_type,
  ];
  $this
    ->drupalPostForm(NULL, $form_values + $facet_source_form, 'Save');
  $this
    ->assertSession()
    ->pageTextNotContains('field is required.');

  // Make sure that the redirection to the display page is correct.
  $this
    ->assertSession()
    ->pageTextContains('Facet ' . $facet_name . ' has been created.');
  $url = Url::fromRoute('entity.facets_facet.edit_form', [
    'facets_facet' => $facet_id,
  ]);
  $this
    ->assertSession()
    ->addressEquals($url);
  $this
    ->drupalGet(Url::fromRoute('entity.facets_facet.collection'));
}