View source
<?php
namespace Drupal\Tests\search_api\Kernel\Datasource;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\Utility\Utility;
class EntityStringIdTest extends KernelTestBase {
protected $testEntityTypeId = 'entity_test_string_id';
protected $server;
protected $index;
public static $modules = [
'search_api',
'search_api_test',
'language',
'user',
'system',
'entity_test',
];
protected $langcodes;
public function setUp() {
parent::setUp();
$this
->installSchema('search_api', [
'search_api_item',
]);
$this
->installEntitySchema('entity_test_string_id');
$this
->installEntitySchema('search_api_task');
$this
->installConfig('search_api');
if (!Utility::isRunningInCli()) {
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
}
$this->server = Server::create([
'name' => 'Test Server',
'id' => 'test_server',
'status' => 1,
'backend' => 'search_api_test',
]);
$this->server
->save();
$this->index = Index::create([
'name' => 'Test Index',
'id' => 'test_index',
'status' => 1,
'datasource_settings' => [
'entity:' . $this->testEntityTypeId => [],
],
'tracker_settings' => [
'default' => [],
],
'server' => $this->server
->id(),
'options' => [
'index_directly' => FALSE,
],
]);
$this->index
->save();
}
public function testUriStringId($entity_id) {
$entity = EntityTestStringId::create([
'id' => $entity_id,
'name' => 'String Test',
'user_id' => $this->container
->get('current_user')
->id(),
]);
$entity
->save();
$datasource = $this->index
->getDatasource('entity:' . $this->testEntityTypeId);
$datasource_item_ids = $datasource
->getItemIds();
$expected = [
$entity_id . ':und',
];
$this
->assertEquals($expected, $datasource_item_ids, 'Datasource returns correct item ids.');
$this
->assertEquals(0, $this->index
->getTrackerInstance()
->getIndexedItemsCount(), 'The index is empty.');
$this
->assertEquals(1, $this->index
->getTrackerInstance()
->getTotalItemsCount(), 'There is one item to be indexed.');
$this->index
->indexItems();
$this
->assertEquals(1, $this->index
->getTrackerInstance()
->getIndexedItemsCount(), 'One item has been indexed.');
}
public function entityStringIdList() {
return [
'Normal machine name' => [
'short_string_id',
],
'URL ID (with special characters)' => [
'http://drupal.org',
],
'Long ID' => [
str_repeat('a', 100),
],
];
}
}