You are here

public function RestIntegrationTest::testRestResults in Facets 8

Tests that the facet results are correct.

File

modules/facets_rest/tests/src/Functional/RestIntegrationTest.php, line 69

Class

RestIntegrationTest
Tests the integration of REST-views and facets.

Namespace

Drupal\Tests\facets_rest\Functional

Code

public function testRestResults() {
  global $base_url;
  $get_options = [
    'query' => [
      '_format' => 'json',
    ],
  ];
  $result = $this
    ->drupalGet('/facets-rest', $get_options);
  $this
    ->assertSession()
    ->responseHeaderEquals('content-type', 'application/json');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $json_decoded = json_decode($result, TRUE);
  $this
    ->assertArrayHasKey('search_results', $json_decoded);
  $this
    ->assertArrayHasKey('facets', $json_decoded);
  $this
    ->assertEmpty($json_decoded['facets']);

  // Add a new facet to filter by content type.
  $this
    ->createFacet('Type', 'type', 'type', 'rest_export_1', 'views_rest__search_api_rest_test_view');

  // Use the array widget.
  $facet_edit_page = '/admin/config/search/facets/type/edit';
  $this
    ->drupalGet($facet_edit_page);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [
    'widget' => 'array',
  ], 'Configure widget');
  $values['widget'] = 'array';
  $values['widget_config[show_numbers]'] = TRUE;
  $values['facet_sorting[count_widget_order][status]'] = TRUE;
  $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
  $values['facet_sorting[display_value_widget_order][status]'] = FALSE;
  $values['facet_sorting[active_widget_order][status]'] = FALSE;
  $values['facet_settings[query_operator]'] = 'or';
  $values['facet_settings[only_visible_when_facet_source_is_visible]'] = TRUE;
  $this
    ->drupalPostForm(NULL, $values, 'Save');

  // Add a new facet to filter by keywords.
  $this
    ->createFacet('Keywords', 'keywords', 'keywords', 'rest_export_1', 'views_rest__search_api_rest_test_view');

  // Use the array widget.
  $facet_edit_page = '/admin/config/search/facets/keywords/edit';
  $this
    ->drupalGet($facet_edit_page);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [
    'widget' => 'array',
  ], 'Configure widget');
  $values['widget'] = 'array';
  $values['widget_config[show_numbers]'] = TRUE;
  $values['facet_sorting[count_widget_order][status]'] = TRUE;
  $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
  $values['facet_sorting[display_value_widget_order][status]'] = FALSE;
  $values['facet_sorting[active_widget_order][status]'] = FALSE;
  $values['facet_settings[query_operator]'] = 'or';
  $values['facet_settings[only_visible_when_facet_source_is_visible]'] = TRUE;
  $this
    ->drupalPostForm(NULL, $values, 'Save');

  // Get the output from the rest view and decode it into an array.
  $result = $this
    ->drupalGet('/facets-rest', $get_options);
  $this
    ->assertSession()
    ->responseHeaderEquals('content-type', 'application/json');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $json_decoded = json_decode($result);
  $this
    ->assertEquals(5, count($json_decoded->search_results));

  // Verify the facet "Type".
  $results = [
    'article' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=type%3Aarticle',
      ],
      'count' => 2,
    ],
    'item' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=type%3Aitem',
      ],
      'count' => 3,
    ],
  ];
  foreach ($json_decoded->facets[1][0]->type as $result) {
    $value = $result->values->value;
    $this
      ->assertEquals($result->values->count, $results[$value]['count']);
    foreach ($results[$value]['url'] as $url_part) {
      $this
        ->assertNotFalse(strpos($result->url, $url_part));
    }
  }

  // Verify the facet "Keywords".
  $results = [
    'banana' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Abanana',
      ],
      'count' => 1,
    ],
    'strawberry' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Astrawberry',
      ],
      'count' => 2,
    ],
    'apple' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Aapple',
      ],
      'count' => 2,
    ],
    'orange' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Aorange',
      ],
      'count' => 3,
    ],
    'grape' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Agrape',
      ],
      'count' => 3,
    ],
  ];
  foreach ($json_decoded->facets[0][0]->keywords as $result) {
    $value = $result->values->value;
    $this
      ->assertEquals($result->values->count, $results[$value]['count']);
    foreach ($results[$value]['url'] as $url_part) {
      $this
        ->assertNotFalse(strpos($result->url, $url_part));
    }
  }

  // Filter and verify that the results are correct.
  $json = $this
    ->drupalGet($base_url . '/facets-rest?f%5B0%5D=type%3Aitem', $get_options);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseHeaderEquals('content-type', 'application/json');
  $json_decoded = json_decode($json);
  $this
    ->assertEquals(3, count($json_decoded->search_results));
  $results = [
    'article' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=type%3Aarticle&f%5B1%5D=type%3Aitem',
      ],
      'count' => 2,
    ],
    'item' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
      ],
      'count' => 3,
    ],
    'banana' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Abanana&f%5B1%5D=type%3Aitem',
      ],
      'count' => 0,
    ],
    'strawberry' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Astrawberry&f%5B1%5D=type%3Aitem',
      ],
      'count' => 0,
    ],
    'apple' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Aapple&f%5B1%5D=type%3Aitem',
      ],
      'count' => 1,
    ],
    'orange' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Aorange&f%5B1%5D=type%3Aitem',
      ],
      'count' => 2,
    ],
    'grape' => [
      'url' => [
        $base_url,
        '/facets-rest',
        '_format=json',
        'f%5B0%5D=keywords%3Agrape&f%5B1%5D=type%3Aitem',
      ],
      'count' => 1,
    ],
  ];
  foreach ($json_decoded->facets[1][0]->type as $result) {
    $value = $result->values->value;
    $this
      ->assertEquals($results[$value]['count'], $result->values->count);
    foreach ($results[$value]['url'] as $url_part) {
      $this
        ->assertContains($url_part, $result->url);
    }
  }
  foreach ($json_decoded->facets[0][0]->keywords as $result) {
    $value = $result->values->value;
    $this
      ->assertEquals($results[$value]['count'], $result->values->count);
    foreach ($results[$value]['url'] as $url_part) {
      $this
        ->assertContains($url_part, $result->url);
    }
  }
}