FacetFacetSourceTest.php 3.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace Drupal\Tests\facets\Kernel\Entity;

use Drupal\facets\Entity\Facet;
use Drupal\KernelTests\KernelTestBase;

/**
 * Class FacetFacetSourceTest.
 *
 * Tests facet source behavior for the facet entity.
 *
 * @group facets
 * @coversDefaultClass \Drupal\facets\Entity\Facet
 */
class FacetFacetSourceTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'facets',
    'field',
    'search_api',
    'search_api_db',
    'search_api_test_db',
    'search_api_test_example_content',
    'search_api_test_views',
    'search_api_test',
    'user',
    'system',
    'entity_test',
    'text',
    'views',
    'rest',
    'serialization',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->installEntitySchema('facets_facet');
    $this->installEntitySchema('entity_test_mulrev_changed');
    $this->installEntitySchema('search_api_task');

    \Drupal::state()->set('search_api_use_tracking_batch', FALSE);

    // Set tracking page size so tracking will work properly.
    \Drupal::configFactory()
      ->getEditable('search_api.settings')
      ->set('tracking_page_size', 100)
      ->save();

    $this->installConfig(array(
      'search_api_test_example_content',
      'search_api_test_db',
    ));

    $this->installConfig('search_api_test_views');
  }

  /**
   * Tests facet source behavior for the facet entity.
   *
   * @covers ::getFacetSourceId
   * @covers ::setFacetSourceId
   * @covers ::getFacetSources
   * @covers ::getFacetSource
   * @covers ::getFacetSourceConfig
   */
  public function testFacetSource() {
    $entity = new Facet([], 'facets_facet');
    $this->assertNull($entity->getFacetSourceId());

    $display_name = 'views_page:search_api_test_view__page_1';
    $display_id = 'views_page__search_api_test_view__page_1';
    $displays = $this->container
      ->get('plugin.manager.search_api.display')
      ->getDefinitions();
    $this->assertArrayHasKey($display_name, $displays);

    $entity->setFacetSourceId($display_name);
    $this->assertEquals($display_name, $entity->getFacetSourceId());
    $this->assertInstanceOf('\Drupal\facets\FacetSource\SearchApiFacetSourceInterface', $entity->getFacetSources()[$display_name]);
    $this->assertInstanceOf('\Drupal\facets\FacetSource\SearchApiFacetSourceInterface', $entity->getFacetSource());
    $this->assertInstanceOf('\Drupal\facets\FacetSourceInterface', $entity->getFacetSourceConfig());
    $this->assertEquals($display_name, $entity->getFacetSourceConfig()->getName());
    $this->assertEquals($display_id, $entity->getFacetSourceConfig()->id());
    $this->assertEquals('f', $entity->getFacetSourceConfig()->getFilterKey());

  }

  /**
   * Tests invalid query type.
   *
   * @covers ::getQueryType
   */
  public function testInvalidQueryType() {
    $entity = new Facet([], 'facets_facet');
    $entity->setWidget('links');
    $entity->setFacetSourceId('views_page:search_api_test_view__page_1');

    $this->setExpectedException('Drupal\facets\Exception\InvalidQueryTypeException');
    $entity->getQueryType();
  }

}