You are here

public function DefaultFacetManagerTest::testGetFacetsByFacetSourceId in Facets 8

Tests the getFacetsByFacetSourceId method.

@covers ::getFacetsByFacetSourceId

File

tests/src/Kernel/FacetManager/DefaultFacetManagerTest.php, line 58

Class

DefaultFacetManagerTest
Class DefaultFacetManagerTest.

Namespace

Drupal\Tests\facets\Kernel\FacetManager

Code

public function testGetFacetsByFacetSourceId() {

  /** @var \Drupal\facets\FacetManager\DefaultFacetManager $dfm */
  $dfm = \Drupal::service('facets.manager');
  $this
    ->assertEmpty($dfm
    ->getFacetsByFacetSourceId('planets'));

  // Create 2 different facets with a unique facet source id.
  $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());

  // Make pluto a planet again.
  $entity
    ->setFacetSourceId('planets');
  $entity
    ->save();

  // Test that we now hit the static cache.
  $planetFacets = $dfm
    ->getFacetsByFacetSourceId('planets');
  $this
    ->assertNotEmpty($planetFacets);
  $this
    ->assertCount(1, $planetFacets);

  // Change the 'facets' property on the manager to public, so we can
  // overwrite it here. This is because otherwise we run into the static
  // caches.
  $facetsProperty = new \ReflectionProperty($dfm, 'facets');
  $facetsProperty
    ->setAccessible(TRUE);
  $facetsProperty
    ->setValue($dfm, []);

  // Now that the static cache is reset, test that we have 2 planets.
  $planetFacets = $dfm
    ->getFacetsByFacetSourceId('planets');
  $this
    ->assertNotEmpty($planetFacets);
  $this
    ->assertCount(2, $planetFacets);
  $this
    ->assertSame('Jupiter', $planetFacets[0]
    ->id());
  $this
    ->assertSame('Pluto', $planetFacets[1]
    ->id());
}