You are here

class SupportedGroupingFieldsTest in Search API Grouping 8

Test the getSupportedFields method.

@group search_api_grouping

Hierarchy

Expanded class hierarchy of SupportedGroupingFieldsTest

File

tests/src/Unit/SupportedGroupingFieldsTest.php, line 20

Namespace

Drupal\Tests\search_api_grouping\Unit
View source
class SupportedGroupingFieldsTest extends UnitTestCase {
  use StringTranslationTrait;
  use ProcessorTestTrait;

  /**
   * A mock of an index.
   *
   * @var \PHPUnit\Framework\MockObject\MockBuilder
   */
  protected $index;

  /**
   * {@inheritdoc}
   */
  public function setUp($processor = NULL) {
    parent::setUp();
    $this->processor = new Grouping([], 'grouping', []);
    $serviceTranslation = new TranslationManager(new LanguageDefault([
      'en',
    ]));
    $container = new ContainerBuilder();
    \Drupal::setContainer($container);
    $container
      ->set('string_translation', $serviceTranslation);
    $this->index = $this
      ->getMockBuilder(IndexInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Check if the supported fields of an index are correct.
   */
  public function testSupportedSortFields() {
    $field_list = [
      'author' => [
        'type' => 'string',
        'label' => 'Author Name',
      ],
      'type' => [
        'type' => 'integer',
        'label' => 'Content Type',
      ],
      'sticky' => [
        'type' => 'boolean',
        'label' => 'Sticky',
      ],
    ];
    $fields = [];
    foreach ($field_list as $item) {
      $field = $this
        ->getMockBuilder(FieldInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
      $field
        ->method('getType')
        ->willReturn($item['type']);
      $field
        ->method('getLabel')
        ->willReturn($item['label']);
      $fields[] = $field;
    }
    $this->index
      ->method('getFields')
      ->willReturn($fields);
    $this->processor
      ->setIndex($this->index);
    $fields = $this->processor
      ->getSupportedFields();
    $this
      ->assertEquals('Author Name', $fields['field_sorts'][0]);
    $this
      ->assertEquals('Content Type', $fields['field_sorts'][1]);
    $this
      ->assertArrayNotHasKey(2, $fields['field_sorts']);
  }

  /**
   * Test if an integer field has the info message in the label.
   */
  public function testSupportedGroupingFields() {
    $fields = [];
    $field = $this
      ->getMockBuilder(FieldInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $field
      ->method('getType')
      ->willReturn('integer');
    $field
      ->method('getLabel')
      ->willReturn('Content Type');
    $fields[] = $field;
    $this->index
      ->method('getFields')
      ->willReturn($fields);
    $this->processor
      ->setIndex($this->index);
    $fields = $this->processor
      ->getSupportedFields();
    $this
      ->assertEquals('Content Type (Converted to string for indexing)', $fields['field_options'][0]);
    $this
      ->assertArrayNotHasKey(2, $fields['field_sorts']);
  }

  /**
   * Test the return of the grouping fields.
   */
  public function testGetGroupingFields() {
    $config = [
      'grouping_fields' => [
        'type' => 'type',
        'uid' => 'uid',
        'author' => 0,
      ],
    ];
    $this->processor
      ->setConfiguration($config);
    $fields = $this->processor
      ->getGroupingFields();
    $this
      ->assertEquals('type', $fields['type']);
    $this
      ->assertEquals('uid', $fields['uid']);
    $this
      ->assertArrayNotHasKey('author', $fields);
  }

}

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.
ProcessorTestTrait::$processor protected property The tested processor.
ProcessorTestTrait::invokeMethod protected function Invokes a method on the processor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SupportedGroupingFieldsTest::$index protected property A mock of an index.
SupportedGroupingFieldsTest::setUp public function Overrides UnitTestCase::setUp
SupportedGroupingFieldsTest::testGetGroupingFields public function Test the return of the grouping fields.
SupportedGroupingFieldsTest::testSupportedGroupingFields public function Test if an integer field has the info message in the label.
SupportedGroupingFieldsTest::testSupportedSortFields public function Check if the supported fields of an index are correct.
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.