View source
<?php
namespace Drupal\Tests\search_api_page\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\Tests\search_api\Functional\ExampleContentTrait;
abstract class FunctionalTestBase extends BrowserTestBase {
protected $strictConfigSchema = FALSE;
use StringTranslationTrait;
use ExampleContentTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'search_api_page',
'node',
'search_api',
'search_api_db',
'block',
'field_ui',
];
protected $adminUser;
protected $unauthorizedUser;
protected $anonymousUser;
protected $server = NULL;
protected $index = NULL;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer search_api',
'administer search_api_page',
'access administration pages',
'administer nodes',
'access content overview',
'administer content types',
'administer blocks',
'view search api pages',
'administer node display',
]);
$this->unauthorizedUser = $this
->drupalCreateUser();
$this->anonymousUser = $this
->drupalCreateUser([
'view search api pages',
]);
$this
->drupalCreateContentType([
'type' => 'article',
]);
for ($i = 1; $i < 50; $i++) {
$body = "Body number{$i} \n Value of the body: ";
if ($i % 3 === 0) {
$body .= "\n lorem ipsum";
}
if ($i % 5 === 0) {
$body .= "\n Owls are birds from the order Strigiformes, which includes about 200 species of mostly solitary and nocturnal birds of prey.";
}
if ($i % 10 === 0) {
$body .= "\n Owls are typified by an upright stance, a large, broad head, binocular vision, binaural hearing, sharp talons, and feathers adapted for silent flight. Exceptions include the diurnal northern hawk-owl and the gregarious burrowing owl.";
}
$this
->drupalCreateNode([
'title' => 'Item number' . $i,
'type' => 'article',
'body' => [
[
'value' => $body,
],
],
]);
}
}
protected function setupSearchApi() {
$this->server = Server::create([
'name' => 'Server',
'id' => 'server_1',
'backend' => 'search_api_db',
'backend_config' => [
'database' => 'default:default',
],
]);
$this->server
->save();
$this->index = Index::create([
'id' => 'Index',
'name' => 'index_1',
'description' => 'Description for the index.',
'server' => $this->server
->id(),
'datasource_settings' => [
'entity:node' => [
'plugin_id' => 'entity:node',
'settings' => [],
],
],
'field_settings' => [
'rendered_item' => [
'label' => 'Rendered HTML output',
'property_path' => 'rendered_item',
'type' => 'text',
'configuration' => [
'roles' => [
'anonymous' => 'anonymous',
],
'view_mode' => [
'entity:node' => [
'article' => 'default',
'page' => '',
'blog' => 'default',
'document' => 'teaser',
],
],
],
],
],
]);
$this->index
->save();
$task_manager = \Drupal::getContainer()
->get('search_api.index_task_manager');
$task_manager
->addItemsAll(Index::load($this->index
->id()));
$this
->indexItems($this->index
->id());
}
}