View source
<?php
namespace Drupal\Tests\search_api\Functional;
use Drupal\block\Entity\Block;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
use Drupal\entity_test\Entity\EntityTestMulRevChanged;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Utility\Utility;
use Drupal\views\Entity\View;
class ViewsTest extends SearchApiBrowserTestBase {
use ExampleContentTrait;
public static $modules = [
'block',
'language',
'search_api_test_views',
'views_ui',
];
protected static $additionalBundles = TRUE;
public function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('nl')
->save();
\Drupal::getContainer()
->get('search_api.index_task_manager')
->addItemsAll(Index::load($this->indexId));
$this
->insertExampleContent();
$this
->indexItems($this->indexId);
if (!Utility::isRunningInCli()) {
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
}
}
public function testSearchView() {
$this
->checkResults([], array_keys($this->entities), 'Unfiltered search');
$this
->checkResults([
'search_api_fulltext' => 'foobar',
], [
3,
], 'Search for a single word');
$this
->checkResults([
'search_api_fulltext' => 'foo test',
], [
1,
2,
4,
], 'Search for multiple words');
$query = [
'search_api_fulltext' => 'foo test',
'search_api_fulltext_op' => 'or',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'OR search for multiple words');
$query = [
'search_api_fulltext' => 'foobar',
'search_api_fulltext_op' => 'not',
];
$this
->checkResults($query, [
1,
2,
4,
5,
], 'Negated search');
$query = [
'search_api_fulltext' => 'foo test',
'search_api_fulltext_op' => 'not',
];
$this
->checkResults($query, [], 'Negated search for multiple words');
$query = [
'search_api_fulltext' => 'fo',
];
$label = 'Search for short word';
$this
->checkResults($query, [], $label);
$this
->assertSession()
->pageTextContains('You must include at least one positive keyword with 3 characters or more');
$query = [
'search_api_fulltext' => 'foo to test',
];
$label = 'Fulltext search including short word';
$this
->checkResults($query, [
1,
2,
4,
], $label);
$this
->assertSession()
->pageTextNotContains('You must include at least one positive keyword with 3 characters or more');
$this
->checkResults([
'id[value]' => 2,
], [
2,
], 'Search with ID filter');
$query = [
'id[min]' => 2,
'id[max]' => 4,
'id_op' => 'between',
];
$this
->checkResults($query, [
2,
3,
4,
], 'Search with ID "in between" filter');
$query = [
'id[min]' => 2,
'id[max]' => 4,
'id_op' => 'not between',
];
$this
->checkResults($query, [
1,
5,
], 'Search with ID "not in between" filter');
$query = [
'id[value]' => 2,
'id_op' => '>',
];
$this
->checkResults($query, [
3,
4,
5,
], 'Search with ID "greater than" filter');
$query = [
'id[value]' => 2,
'id_op' => '!=',
];
$this
->checkResults($query, [
1,
3,
4,
5,
], 'Search with ID "not equal" filter');
$query = [
'id_op' => 'empty',
];
$this
->checkResults($query, [], 'Search with ID "empty" filter');
$query = [
'id_op' => 'not empty',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with ID "not empty" filter');
$yesterday = strtotime('-1DAY');
$query = [
'created[value]' => date('Y-m-d', $yesterday),
'created_op' => '>',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with "Created after" filter');
$query = [
'created[value]' => date('Y-m-d', $yesterday),
'created_op' => '<',
];
$this
->checkResults($query, [], 'Search with "Created before" filter');
$query = [
'created_op' => 'empty',
];
$this
->checkResults($query, [], 'Search with "empty creation date" filter');
$query = [
'created_op' => 'not empty',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with "not empty creation date" filter');
$this
->checkResults([
'keywords[value]' => 'apple',
], [
2,
4,
], 'Search with Keywords filter');
$query = [
'keywords[min]' => 'aardvark',
'keywords[max]' => 'calypso',
'keywords_op' => 'between',
];
$this
->checkResults($query, [
2,
4,
5,
], 'Search with Keywords "in between" filter');
$query = [
'id[value]' => 1,
'id_op' => '!=',
'keywords[value]' => 'melon',
'keywords_op' => '>=',
];
$this
->checkResults($query, [
2,
4,
5,
], 'Search with Keywords "greater than or equal" filter');
$query = [
'id[value]' => 1,
'id_op' => '!=',
'keywords[value]' => 'banana',
'keywords_op' => '<',
];
$this
->checkResults($query, [
2,
4,
], 'Search with Keywords "less than" filter');
$query = [
'keywords[value]' => 'orange',
'keywords_op' => '!=',
];
$this
->checkResults($query, [
3,
4,
], 'Search with Keywords "not equal" filter');
$query = [
'keywords_op' => 'empty',
];
$label = 'Search with Keywords "empty" filter';
$this
->checkResults($query, [
3,
], $label, 'all/all/all');
$query = [
'keywords_op' => 'not empty',
];
$this
->checkResults($query, [
1,
2,
4,
5,
], 'Search with Keywords "not empty" filter');
$query = [
'name[value]' => 'foo',
];
$this
->checkResults($query, [
1,
2,
4,
], 'Search with Name "contains" filter');
$query = [
'name[value]' => 'foo',
'name_op' => '!=',
];
$this
->checkResults($query, [
3,
5,
], 'Search with Name "doesn\'t contain" filter');
$query = [
'name_op' => 'empty',
];
$this
->checkResults($query, [], 'Search with Name "empty" filter');
$query = [
'name_op' => 'not empty',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with Name "not empty" filter');
$query = [
'language' => [
'***LANGUAGE_site_default***',
],
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with "Page content language" filter');
$query = [
'language' => [
'en',
],
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with "English" language filter');
$query = [
'language' => [
Language::LANGCODE_NOT_SPECIFIED,
],
];
$this
->checkResults($query, [], 'Search with "Not specified" language filter');
$query = [
'language' => [
'***LANGUAGE_language_interface***',
'zxx',
],
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with multiple languages filter');
$query = [
'search_api_fulltext' => 'foo to test',
'id[value]' => 2,
'id_op' => '>',
'keywords_op' => 'not empty',
];
$this
->checkResults($query, [
4,
], 'Search with multiple filters');
$this
->checkResults([], [
4,
5,
], 'Search with arguments', 'entity:entity_test_mulrev_changed/item/grape');
$this
->checkResults([], [
2,
4,
5,
], 'Search with arguments', 'all/item+article/strawberry+apple');
$this
->checkResults([], [
4,
], 'Search with OR arguments', 'all/item,article/strawberry,apple');
$this
->checkResults([], [], 'Search with unknown datasource argument', 'entity:foobar/all/all');
$query = [
'id[value]' => 1,
'id_op' => '!=',
'keywords[value]' => 'melon',
'keywords_op' => '>=',
];
$this
->checkResults($query, [
2,
5,
], 'Search with arguments and filters', 'entity:entity_test_mulrev_changed/all/orange');
$index = Index::load($this->indexId);
$datasource = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createDatasourcePlugin($index, 'entity:user');
$index
->addDatasource($datasource);
$index
->save();
$query = [
'datasource' => [
'entity:user',
'entity:entity_test_mulrev_changed',
],
'datasource_op' => 'or',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search with multiple datasource filters (OR)');
$query = [
'datasource' => [
'entity:user',
'entity:entity_test_mulrev_changed',
],
'datasource_op' => 'and',
];
$this
->checkResults($query, [], 'Search with multiple datasource filters (AND)');
$query = [
'datasource' => [
'entity:user',
],
'datasource_op' => 'not',
];
$this
->checkResults($query, [
1,
2,
3,
4,
5,
], 'Search for non-user results');
$query = [
'datasource' => [
'entity:entity_test_mulrev_changed',
],
'datasource_op' => 'not',
];
$this
->checkResults($query, [], 'Search for non-test entity results');
$query = [
'datasource' => [
'entity:user',
'entity:entity_test_mulrev_changed',
],
'datasource_op' => 'not',
];
$this
->checkResults($query, [], 'Search for results of no available datasource');
$this
->regressionTests();
$this
->checkExposedSearchFields();
$displays = \Drupal::getContainer()
->get('plugin.manager.search_api.display')
->getInstances();
$display_id = 'views_page:search_api_test_view__page_1';
$this
->assertArrayHasKey($display_id, $displays, 'A display plugin was created for the test view page display.');
$this
->assertArrayHasKey('views_block:search_api_test_view__block_1', $displays, 'A display plugin was created for the test view block display.');
$this
->assertArrayHasKey('views_rest:search_api_test_view__rest_export_1', $displays, 'A display plugin was created for the test view block display.');
$this
->assertEquals('/search-api-test', $displays[$display_id]
->getPath(), 'Display returns the correct path.');
$view_url = Url::fromUserInput('/search-api-test')
->toString();
$display_url = Url::fromUserInput($displays[$display_id]
->getPath())
->toString();
$this
->assertEquals($view_url, $display_url, 'Display returns the correct URL.');
$this
->assertNull($displays['views_block:search_api_test_view__block_1']
->getPath(), 'Block display returns the correct path.');
$this
->assertEquals('/search-api-rest-test', $displays['views_rest:search_api_test_view__rest_export_1']
->getPath(), 'REST display returns the correct path.');
$this
->assertEquals('database_search_index', $displays[$display_id]
->getIndex()
->id(), 'Display returns the correct search index.');
$admin_user = $this
->drupalCreateUser([
'administer search_api',
'access administration pages',
'administer views',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/views/view/search_api_test_view/edit/page_1');
$this
->submitForm([], 'Delete Page');
$this
->submitForm([], 'Save');
drupal_flush_all_caches();
$displays = \Drupal::getContainer()
->get('plugin.manager.search_api.display')
->getInstances();
$this
->assertArrayNotHasKey('views_page:search_api_test_view__page_1', $displays, 'No display plugin was created for the test view page display.');
$this
->assertArrayHasKey('views_block:search_api_test_view__block_1', $displays, 'A display plugin was created for the test view block display.');
$this
->assertArrayHasKey('views_rest:search_api_test_view__rest_export_1', $displays, 'A display plugin was created for the test view block display.');
}
public function testViewWithOperations() {
$this
->drupalGet('search-api-test-operations/', [
'query' => [],
]);
$this
->assertSession()
->linkByHrefExists('/entity_test_mulrev_changed/manage/1/edit');
$this
->assertSession()
->linkByHrefExists('/entity_test/delete/entity_test_mulrev_changed/1');
$this
->assertSession()
->linkByHrefExists('/entity_test_mulrev_changed/manage/5/edit');
$this
->assertSession()
->linkByHrefExists('/entity_test/delete/entity_test_mulrev_changed/5');
$this
->assertSession()
->linkByHrefNotExists('/entity_test_mulrev_changed/manage/2/edit');
$this
->assertSession()
->linkByHrefNotExists('/entity_test/delete/entity_test_mulrev_changed/2');
}
protected function regressionTests() {
$this
->regressionTest3187134();
$this
->regressionTest2869121();
$this
->regressionTest3031991();
$this
->regressionTest3136277();
}
protected function regressionTest2869121() {
$view = View::load('search_api_test_view');
$displays = $view
->get('display');
$displays['default']['display_options']['filters']['search_api_fulltext']['expose']['required'] = TRUE;
$displays['default']['display_options']['cache']['type'] = 'search_api_time';
$view
->set('display', $displays);
$view
->save();
$this
->checkResults([], [], 'Search without required fulltext keywords');
$this
->assertSession()
->responseNotContains('Error message');
$this
->checkResults([
'search_api_fulltext' => 'foo test',
], [
1,
2,
4,
], 'Search for multiple words');
$this
->assertSession()
->responseNotContains('Error message');
$this
->checkResults([
'search_api_fulltext' => 'fo',
], [], 'Search for short word');
$this
->assertSession()
->pageTextContains('You must include at least one positive keyword with 3 characters or more');
$view = View::load('search_api_test_view');
$displays = $view
->get('display');
$displays['page_1']['display_options']['exposed_block'] = TRUE;
$view
->set('display', $displays);
$view
->save();
Block::create([
'id' => 'search_api_test_view',
'theme' => $this->defaultTheme,
'weight' => -20,
'plugin' => 'views_exposed_filter_block:search_api_test_view-page_1',
'region' => 'content',
])
->save();
$this
->drupalGet('');
for ($i = 0; $i < 3; ++$i) {
\Drupal::getContainer()
->get('cache.page')
->deleteAll();
\Drupal::getContainer()
->get('cache.dynamic_page_cache')
->deleteAll();
$this
->submitForm([], 'Search');
$this
->assertSession()
->addressMatches('#^/search-api-test#');
$this
->assertSession()
->responseNotContains('Error message');
$this
->assertSession()
->pageTextNotContains('search results');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
}
}
protected function regressionTest3031991() {
$query = [
'search_api_fulltext' => 'foo blabla',
'search_api_fulltext_op' => 'or',
'search_api_fulltext_2' => 'bar',
'search_api_fulltext_2_op' => 'not',
];
$this
->checkResults($query, [
4,
], 'Search with multiple fulltext filters');
}
protected function regressionTest3136277() {
$block = $this
->drupalPlaceBlock('views_block:search_api_test_block_view-block_1', [
'region' => 'content',
]);
$index = Index::load($this->indexId);
$processor = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createProcessorPlugin($index, 'ignorecase');
$index
->addProcessor($processor)
->save();
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('Search API Test Block View: Found 4 items');
$index
->removeProcessor('ignorecase')
->save();
$block
->delete();
}
protected function regressionTest3187134() {
$modules = [
'datetime',
'datetime_range',
];
$success = $this->container
->get('module_installer')
->install($modules, TRUE);
$this
->assertTrue($success, new FormattableMarkup('Enabled modules: %modules', [
'%modules' => implode(', ', $modules),
]));
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_date_range',
'entity_type' => 'entity_test_mulrev_changed',
'type' => 'daterange',
'settings' => [
'datetime_type' => DateRangeItem::DATETIME_TYPE_DATETIME,
],
'cardinality' => 1,
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'item',
])
->save();
$index = Index::load($this->indexId);
$field = \Drupal::getContainer()
->get('search_api.fields_helper')
->createField($index, 'field_date_range_end', [
'label' => 'Date range (end)',
'type' => 'date',
'datasource_id' => 'entity:entity_test_mulrev_changed',
'property_path' => 'field_date_range:end_value',
]);
$index
->addField($field)
->save();
$this
->assertNotEmpty($field
->getDataDefinition());
$entity = EntityTestMulRevChanged::load(reset($this->entities)
->id());
$this
->assertEquals('item', $entity
->bundle());
$entity->field_date_range = [
'value' => '2021-01-11T10:12:02',
'end_value' => '2021-01-22T10:12:02',
];
$entity
->save();
$this
->indexItems($this->indexId);
$key = 'display.page_2.display_options.fields';
$view = \Drupal::configFactory()
->getEditable('views.view.search_api_test_view');
$fields = $view
->get($key);
$fields['field_date_range_end'] = [
'id' => 'field_date_range_end',
'table' => 'search_api_index_database_search_index',
'field' => 'field_date_range_end',
'plugin_id' => 'search_api_date',
'date_format' => 'custom',
'custom_date_format' => 'Y-m-d',
'timezone' => 'UTC',
];
$view
->set($key, $fields);
$view
->save();
$this
->drupalGet('search-api-test-operations');
$this
->assertSession()
->pageTextContains('2021-01-22');
}
protected function checkExposedSearchFields() {
$key = 'display.default.display_options.filters.search_api_fulltext.expose.expose_fields';
$view = \Drupal::configFactory()
->getEditable('views.view.search_api_test_view');
$view
->set($key, TRUE);
$view
->save();
$query = [
'search_api_fulltext' => 'foo',
'search_api_fulltext_searched_fields' => [
'name',
],
];
$this
->checkResults($query, [
1,
2,
4,
], 'Search for results in name field only');
$query = [
'search_api_fulltext' => 'foo',
'search_api_fulltext_searched_fields' => [
'body',
],
];
$this
->checkResults($query, [
5,
], 'Search for results in body field only');
$view
->set($key, FALSE);
$view
->save();
}
protected function checkResults(array $query, array $expected_results = NULL, $label = 'Search', $arguments = '') {
$this
->drupalGet('search-api-test/' . $arguments, [
'query' => $query,
]);
if (isset($expected_results)) {
$count = count($expected_results);
if ($count) {
$this
->assertSession()
->pageTextContains("Displaying {$count} search results");
}
else {
$this
->assertSession()
->pageTextNotContains('search results');
}
$expected_results = array_combine($expected_results, $expected_results);
$actual_results = [];
foreach ($this->entities as $id => $entity) {
$entity_label = Html::escape($entity
->label());
if (strpos($this
->getSession()
->getPage()
->getContent(), ">{$entity_label}<") !== FALSE) {
$actual_results[$id] = $id;
}
}
$this
->assertEquals($expected_results, $actual_results, "{$label} returned correct results.");
}
}
public function testViewSorts() {
$this
->checkResultsOrder([], [
1,
2,
3,
4,
5,
]);
$query = [
'sort_by' => 'search_api_id_desc',
];
$this
->checkResultsOrder($query, [
5,
4,
3,
2,
1,
]);
}
protected function checkResultsOrder(array $query, array $expected_results) {
$this
->drupalGet('search-api-test-sorts', [
'query' => $query,
]);
$web_assert = $this
->assertSession();
$rows_xpath = '//div[contains(@class, "views-row")]';
$web_assert
->elementsCount('xpath', $rows_xpath, count($expected_results));
foreach (array_values($expected_results) as $i => $id) {
$entity_label = Html::escape($this->entities[$id]
->label());
++$i;
$web_assert
->elementContains('xpath', "({$rows_xpath})[{$i}]", $entity_label);
}
}
public function testViewsAdmin() {
$index = Index::load($this->indexId);
$datasource_id = 'entity:entity_test_mulrev_changed';
$field = \Drupal::getContainer()
->get('search_api.fields_helper')
->createField($index, 'author', [
'label' => 'Author name',
'type' => 'string',
'datasource_id' => $datasource_id,
'property_path' => 'user_id:entity:name',
]);
$index
->addField($field);
$field = \Drupal::getContainer()
->get('search_api.fields_helper')
->createField($index, 'rendered_item', [
'label' => 'Rendered HTML output',
'type' => 'text',
'property_path' => 'rendered_item',
'configuration' => [
'roles' => [
AccountInterface::ANONYMOUS_ROLE,
],
'view_mode' => [
$datasource_id => [
'article' => 'full',
'item' => 'full',
],
],
],
]);
$index
->addField($field);
$index
->save();
foreach ([
1,
2,
3,
4,
5,
] as $id) {
$entity = EntityTestMulRevChanged::load($id);
$entity = $entity
->addTranslation('nl', [
'body' => "dutch node {$id}",
'category' => "dutch category {$id}",
'keywords' => [
"dutch {$id} A",
"dutch {$id} B",
],
]);
$entity
->save();
}
$this->entities = EntityTestMulRevChanged::loadMultiple();
$this
->indexItems($this->indexId);
$permissions = [
'administer search_api',
'access administration pages',
'administer views',
'administer users',
'administer permissions',
];
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/views/view/search_api_test_view/edit/page_1');
$this
->assertSession()
->statusCodeEquals(200);
$users[] = $this
->createUser();
$users[] = $this
->createUser();
$users[] = $this
->createUser();
$this->entities[1]
->setOwnerId($users[0]
->id())
->save();
$this->entities[2]
->setOwnerId($users[0]
->id())
->save();
$this->entities[3]
->setOwnerId($users[1]
->id())
->save();
$this->entities[4]
->setOwnerId($users[1]
->id())
->save();
$this->entities[5]
->setOwnerId($users[2]
->id())
->save();
$this
->clickLink('Unformatted list');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'style[type]' => 'table',
];
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->statusCodeEquals(200);
$this
->submitForm([], 'Apply');
$this
->assertSession()
->statusCodeEquals(200);
$this
->clickLink('Add relationships');
$edit = [
'name[search_api_datasource_database_search_index_entity_entity_test_mulrev_changed.user_id]' => 'search_api_datasource_database_search_index_entity_entity_test_mulrev_changed.user_id',
];
$this
->submitForm($edit, 'Add and configure relationships');
$this
->submitForm([], 'Apply');
$this
->clickLink('Add fields');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Test entity - revisions and data table datasource');
$this
->assertSession()
->pageTextContains('Authored on');
$this
->assertSession()
->pageTextContains('Body (indexed field)');
$this
->assertSession()
->pageTextContains('Index Test index');
$this
->assertSession()
->pageTextContains('Item ID');
$this
->assertSession()
->pageTextContains('Excerpt');
$this
->assertSession()
->pageTextContains('The search result excerpted to show found search terms');
$this
->assertSession()
->pageTextContains('Relevance');
$this
->assertSession()
->pageTextContains('The relevance of this search result with respect to the query');
$this
->assertSession()
->pageTextContains('Language code');
$this
->assertSession()
->pageTextContains('The user language code.');
$this
->assertSession()
->pageTextContains('(No description available)');
$this
->assertSession()
->pageTextNotContains('Error: missing help');
$fields = [
'views.counter',
'search_api_datasource_database_search_index_entity_entity_test_mulrev_changed.id',
'search_api_index_database_search_index.search_api_datasource',
'search_api_datasource_database_search_index_entity_entity_test_mulrev_changed.body',
'search_api_index_database_search_index.category',
'search_api_index_database_search_index.keywords',
'search_api_datasource_database_search_index_entity_entity_test_mulrev_changed.user_id',
'search_api_entity_user.name',
'search_api_index_database_search_index.author',
'search_api_entity_user.roles',
'search_api_index_database_search_index.rendered_item',
'search_api_index_database_search_index.search_api_rendered_item',
];
$edit = [];
foreach ($fields as $field) {
$edit["name[{$field}]"] = $field;
}
$this
->submitForm($edit, 'Add and configure fields');
$this
->assertSession()
->statusCodeEquals(200);
$get_field_id = function ($key) {
return Utility::splitPropertyPath($key, TRUE, '.')[1];
};
$fields = array_map($get_field_id, $fields);
$fields = array_combine($fields, $fields);
for ($i = 0; $i < count($fields); ++$i) {
$field = $this
->submitFieldsForm();
if (!$field) {
break;
}
unset($fields[$field]);
}
foreach ($fields as $field) {
$this
->drupalGet('admin/structure/views/nojs/handler/search_api_test_view/page_1/field/' . $field);
$this
->submitFieldsForm();
}
$this
->clickLink('Settings', 0);
$edit = [
'style_options[info][search_api_datasource][sortable]' => 1,
'style_options[info][category][sortable]' => 1,
'style_options[info][keywords][sortable]' => 1,
];
$this
->submitForm($edit, 'Apply');
$this
->clickLink('Add filter criteria');
$edit = [
'name[search_api_index_database_search_index.name]' => 'search_api_index_database_search_index.name',
];
$this
->submitForm($edit, 'Add and configure filter criteria');
$edit = [
'options[expose_button][checkbox][checkbox]' => 1,
];
$this
->submitForm($edit, 'Expose filter');
$this
->submitPluginForm([]);
$this
->clickLink('Add filter criteria');
$edit = [
'name[search_api_index_database_search_index.search_api_fulltext]' => 'search_api_index_database_search_index.search_api_fulltext',
];
$this
->submitForm($edit, 'Add and configure filter criteria');
$this
->assertSession()
->pageTextNotContains('No UI parse mode');
$edit = [
'options[expose_button][checkbox][checkbox]' => 1,
];
$this
->submitForm($edit, 'Expose filter');
$this
->submitPluginForm([]);
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('search-api-test');
$this
->assertSession()
->statusCodeEquals(200);
$fields = [
'search_api_datasource',
'id',
'body',
'category',
'keywords',
'user_id',
'user_id:name',
'user_id:roles',
'rendered_item',
'search_api_rendered_item',
];
$rendered_item_fields = [
'rendered_item',
'search_api_rendered_item',
];
foreach ($this->entities as $id => $entity) {
foreach ($fields as $field) {
$field_entity = $entity;
while (strpos($field, ':')) {
list($direct_property, $field) = Utility::splitPropertyPath($field, FALSE);
if (empty($field_entity->{$direct_property}[0]->entity)) {
continue 2;
}
$field_entity = $field_entity->{$direct_property}[0]->entity;
}
$entities = [
$field_entity,
];
if ($field_entity
->hasTranslation('nl')) {
$entities[] = $field_entity
->getTranslation('nl');
}
foreach ($entities as $i => $field_entity) {
if ($field === 'search_api_datasource') {
$data = [
$datasource_id,
];
}
elseif (in_array($field, $rendered_item_fields)) {
$view_mode = $field === 'rendered_item' ? 'full' : 'teaser';
$data = [
$view_mode,
];
}
else {
$data = \Drupal::getContainer()
->get('search_api.fields_helper')
->extractFieldValues($field_entity
->get($field));
if (!$data) {
$data = [
'[EMPTY]',
];
}
}
$row_num = 2 * $id + $i - 1;
$prefix = "#{$row_num} [{$field}] ";
$text = $prefix . implode("|{$prefix}", $data);
$this
->assertSession()
->pageTextContains($text);
if ($field === 'name') {
$text = str_replace('[name]', '[author]', $text);
$this
->assertSession()
->pageTextContains($text);
}
}
}
}
$retrieved_fields = [
'keywords',
'rendered_item',
];
foreach ($retrieved_fields as $field_id) {
$this
->assertSession()
->pageTextContains("'{$field_id}'");
}
$options = [
'query' => [
'order' => 'category',
'sort' => 'asc',
],
];
$this
->drupalGet('search-api-test', $options);
$this
->assertSession()
->statusCodeEquals(200);
$ordered_categories = [
'[EMPTY]',
'article_category',
'article_category',
'dutch category 1',
'dutch category 2',
'dutch category 3',
'dutch category 4',
'dutch category 5',
'item_category',
'item_category',
];
foreach ($ordered_categories as $i => $category) {
++$i;
$this
->assertSession()
->pageTextContains("#{$i} [category] {$category}");
}
$options['query']['sort'] = 'desc';
$this
->drupalGet('search-api-test', $options);
$this
->assertSession()
->statusCodeEquals(200);
foreach (array_reverse($ordered_categories) as $i => $category) {
++$i;
$this
->assertSession()
->pageTextContains("#{$i} [category] {$category}");
}
$this
->drupalLogout();
$this
->drupalGet('search-api-test');
$this
->assertSession()
->statusCodeEquals(200);
$html = $this
->getSession()
->getPage()
->getContent();
$this
->assertEquals(10, substr_count($html, '[name] [EMPTY]'));
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/views/nojs/handler/search_api_test_view/page_1/relationship/user_id');
$this
->submitForm([
'options[skip_access]' => 1,
], 'Apply');
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogout();
$this
->drupalGet('search-api-test');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('[name] [EMPTY]');
$this
->drupalLogin($admin_user);
$this
->adminUiRegressionTests();
}
protected function submitFieldsForm() {
$url_parts = explode('/', $this
->getUrl());
$field = array_pop($url_parts);
if (array_pop($url_parts) != 'field') {
return NULL;
}
$non_entity_fields = [
'search_api_datasource',
'rendered_item',
'search_api_rendered_item',
];
if (!in_array($field, $non_entity_fields, TRUE)) {
$edit['options[fallback_options][multi_separator]'] = '|';
}
$edit['options[alter][alter_text]'] = TRUE;
$edit['options[alter][text]'] = "#{{counter}} [{$field}] {{ {$field} }}";
$edit['options[empty]'] = "#{{counter}} [{$field}] [EMPTY]";
switch ($field) {
case 'counter':
$edit = [
'options[exclude]' => TRUE,
];
break;
case 'id':
$edit['options[field_rendering]'] = FALSE;
break;
case 'search_api_datasource':
break;
case 'body':
break;
case 'category':
break;
case 'keywords':
$edit['options[field_rendering]'] = FALSE;
break;
case 'user_id':
$edit['options[field_rendering]'] = FALSE;
$edit['options[fallback_options][display_methods][user][display_method]'] = 'id';
break;
case 'author':
break;
case 'roles':
$edit['options[field_rendering]'] = FALSE;
$edit['options[fallback_options][display_methods][user_role][display_method]'] = 'id';
break;
case 'rendered_item':
break;
case 'search_api_rendered_item':
$edit['options[view_modes][entity:entity_test_mulrev_changed][article]'] = 'teaser';
$edit['options[view_modes][entity:entity_test_mulrev_changed][item]'] = 'teaser';
break;
}
$this
->submitPluginForm($edit);
return $field;
}
protected function submitPluginForm(array $edit) {
$button_label = 'Apply';
$buttons = $this
->xpath('//input[starts-with(@value, :label)]', [
':label' => $button_label,
]);
if ($buttons) {
$button_label = $buttons[0]
->getAttribute('value');
}
$this
->submitForm($edit, $button_label);
$this
->assertSession()
->statusCodeEquals(200);
}
protected function adminUiRegressionTests() {
$this
->regressionTest2883807();
}
protected function regressionTest2883807() {
$this
->drupalGet('admin/structure/views/nojs/add-handler/search_api_test_view/page_1/argument');
$edit = [
'name[search_api_index_database_search_index.author]' => TRUE,
];
$this
->submitForm($edit, 'Add and configure contextual filters');
$this
->submitForm([], 'Apply');
$this
->submitForm([], 'Save');
}
public function testHighlighting() {
$index = Index::load('database_search_index');
$processor = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createProcessorPlugin($index, 'highlight');
$index
->addProcessor($processor);
$index
->save();
$path = 'search-api-test-search-view-caching-none';
$this
->drupalGet($path);
$this
->assertSession()
->responseContains('foo bar baz');
$options['query']['search_api_fulltext'] = 'foo';
$this
->drupalGet($path, $options);
$this
->assertSession()
->responseContains('<strong>foo</strong> bar baz');
$options['query']['search_api_fulltext'] = 'bar';
$this
->drupalGet($path, $options);
$this
->assertSession()
->responseContains('foo <strong>bar</strong> baz');
}
public function testCreatingIndexClearsRowPluginCache() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer search_api',
'access administration pages',
'administer views',
]));
$index_id = 'my_custom_index';
Index::create([
'name' => 'My custom index',
'id' => $index_id,
'status' => TRUE,
'datasource_settings' => [
'entity:node' => [],
'entity:user' => [],
],
])
->save();
$this
->drupalGet('/admin/structure/views/add');
$this
->submitForm([
'label' => 'Test view',
'id' => 'test',
'show[wizard_key]' => "standard:search_api_index_{$index_id}",
], 'Save and edit');
$this
->drupalGet('/admin/structure/views/nojs/display/test/default/row');
$this
->assertSession()
->elementExists('css', '#edit-row-type [value="search_api"]');
}
}