updateIndex example



  /** * Tests that search works with punctuation and HTML entities. */
  public function testPhraseSearchPunctuation() {
    $node = $this->drupalCreateNode(['body' => [['value' => "The bunny's ears were fluffy."]]]);
    // cSpell:disable-next-line     $this->drupalCreateNode(['body' => [['value' => 'Dignissim Aliquam & Quieligo meus natu quae quia te. Damnum© erat— neo pneum. Facilisi feugiat ibidem ratis.']]]);

    // Update the search index.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Refresh variables after the treatment.     $this->refreshVariables();

    // Submit a phrase wrapped in double quotes to include the punctuation.     $edit = ['keys' => '"bunny\'s"'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->pageTextContains($node->label());

    // Check if the author is linked correctly to the user profile page.
// Create a user and a node, and update the search index.     $test_user = $this->drupalCreateUser([
      'access content',
      'search content',
      'administer nodes',
    ]);
    $this->drupalLogin($test_user);

    $this->node = $this->drupalCreateNode();

    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Set up a dummy initial count of times the form has been submitted.     $this->submitCount = \Drupal::state()->get('search_embedded_form.submit_count');
    $this->refreshVariables();
  }

  /** * Tests that the embedded form appears and can be submitted. */
  public function testEmbeddedForm() {
    // First verify we can submit the form from the module's page.
'type' => 'page',
      'title' => 'test page',
      'body' => [['value' => 'pizza']],
    ];
    $this->drupalCreateNode($data);

    $data['type'] = 'article';
    $data['title'] = 'test article';
    $this->drupalCreateNode($data);

    // Update the search index.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Search for the body keyword 'pizza'.     $this->drupalGet('search/node');
    $this->submitForm(['keys' => 'pizza'], 'Search');
    // The article should be there but not the page.     $this->assertSession()->pageTextContains('article');
    $this->assertSession()->pageTextNotContains('page');
  }

}
'access content',
      'search content',
      'use advanced search',
      'administer nodes',
    ]);
    $this->drupalLogin($test_user);

    // Create initial node.     $this->node = $this->drupalCreateNode();

    // First update the index. This does the initial processing.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
  }

  /** * Tests advanced search by node type. */
  public function testNodeType() {
    // Verify some properties of the node that was created.     $this->assertSame('page', $this->node->getType(), 'Node type is Basic page.');
    $dummy_title = 'Lorem ipsum';
    $this->assertNotEquals($dummy_title$this->node->label(), "Dummy title doesn't equal node title.");

    
// Create a comment array     $edit_comment = [];
    $edit_comment['subject[0][value]'] = $this->randomMachineName();
    $edit_comment['comment_body[0][value]'] = $this->randomMachineName();

    // Post comment to the test node with comment     $this->drupalGet('comment/reply/node/' . $this->searchableNodes['1 comment']->id() . '/comment');
    $this->submitForm($edit_comment, 'Save');

    // First update the index. This does the initial processing.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
  }

  /** * Verify that comment count display toggles properly on comment status of node. */
  public function testSearchCommentCountToggle() {
    // Search for the nodes by string in the node body.     $edit = [
      'keys' => "'SearchCommentToggleTestCase'",
    ];
    $this->drupalGet('search/node');

    

    // Create nodes containing keywords.     for ($i = 0; $i <= 17; $i++) {
      $settings['body'] = [['value' => 'love cheesy pizza']];
      $this->drupalCreateNode($settings);
    }
    // Create another node and save it for later.     $settings['body'] = [['value' => 'Druplicon']];
    $node = $this->drupalCreateNode($settings);

    // Update the search index.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Refresh variables after the treatment.     $this->refreshVariables();

    // Test that the correct number of pager links are found for keyword search.     $edit = ['keys' => 'love pizza'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->linkByHrefExists('page=1', 0, '2nd page link is found for keyword search.');
    $this->assertSession()->linkByHrefExists('page=2', 0, '3rd page link is found for keyword search.');
    $this->assertSession()->linkByHrefExists('page=3', 0, '4th page link is found for keyword search.');
    

  protected function setUp(): void {
    parent::setUp();

    $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);

    // Create a plugin instance.     $this->nodeSearchPlugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
    // Create a node with a very simple body.     $this->drupalCreateNode(['body' => [['value' => 'tapir']]]);
    // Update the search index.     $this->nodeSearchPlugin->updateIndex();
  }

  /** * Verify that search works with a numeric locale set. */
  public function testSearchWithNumericLocale() {
    // French decimal point is comma.     setlocale(LC_NUMERIC, 'fr_FR');
    $this->nodeSearchPlugin->setSearch('tapir', [][]);
    // The call to execute will throw an exception if a float in the wrong     // format is passed in the query to the database, so an assertion is not
$this->drupalLogin($web_user);
  }

  /** * Tests that hook_search_preprocess() returns the correct langcode. */
  public function testPreprocessLangcode() {
    // Create a node.     $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);

    // First update the index. This does the initial processing.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Search for the additional text that is added by the preprocess     // function. If you search for text that is in the node, preprocess is     // not invoked on the node during the search excerpt generation.     $edit = ['or' => 'Additional text'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'edit-submit--2');

    // Checks if the langcode message has been set by hook_search_preprocess().     $this->assertSession()->pageTextContains('Langcode Preprocess Test: en');
  }

  
'created' => $created_time_en->getTimestamp(),
    ]);

    // Add Spanish translation to the node.     $translation = $node->addTranslation('es', ['title' => 'Node ES']);
    $translation->body->value = $this->randomMachineName(32);
    $translation->created->value = $created_time_es->getTimestamp();
    $node->save();

    // Update the index.     $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
    $plugin->updateIndex();
  }

  /** * Tests searching with date filters that exclude some translations. */
  public function testDateIntervalQueryAlter() {
    // Search for keyword node.     $edit = ['keys' => 'node'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');

    
'sticky' => (int) $node->isSticky(),
          'created' => $node->getCreatedTime(),
          'comment_count' => 0,
          'last_comment_timestamp' => $node->getCreatedTime(),
        ]);
      }
    }
    $query->execute();
    // The logic for determining last_comment_count is fairly complex, so     // update the index too.     if ($node->isNew()) {
      $this->updateIndex($node);
    }
  }

  /** * {@inheritdoc} */
  public function deleteIndex(NodeInterface $node) {
    $this->database->delete('forum_index')
      ->condition('nid', $node->id())
      ->execute();
  }

}
// Create user.     $this->searchingUser = $this->drupalCreateUser([
      'search content',
      'access user profiles',
    ]);

    // Create a node and update the search index.     $this->node = $this->drupalCreateNode(['title' => 'bike shed shop']);
    $this->node->setOwner($this->searchingUser);
    $this->node->save();
    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
  }

  /** * Tests the presence of the expected cache tag in various situations. */
  public function testSearchText() {
    $this->drupalLogin($this->searchingUser);

    // Initial page for searching nodes.     $this->drupalGet('search/node');
    $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
    
    $translation = $this->searchableNodes[1]->addTranslation('en', ['title' => 'Second node en']);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[1]->save();

    // Add Spanish translation to the third node.     $translation = $this->searchableNodes[2]->addTranslation('es', ['title' => 'Third node es']);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[2]->save();

    // Update the index and then run the shutdown method.     $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
    $plugin->updateIndex();
  }

  public function testLanguages() {
    // Add predefined language.     $edit = ['predefined_langcode' => 'fr'];
    $this->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add language');
    $this->assertSession()->pageTextContains('French');

    // Now we should have languages displayed.     $this->drupalGet('search/node');
    
/** * Tests the indexing throttle and search results with multilingual nodes. */
  public function testMultilingualSearch() {
    // Index only 2 nodes per cron run. We cannot do this setting in the UI,     // because it doesn't go this low.     $this->config('search.settings')->set('index.cron_limit', 2)->save();
    // Get a new search plugin, to make sure it has this setting.     $this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');

    // Update the index. This does the initial processing.     $this->plugin->updateIndex();
    // Run the shutdown function. Testing is a unique case where indexing     // and searching has to happen in the same request, so running the shutdown     // function manually is needed to finish the indexing process.     $search_index = \Drupal::service('search.index');
    assert($search_index instanceof SearchIndexInterface);
    $this->assertIndexCounts(6, 8, 'after updating partially');
    $this->assertDatabaseCounts(2, 0, 'after updating partially');

    // Now index the rest of the nodes.     // Make sure index throttle is high enough, via the UI.     $this->drupalGet('admin/config/search/pages');
    
/** * Tests that search returns results with diacritics in the search phrase. */
  public function testPhraseSearchPunctuation() {
    // cSpell:disable     $body_text = 'The Enricþment Center is cómmīŦŧęđ to the well BɆĬŇĜ of æll påŔťıçȉpǎǹţș. ';
    $body_text .= 'Also meklēt (see #731298)';
    $this->drupalCreateNode(['body' => [['value' => $body_text]]]);

    // Update the search index.     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Refresh variables after the treatment.     $this->refreshVariables();

    $edit = ['keys' => 'meklet'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->responseContains('<strong>meklēt</strong>');

    $edit = ['keys' => 'meklēt'];
    $this->drupalGet('search/node');
    
// Add a single piece of content and index it.     $node = $this->drupalCreateNode();
    $this->searchNode = $node;
    // Link the node to itself to test that it's only indexed once. The content     // also needs the word "pizza" so we can use it as the search keyword.     $body_key = 'body[0][value]';
    $edit[$body_key] = Link::fromTextAndUrl($node->label()$node->toUrl())->toString() . ' pizza sandwich';
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');

    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();

    // Enable the search block.     $this->drupalPlaceBlock('search_form_block');
    $this->drupalPlaceBlock('local_tasks_block', ['id' => 'local_tasks']);
    $this->drupalPlaceBlock('page_title_block');
  }

  /** * Verifies the search settings form. */
  public function testSearchSettingsPage() {

    
Home | Imprint | This part of the site doesn't use cookies.