View source
<?php
namespace Drupal\Tests\search_api\Functional;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Utility\Utility;
trait ExampleContentTrait {
protected $entities = [];
protected $ids = [];
protected function setUpExampleStructure() {
entity_test_create_bundle('item', NULL, 'entity_test_mulrev_changed');
entity_test_create_bundle('article', NULL, 'entity_test_mulrev_changed');
}
protected function insertExampleContent() {
$smiley = json_decode('"\\u1F601"');
$this
->addTestEntity(1, [
'name' => 'foo bar baz foobaz föö smile' . $smiley,
'body' => 'test test case Case casE',
'type' => 'item',
'keywords' => [
'Orange',
'orange',
'örange',
'Orange',
$smiley,
],
'category' => 'item_category',
]);
$this
->addTestEntity(2, [
'name' => 'foo test foobuz',
'body' => 'bar test casE',
'type' => 'item',
'keywords' => [
'orange',
'apple',
'grape',
],
'category' => 'item_category',
]);
$this
->addTestEntity(3, [
'name' => 'bar',
'body' => 'test foobar Case',
'type' => 'item',
]);
$this
->addTestEntity(4, [
'name' => 'foo baz',
'body' => 'test test test',
'type' => 'article',
'keywords' => [
'apple',
'strawberry',
'grape',
],
'category' => 'article_category',
'width' => '1.0',
]);
$this
->addTestEntity(5, [
'name' => 'bar baz',
'body' => 'foo',
'type' => 'article',
'keywords' => [
'orange',
'strawberry',
'grape',
'banana',
],
'category' => 'article_category',
'width' => '2.0',
]);
$count = \Drupal::entityQuery('entity_test_mulrev_changed')
->accessCheck(FALSE)
->count()
->execute();
$this
->assertEquals(5, $count, "{$count} items inserted.");
}
protected function addTestEntity($id, array $values) {
$entity_type = 'entity_test_mulrev_changed';
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
$values['id'] = $id;
$this->entities[$id] = $storage
->create($values);
$this->entities[$id]
->save();
$this->ids[$id] = Utility::createCombinedId("entity:{$entity_type}", "{$id}:en");
return $this->entities[$id];
}
protected function removeTestEntity($id) {
$this->entities[$id]
->delete();
unset($this->entities[$id]);
return $this;
}
protected function indexItems($index_id) {
$index = Index::load($index_id);
return $index
->indexItems();
}
protected function getItemIds(array $entity_ids) {
$map = $this->ids;
$translate_ids = function ($entity_id) use ($map) {
return $map[$entity_id];
};
return array_map($translate_ids, $entity_ids);
}
}