class AddURLTest in Search API 8
Tests the "URL field" processor.
@group search_api
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api\Unit\Processor\AddURLTest uses TestItemsTrait
Expanded class hierarchy of AddURLTest
See also
\Drupal\search_api\Plugin\search_api\processor\AddURL
File
- tests/
src/ Unit/ Processor/ AddURLTest.php, line 20
Namespace
Drupal\Tests\search_api\Unit\ProcessorView source
class AddURLTest extends UnitTestCase {
use TestItemsTrait;
/**
* The processor to be tested.
*
* @var \Drupal\search_api\Plugin\search_api\processor\AddURL
*/
protected $processor;
/**
* A search index mock for the tests.
*
* @var \Drupal\search_api\IndexInterface
*/
protected $index;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this
->setUpMockContainer();
// Mock the datasource of the indexer to return the mocked url object.
$datasource = $this
->createMock(DatasourceInterface::class);
$datasource
->expects($this
->any())
->method('getItemUrl')
->withAnyParameters()
->will($this
->returnValue(new TestUrl('/node/example')));
// Create a mock for the index to return the datasource mock.
/** @var \Drupal\search_api\IndexInterface $index */
$index = $this->index = $this
->createMock(IndexInterface::class);
$this->index
->expects($this
->any())
->method('getDatasource')
->with('entity:node')
->will($this
->returnValue($datasource));
// Create the tested processor and set the mocked indexer.
$this->processor = new AddURL([], 'add_url', []);
$this->processor
->setIndex($index);
/** @var \Drupal\Core\StringTranslation\TranslationInterface $translation */
$translation = $this
->getStringTranslationStub();
$this->processor
->setStringTranslation($translation);
}
/**
* Tests whether the "URI" field is correctly filled by the processor.
*/
public function testAddFieldValues() {
/** @var \Drupal\node\Entity\Node $node */
$node = $this
->getMockBuilder('Drupal\\node\\Entity\\Node')
->disableOriginalConstructor()
->getMock();
$body_value = [
'Some text value',
];
$fields = [
'search_api_url' => [
'type' => 'string',
],
'entity:node/body' => [
'type' => 'text',
'values' => $body_value,
],
];
$items = $this
->createItems($this->index, 2, $fields, EntityAdapter::createFromEntity($node));
foreach ($items as $item) {
// Add a second URL field with "Generate absolute URL" enabled.
$field = (clone $item
->getField('url'))
->setFieldIdentifier('url_1')
->setConfiguration([
'absolute' => TRUE,
]);
$item
->setField('url_1', $field);
// Add the processor's field values to the items.
$this->processor
->addFieldValues($item);
}
// Check the generated URLs.
$item_1 = $items[$this->itemIds[0]];
$this
->assertEquals([
'/node/example',
], $item_1
->getField('url')
->getValues());
$this
->assertEquals([
'http://www.example.com/node/example',
], $item_1
->getField('url_1')
->getValues());
// Check that no other fields were changed.
$this
->assertEquals($body_value, $item_1
->getField('body')
->getValues());
// Check the second item to be sure that all are processed.
$item_2 = $items[$this->itemIds[1]];
$this
->assertEquals([
'/node/example',
], $item_2
->getField('url')
->getValues());
$this
->assertEquals([
'http://www.example.com/node/example',
], $item_2
->getField('url_1')
->getValues());
}
/**
* Tests whether the properties are correctly altered.
*
* @see \Drupal\search_api\Plugin\search_api\processor\AddURL::alterPropertyDefinitions()
*/
public function testAlterPropertyDefinitions() {
// Check for added properties when no datasource is given.
$properties = $this->processor
->getPropertyDefinitions(NULL);
$this
->assertArrayHasKey('search_api_url', $properties);
$this
->assertInstanceOf(AddURLProperty::class, $properties['search_api_url'], 'The "search_api_url" property contains a valid data definition.');
$this
->assertEquals('string', $properties['search_api_url']
->getDataType(), 'Correct data type set in the data definition.');
$this
->assertEquals('URI', $properties['search_api_url']
->getLabel(), 'Correct label set in the data definition.');
$this
->assertEquals('A URI where the item can be accessed', $properties['search_api_url']
->getDescription(), 'Correct description set in the data definition.');
// Verify that there are no properties if a datasource is given.
$datasource = $this
->createMock(DatasourceInterface::class);
$properties = $this->processor
->getPropertyDefinitions($datasource);
$this
->assertEmpty($properties, 'Datasource-specific properties did not get changed.');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddURLTest:: |
protected | property | A search index mock for the tests. | |
AddURLTest:: |
protected | property | The processor to be tested. | |
AddURLTest:: |
protected | function |
Creates a new processor object for use in the tests. Overrides UnitTestCase:: |
|
AddURLTest:: |
public | function | Tests whether the "URI" field is correctly filled by the processor. | |
AddURLTest:: |
public | function | Tests whether the properties are correctly altered. | |
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. | |
TestItemsTrait:: |
protected | property | The class container. | |
TestItemsTrait:: |
protected | property | The used item IDs for test items. | |
TestItemsTrait:: |
public | function | Creates a certain number of test items. | |
TestItemsTrait:: |
public | function | Creates an array with a single item which has the given field. | |
TestItemsTrait:: |
protected | function | Adds a container with several mock services commonly needed by our tests. | |
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. |