You are here

public function ProcessorIntegrationTest::testBooleanProcessorIntegration in Facets 8

Tests the for processors in the frontend with a 'boolean' facet.

File

tests/src/Functional/ProcessorIntegrationTest.php, line 133

Class

ProcessorIntegrationTest
Tests the processor functionality.

Namespace

Drupal\Tests\facets\Functional

Code

public function testBooleanProcessorIntegration() {
  $field_name = 'field_boolean';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test_mulrev_changed',
    'type' => 'boolean',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'item',
  ]);
  $field
    ->save();
  $index = $this
    ->getIndex();

  // Index a boolean field.
  $boolean_field = new Field($index, $field_name);
  $boolean_field
    ->setType('integer');
  $boolean_field
    ->setPropertyPath($field_name);
  $boolean_field
    ->setDatasourceId('entity:entity_test_mulrev_changed');
  $boolean_field
    ->setLabel('BooleanField');
  $index
    ->addField($boolean_field);
  $index
    ->save();
  $this
    ->indexItems($this->indexId);
  $entity_test_storage = \Drupal::entityTypeManager()
    ->getStorage('entity_test_mulrev_changed');
  $entity_test_storage
    ->create([
    'name' => 'foo bar baz',
    'body' => 'test test',
    'type' => 'item',
    'keywords' => [
      'orange',
    ],
    'category' => 'item_category',
    $field_name => TRUE,
  ])
    ->save();
  $entity_test_storage
    ->create([
    'name' => 'quux quuux',
    'body' => 'test test',
    'type' => 'item',
    'keywords' => [
      'apple',
    ],
    'category' => 'item_category',
    $field_name => FALSE,
  ])
    ->save();
  $this
    ->indexItems($this->indexId);
  $facet_name = "Boolean";
  $facet_id = "boolean";

  // Create facet.
  $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
  $this
    ->createFacet($facet_name, $facet_id, $field_name);

  // Check values.
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertFacetLabel('1');
  $this
    ->assertFacetLabel('0');
  $form = [
    'facet_settings[boolean_item][status]' => TRUE,
    'facet_settings[boolean_item][settings][on_value]' => 'Yes',
    'facet_settings[boolean_item][settings][off_value]' => 'No',
  ];
  $this
    ->drupalPostForm($this->editForm, $form, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->checkboxChecked('edit-facet-settings-boolean-item-status');
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertFacetLabel('Yes');
  $this
    ->assertFacetLabel('No');
  $form = [
    'facet_settings[boolean_item][status]' => TRUE,
    'facet_settings[boolean_item][settings][on_value]' => 'Øn',
  ];
  $this
    ->drupalPostForm($this->editForm, $form, 'Save');
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertFacetLabel('Øn');
  $this
    ->assertEmpty($this
    ->findFacetLink('1'));
  $this
    ->assertEmpty($this
    ->findFacetLink('0'));
  $form = [
    'facet_settings[boolean_item][status]' => TRUE,
    'facet_settings[boolean_item][settings][off_value]' => 'Øff',
  ];
  $this
    ->drupalPostForm($this->editForm, $form, 'Save');
  $this
    ->drupalGet('search-api-test-fulltext');
  $this
    ->assertFacetLabel('Øff');
  $this
    ->assertEmpty($this
    ->findFacetLink('1'));
  $this
    ->assertEmpty($this
    ->findFacetLink('0'));
}