You are here

class SearchApiSortsManagerTest in Search API sorts 8

Tests the sorts manager.

@group search_api_sorts

@coversDefaultClass \Drupal\search_api_sorts\SearchApiSortsManager

Hierarchy

Expanded class hierarchy of SearchApiSortsManagerTest

File

tests/src/Unit/SearchApiSortsManagerTest.php, line 25

Namespace

Drupal\Tests\search_api_sorts\Unit
View source
class SearchApiSortsManagerTest extends UnitTestCase {

  /**
   * ModuleHandler object.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManagerProphecy;

  /**
   * A display object.
   *
   * @var \Drupal\search_api\Display\DisplayInterface
   */
  private $display;

  /**
   * A request object to use for each test case.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  private $request;

  /**
   * A request stack object to store requests.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * The search API Display manager.
   *
   * @var \Drupal\search_api\Display\DisplayPluginManagerInterface
   */
  private $searchApiDisplayManager;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->moduleHandler = $this
      ->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $this->display = $this
      ->prophesize(DisplayInterface::class)
      ->reveal();
    $this->requestStack = new RequestStack();

    // Only set up the prophecy for now,
    // it will need to be configured differently for each test function.
    $this->entityTypeManagerProphecy = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->request = new Request();
    $this->searchApiDisplayManager = $this
      ->prophesize(DisplayPluginManagerInterface::class)
      ->reveal();
  }

  /**
   * Tests getActiveSort.
   *
   * @dataProvider provideSortOrders
   *
   * @covers ::getActiveSort
   */
  public function testGetActiveSort($order_argument, $expected) {
    $this->request->query = new ParameterBag([
      'sort' => 'sort_field',
      'order' => $order_argument,
    ]);
    $this->requestStack
      ->push($this->request);
    $manager = $this->entityTypeManagerProphecy
      ->reveal();
    $searchApiSortsManager = new SearchApiSortsManager($this->requestStack, $manager, $this->moduleHandler, $this->searchApiDisplayManager);
    $sorts = $searchApiSortsManager
      ->getActiveSort($this->display);
    $this
      ->assertEquals('sort_field', $sorts
      ->getFieldName());
    $this
      ->assertEquals($expected, $sorts
      ->getOrder());
  }

  /**
   * Tests getEnabledSorts.
   *
   * @covers ::getEnabledSorts
   */
  public function testGetEnabledSorts() {
    $this->requestStack
      ->push($this->request);
    $sortsField = new SearchApiSortsField([
      'id' => $this
        ->randomMachineName(),
    ], 'search_api_sorts_field');
    $storage = $this
      ->getMockBuilder(EntityStorageInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $storage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->willReturn($sortsField);
    try {
      $this->entityTypeManagerProphecy
        ->getStorage('search_api_sorts_field')
        ->willReturn($storage);
    } catch (InvalidPluginDefinitionException $e) {
      $this
        ->fail("search_api_sorts storage not found.");
    }
    $manager = $this->entityTypeManagerProphecy
      ->reveal();
    $searchApiSortsManager = new SearchApiSortsManager($this->requestStack, $manager, $this->moduleHandler, $this->searchApiDisplayManager);
    $enabledSorts = $searchApiSortsManager
      ->getEnabledSorts($this->display);
    $this
      ->assertEquals($sortsField, $enabledSorts);
  }

  /**
   * Provides mock data and expected results for ::testActiveSortOrder.
   *
   * @return array
   *   An array of mockable data.
   */
  public function provideSortOrders() {
    return [
      [
        'asc',
        'asc',
      ],
      [
        'desc',
        'desc',
      ],
      [
        'aaa',
        'asc',
      ],
      [
        NULL,
        'asc',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SearchApiSortsManagerTest::$display private property A display object.
SearchApiSortsManagerTest::$entityTypeManagerProphecy private property Entity type manager.
SearchApiSortsManagerTest::$moduleHandler private property ModuleHandler object.
SearchApiSortsManagerTest::$request private property A request object to use for each test case.
SearchApiSortsManagerTest::$requestStack private property A request stack object to store requests.
SearchApiSortsManagerTest::$searchApiDisplayManager private property The search API Display manager.
SearchApiSortsManagerTest::provideSortOrders public function Provides mock data and expected results for ::testActiveSortOrder.
SearchApiSortsManagerTest::setUp public function Overrides UnitTestCase::setUp
SearchApiSortsManagerTest::testGetActiveSort public function Tests getActiveSort.
SearchApiSortsManagerTest::testGetEnabledSorts public function Tests getEnabledSorts.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.