class SupportedGroupingFieldsTest in Search API Grouping 8
Test the getSupportedFields method.
@group search_api_grouping
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api_grouping\Unit\SupportedGroupingFieldsTest uses StringTranslationTrait, ProcessorTestTrait
Expanded class hierarchy of SupportedGroupingFieldsTest
File
- tests/
src/ Unit/ SupportedGroupingFieldsTest.php, line 20
Namespace
Drupal\Tests\search_api_grouping\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ProcessorTestTrait:: |
protected | property | The tested processor. | |
ProcessorTestTrait:: |
protected | function | Invokes a method on the processor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
SupportedGroupingFieldsTest:: |
protected | property | A mock of an index. | |
SupportedGroupingFieldsTest:: |
public | function |
Overrides UnitTestCase:: |
|
SupportedGroupingFieldsTest:: |
public | function | Test the return of the grouping fields. | |
SupportedGroupingFieldsTest:: |
public | function | Test if an integer field has the info message in the label. | |
SupportedGroupingFieldsTest:: |
public | function | Check if the supported fields of an index are correct. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |