You are here

search_by_page.test in Search by Page 8

Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com

File

tests/src/Functional/search_by_page.test
View source
<?php

/**
 * @file
 * Tests for the Search by Page module.
 * By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com
 */
namespace Drupal\Tests\search_by_page\Functional;

use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\BrowserTestBase;

/**
 * Functionality tests for Search by Page Nodes.
 */
class SearchByPageNodesTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public $noprivuser;
  public $privuser;
  public $privnodeid;
  public $nodetitle1;
  public $nodeid1;
  public $exclnodetitle;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Nodes Tests'),
      'description' => t('Test functionality of search_by_page_nodes.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_nodes',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_nodes', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();

    // Set up super-user and other users with different privileges.
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo1),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
      'bypass node access',
    ));
    $this->noprivuser = $this
      ->drupalCreateUser(array(
      'access content',
      'search content',
      $this
        ->searchPerm($this->envinfo1),
    ));
    $this->privuser = $this
      ->drupalCreateUser(array(
      'access content',
      'search content',
      $this
        ->searchPerm($this->envinfo1),
      'view test private content',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up so "search_by_page_indexed" nodes are searchable, and "search_by_page_hidden" nodes are
    // not. Index them with the priveleged user.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_types_indexed[search_by_page_indexed]' => TRUE,
      'search_by_page_nodes_display_type' => 'excerpts',
      'search_by_page_nodes_role' => $this
        ->getNewRoleID($this->privuser),
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE => array(
          array(
            'value' => 'double trouble',
          ),
        ),
      ),
      'type' => 'page',
      'language' => self::LANGUAGE_NONE,
      'title' => 'first page',
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('trouble', "Node page could be found");

    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
            'summary' => 'blah',
          ),
        ),
      ),
      'title' => 'blah',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('blah', "Node page could be found");
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('blah', "Node page could be found");
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('blah', "Node page could be found");

    // Create some content that we'll look for - make sure it has the
    // word "walk" in it.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I walk through the streets, looking around for trouble',
            'summary' => 'I walk on a path, where it is quiet',
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'test_private' => FALSE,
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this->nodetitle1 = $node->title;
    $this->nodeid1 = $node->nid;
    $this
      ->assertFalse($node->test_private, "Created a non-private node");
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('streets', "Streets appears on first node page");
    $this
      ->assertNoText('north', "North does not appear on first node page");
    $this
      ->assertNoText('people', "People does not appear on first node page");

    // Create some "private" content, also with the word "walk" in it
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I can walk really far, maybe even to the north pole.',
            'summary' => 'I will walk to the south pole.',
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'test_private' => TRUE,
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this->privnodeid = $node->nid;
    $this
      ->assertTrue($node->test_private, "Created a private node");
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertNoText('streets', "Streets does not appear on second node page");
    $this
      ->assertText('north', "North appears on second node page");
    $this
      ->assertNoText('people', "People does not appear on second node page");

    // Create some non-indexed content, also with the word "walk" in it
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'When I walk, I try not to run into people.',
          ),
        ),
      ),
      'type' => 'search_by_page_hidden',
      'test_private' => FALSE,
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertNoText('streets', "Streets does not appear on third node page");
    $this
      ->assertNoText('north', "North does not appear on third node page");
    $this
      ->assertText('people', "People appears on third node page");

    // Create some content with excluded tags. Make sure to use Full HTML
    // input format.
    $excluded = search_by_page_setting_get('exclude_tags', $this->envid1, '');
    $this
      ->assertEqual($excluded, 'tagtoexclude', 'Correct tag is marked excluded');

    // Find the Full HTML format.
    $filters = filter_formats();
    $filternum = 0;
    foreach ($filters as $filter) {
      if ($filter->name == 'Full HTML') {
        $filternum = $filter->format;
        break;
      }
    }
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'This content <tagtoexclude>only has walk gobble in the excluded tag</tagtoexclude> and then gobble appears after it.',
            'summary' => 'Short version of this content',
            'format' => $filternum,
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'test_private' => FALSE,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this->exclnodetitle = $node->title;
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('walk', "walk appears on the excluded node page");
    $this
      ->assertText('gobble', "gobble appears on the excluded node page");
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that the content of the designated types only can be searched.
   */
  function testSearchTypes() {
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalLogin($this->privuser);
    $this
      ->drupalGet('node/' . $this->nodeid1);
    $this
      ->assertText('streets', "Priv user can see first node page");

    // Search for the word 'walk'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));

    // Verify that the "indexed" pages are searchable, and the non-indexed not.
    $this
      ->assertText('walk', "Walk appears in search results for walk");
    $this
      ->assertText('streets', "First page was found");
    $this
      ->assertText('north', "Second page was found");
    $this
      ->assertNoText('people', "Non-searchable content was not found");
    $this
      ->assertLink($this->nodetitle1, 0, "Link to first node is found");

    // Try searching for something that isn't there
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'blsdfsdfsadf',
    ), t('Search pages'));
    $this
      ->assertNoText('walk', "Walk does not appear in search results for gibberish");
    $this
      ->assertText('no results', "No results appears on search results page for gibberish");
  }

  /**
   * Tests that access permissions are obeyed.
   */
  function testSearchAccess() {

    // Log in as a non-priv user
    $this
      ->drupalLogin($this->noprivuser);

    // Some node access tests...
    // Note: The following access tests are low-level, but they were failing at
    // one time, until I rebuilt node access permissions in startup. So, leave
    // them here -- at the least they are useful for debugging if other tests
    // start failing later!
    $node = \Drupal\node\Entity\Node::load($this->privnodeid, NULL, TRUE);
    $this
      ->assertTrue($node->test_private, "Node is marked private");
    $this
      ->assertTrue($node->status, "Node is published");
    $grants = node_access_grants('view', $this->noprivuser);
    $this
      ->assertFalse(isset($grants['search_by_page_test']), "Non-priv user does not have sbp test grant");
    $grants = node_access_grants('view', $this->privuser);
    $this
      ->assertTrue($grants['search_by_page_test'], "Priv user does have sbp test grant");
    $this
      ->assertFalse(user_access('administer nodes', $this->noprivuser), "Non priv user does not have administer nodes permission");
    $this
      ->assertTrue(user_access('access content', $this->noprivuser), "Non priv user does have access content permission");
    $this
      ->assertFalse(node_access("view", $node, $this->noprivuser), "Node access is restricted for non-priv user");
    $this
      ->assertTrue(node_access("view", $node, $this->privuser), "Node access is not restricted for priv user");

    // Verify we cannot see the private page
    $this
      ->drupalGet('node/' . $this->privnodeid);
    $this
      ->assertNoText('walk', "Cannot see private page");

    // Run  a search for "walk"
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));

    // Should be able to see the public page, but not the private page.
    $this
      ->assertText('walk', "Walk appears in search results");
    $this
      ->assertText('streets', "Streets appears in search results");
    $this
      ->assertNoText('north', "North does not appear in search results for nonpriv user");
  }

  /**
   * Tests that the teaser display option works.
   */
  function testTeaserDisplay() {
    $this
      ->drupalLogin($this->superuser);

    // Set display output to display teasers
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_display_type' => 'teasers',
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Search for the word 'walk'
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));

    // Verify that teasers are displayed instead of excerpts
    $this
      ->assertText('walk', "Walk appears in search results for walk");
    $this
      ->assertNoText('streets', "First page excerpt not displayed");
    $this
      ->assertText('path', "First page teaser displayed");

    // Set display output to display excerpts
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_display_type' => 'excerpts',
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Search for the word 'walk'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));

    // Verify that excerpts are displayed instead of teasers
    $this
      ->assertText('walk', "Walk appears in search results for walk");
    $this
      ->assertText('streets', "First page excerpt displayed");
    $this
      ->assertNoText('path', "First page teaser not displayed");
  }

  /**
   * Tests that the excluded tag is excluded from search.
   */
  function testSearchExclude() {

    // Make sure to use environment 1, since that has the exclude tags.
    $search_path = $this->envinfo1['page_path'];
    $excluded = search_by_page_setting_get('exclude_tags', $this->envid1, '');
    $this
      ->assertEqual($excluded, 'tagtoexclude', 'Correct tag is marked excluded');
    $this
      ->drupalLogin($this->privuser);

    // Search for the word 'walk'.
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));

    // Verify that the "excluded" page doesn't show up.
    $this
      ->assertNoText($this->exclnodetitle, "Excluded node was not found in search for walk");
    $this
      ->assertNoText('gobble', "Gobble is not shown on the page in search for walk");

    // Search for the word 'gobble'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'gobble',
    ), t('Search pages'));

    // Verify that the "excluded" page does show up, but not with the word walk showing.
    $this
      ->assertText($this->exclnodetitle, "Excluded node was found in search for gobble");
    $this
      ->assertNoText('walk', "Walk is not shown on the page");
  }

}

/**
 * Tests when nodes are reindexed.
 */
class SearchByPageNodesReindexTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public $nodes;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Nodes Reindex'),
      'description' => t('Test reindexing in search_by_page_nodes.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_nodes',
        'dblog',
        'search_by_page',
        'comment',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_nodes', 'search_by_page_test', 'dblog', 'comment');
    $this
      ->setUpEnvironments();

    // Set up super-user.
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo1),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
      'access comments',
      'skip comment approval',
      'administer comments',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up so "search_by_page_indexed" nodes are searchable.
    // Index them with the super-user.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_types_indexed[search_by_page_indexed]' => TRUE,
      'search_by_page_nodes_display_type' => 'excerpts',
      'search_by_page_nodes_role' => $this
        ->getNewRoleID($this->superuser),
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Create 5 nodes with the same content.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'language' => self::LANGUAGE_NONE_,
      'title' => 'blah',
      'test_private' => FALSE,
    );
    $this->nodes = array();
    for ($i = 0; $i < 5; $i++) {
      $this->nodes[] = $this
        ->drupalCreateNode($info);
    }

    // Run cron to index these nodes.
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalLogout();
  }

  /**
   * Tests that node reindexing happens in the right order on node update.
   */
  function testReindexingOnUpdate() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo1['page_path'];

    // Set to never reindex automatically.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_min_time' => 0,
      'search_by_page_nodes_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 this test several times...
    for ($j = 0; $j < 10; $j++) {

      // Choose a random title and index.
      $newtitle = $this
        ->randomName();
      $i = rand(0, count($this->nodes) - 1);
      $this->nodes[$i]->title = $newtitle;
      node_save($this->nodes[$i]);

      // Verify new title is not searchable.
      $this
        ->drupalPostForm($search_path, array(
        'keys' => $newtitle,
      ), t('Search pages'));
      $this
        ->assertNoText($newtitle, 'New title not found in search');

      // Run cron - should reindex just this node.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Verify new title is searchable.
      $this
        ->drupalPostForm($search_path, array(
        'keys' => $newtitle,
      ), t('Search pages'));
      $this
        ->assertText($newtitle, 'New title found in search after reindex');
    }
  }

  /**
   * Tests that node reindexing happens in the right order on comment update.
   */
  function testReindexingOnComment() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo1['page_path'];

    // Turn on comments for 'search_by_page_indexed' content type, and put the
    // comment form on the node.
    $this
      ->drupalPostForm('admin/structure/types/manage/sbp-indexed', array(
      'comment' => '2',
      'comment_form_location' => '1',
      'comment_preview' => '0',
    ), 'Save content type');

    // Set to never reindex automatically.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_min_time' => 0,
      'search_by_page_nodes_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 this test several times...
    for ($j = 0; $j < 10; $j++) {

      // Enter a random comment.
      $newsubj = $this
        ->randomName();
      $newbody = $this
        ->randomName();
      $i = rand(0, count($this->nodes) - 1);
      $this
        ->drupalPostForm('node/' . $this->nodes[$i]->nid, array(
        'subject' => $newsubj,
        'comment_body[und][0][value]' => $newbody,
      ), 'Save');

      // Verify new comment subject is not searchable.
      $this
        ->drupalPostForm($search_path, array(
        'keys' => $newsubj,
      ), t('Search pages'));
      $this
        ->assertNoText($newsubj, 'New comment not found in search');

      // Run cron - should reindex just this node.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Verify new title is searchable.
      $this
        ->drupalPostForm($search_path, array(
        'keys' => $newsubj,
      ), t('Search pages'));
      $this
        ->assertText($newsubj, 'New comment found in search after reindex');
    }
  }

  /**
   * Tests that cycling through content reindexing happens correctly.
   */
  function testReindexingAutomatic() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo1['page_path'];

    // Set to reindex automatically on normal cycle.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_min_time' => 1,
      'search_by_page_nodes_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);

    // Figure out the current index times of the nodes.
    $orig = $this
      ->getIndexTimes();

    // In a loop: run cron, and verify each time that the oldest item
    // (or one of them, if there was a tie) was reindexed.
    for ($i = 0; $i < 10; $i++) {

      // Run cron - should reindex just one node.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Figure out the current index times of the nodes.
      $new = $this
        ->getIndexTimes();

      // Verify that only one was indexed, and it was the oldest one.
      $this
        ->verifyIndexCycling($orig, $new);
      $orig = $new;
    }
  }

}

/**
 * Tests that no extra Search by Page indexing users are created.
 */
class SearchByPageExtraUsersTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public $nodes;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Extra Users'),
      'description' => t('Test that only the necessary users are created.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_nodes',
        'dblog',
        'search_by_page',
        'comment',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_nodes', 'search_by_page_test', 'dblog', 'comment');
    $this
      ->setUpEnvironments();

    // Set up super-user.
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo1),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
      'access comments',
      'skip comment approval',
      'administer comments',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up so "search_by_page_indexed" nodes are searchable.
    // Index them with the super-user.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_types_indexed[search_by_page_indexed]' => TRUE,
      'search_by_page_nodes_display_type' => 'excerpts',
      'search_by_page_nodes_role' => $this
        ->getNewRoleID($this->superuser),
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Create 5 nodes with the same content.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'language' => self::LANGUAGE_NONE_,
      'title' => 'blah',
      'test_private' => FALSE,
    );
    $this->nodes = array();
    for ($i = 0; $i < 5; $i++) {
      $this->nodes[] = $this
        ->drupalCreateNode($info);
    }

    // Run cron to index these nodes.
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalLogout();
  }

  /**
   * Tests that no new users are created when reindexing.
   */
  function testReindexingUsers() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo1['page_path'];

    // Count the number of users.
    $num_before = $this
      ->howManyUsers();

    // Set to reindex automatically on normal cycle.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_nodes_min_time' => 1,
      'search_by_page_nodes_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);

    // Figure out the current index times of the nodes.
    $orig = $this
      ->getIndexTimes();

    // In a loop: run cron, and verify each time that something was reindexed.
    for ($i = 0; $i < 10; $i++) {

      // Run cron - should reindex just one node.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Figure out the current index times of the nodes.
      $new = $this
        ->getIndexTimes();

      // Verify that only one was indexed, and it was the oldest one.
      $this
        ->verifyIndexCycling($orig, $new);
      $orig = $new;
    }

    // Now see how many users there are.
    $num_after = $this
      ->howManyUsers();
    $this
      ->assertEqual($num_before, $num_after, "Number of users before ({$num_before}) and after ({$num_after}) is the same");
  }

  // Counts how many user accounts there are.
  function howManyUsers() {
    $query = \Drupal::database()
      ->select('users', 'u');
    $query
      ->addExpression('count(*)');
    return $query
      ->execute()
      ->fetchField();
  }

}

/**
 * Functionality tests for Search by Page Users.
 */
class SearchByPageUsersTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $usera;
  public $userb;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Users Tests'),
      'description' => t('Test functionality of search_by_page_users.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_users',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_users', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();

    // Create some random users that won't be found
    $u = $this
      ->drupalCreateUser();
    $u = $this
      ->drupalCreateUser();
    $u = $this
      ->drupalCreateUser();
    $u = $this
      ->drupalCreateUser();

    // Set up two users for searching - just make sure they have different
    // permissions, so they get different roles.
    $this->usera = $this
      ->drupalCreateUser(array(
      'access content',
    ));
    $this->userb = $this
      ->drupalCreateUser(array(
      'access content',
      $this
        ->searchPerm($this->envinfo2),
    ));

    // Set up a user that can see user profiles, to use when indexing.
    $this->indexuser = $this
      ->drupalCreateUser(array(
      'access user profiles',
    ));

    // Verify that this user can see user profiles A and B
    $this
      ->drupalLogin($this->indexuser);
    $this
      ->drupalGet('user/' . $this->usera->uid);
    $this
      ->assertText($this->usera->name, "Indexing user can see user A page");
    $this
      ->drupalGet('user/' . $this->userb->uid);
    $this
      ->assertText($this->userb->name, "Indexing user can see user B page");

    // Set up an admin user and login as that user
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo2),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access user profiles',
      'access site reports',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up so user a is indexed and user b is not.
    // First have to find the role ID that SimpleTest set up.
    // Relies on the role name having the word 'test' in it somewhere.
    $rid = $this
      ->getNewRoleID($this->usera);
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
      'search_by_page_users_roles_indexed[' . $rid . ']' => TRUE,
      'search_by_page_users_role' => $this
        ->getNewRoleID($this->indexuser),
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that users can be searched.
   */
  function testSearchUsers() {

    // Search for user a
    $search_path = $this->envinfo2['page_path'];
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => $this->usera->name,
    ), t('Search pages'));

    // Verify we found user a
    $this
      ->assertText($this->usera->name, "User a appears in search results");
    $this
      ->assertLink($this->usera->name, 0, "Link to user a appears in search results");

    // Search for user b
    $this
      ->drupalPostForm($search_path, array(
      'keys' => $this->userb->name,
    ), t('Search pages'));

    // Verify we did not find user b, and have "no results"
    $this
      ->assertNoText($this->userb->name, "User b does not appear in search results");
    $this
      ->assertText('no results', "No results appears on search results page for user b");
  }

}

/**
 * Tests when users are reindexed.
 */
class SearchByPageUsersReindexTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public $users;
  public $indexuser;
  public $roles;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Users Reindex'),
      'description' => t('Test reindexing in search_by_page_users.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_users',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_users', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();

    // Set up five users for searching
    for ($i = 0; $i < 5; $i++) {
      $acct = $this
        ->drupalCreateUser(array(
        'access content',
      ));
      $this->users[] = $acct;
      $this->roles[$this
        ->getNewRoleID($acct)] = 1;
    }

    // Set up a user that can see user profiles, to use when indexing.
    $this->indexuser = $this
      ->drupalCreateUser(array(
      'access user profiles',
    ));
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo2),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access user profiles',
      'access site reports',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Make sure all our test users are indexed.
    $edit = array(
      'search_by_page_users_role' => $this
        ->getNewRoleID($this->indexuser),
      'button_label' => t('Search pages'),
    );
    foreach (array_keys($this->roles) as $rid) {
      $edit['search_by_page_users_roles_indexed[' . $rid . ']'] = TRUE;
    }
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, $edit, 'Save configuration');
    drupal_flush_all_caches();
    variable_initialize();
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalLogout();
  }

  /**
   * Tests that user reindexing happens in the right order on user update.
   */
  function testReindexingOnUpdate() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo2['page_path'];

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

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

    // Run this test several times...
    for ($j = 0; $j < 10; $j++) {

      // Choose a random user name.
      $newname = $this
        ->randomName();
      $i = rand(0, count($this->users) - 1);
      user_save($this->users[$i], array(
        'name' => $newname,
      ));

      // Verify user can be loaded and has right name.
      $acct = Drupal\user\Entity\User::load($this->users[$i]->uid);
      $this
        ->assertEqual($acct->name, $newname, "New user name was saved");

      // Verify new name shows on user page
      $this
        ->drupalGet('user/' . $this->users[$i]->uid);
      $this
        ->assertText($newname, 'New user name appears on user page');

      // Verify new name is not searchable.
      $this
        ->drupalPostForm($search_path, array(
        'keys' => $newname,
      ), t('Search pages'));
      $this
        ->assertNoText($newname, 'New user name not found in search');

      // Run cron - should reindex just this user.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);
      $this
        ->drupalGet('admin/reports/dblog');
      $this
        ->assertText(t('Cron run completed'), 'Log shows cron run completed');

      // Verify new name is searchable.
      $this
        ->drupalPostForm($search_path, array(
        'keys' => $newname,
      ), t('Search pages'));
      $this
        ->assertText($newname, 'New user name found in search after reindex');
    }
  }

  /**
   * Tests that cycling through user reindexing happens correctly.
   */
  function testReindexingAutomatic() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo2['page_path'];

    // Set to reindex automatically on normal cycle.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
      'search_by_page_users_min_time' => 1,
      'search_by_page_users_max_time' => 0,
    ), 'Save configuration');
    drupal_flush_all_caches();
    variable_initialize();

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

    // Figure out the current index times of the users.
    $orig = $this
      ->getIndexTimes();

    // In a loop: run cron, and verify each time that the oldest item
    // (or one of them, if there was a tie) was reindexed.
    for ($i = 0; $i < 10; $i++) {

      // Run cron - should reindex just one node.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Figure out the current index times of the nodes.
      $new = $this
        ->getIndexTimes();

      // Verify that only one was indexed, and it was the oldest one.
      $this
        ->verifyIndexCycling($orig, $new);
      $orig = $new;
    }
  }

}

/**
 * Functionality tests for Search by Page Paths.
 */
class SearchByPagePathsTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $noprivuser;
  public $privuser;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Paths Tests'),
      'description' => t('Test functionality of search_by_page_paths.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      $this
        ->searchPerm($this->envinfo1),
      'access administration pages',
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
    ));
    $this->noprivuser = $this
      ->drupalCreateUser(array(
      $this
        ->searchPerm($this->envinfo1),
      'access content',
      'search content',
    ));
    $this->privuser = $this
      ->drupalCreateUser(array(
      'access content',
      'search content',
      $this
        ->searchPerm($this->envinfo1),
      'view test private content',
    ));

    // Verify that the priv user can see the pages.
    $this
      ->drupalLogin($this->privuser);
    $this
      ->drupalGet('search_by_page_test_priv_page');
    $this
      ->assertText('Trees', "Private page can be seen.");
    $this
      ->drupalGet('search_by_page_test_pub_page');
    $this
      ->assertText('Trees', "Public page can be seen.");
    $this
      ->drupalGet('search_by_page_test_another_page');
    $this
      ->assertText('Trees', "Another page can be seen.");

    // Verify that the non-priv user can see only some of the pages.
    $this
      ->drupalLogin($this->noprivuser);
    $this
      ->drupalGet('search_by_page_test_priv_page');
    $this
      ->assertNoText('Trees', "Private page cannot be seen.");
    $this
      ->drupalGet('search_by_page_test_pub_page');
    $this
      ->assertText('Trees', "Public page can be seen.");
    $this
      ->drupalGet('search_by_page_test_another_page');
    $this
      ->assertText('Trees', "Another page can be seen.");
    $this
      ->drupalLogin($this->superuser);

    // Set up indexing of pages.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_priv_page',
      'title' => t("Private page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->privuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_pub_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_another_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => t('Trees: Orange and Lemon'),
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    $this
      ->drupalGet('search_by_page_test_queried_page', array(
      'query' => array(
        'a' => 3,
      ),
    ));
    $this
      ->assertText('three', 'First query page shows query text');
    $this
      ->drupalGet('search_by_page_test_queried_page', array(
      'query' => array(
        'a' => 4,
      ),
    ));
    $this
      ->assertText('four', 'Second query page shows query text');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_queried_page?a=3',
      'title' => t("Public page with query"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_queried_page?a=4',
      'title' => t("Public page with other query"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_exclude_page',
      'title' => t("Public page with excluded text"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that pages can be searched.
   */
  function testSearchPages() {

    // Non-privileged user - should only see the public pages
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalLogin($this->noprivuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'Trees',
    ), t('Search pages'));
    $this
      ->assertText('Trees', "Found Trees on the search results page");
    $this
      ->assertNoText('Pine', "Cannot see private page in search results");
    $this
      ->assertText('Oak', "Can see public page in search results");
    $this
      ->assertText('Orange', "Snippet override is shown");
    $this
      ->assertNoText('Apple', "Overridden excerpt is not shown");
    $this
      ->assertText('Walnut', "Page with query is shown");
    $this
      ->assertText('three', "Page with query shows query text");
    $this
      ->assertText('four', "Second page with query shows query text");

    // Privileged user - should see the public and private pages
    $this
      ->drupalLogin($this->privuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'Trees',
    ), t('Search pages'));
    $this
      ->assertText('Trees', "Found Trees on the search results page");
    $this
      ->assertText('Pine', "Can see private page in search results");
    $this
      ->assertText('Oak', "Can see public page in search results");
    $this
      ->assertText('Orange', "Snippet override is shown");
    $this
      ->assertNoText('Apple', "Overridden excerpt is not shown");
    $this
      ->assertText('Walnut', "Page with query is shown");
    $this
      ->assertText('three', "Page with query shows query text");
    $this
      ->assertText('four', "Second page with query shows query text");
  }

  /**
   * Tests that the text in the excluded tag is actually excluded.
   */
  function testSearchExclusions() {
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalLogin($this->noprivuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'Trees',
    ), t('Search pages'));
    $this
      ->assertNoText('Poplar', "Excluded text is not shown in search for Trees");
    $this
      ->assertText('Cottonwood', "Non-excluded text is shown in search for Trees");
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'Poplar',
    ), t('Search pages'));
    $this
      ->assertNoText('Poplar', "Excluded text is not shown in search for excluded text");
    $this
      ->assertNoText('Trees', "Trees is not shown in search for excluded text");
    $this
      ->assertNoText('Cottonwood', "Non-excluded text is not shown in search for excluded text");
  }

}

/**
 * Functionality test for removing paths from Search by Page Paths.
 */
class SearchByPagePathsRemoveTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $noprivuser;
  public $privuser;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Paths Remove Tests'),
      'description' => t('Test functionality of search_by_page_paths.module when removing paths.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      $this
        ->searchPerm($this->envinfo1),
      'access administration pages',
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
    ));
    $this->noprivuser = $this
      ->drupalCreateUser(array(
      $this
        ->searchPerm($this->envinfo1),
      'access content',
      'search content',
    ));
    $this->privuser = $this
      ->drupalCreateUser(array(
      'access content',
      'search content',
      $this
        ->searchPerm($this->envinfo1),
      'view test private content',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up indexing of pages.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_priv_page',
      'title' => t("Private page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->privuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_pub_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_another_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => t('Trees: Orange and Lemon'),
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');

    // Assuming that the regular tests worked, we know at this point that
    // these pages will have been indexed. So now remove two of the pages
    // from SBP Paths, and we'll test that they can no longer be found.
    $this
      ->drupalLogin($this->superuser);
    $path_del_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/delete/';
    $this
      ->drupalPostForm($path_del_path . '1', array(), t('Confirm'));
    $this
      ->drupalPostForm($path_del_path . '2', array(), t('Confirm'));
  }

  /**
   * Tests that pages can be searched.
   */
  function testSearchPages() {

    // Privileged user - should see the one remaining page.
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalLogin($this->privuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'Trees',
    ), t('Search pages'));
    $this
      ->assertText('Trees', "Found Trees on the search results page");
    $this
      ->assertNoText('Pine', "Cannot see first deleted page in search results");
    $this
      ->assertNoText('Oak', "Cannot see second deleted page in search results");
    $this
      ->assertText('Orange', "Can see third page in search results");
  }

}

/**
 * Tests for Search by Page Paths - searching nodes.
 */
class SearchByPagePathsNodesTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Paths Tests for Nodes'),
      'description' => t('Test node indexing/searching of search_by_page_paths.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo2),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'administer filters',
      'access site reports',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up Search by Page button name
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
            'summary' => 'blah',
          ),
        ),
      ),
      'title' => 'blah',
      'language' => self::LANGUAGE_NONE_,
    );
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);

    // Create some content with different input formats and add to SBP Paths.
    $format_to_use = filter_default_format($this->superuser);
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I walk through the streets, looking around for trouble',
            'summary' => 'I walk on a path, where it is quiet',
            'format' => $format_to_use,
          ),
        ),
      ),
      'type' => 'page',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('walk', 'Walk found on node page');
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid2 . '/paths/add';
    $rid = $this
      ->getNewRoleID($this->superuser);
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'node/' . $node->nid,
      'title' => t($node->title),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $rid,
    ), t('Create new indexed page'));

    // Make sure there's a second text format.
    $this
      ->drupalPostForm('admin/config/content/formats/add', array(
      'name' => $this
        ->randomName(10),
      'roles[' . $rid . ']' => TRUE,
    ), t('Save configuration'));

    // Pick out a new format to use.
    $formats = filter_formats($this->superuser);
    foreach ($formats as $fid => $info) {
      if ($fid != $format_to_use) {
        $format_to_use = $fid;
        break;
      }
    }
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I can walk really far, maybe even to the north pole.',
            'summary' => 'I will walk to the south pole.',
            'format' => $format_to_use,
          ),
        ),
      ),
      'type' => 'page',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('walk', 'Walk found on node page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'node/' . $node->nid,
      'title' => t($node->title),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->superuser),
    ), 'Create new indexed page');
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that pages can be searched.
   */
  function testSearchPages() {

    // Search for 'walk' - should find both pages
    $search_path = $this->envinfo2['page_path'];
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));
    $this
      ->assertText('walk', "Walk appears in search results for walk");
    $this
      ->assertText('streets', "First page was found");
    $this
      ->assertText('north', "Second page was found");
  }

}

/**
 * Tests for Search by Page Paths - searching nodes with aliases.
 */
class SearchByPagePathsAliasTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Paths Tests for Aliases'),
      'description' => t('Tests that path aliases can be used in search_by_page_paths.module'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'path', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($this->envinfo1),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'administer url aliases',
      'create url aliases',
      'access site reports',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up Search by Page button name
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
            'summary' => 'blah',
          ),
        ),
      ),
      'title' => 'blah',
      'language' => self::LANGUAGE_NONE_,
    );
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);

    // Create some content with a path alias and add to SBP Paths.
    // Create the content.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I walk through the streets, looking around for trouble',
            'summary' => 'I walk on a path, where it is quiet',
          ),
        ),
      ),
      'type' => 'page',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);

    // Add a URL alias to the system.
    $alias = $this
      ->randomName();
    $this
      ->drupalPostForm('admin/config/search/path/add', array(
      'source' => 'node/' . $node->nid,
      'alias' => $alias,
    ), t('Save'));

    // Add it to Search by Page Paths.
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => $alias,
      'title' => t($node->title),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->superuser),
    ), 'Create new indexed page');
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that aliased page can be searched.
   */
  function testSearchPage() {

    // Search for 'walk' - should find the page
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'walk',
    ), t('Search pages'));
    $this
      ->assertText('walk', "Walk appears in search results for walk");
    $this
      ->assertText('streets', "Aliased page was found");
  }

}

/**
 * Reindexing test for Search by Page Paths.
 */
class SearchByPagePathsReindexTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $noprivuser;
  public $privuser;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Paths Reindex Tests'),
      'description' => t('Test reindexing of search_by_page_paths.module.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      $this
        ->searchPerm($this->envinfo1),
      'access administration pages',
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
    ));
    $this->noprivuser = $this
      ->drupalCreateUser(array(
      $this
        ->searchPerm($this->envinfo1),
      'access content',
      'search content',
    ));
    $this->privuser = $this
      ->drupalCreateUser(array(
      'access content',
      'search content',
      $this
        ->searchPerm($this->envinfo1),
      'view test private content',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up indexing of pages.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_priv_page',
      'title' => t("Private page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->privuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_pub_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_another_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => t('Trees: Orange and Lemon'),
      'role' => $this
        ->getNewRoleID($this->noprivuser),
    ), 'Create new indexed page');
    drupal_flush_all_caches();
    variable_initialize();
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that cycling through path reindexing happens correctly.
   */
  function testReindexingAutomatic() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo1['page_path'];

    // Set to reindex automatically on normal cycle.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_paths_min_time' => 1,
      'search_by_page_paths_max_time' => 0,
    ), 'Save configuration');
    drupal_flush_all_caches();
    variable_initialize();

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

    // Figure out the current index times of the attachments.
    $orig = $this
      ->getIndexTimes();

    // In a loop: run cron, and verify each time that the oldest item
    // (or one of them, if there was a tie) was reindexed.
    for ($i = 0; $i < 10; $i++) {

      // Run cron - should reindex just one path.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Figure out the current index times of the paths.
      $new = $this
        ->getIndexTimes();

      // Verify that only one was indexed, and it was the oldest one.
      $this
        ->verifyIndexCycling($orig, $new);
      $orig = $new;
    }
  }

}

/**
 * Functionality test 1 for Search by Page Attachments.
 */
class SearchByPageAttachTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public $noprivuser;
  public $fieldid;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Attach Test 1'),
      'description' => t('Test functionality of search_by_page_attach.module. Note that this test assumes you have uploaded the Search Files API module, and it also assumes that the default configuration of the Search Files API module "text helper" will work.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_attach',
        'file',
        'search_files',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page_test', 'search_by_page_attach', 'file', 'search_files', 'dblog', 'search_by_page');
    $this
      ->setUpEnvironments();
    $this
      ->setUpUsers($this->envinfo2);
    $this
      ->setUpTypes();
    $this
      ->makeContent();

    // Set up so "search_by_page_indexed" node attachments are searchable and
    // "search_by_page_hidden" nodes are not. Also make it so only listed files are
    // searchable.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
      'search_by_page_attach_node_types[]' => 'search_by_page_indexed',
      'search_by_page_attach_field_types[]' => 'field_myfile',
      'search_by_page_attach_only_listed' => TRUE,
      'search_by_page_attach_prepend_node_title' => FALSE,
      'search_by_page_attach_title_sep' => " bush ",
      'search_by_page_attach_use_description' => FALSE,
      'search_by_page_attach_role' => $this
        ->getNewRoleID($this->superuser),
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that attachments are searched, and permissions, and display options.
   */
  function testSearchAttach() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo2['page_path'];

    // Search for the word 'flowers'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));

    // Should find just the one listed attachment
    $this
      ->assertText('flowers', "Flowers appears in search results for flowers");
    $this
      ->assertText('daisy', "First attachment was found");
    $this
      ->assertNoText('snapdragon', "Second attachment was not found");
    $this
      ->assertNoText('dahlia', "Third attachment was not found");

    // Non-privileged user
    $this
      ->drupalLogin($this->noprivuser);

    // Search for the word 'flowers'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));

    // Should find no attachments
    $this
      ->assertNoText('flowers', "Flowers does not appear in search results for flowers");
    $this
      ->assertNoText('daisy', "First attachment was not found");
    $this
      ->assertNoText('snapdragon', "Second attachment was not found");
    $this
      ->assertNoText('dahlia', "Third attachment was not found");

    // Display options section...
    $this
      ->drupalLogin($this->superuser);

    // Set display options so it should have attachment file name only
    $this
      ->setDisplayOptions(FALSE, " bush ", FALSE, $this->envid2);

    // Search for the word 'flowers'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));

    // Verify display options
    $this
      ->assertText('attach1.txt', "File name shown");
    $this
      ->assertNoText('foods', "Page title not shown");
    $this
      ->assertNoText('bush', "Separator not shown");
    $this
      ->assertNoText('lonely', "Description not shown");

    // Also verify that the upper-case extension was found.
    $this
      ->assertText('attach5.TXT', "Upper case extension file name shown");

    // Set display options so it should have page title shown with separator
    $this
      ->setDisplayOptions(TRUE, " bush ", FALSE, $this->envid2);

    // Search for the word 'flowers'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));

    // Verify display options
    $this
      ->assertText('attach1.txt', "File name shown");
    $this
      ->assertText('foods', "Page title shown");
    $this
      ->assertText('bush', "Separator shown");
    $this
      ->assertNoText('lonely', "Description not shown");

    // Set display options so it should have description, no page title
    $this
      ->setDisplayOptions(FALSE, " bush ", TRUE, $this->envid2);

    // Search for the word 'flowers'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));

    // Verify display options
    $this
      ->assertNoText('attach1.txt', "File name not shown");
    $this
      ->assertNoText('foods', "Page title not shown");
    $this
      ->assertNoText('bush', "Separator not shown");
    $this
      ->assertText('lonely', "Description shown");
  }

  /**
   * Sets up superuser and low-privilege user.
   */
  public function setUpUsers($env) {
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      'access administration pages',
      $this
        ->searchPerm($env),
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'administer search_files configuration',
      'access site reports',
    ));
    $this->noprivuser = $this
      ->drupalCreateUser(array(
      $this
        ->searchPerm($env),
      'search content',
    ));

    // Clear default permissions for authenticated users.
    \Drupal::database()
      ->delete('role_permission')
      ->condition('rid', AccountInterface::AUTHENTICATED_ROLE)
      ->execute();
    $this
      ->assertFalse(user_access('access content', $this->noprivuser), 'No priv user cannot access content');
    $this
      ->drupalLogin($this->superuser);
  }

  /**
   * Sets up File field for content type.
   */
  public function setUpTypes() {

    // Create the field type.
    $field = array(
      'field_name' => 'field_myfile',
      'type' => 'file',
      'cardinality' => 5,
      'settings' => array(
        'display_field' => 1,
        'display_default' => 1,
      ),
    );
    $field = field_create_field($field);
    $this
      ->assertTrue($field['id'], "Field was given an ID");
    $this->fieldid = $field['id'];

    // Attach it to both search_by_page_indexed and spb_hidden content types.
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => 'node',
      'bundle' => 'search_by_page_indexed',
      'settings' => array(
        'file_extensions' => 'txt not',
        'description_field' => 1,
      ),
      'label' => 'A file',
    );
    field_create_instance($instance);
    $instance['bundle'] = 'search_by_page_hidden';
    field_create_instance($instance);

    // Verify the field was attached.
    $fieldinfo = field_info_field_by_id($field['id']);
    $this
      ->assertTrue($fieldinfo, "Field information could be read");
    $this
      ->assertEqual($fieldinfo['field_name'], 'field_myfile', "Field name is correct");
    $this
      ->assertTrue(count($fieldinfo['bundles']), 'Field is attached to a bundle');
    $this
      ->assertTrue(count($fieldinfo['bundles']['node']), 'Field is attached to a node bundle');
    $this
      ->assertTrue(in_array('search_by_page_indexed', $fieldinfo['bundles']['node']), 'Field is attached to search_by_page_indexed');
    $this
      ->assertTrue(in_array('search_by_page_hidden', $fieldinfo['bundles']['node']), 'Field is attached to search_by_page_hidden');
  }

  /**
   * Makes 2 nodes with attachments, for search testing.
   */
  public function makeContent() {

    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
            'summary' => 'blah',
          ),
        ),
      ),
      'title' => 'blah',
      'language' => self::LANGUAGE_NONE_,
    );
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $info = array(
      'title' => 'Favorite foods',
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I eat pizza every day.',
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->attachFileUpload($node, 'attach2.txt', 0, 'A very happy file');
    $this
      ->attachFileUpload($node, 'attach1.txt', 1, 'A very lonely file');
    $this
      ->attachFileUpload($node, 'attach5.TXT', 1, 'An upper-case extension file');
    $info = array(
      'title' => 'Favorite cars',
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I drive a hatchback.',
          ),
        ),
      ),
      'type' => 'search_by_page_hidden',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->attachFileUpload($node, 'attach3.txt', 1, 'A very pretty file');

    // Configure Search Files API helpers
    $this
      ->drupalPostForm('admin/config/search/search_files/helpers/autodetect', array(), t('Autodetect'));
    $this
      ->drupalGet('admin/config/search/search_files/helpers/list');
    $this
      ->assertText('Text files', 'Text file helper for Search Files set up (without this, no file searching is possible)');
    $this
      ->assertText('Good', 'Safe mode message found (without this, no file searching is possible)');
  }

  /**
   * Attaches a file to a node, using File field.
   *
   * @param $node
   *   Node object to attach to.
   * @param $filename
   *   File name, not including path (assumed to be in test directory).
   * @param $listed
   *   Whether to mark as displayed (1) or not (0).
   * @param $desc
   *   Description to put on the file upload.
   */
  public function attachFileUpload($node, $filename, $listed, $desc) {
    $path = realpath(drupal_get_path('module', 'search_by_page') . "/tests/" . $filename);

    // Add the file to the files table and file downloads directory.
    $file = file_get_contents($path);
    $file = file_save_data($file, 'public://' . $filename);
    $this
      ->assertTrue($file->fid, "File record was created");
    $info = array(
      'display' => $listed,
      'description' => $desc,
      'fid' => $file->fid,
    );

    // Load the node, add the file to it, and save.
    $node = \Drupal\node\Entity\Node::load($node->nid, NULL, TRUE);
    $done = FALSE;
    foreach ($node->field_myfile as $lang => $langstuff) {
      $node->field_myfile[$lang][] = $info;
      $done = TRUE;
      break;
    }
    if (!$done) {
      $node->field_myfile['und'] = array(
        $info,
      );
    }
    node_save($node);

    // Verify the file is attached
    $node = \Drupal\node\Entity\Node::load($node->nid, NULL, TRUE);
    $found = FALSE;
    foreach ($node->field_myfile as $lang => $langstuff) {
      foreach ($langstuff as $stuff) {
        if ($stuff['fid'] == $file->fid) {
          $found = TRUE;
          break;
        }
      }
    }
    $this
      ->assertTrue($found, "File was actually attached");
  }

  /**
   * Sets display options for Search by Page Attachments.
   *
   * @param $pre_title
   *   TRUE to prepend node title, FALSE to not do so.
   * @param $sep
   *   Separator to use between node title and file name.
   * @param $use_desc
   *   TRUE to replace file name with description, FALSE to not do so.
   * @param $env
   *   ID of Search by Page environment to use.
   */
  function setDisplayOptions($pre_title, $sep, $use_desc, $env) {
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $env, array(
      'search_by_page_attach_prepend_node_title' => $pre_title,
      'search_by_page_attach_title_sep' => $sep,
      'search_by_page_attach_use_description' => $use_desc,
    ), 'Save configuration');

    // Reset variables, because Drupal doesn't realize they could be
    // reset within the same PHP session... sigh.
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Verify settings
    $this
      ->assertEqual(search_by_page_setting_get('search_by_page_attach_prepend_node_title', $env, 0), $pre_title, 'Prepend node title is set correctly');
    $this
      ->assertEqual(search_by_page_setting_get('search_by_page_attach_use_description', $env, 0), $use_desc, 'Use description is set correctly');
    $this
      ->assertEqual(search_by_page_setting_get('search_by_page_attach_title_sep', $env, "/"), $sep, 'Separator is set correctly');
  }

}

/**
 * Functionality test 2 for Search by Page Attachments.
 */
class SearchbyPageAttach2Test extends SearchByPageAttachTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Attach Test 2'),
      'description' => t('Test functionality of search_by_page_attach.module. Note that this test assumes you have uploaded the Search Files API module, and it also assumes that the default configuration of the Search Files API module "text helper" will work.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_attach',
        'file',
        'search_files',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    BrowserTestBase::setUp('search', 'search_by_page_test', 'search_by_page_attach', 'file', 'search_files', 'dblog', 'search_by_page');
    $this
      ->setUpEnvironments();
    $this
      ->setUpUsers($this->envinfo1);
    $this
      ->setUpTypes();
    $this
      ->makeContent();

    // Set up so "search_by_page_indexed" node attachments are searchable and
    // "search_by_page_hidden" attachments are not. Also make it so both listed and
    // unlisted files are searchable (functionality test 1 did only listed
    // files).
    // NOTE: When posting to a page with checkboxes, it is VITAL to pass in
    // FALSE rather than 0 for the value, if you want to unset the checkbox!
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'search_by_page_attach_node_types[]' => 'search_by_page_indexed',
      'search_by_page_attach_field_types[]' => 'field_myfile',
      'search_by_page_attach_only_listed' => FALSE,
      'search_by_page_attach_prepend_node_title' => FALSE,
      'search_by_page_attach_title_sep' => " bush ",
      'search_by_page_attach_use_description' => FALSE,
      'search_by_page_attach_role' => $this
        ->getNewRoleID($this->superuser),
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->assertEqual(search_by_page_setting_get('search_by_page_attach_only_listed', $this->envid1, 0), 0, 'Search only listed files is set to FALSE');
    $this
      ->doCronRun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
  }

  /**
   * Tests that attachments are searched, and permissions.
   */
  function testSearchAttach() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo1['page_path'];

    // Search for the word 'flowers'
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));

    // Should find both listed attachments on the first node
    $this
      ->assertText('flowers', "Flowers appears in search results for flowers");
    $this
      ->assertText('daisy', "First attachment was found");
    $this
      ->assertText('snapdragon', "Second attachment was found");
    $this
      ->assertNoText('dahlia', "Third attachment was not found");
  }

}

/**
 * Tests attachments that cannot be read.
 */
class SearchbyPageAttachNotReadable extends SearchByPageAttachTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Attach Not Readable'),
      'description' => t('Test functionality of search_by_page_attach.module with files that cannot be read. Note that this test assumes you have uploaded the Search Files API module.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_attach',
        'file',
        'search_files',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    BrowserTestBase::setUp('search', 'search_by_page_test', 'search_by_page_attach', 'file', 'search_files', 'dblog', 'search_by_page');
    $this
      ->setUpEnvironments();
    $this
      ->setUpUsers($this->envinfo2);
    $this
      ->setUpTypes();
    $this
      ->makeContent();

    // Set up so "search_by_page_indexed" node attachments are searchable, and that our
    // file field is searchable.
    // NOTE: When posting to a page with checkboxes, it is VITAL to pass in
    // FALSE rather than 0 for the value, if you want to unset the checkbox!
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
      'search_by_page_attach_node_types[]' => 'search_by_page_indexed',
      'search_by_page_attach_field_types[]' => 'field_myfile',
      'search_by_page_attach_only_listed' => FALSE,
      'search_by_page_attach_prepend_node_title' => TRUE,
      'search_by_page_attach_title_sep' => " bush ",
      'search_by_page_attach_use_description' => TRUE,
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->assertEqual(search_by_page_setting_get('search_by_page_attach_only_listed', $this->envid2, 0), 0, 'Search only listed files is set to FALSE');
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalLogout();
  }

  /**
   * Makes a node with an unreadable attachment, for search testing.
   *
   * This is an override of the inherited method from SearchByPageAttachTest,
   * which attaches one file that cannot be read (no helpers).
   */
  public function makeContent() {

    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
            'summary' => 'blah',
          ),
        ),
      ),
      'title' => 'blah',
      'language' => self::LANGUAGE_NONE_,
    );
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $info = array(
      'title' => 'Favorite foods',
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I eat pizza every day.',
          ),
        ),
      ),
      'type' => 'search_by_page_indexed',
      'language' => self::LANGUAGE_NONE_,
    );
    $node = $this
      ->drupalCreateNode($info);
    $this
      ->attachFileUpload($node, 'attach4.not', TRUE, 'A very lonely file');

    // Configure Search Files API helpers
    $this
      ->drupalPostForm('admin/config/search/search_files/helpers/autodetect', array(), t('Autodetect'));
    $this
      ->drupalGet('admin/config/search/search_files/helpers/list');
    $this
      ->assertText('Text files', 'Text file helper for Search Files set up (without this, no file searching is possible)');
    $this
      ->assertText('Good', 'Safe mode message found (without this, no file searching is possible)');
  }

  /**
   * Tests that attachments with unreadable text are handled OK.
   *
   * They shouldn't appear in search results, but they shouldn't screw up
   * search indexing.
   */
  function testSearchAttach() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo2['page_path'];

    // Search for the word 'flowers' -- should find nothing.
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'flowers',
    ), t('Search pages'));
    $this
      ->assertNoText('flowers', "Flowers does not appear in search results for flowers");

    // Search indexing should show 100%.
    $this
      ->drupalGet('admin/config/search/settings');
    $this
      ->assertText('100% of the site has been indexed.');
    $this
      ->assertText('There are 0 items left to index.');
  }

}

/**
 * Tests attachment reindexing.
 */
class SearchbyPageAttachReindexTest extends SearchByPageAttachTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $nodes;
  public $descs;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Attach Reindex'),
      'description' => t('Test reindexing in search_by_page_attach.module. Note that this test assumes you have uploaded the Search Files API module.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_attach',
        'file',
        'search_files',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    BrowserTestBase::setUp('search', 'search_by_page', 'search_by_page_attach', 'file', 'search_files', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this
      ->setUpUsers($this->envinfo2);
    $this
      ->setUpTypes();
    $this
      ->makeContent();

    // Set up so "search_by_page_indexed" node attachments are searchable, and that our
    // file field is searchable.
    // NOTE: When posting to a page with checkboxes, it is VITAL to pass in
    // FALSE rather than 0 for the value, if you want to unset the checkbox!
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid2, array(
      'search_by_page_attach_node_types[]' => 'search_by_page_indexed',
      'search_by_page_attach_field_types[]' => 'field_myfile',
      'search_by_page_attach_only_listed' => FALSE,
      'search_by_page_attach_prepend_node_title' => TRUE,
      'search_by_page_attach_title_sep' => " bush ",
      'search_by_page_attach_use_description' => TRUE,
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalLogout();
  }

  /**
   * Makes several nodes with attachments, for search testing.
   */
  public function makeContent() {

    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'blah',
            'summary' => 'blah',
          ),
        ),
      ),
      'title' => 'blah',
      'language' => self::LANGUAGE_NONE_,
    );
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);
    $this
      ->drupalCreateNode($info);

    // Create a bunch of indexed nodes, with random titles.
    // Throw in a few nodes that won't match our tests.
    $info = array(
      'body' => array(
        self::LANGUAGE_NONE_ => array(
          array(
            'value' => 'I eat pizza every day.',
            'summary' => 'blah',
          ),
        ),
      ),
      'language' => self::LANGUAGE_NONE_,
      'type' => 'search_by_page_indexed',
    );
    $this->nodes = array();
    for ($i = 0; $i < 5; $i++) {
      $this->nodes[] = $this
        ->drupalCreateNode($info);
      $this->descs[] = $this
        ->randomName();
      $this
        ->attachFileUpload($this->nodes[$i], 'attach1.txt', TRUE, $this->descs[$i]);
    }

    // Configure Search Files API helpers
    $this
      ->drupalPostForm('admin/config/search/search_files/helpers/autodetect', array(), t('Autodetect'));
    $this
      ->drupalGet('admin/config/search/search_files/helpers/list');
    $this
      ->assertText('Text files', 'Text file helper for Search Files set up (without this, no file searching is possible)');
    $this
      ->assertText('Good', 'Safe mode message found (without this, no file searching is possible)');
  }

  /**
   * 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.
   */
  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");
    }
  }

  /**
   * Tests that cycling through attachment reindexing happens correctly.
   */
  function testReindexingAutomatic() {
    $this
      ->drupalLogin($this->superuser);
    $search_path = $this->envinfo2['page_path'];

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

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

    // Figure out the current index times of the attachments.
    $orig = $this
      ->getIndexTimes();

    // In a loop: run cron, and verify each time that the oldest item
    // (or one of them, if there was a tie) was reindexed.
    for ($i = 0; $i < 10; $i++) {

      // Run cron - should reindex just one node.
      $this
        ->doCronrun();
      $this
        ->drupalLogin($this->superuser);

      // Figure out the current index times of the nodes.
      $new = $this
        ->getIndexTimes();

      // Verify that only one was indexed, and it was the oldest one.
      $this
        ->verifyIndexCycling($orig, $new);
      $orig = $new;
    }
  }

}

/**
 * Tests that Search by Page doesn't get fooled by page output.
 */
class SearchByPageOutputTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Output Tests'),
      'description' => t('Test that page title, active trail, etc. do not affect search results display'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      $this
        ->searchPerm($this->envinfo1),
      'access administration pages',
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up indexing of pages.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_output_page',
      'title' => t("Output page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->superuser),
    ), 'Create new indexed page');
    \Drupal::service('cache.botstrap')
      ->invalidateAll();
    variable_initialize();
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->drupalLogout();
  }

  /**
   * Tests that pages can be searched.
   */
  function testSearchPages() {

    // Search and verify that the word "wrong" doesn't appear on the page.
    // It would appear if some of the bad output got through. Also make
    // sure that the correct output does appear.
    $search_path = $this->envinfo1['page_path'];
    $this
      ->drupalLogin($this->superuser);
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'Trees',
    ), t('Search pages'));
    $this
      ->assertText('Trees', "Found Trees on the search results page");
    $this
      ->assertText('Apple', "Found Apple on the search results page");
    $this
      ->assertNoText('Wrong', "Bad output did not make it to the results page");
  }

}

/**
 * Environment internationalization test.
 */
class SearchByPageIntlTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public $lang_to_use = 'es';
  public $strings_to_test = array(
    'page_title' => 'Translated page title',
    'block_title' => 'Translated block title',
    'field_label' => 'Translated field label',
    'button_label' => 'Translated button label',
    'path_title' => 'Translated path title',
    'path_type' => 'Translated path type',
    'path_snippet' => 'Translated path snippet',
  );
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Internationalization Tests'),
      'description' => t('Test that search environments can be internationalized.'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page',
        'variable',
        'i18n',
        'i18n_string',
        'search_by_page_paths',
        'search_by_page_test',
        'locale',
        'translation',
        'block',
      ),
    );
  }
  public function setUp() {
    parent::setUp('locale', 'translation', 'search', 'search_by_page', 'variable', 'i18n', 'i18n_string', 'search_by_page_paths', 'search_by_page_test', 'block');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer blocks',
      'access administration pages',
      'administer search',
      'administer search by page',
      'search content',
      'access content',
      $this
        ->searchPerm($this->envinfo1),
      'administer site configuration',
      'administer permissions',
      'administer languages',
      'translate interface',
      'translate user-defined strings',
      'translate admin strings',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up the languages, environments, path, and block. Run cron.
    $this
      ->setUpLanguages();
    $this
      ->setUpPath();
    $this
      ->drupalPostForm('admin/structure/block', array(
      'blocks[search_by_page_' . $this->envid1 . '][region]' => 'content',
    ), 'Save blocks');
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();
    drupal_static_reset();

    // Visit the foreign-language search page to trigger the page title getting
    // added to the translation interface.
    $languageManager = new \Drupal\Core\Language\LanguageManager();
    $langs = $languageManager
      ->getLanguages();
    $lang = $langs[$this->lang_to_use];
    $this
      ->drupalGet($this->envinfo1['page_path'], array(
      'language' => $lang,
    ));

    // Run cron to index.
    $this
      ->doCronrun();
    $this
      ->drupalLogin($this->superuser);
  }

  /**
   * Tests that environment settings for main module and paths are translated.
   */
  public function testTranslations() {
    $this
      ->translateEnvironment();

    // Visit the search results page in English, and verify that all the
    // test strings are there.
    $this
      ->drupalPostForm($this->envinfo1['page_path'], array(
      'keys' => 'Trees',
    ), t('Search button 1 label'));
    $this
      ->doLanguageTest('en');

    // Now visit the page in the other language and test.
    $languageManager = new \Drupal\Core\Language\LanguageManager();
    $langs = $languageManager
      ->getLanguages();
    $lang = $langs[$this->lang_to_use];
    $this
      ->drupalPostForm($this->envinfo1['page_path'], array(
      'keys' => 'Trees',
    ), $this->strings_to_test['button_label'], array(
      'language' => $lang,
    ));
    $this
      ->doLanguageTest($this->lang_to_use);
  }

  /**
   * Sets up languages for this test.
   */
  public function setUpLanguages() {
    $lang = $this->lang_to_use;

    // Add the language.
    $this
      ->drupalPostForm('admin/config/regional/language/add', array(
      'langcode' => $lang,
    ), t('Add language'));

    // Set up path prefix language negotiation.
    $this
      ->drupalPostForm('admin/config/regional/language/configure', array(
      'language[enabled][locale-url]' => 1,
    ), t('Save settings'));
  }

  /**
   * Sets up SBP Paths with a path to index.
   */
  function setupPath() {
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this->envinfo1['path_title'] = t('Public page title');
    $this->envinfo1['path_type'] = t('Public page type');
    $this->envinfo1['path_snippet'] = t('Public page snippet');
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_pub_page',
      'title' => $this->envinfo1['path_title'],
      'type' => $this->envinfo1['path_type'],
      'snippet' => $this->envinfo1['path_snippet'],
      'role' => $this
        ->getNewRoleID($this->superuser),
      'languages[en]' => 'en',
      'languages[' . $this->lang_to_use . ']' => $this->lang_to_use,
    ), 'Create new indexed page');
  }

  /**
   * Translates environment 1.
   */
  public function translateEnvironment() {
    $stuff = $this->envinfo1;
    $lang = $this->lang_to_use;
    foreach ($this->strings_to_test as $key => $trans) {

      // Find the string in the translate interface.
      $english = $stuff[$key];
      $this
        ->drupalPostForm('admin/config/regional/translate/translate', array(
        'string' => $english,
      ), 'Filter');
      $this
        ->assertLink('edit', 0, 'Found translation link for ' . $english);
      $this
        ->clickLink('edit');
      $this
        ->drupalPostForm(NULL, array(
        'translations[' . $lang . ']' => $trans,
      ), 'Save translations');
    }

    // Special case for the page title: since it's in hook_menu, it gets
    // into the translate interface as part of the built-in interface. So
    // make sure that version of it is translated too.
    $key = 'page_title';
    $english = $stuff[$key];
    $this
      ->drupalPostForm('admin/config/regional/translate/translate', array(
      'string' => $english,
      'group' => 'default',
    ), 'Filter');
    $this
      ->assertLink('edit', 0, 'Found translation link for ' . $english);
    $this
      ->clickLink('edit');
    $this
      ->drupalPostForm(NULL, array(
      'translations[' . $lang . ']' => $this->strings_to_test[$key],
    ), 'Save translations');
  }

  /**
   * Tests that the right strings are present for the given language.
   */
  public function doLanguageTest($lang) {

    // Figure out which strings we want to look for and verify are not there.
    $good = $this->envinfo1;
    $bad = $this->strings_to_test;
    if ($lang != 'en') {
      $bad = $this->envinfo1;
      $good = $this->strings_to_test;
    }

    // Verify that all the good strings are there and not the bad ones.
    foreach ($this->strings_to_test as $key => $ignore) {
      $this
        ->assertRaw($good[$key], "Text for {$key} in right language is present");
      $this
        ->assertNoRaw($bad[$key], "Text for {$key} in other language is not present");
    }
  }

}

/**
 * Tests that the right theme is used for indexing.
 *
 * This was issue http://drupal.org/node/1598710.
 */
class SearchByPageThemeTest extends SearchByPageTesterTest {
  const LANGUAGE_NONE = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
  public $superuser;
  public static function getInfo() {
    return array(
      'name' => t('Search by Page Theme Test'),
      'description' => t('Test that the right theme is used for indexing'),
      'group' => t('Search by Page'),
      'dependencies' => array(
        'search',
        'search_by_page_test',
        'search_by_page_paths',
        'dblog',
        'search_by_page',
      ),
    );
  }
  public function setUp() {
    parent::setUp('search', 'search_by_page', 'search_by_page_paths', 'search_by_page_test', 'dblog');
    $this
      ->setUpEnvironments();
    $this->superuser = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'access content',
      'administer content types',
      'administer blocks',
      'administer search',
      'search content',
      'administer search by page',
      $this
        ->searchPerm($this->envinfo1),
      'access administration pages',
      'administer site configuration',
      'administer users',
      'administer permissions',
      'view test private content',
      'access site reports',
      'administer themes',
      'view the administration theme',
    ));
    $this
      ->drupalLogin($this->superuser);

    // Set up indexing of pages.
    $this
      ->drupalPostForm('admin/config/search/search_by_page/edit/' . $this->envid1, array(
      'button_label' => t('Search pages'),
    ), 'Save configuration');
    $path_add_path = 'admin/config/search/search_by_page/edit/' . $this->envid1 . '/paths/add';
    $this
      ->drupalPostForm($path_add_path, array(
      'path' => 'search_by_page_test_pub_page',
      'title' => t("Public page"),
      'type' => t('Page'),
      'snippet' => 'yes',
      'role' => $this
        ->getNewRoleID($this->superuser),
    ), 'Create new indexed page');

    // Turn on the administrative theme.
    $this
      ->drupalPostForm('admin/appearance', array(
      'admin_theme' => 'seven',
    ), t('Save configuration'));

    // Add different blocks to the Content region in the admin and regular
    // themes.
    $this
      ->drupalPostForm('admin/structure/block', array(
      'blocks[search_by_page_test_cat][region]' => 'content',
    ), t('Save blocks'));
    $this
      ->drupalPostForm('admin/structure/block/list/seven', array(
      'blocks[search_by_page_test_dog][region]' => 'content',
    ), t('Save blocks'));
    \Drupal::service('cache.bootstrap')
      ->invalidateAll();
    variable_initialize();

    // Run cron by clicking on the Status Report link.
    $this
      ->drupalGet('admin/reports/status');
    $this
      ->clickLink('run cron manually');
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertText(t('Cron run completed'), 'Log shows cron run completed');
    $this
      ->assertText('dog', 'Dog block is showing on admin page');
  }

  /**
   * Tests that the right theme was used to index the page.
   */
  function testTheme() {
    $search_path = $this->envinfo1['page_path'];

    // Search for Cat (block in the right theme) should be successful.
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'cat',
    ), t('Search pages'));
    $this
      ->assertText('Trees', "Found Trees on the search results page");
    $this
      ->assertText('cat', "Found cat block on the search results page");
    $this
      ->assertNoText('dog', "Did not find dog block on the search results page");

    // Search for Dog (block in the wrong theme) should not be successful.
    $this
      ->drupalPostForm($search_path, array(
      'keys' => 'dog',
    ), t('Search pages'));
    $this
      ->assertText('Your search yielded no results', 'Search for dog was unsuccessful');
  }

}

Classes

Namesort descending Description
SearchbyPageAttach2Test Functionality test 2 for Search by Page Attachments.
SearchbyPageAttachNotReadable Tests attachments that cannot be read.
SearchbyPageAttachReindexTest Tests attachment reindexing.
SearchByPageAttachTest Functionality test 1 for Search by Page Attachments.
SearchByPageExtraUsersTest Tests that no extra Search by Page indexing users are created.
SearchByPageIntlTest Environment internationalization test.
SearchByPageNodesReindexTest Tests when nodes are reindexed.
SearchByPageNodesTest Functionality tests for Search by Page Nodes.
SearchByPageOutputTest Tests that Search by Page doesn't get fooled by page output.
SearchByPagePathsAliasTest Tests for Search by Page Paths - searching nodes with aliases.
SearchByPagePathsNodesTest Tests for Search by Page Paths - searching nodes.
SearchByPagePathsReindexTest Reindexing test for Search by Page Paths.
SearchByPagePathsRemoveTest Functionality test for removing paths from Search by Page Paths.
SearchByPagePathsTest Functionality tests for Search by Page Paths.
SearchByPageThemeTest Tests that the right theme is used for indexing.
SearchByPageUsersReindexTest Tests when users are reindexed.
SearchByPageUsersTest Functionality tests for Search by Page Users.