DefaultFacetManagerTest.php in Facets 8
File
tests/src/Kernel/FacetManager/DefaultFacetManagerTest.php
View source
<?php
namespace Drupal\Tests\facets\Kernel\FacetManager;
use Drupal\facets\Entity\Facet;
use Drupal\KernelTests\KernelTestBase;
class DefaultFacetManagerTest extends KernelTestBase {
public static $modules = [
'facets',
'search_api',
'system',
'user',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('facets_facet');
}
public function testGetEnabledFacets() {
$dfm = \Drupal::service('facets.manager');
$returnValue = $dfm
->getEnabledFacets();
$this
->assertEmpty($returnValue);
$entity = $this
->createAndSaveFacet('test_facet');
$returnValue = $dfm
->getEnabledFacets();
$this
->assertNotEmpty($returnValue);
$this
->assertSame($entity
->id(), $returnValue['test_facet']
->id());
}
public function testGetFacetsByFacetSourceId() {
$dfm = \Drupal::service('facets.manager');
$this
->assertEmpty($dfm
->getFacetsByFacetSourceId('planets'));
$entity = $this
->createAndSaveFacet('Jupiter');
$entity
->setFacetSourceId('planets');
$entity
->save();
$entity = $this
->createAndSaveFacet('Pluto');
$entity
->setFacetSourceId('former_planets');
$entity
->save();
$planetFacets = $dfm
->getFacetsByFacetSourceId('planets');
$this
->assertNotEmpty($planetFacets);
$this
->assertCount(1, $planetFacets);
$this
->assertSame('Jupiter', $planetFacets[0]
->id());
$formerPlanetFacets = $dfm
->getFacetsByFacetSourceId('former_planets');
$this
->assertNotEmpty($formerPlanetFacets);
$this
->assertCount(1, $formerPlanetFacets);
$this
->assertSame('Pluto', $formerPlanetFacets[0]
->id());
$entity
->setFacetSourceId('planets');
$entity
->save();
$planetFacets = $dfm
->getFacetsByFacetSourceId('planets');
$this
->assertNotEmpty($planetFacets);
$this
->assertCount(1, $planetFacets);
$facetsProperty = new \ReflectionProperty($dfm, 'facets');
$facetsProperty
->setAccessible(TRUE);
$facetsProperty
->setValue($dfm, []);
$planetFacets = $dfm
->getFacetsByFacetSourceId('planets');
$this
->assertNotEmpty($planetFacets);
$this
->assertCount(2, $planetFacets);
$this
->assertSame('Jupiter', $planetFacets[0]
->id());
$this
->assertSame('Pluto', $planetFacets[1]
->id());
}
protected function createAndSaveFacet($id) {
$entity = Facet::create([
'id' => $id,
'name' => 'Test facet',
]);
$entity
->setWidget('links');
$entity
->setEmptyBehavior([
'behavior' => 'none',
]);
$entity
->setFacetSourceId('fluffy');
$entity
->save();
return $entity;
}
}