You are here

function SearchbyPageAttachReindexTest::testSearchAttach in Search by Page 8

Tests that attachments are reindexed when the node changes.

Note that the name must be testSearchAttach() to override the function by the same name in the base class.

Overrides SearchByPageAttachTest::testSearchAttach

File

tests/src/Functional/search_by_page.test, line 2422
Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com

Class

SearchbyPageAttachReindexTest
Tests attachment reindexing.

Namespace

Drupal\Tests\search_by_page\Functional

Code

function testSearchAttach() {
  $this
    ->drupalLogin($this->superuser);
  $search_path = $this->envinfo2['page_path'];

  // Verify that all the test nodes are found on a search for their
  // descriptions, and for the attachment (contains daisy).
  for ($i = 0; $i < count($this->nodes); $i++) {
    $node = $this->nodes[$i];
    $desc = $this->descs[$i];
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'daisy',
    ), t('Search pages'));
    $this
      ->assertText($node->title, "Node appears in search of first attachment");
    $this
      ->drupalPostForm($search_path, array(
      'keys' => $desc,
    ), t('Search pages'));
    $this
      ->assertText($node->title, "Node appears in search of attachment description");
  }

  // Set to never reindex automatically.
  $this
    ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
    'search_by_page_attach_min_time' => 0,
    'search_by_page_attach_max_time' => 0,
  ), 'Save configuration');
  drupal_flush_all_caches();
  variable_initialize();

  // Set search so it only indexes 1 node per cron run.
  \Drupal::state()
    ->set('search_by_page_cron_limit', 1);

  // Run the next test several times. Pick a random node, attach the
  // file again with a new description, and run cron to index it.
  // Verify the new description can be found.
  for ($i = 0; $i < 10; $i++) {

    // Choose a random title and index.
    $newdesc = $this
      ->randomName();
    $key = rand(0, count($this->nodes) - 1);
    $node = \Drupal\node\Entity\Node::load($this->nodes[$key]->nid, NULL, TRUE);
    foreach ($node->field_myfile as $lang => $stuff) {
      $node->field_myfile[$lang][0]['description'] = $newdesc;
    }
    node_save($node);
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalPostForm($search_path, array(
      'keys' => $newdesc,
    ), t('Search pages'));
    $this
      ->assertText($this->nodes[$key]->title, "Node appears in search of attachment description after reindex");
  }
}