Commit ec8066c3 authored by borisson_'s avatar borisson_ Committed by StryKaizer

Issue #2951860 by borisson_: Refactor FacetsUrlGenerator to allow for better testing

parent 27d088d7
......@@ -32,6 +32,7 @@ services:
class: Drupal\facets\Utility\FacetsUrlGenerator
arguments:
- '@plugin.manager.facets.url_processor'
- '@entity_type.manager'
facets.event_subscriber:
class: Drupal\facets\EventSubscriber
arguments: ['@plugin.manager.block']
......
......@@ -2,7 +2,7 @@
namespace Drupal\facets\Utility;
use Drupal\facets\Entity\Facet;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\Result\Result;
use Drupal\facets\UrlProcessor\UrlProcessorPluginManager;
......@@ -18,14 +18,22 @@ class FacetsUrlGenerator {
*/
protected $urlProcessorPluginManager;
/**
* The entity storage for facets.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $facetStorage;
/**
* Constructs a new instance of the FacetsUrlGenerator.
*
* @param \Drupal\facets\UrlProcessor\UrlProcessorPluginManager $urlProcessorPluginManager
* The url processor plugin manager.
*/
public function __construct(UrlProcessorPluginManager $urlProcessorPluginManager) {
public function __construct(UrlProcessorPluginManager $urlProcessorPluginManager, EntityTypeManagerInterface $entityTypeManager) {
$this->urlProcessorPluginManager = $urlProcessorPluginManager;
$this->facetStorage = $entityTypeManager->getStorage('facets_facet');
}
/**
......@@ -58,15 +66,20 @@ class FacetsUrlGenerator {
// should be from the same facet source, this is fine.
// This is because we don't support generating a url over multiple facet
// sources.
$facet_id = key($active_filters);
$facet = Facet::load($facet_id);
if ($facet === NULL) {
throw new \InvalidArgumentException("The Facet $facet_id could not be loaded.");
if (empty($active_filters)) {
throw new \InvalidArgumentException("The active filters passed in are invalid. They should look like: ['facet_id' => ['value1', 'value2']]");
}
$facet_id = key($active_filters);
if (!is_array($active_filters[$facet_id])) {
throw new \InvalidArgumentException("The active filters passed in are invalid. They should look like: [$facet_id => ['value1', 'value2']]");
}
$facet = $this->facetStorage->load($facet_id);
if ($facet === NULL) {
throw new \InvalidArgumentException("The Facet $facet_id could not be loaded.");
}
// We need one raw value to build a Result. If we have the raw value in the
// already active filters, it will be removed in the final result. So
// instead we copy the value into a variable and unset it from the list.
......
......@@ -8,7 +8,6 @@ use Drupal\facets\Entity\Facet;
* Class FacetsUrlGeneratorTest.
*
* @group facets
* @coversDefaultClass \Drupal\facets\Utility\FacetsUrlGenerator
*/
class FacetsUrlGeneratorTest extends FacetsTestBase {
......@@ -48,7 +47,7 @@ class FacetsUrlGeneratorTest extends FacetsTestBase {
}
/**
* @covers ::getUrl
* Create url.
*/
public function testCreateUrl() {
/** @var \Drupal\facets\FacetInterface $entity */
......@@ -68,34 +67,7 @@ class FacetsUrlGeneratorTest extends FacetsTestBase {
}
/**
* Tests that passing an invalid facet ID throws an InvalidArgumentException.
*
* @covers ::getUrl
*/
public function testInvalidFacet() {
$this->setExpectedException(\InvalidArgumentException::class, 'The Facet imaginary could not be loaded.');
$this->urlGenerator->getUrl(['imaginary' => ['unicorn']]);
}
/**
* Tests that passing an invalid facet ID throws an InvalidArgumentException.
*
* @covers ::getUrl
*/
public function testInvalidArguments() {
$entity = Facet::create([
'id' => 'test_facet',
'name' => 'Test facet',
]);
$entity->setWidget('links');
$entity->setEmptyBehavior(['behavior' => 'none']);
$entity->save();
$this->setExpectedException(\InvalidArgumentException::class, 'The active filters passed in are invalid. They should look like: [test_facet => [\'value1\', \'value2\']]');
$this->urlGenerator->getUrl(['test_facet' => 'unicorn']);
}
/**
* @covers ::getUrl
* Create url with already set facet.
*/
public function testWithAlreadySetFacet() {
$this->drupalPlaceBlock('display_generated_link');
......
<?php
namespace Drupal\Tests\facets\Unit\Utility;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\UrlProcessor\UrlProcessorPluginManager;
use Drupal\facets\Utility\FacetsUrlGenerator;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
/**
* Class FacetsUrlGeneratorTest.
*
* @group facets
* @coversDefaultClass \Drupal\facets\Utility\FacetsUrlGenerator
*/
class FacetsUrlGeneratorTest extends UnitTestCase {
/**
* Tests that passing an invalid facet ID throws an InvalidArgumentException.
*
* @covers ::getUrl
*/
public function testEmptyArray() {
$url_processor_plugin_manager = $this->prophesize(UrlProcessorPluginManager::class)->reveal();
$storage = $this->prophesize(EntityStorageInterface::class);
$etm = $this->prophesize(EntityTypeManagerInterface::class);
$etm->getStorage('facets_facet')->willReturn($storage->reveal());
$url_generator = new FacetsUrlGenerator($url_processor_plugin_manager, $etm->reveal());
$this->setExpectedException(\InvalidArgumentException::class, "The active filters passed in are invalid. They should look like: ['facet_id' => ['value1', 'value2']]");
$url_generator->getUrl([]);
}
/**
* Tests that passing an invalid facet ID throws an InvalidArgumentException.
*
* @covers ::getUrl
*/
public function testInvalidArray() {
$url_processor_plugin_manager = $this->prophesize(UrlProcessorPluginManager::class)->reveal();
$storage = $this->prophesize(EntityStorageInterface::class);
$etm = $this->prophesize(EntityTypeManagerInterface::class);
$etm->getStorage('facets_facet')->willReturn($storage->reveal());
$url_generator = new FacetsUrlGenerator($url_processor_plugin_manager, $etm->reveal());
$this->setExpectedException(\InvalidArgumentException::class, "The active filters passed in are invalid. They should look like: [imaginary => ['value1', 'value2']]");
$url_generator->getUrl(['imaginary' => 'unicorn']);
}
/**
* Tests that passing an invalid facet ID throws an InvalidArgumentException.
*
* @covers ::getUrl
*/
public function testInvalidFacet() {
$url_processor_plugin_manager = $this->prophesize(UrlProcessorPluginManager::class)->reveal();
$storage = $this->prophesize(EntityStorageInterface::class);
$storage->load(Argument::type('string'))->willReturn(NULL);
$etm = $this->prophesize(EntityTypeManagerInterface::class);
$etm->getStorage('facets_facet')->willReturn($storage->reveal());
$url_generator = new FacetsUrlGenerator($url_processor_plugin_manager, $etm->reveal());
$this->setExpectedException(\InvalidArgumentException::class, 'The Facet imaginary could not be loaded.');
$url_generator->getUrl(['imaginary' => ['unicorn']]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment