diff --git a/tests/search_api_test_node_indexing/config/install/search_api.index.test_node_index.yml b/tests/search_api_test_node_indexing/config/install/search_api.index.test_node_index.yml index 4b3488e9..b2af7e1d 100644 --- a/tests/search_api_test_node_indexing/config/install/search_api.index.test_node_index.yml +++ b/tests/search_api_test_node_indexing/config/install/search_api.index.test_node_index.yml @@ -25,7 +25,7 @@ processor_settings: rendered_item: { } options: cron_limit: -1 - index_directly: true + index_directly: false datasource_settings: 'entity:node': bundles: diff --git a/tests/search_api_test_node_indexing/config/install/views.view.search_api_test_node_view.yml b/tests/search_api_test_node_indexing/config/install/views.view.search_api_test_node_view.yml index 6f381fab..0ee6bce9 100644 --- a/tests/search_api_test_node_indexing/config/install/views.view.search_api_test_node_view.yml +++ b/tests/search_api_test_node_indexing/config/install/views.view.search_api_test_node_view.yml @@ -1,8 +1,18 @@ -base_field: search_api_id +langcode: en +status: true +dependencies: + config: + - search_api.index.test_node_index + module: + - search_api +id: search_api_test_node_view +label: 'Search API test node view' +module: views +description: '' +tag: '' base_table: search_api_index_test_node_index +base_field: search_api_id core: 8.x -description: '' -status: true display: default: display_plugin: default @@ -14,7 +24,7 @@ display: type: none options: { } cache: - type: tag + type: search_api_tag options: { } query: type: search_api_query @@ -59,71 +69,6 @@ display: id: title table: search_api_index_test_node_index field: title - relationship: none - group_type: group - admin_label: '' - label: '' - exclude: false - alter: - alter_text: false - text: '' - make_link: false - path: '' - absolute: false - external: false - replace_spaces: false - path_case: none - trim_whitespace: false - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: false - max_length: 0 - word_boundary: true - ellipsis: true - more_link: false - more_link_text: '' - more_link_path: '' - strip_tags: false - trim: false - preserve_tags: '' - html: false - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: false - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: true - empty: '' - hide_empty: false - empty_zero: false - hide_alter_empty: true - click_sort_column: value - type: string - settings: - link_to_entity: false - group_column: value - group_columns: { } - group_rows: true - delta_limit: 0 - delta_offset: 0 - delta_reversed: false - delta_first_last: false - multi_type: separator - separator: ', ' - field_api_classes: false - field_rendering: true - fallback_handler: search_api - fallback_options: - link_to_item: false - use_highlighting: false - multi_type: separator - multi_separator: ', ' plugin_id: search_api_field filters: { } sorts: { } @@ -140,7 +85,6 @@ display: - 'languages:language_content' - 'languages:language_interface' - url.query_args - - user.is_super_user - 'user.node_grants:view' tags: - 'config:search_api.index.test_node_index' @@ -161,13 +105,3 @@ display: - 'user.node_grants:view' tags: - 'config:search_api.index.test_node_index' -label: 'Search API test node view' -module: views -id: search_api_test_node_view -tag: '' -langcode: en -dependencies: - config: - - search_api.index.test_node_index - module: - - search_api diff --git a/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php b/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php index 7de4942d..6eb8197b 100644 --- a/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php +++ b/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php @@ -4,8 +4,10 @@ namespace Drupal\Tests\search_api\Kernel\Views; use Drupal\Core\Cache\Cache; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Session\AccountInterface; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\Node; +use Drupal\node\Entity\NodeType; use Drupal\search_api\Entity\Index; use Drupal\Tests\user\Traits\UserCreationTrait; use Drupal\views\Tests\AssertViewsCacheTagsTrait; @@ -79,6 +81,13 @@ class ViewsCacheInvalidationTest extends KernelTestBase { */ protected $users; + /** + * A test content type. + * + * @var \Drupal\node\NodeTypeInterface + */ + protected $contentType; + /** * Test nodes. * @@ -152,6 +161,13 @@ class ViewsCacheInvalidationTest extends KernelTestBase { // Use the test search index from the search_api_test_db module. $this->index = Index::load('test_node_index'); + // Create a test content type. + $this->contentType = NodeType::create([ + 'name' => 'Page', + 'type' => 'page', + ]); + $this->contentType->save(); + // Create some test content and index it. foreach (['Cheery' => TRUE, 'Carrot' => TRUE, 'Detritus' => FALSE] as $title => $status) { $this->createNode($title, $status); @@ -160,8 +176,8 @@ class ViewsCacheInvalidationTest extends KernelTestBase { // Create two test users, one with permission to view unpublished entities, // and one without. - $this->users['no-access'] = $this->createUser(); - $this->users['has-access'] = $this->createUser(['bypass node access']); + $this->users['no-access'] = $this->createUser(['access content']); + $this->users['has-access'] = $this->createUser(['access content', 'bypass node access']); } /** @@ -216,7 +232,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { $this->assertCached('has-access'); // Grant the permission to 'bypass node access' to the unprivileged user. - $privileged_role = reset($this->users['has-access']->getRoles()); + $privileged_role = $this->users['has-access']->getRoles()[1]; $this->users['no-access']->addRole($privileged_role); $this->users['no-access']->save(); @@ -237,7 +253,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { // The view should show the updated title when displayed, and the result // should be cached. - $this->assertViewsResult('has-access', ['Angua', 'Cheery Littlebottom', 'Carrot', 'Detritus']); + $this->assertViewsResult('has-access', ['Angua', 'Cheery', 'Carrot', 'Detritus']); $this->assertCached('has-access'); // Delete one of the test content entities. This should not affect the @@ -250,7 +266,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { // The view should no longer include the deleted content now, and the result // should be cached after the view has been displayed. - $this->assertViewsResult('has-access', ['Angua', 'Cheery Littlebottom', 'Detritus']); + $this->assertViewsResult('has-access', ['Angua', 'Cheery', 'Detritus']); $this->assertCached('has-access'); } @@ -283,7 +299,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { * The key of the user for which to perform the check. */ protected function doAssertCached($assert_method, $user_key) { - // $this->setCurrentUser($this->users[$user_key]); + $this->setCurrentUser($this->users[$user_key]); // Retrieve the view to render, and apply the required cache contexts that // are also applied when RendererInterface::renderRoot() is executed. This @@ -305,19 +321,29 @@ class ViewsCacheInvalidationTest extends KernelTestBase { // been requested, and the static caches are empty. $this->cacheTagsInvalidator->resetChecksums(); - // $this->setCurrentUser($this->users[$user_key]); + $this->setCurrentUser($this->users[$user_key]); $render_array = $this->getRenderableView(); - $html = $this->renderer->renderRoot($render_array); + $html = (string) $this->renderer->renderRoot($render_array); + // Check that the titles of the expected results are present. foreach ($node_keys as $node_key) { $label = $this->nodes[$node_key]->label(); $this->assertContains($label, $html); } + + // Also check that none of the titles of the remaining search items are + // unexpectedly present. + $unexpected_keys = array_diff(array_keys($this->nodes), $node_keys); + foreach ($unexpected_keys as $unexpected_key) { + $label = $this->nodes[$unexpected_key]->label(); + $this->assertNotContains($label, $html); + } } - protected function setCurrentUser($user_key) { - throw new \Exception(__METHOD__ . ' is not implemented'); + protected function setCurrentUser(AccountInterface $user) { + // @todo access on property. + \Drupal::currentUser()->setAccount($user); } protected function getView() { @@ -336,11 +362,22 @@ class ViewsCacheInvalidationTest extends KernelTestBase { return $render_array; } + /** + * Creates a node with the given title and publication status. + * + * @param string $title + * The title for the node. + * @param bool $status + * The publication status to set. + * + * @throws \Drupal\Core\Entity\EntityStorageException + * Thrown if an error occurred during the saving of the node. + */ protected function createNode($title, $status) { $values = [ 'title' => $title, 'status' => $status, - 'type' => 'page', + 'type' => $this->contentType->id(), ]; $this->nodes[$title] = Node::create($values); $this->nodes[$title]->save();