View source
<?php
namespace Drupal\Tests\search_api\Unit\Processor;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\search_api\processor\AddURL;
use Drupal\search_api\Plugin\search_api\processor\Property\AddURLProperty;
use Drupal\Tests\search_api\Unit\TestUrl;
use Drupal\Tests\UnitTestCase;
class AddURLTest extends UnitTestCase {
use TestItemsTrait;
protected $processor;
protected $index;
protected function setUp() {
parent::setUp();
$this
->setUpMockContainer();
$datasource = $this
->createMock(DatasourceInterface::class);
$datasource
->expects($this
->any())
->method('getItemUrl')
->withAnyParameters()
->will($this
->returnValue(new TestUrl('/node/example')));
$index = $this->index = $this
->createMock(IndexInterface::class);
$this->index
->expects($this
->any())
->method('getDatasource')
->with('entity:node')
->will($this
->returnValue($datasource));
$this->processor = new AddURL([], 'add_url', []);
$this->processor
->setIndex($index);
$translation = $this
->getStringTranslationStub();
$this->processor
->setStringTranslation($translation);
}
public function testAddFieldValues() {
$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) {
$field = (clone $item
->getField('url'))
->setFieldIdentifier('url_1')
->setConfiguration([
'absolute' => TRUE,
]);
$item
->setField('url_1', $field);
$this->processor
->addFieldValues($item);
}
$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());
$this
->assertEquals($body_value, $item_1
->getField('body')
->getValues());
$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());
}
public function testAlterPropertyDefinitions() {
$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.');
$datasource = $this
->createMock(DatasourceInterface::class);
$properties = $this->processor
->getPropertyDefinitions($datasource);
$this
->assertEmpty($properties, 'Datasource-specific properties did not get changed.');
}
}