diff --git a/search_api_db/tests/src/Kernel/BackendTest.php b/search_api_db/tests/src/Kernel/BackendTest.php index a21acac..7a16cfa 100644 --- a/search_api_db/tests/src/Kernel/BackendTest.php +++ b/search_api_db/tests/src/Kernel/BackendTest.php @@ -356,19 +356,57 @@ protected function searchSuccess1() { $results = $this->buildSearch()->addCondition('keywords', array('grape', 'apple'), 'IN')->execute(); $this->assertEquals(3, $results->getResultCount(), 'Query with IN filter returned correct number of results.'); - $this->assertEquals($this->getItemIds(array(2, 4, 5)), array_keys($results->getResultItems()), 'Query with IN filter field returned correct result.'); + $this->assertEquals($this->getItemIds(array(2, 4, 5)), array_keys($results->getResultItems()), 'Query with IN filter returned correct result.'); $this->assertIgnored($results); $this->assertWarnings($results); $results = $this->buildSearch()->addCondition('keywords', array('grape', 'apple'), 'NOT IN')->execute(); $this->assertEquals(2, $results->getResultCount(), 'Query with NOT IN filter returned correct number of results.'); - $this->assertEquals($this->getItemIds(array(1, 3)), array_keys($results->getResultItems()), 'Query with NOT IN filter field returned correct result.'); + $this->assertEquals($this->getItemIds(array(1, 3)), array_keys($results->getResultItems()), 'Query with NOT IN filter returned correct result.'); $this->assertIgnored($results); $this->assertWarnings($results); $results = $this->buildSearch()->addCondition('width', array('0.9', '1.5'), 'BETWEEN')->execute(); $this->assertEquals(1, $results->getResultCount(), 'Query with BETWEEN filter returned correct number of results.'); - $this->assertEquals($this->getItemIds(array(4)), array_keys($results->getResultItems()), 'Query with BETWEEN filter field returned correct result.'); + $this->assertEquals($this->getItemIds(array(4)), array_keys($results->getResultItems()), 'Query with BETWEEN filter returned correct result.'); + $this->assertIgnored($results); + $this->assertWarnings($results); + + $results = $this->buildSearch() + ->setLanguages(array('und', 'en')) + ->addCondition('keywords', array('grape', 'apple'), 'IN') + ->execute(); + $this->assertEquals(3, $results->getResultCount(), 'Query with IN filter returned correct number of results.'); + $this->assertEquals($this->getItemIds(array(2, 4, 5)), array_keys($results->getResultItems()), 'Query with IN filter returned correct result.'); + $this->assertIgnored($results); + $this->assertWarnings($results); + + $query = $this->buildSearch(); + $conditions = $query->createConditionGroup('OR'); + $conditions->addCondition('search_api_language', 'und'); + $conditions->addCondition('width', array('0.9', '1.5'), 'BETWEEN'); + $query->addConditionGroup($conditions); + $results = $query->execute(); + $this->assertEquals(1, $results->getResultCount(), 'Query with search_api_language filter returned correct number of results.'); + $this->assertEquals($this->getItemIds(array(4)), array_keys($results->getResultItems()), 'Query with search_api_language filter returned correct result.'); + $this->assertIgnored($results); + $this->assertWarnings($results); + + $query = $this->buildSearch(); + $query->addCondition('search_api_language', array('und', 'en'), 'IN'); + $query->addCondition('width', array('0.9', '1.5'), 'BETWEEN'); + $results = $query->execute(); + $this->assertEquals(1, $results->getResultCount(), 'Query with search_api_language "IN" filter returned correct number of results.'); + $this->assertEquals($this->getItemIds(array(4)), array_keys($results->getResultItems()), 'Query with search_api_language filter returned correct result.'); + $this->assertIgnored($results); + $this->assertWarnings($results); + + $query = $this->buildSearch(); + $query->addCondition('search_api_language', array('und', 'de'), 'NOT IN'); + $query->addCondition('width', array('0.9', '1.5'), 'BETWEEN'); + $results = $query->execute(); + $this->assertEquals(1, $results->getResultCount(), 'Query with search_api_language "NOT IN" filter returned correct number of results.'); + $this->assertEquals($this->getItemIds(array(4)), array_keys($results->getResultItems()), 'Query with search_api_language filter returned correct result.'); $this->assertIgnored($results); $this->assertWarnings($results); } diff --git a/src/Plugin/search_api/datasource/ContentEntity.php b/src/Plugin/search_api/datasource/ContentEntity.php index 69394de..fe2b85b 100755 --- a/src/Plugin/search_api/datasource/ContentEntity.php +++ b/src/Plugin/search_api/datasource/ContentEntity.php @@ -581,7 +581,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s */ public function getItemId(ComplexDataInterface $item) { if ($item instanceof EntityAdapter) { - return $item->getValue()->id() . ':' . $item->getValue()->language(); + return $item->getValue()->id() . ':' . $item->getValue()->language()->getId(); } return NULL; } diff --git a/src/Query/Query.php b/src/Query/Query.php index f21c366..124c404 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -6,6 +6,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\search_api\IndexInterface; use Drupal\search_api\SearchApiException; +use Drupal\search_api\Utility; /** * Provides a standard implementation for a Search API query. @@ -329,20 +330,23 @@ public function range($offset = NULL, $limit = NULL) { * {@inheritdoc} */ public function execute() { - // @todo Abort if $languages is an empty array. + if ($this->languages === array()) { + return Utility::createSearchResultSet($this)->setResultCount(0); + } // Prepare the query for execution by the server. $this->preExecute(); + if ($this->languages === array()) { + return Utility::createSearchResultSet($this)->setResultCount(0); + } + // Execute query. $response = $this->index->getServerInstance()->search($this); // Postprocess the search results. $this->postExecute($response); - // Store search for later retrieval for facets, etc. - // @todo Figure out how to store the executed searches for the request. - // search_api_current_search(NULL, $this, $response); return $response; } @@ -531,8 +535,6 @@ public function __toString() { } $ret .= 'Sorting: ' . implode(', ', $sorts) . "\n"; } - // @todo Fix for entities contained in options (which might kill - // var_export() due to circular references). $options = $this->sanitizeOptions($this->options); $options = str_replace("\n", "\n ", var_export($options, TRUE)); $ret .= 'Options: ' . $options . "\n"; diff --git a/src/Tests/ViewsTest.php b/src/Tests/ViewsTest.php index cf1b10f..11ef530 100644 --- a/src/Tests/ViewsTest.php +++ b/src/Tests/ViewsTest.php @@ -115,7 +115,7 @@ public function testView() { $this->checkResults($query, array(1, 2, 3, 4, 5), 'Search with ID "not empty" filter'); $this->checkResults(array('keywords[value]' => 'apple'), array(2, 4), 'Search with Keywords filter'); - // @todo Enable "between" again. See #2624870. + // @todo Enable "between" again. See #2695627. // $query = array( // 'keywords[min]' => 'aardvark', // 'keywords[max]' => 'calypso', @@ -142,6 +142,26 @@ public function testView() { $this->checkResults($query, array(1, 2, 4, 5), 'Search with Keywords "not empty" filter'); $query = array( + 'language' => array('***LANGUAGE_language_content***'), + ); + $this->checkResults($query, array(1, 2, 3, 4, 5), 'Search with "Page content language" filter'); + $query = array( + 'language' => array('en'), + ); + $this->checkResults($query, array(1, 2, 3, 4, 5), 'Search with "English" language filter'); + $query = array( + 'language' => array('und'), + ); + $this->checkResults($query, array(), 'Search with "Not specified" language filter'); + $query = array( + 'language' => array( + '***LANGUAGE_language_interface***', + 'zxx', + ), + ); + $this->checkResults($query, array(1, 2, 3, 4, 5), 'Search with multiple languages filter'); + + $query = array( 'search_api_fulltext' => 'foo to test', 'id[value]' => 2, 'id_op' => '>', diff --git a/tests/search_api_test_views/config/install/views.view.search_api_test_view.yml b/tests/search_api_test_views/config/install/views.view.search_api_test_view.yml index 47400ce..e0445fc 100644 --- a/tests/search_api_test_views/config/install/views.view.search_api_test_view.yml +++ b/tests/search_api_test_views/config/install/views.view.search_api_test_view.yml @@ -153,6 +153,31 @@ display: anonymous: '0' administrator: '0' is_grouped: false + search_api_language: + plugin_id: search_api_language + id: search_api_language + table: search_api_index_database_search_index + field: search_api_language + relationship: none + admin_label: '' + operator: 'in' + group: 1 + exposed: true + expose: + operator_id: language_op + label: '' + description: '' + use_operator: true + operator: language_op + identifier: language + required: false + remember: false + multiple: false + remember_roles: + authenticated: authenticated + anonymous: '0' + administrator: '0' + is_grouped: false sorts: search_api_id: id: search_api_id diff --git a/tests/src/Kernel/LanguageKernelTest.php b/tests/src/Kernel/LanguageKernelTest.php index b1e2321..87cd9c3 100644 --- a/tests/src/Kernel/LanguageKernelTest.php +++ b/tests/src/Kernel/LanguageKernelTest.php @@ -8,6 +8,7 @@ use Drupal\language\Entity\ConfigurableLanguage; use Drupal\search_api\Entity\Index; use Drupal\search_api\Entity\Server; +use Drupal\search_api\Utility; /** * Tests translation handling of the content entity datasource. @@ -201,6 +202,20 @@ public function testItemTranslations() { ); $this->assertEquals($expected, $datasource_item_ids, 'Datasource returns correct item ids for a translated entity.'); + // Test whether all items report the correct language. + foreach ($datasource->loadMultiple($datasource_item_ids) as $id => $object) { + list(, $langcode) = explode(':', $id, 2); + $item = Utility::createItemFromObject($this->index, $object, NULL, $datasource); + $this->assertEquals($langcode, $item->getLanguage(), "Item with ID '$id' has the correct language set."); + } + + // Tests that a query with an empty array of languages will return an empty + // result set, without going through the server. (Our test backend wouldn't + // care about languages.) + $results = $this->index->query()->setLanguages(array())->execute(); + $this->assertEquals(0, $results->getResultCount(), 'Query with empty languages list returned correct number of results.'); + $this->assertEquals(array(), $results->getResultItems(), 'Query with empty languages list returned correct result.'); + // Test that the index needs to be updated. $this->assertEquals(1, $this->index->getTrackerInstance()->getIndexedItemsCount(), 'The updated items needs to be reindexed.'); $this->assertEquals(4, $this->index->getTrackerInstance()->getTotalItemsCount(), 'There are four items in total.');