FacetFacetSourceTest.php 3.96 KB
Newer Older
1
2
3
4
5
<?php

namespace Drupal\Tests\facets\Kernel\Entity;

use Drupal\facets\Entity\Facet;
6
7
8
use Drupal\facets\FacetSourceInterface;
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
9
10
11
12
13
14
15
16
17

/**
 * Class FacetFacetSourceTest.
 *
 * Tests facet source behavior for the facet entity.
 *
 * @group facets
 * @coversDefaultClass \Drupal\facets\Entity\Facet
 */
18
class FacetFacetSourceTest extends EntityKernelTestBase {
19
20
21
22
23
24

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'facets',
25
    'facets_search_api_dependency',
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
    'search_api',
    'search_api_db',
    'search_api_test_db',
    'search_api_test_example_content',
    'search_api_test_views',
    '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());

75
    // Check that the facet source is in the list of search api displays.
76
77
78
    $displays = $this->container
      ->get('plugin.manager.search_api.display')
      ->getDefinitions();
79
80
    $this->assertTrue(isset($displays['views_page:search_api_test_view__page_1']));
    $this->assertArrayHasKey('views_page:search_api_test_view__page_1', $displays);
81

82
83
84
85
86
87
88
89
    // Check that has transformed into a facet source as expected.
    $facet_sources = $this->container
      ->get('plugin.manager.facets.facet_source')
      ->getDefinitions();
    $this->assertArrayHasKey('search_api:views_page__search_api_test_view__page_1', $facet_sources);

    // Check the behavior of the facet sources.
    $display_name = 'search_api:views_page__search_api_test_view__page_1';
90
91
    $entity->setFacetSourceId($display_name);
    $this->assertEquals($display_name, $entity->getFacetSourceId());
92
93
    $this->assertInstanceOf(SearchApiDisplay::class, $entity->getFacetSources()[$display_name]);
    $this->assertInstanceOf(SearchApiDisplay::class, $entity->getFacetSource());
borisson_'s avatar
borisson_ committed
94
    $this->assertInstanceOf(FacetSourceInterface::class, $entity->getFacetSourceConfig());
95
96
97
98
99
100
101
    $this->assertEquals($display_name, $entity->getFacetSourceConfig()->getName());
    $this->assertEquals('f', $entity->getFacetSourceConfig()->getFilterKey());
  }

  /**
   * Tests invalid query type.
   *
102
103
   * The error here is triggered because no field id is set.
   *
104
   * @covers ::getQueryType
105
   * @covers ::getFacetSource
106
107
108
109
   */
  public function testInvalidQueryType() {
    $entity = new Facet([], 'facets_facet');
    $entity->setWidget('links');
110
    $entity->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
111
112
113
114
115

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

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  /**
   * Tests invalid query type.
   *
   * @covers ::getQueryType
   * @covers ::getFacetSource
   */
  public function testQueryType() {
    $entity = new Facet([], 'facets_facet');
    $entity->setWidget('links');
    $entity->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
    $entity->setFieldIdentifier('name');

    $aa = $entity->getQueryType();
    $this->assertEquals('search_api_string', $aa);
  }

132
}