SearchByPageTesterTest.php in Search by Page 8
General Functional Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com
Namespace
Drupal\Tests\search_by_page\FunctionalFile
tests/src/Functional/SearchByPageTesterTest.phpView source
<?php
/**
* @file
* General Functional 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\Component\Utility\Html;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\BrowserTestBase;
/**
* Base class for testing Search by Page.
*
* @group search_by_page
*/
class SearchByPageTesterTest extends BrowserTestBase {
const LANGUAGE_NONE = Language::LANGCODE_NOT_SPECIFIED;
public $envinfo1, $envinfo2, $envid1, $envid2 = [];
public static $modules = [
'search_by_page',
'search_by_page_paths',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this
->setUpEnvironments();
}
/**
* Creates two environments for Search by Page.
*/
public function setUpEnvironments() {
$user = $this
->createUser([
'administer search by page',
'access administration pages',
'administer site configuration',
'administer search',
]);
$this
->drupalLogin($user);
// Content access permissions always need to be rebuilt for some reason.
node_access_rebuild();
// Verify this module is active in Search.
$active = \Drupal::state()
->get('search_active_modules', [
'node',
'user',
]);
$this
->assertTrue(in_array('search_by_page', $active));
// Clear out any existing search environments that may have been defined.
$envs = search_by_page_list_environments();
foreach ($envs as $env) {
\Drupal::moduleHandler()
->invokeAll('search_by_page_delete_environment', $env);
}
// Define two new environments. They should have ID numbers 1 and 2.
$this->envinfo1 = [
'environment_name' => 'env1',
'set_as_default' => 1,
'page_title' => t('Search page 1 title'),
'block_title' => t('Search block 1 title'),
'field_label' => t('Search field 1 label'),
'button_label' => t('Search button 1 label'),
'page_path' => 'search_path_1',
'exclude_tags' => 'tagtoexclude',
];
$this
->drupalPostForm('admin/config/search/search_by_page/add', $this->envinfo1, 'Save configuration');
$this->envid1 = 1;
$this->envinfo2 = [
'environment_name' => 'env2',
'set_as_default' => 2,
'page_title' => t('Search page 2 title'),
'block_title' => t('Search block 2 title'),
'field_label' => t('Search field 2 label'),
'button_label' => t('Search button 2 label'),
'page_path' => 'search_path_2',
];
$this
->drupalPostForm('admin/config/search/search_by_page/add', $this->envinfo2, 'Save configuration');
$this->envid2 = 2;
// Reset the permissions cache.
$this
->checkPermissions([]);
$this
->drupalLogout();
}
/**
* Returns the permission name to search the given search environment.
*/
public function searchPerm($envinfo) {
return 'search page environment ' . $envinfo['environment_name'];
}
/**
* Updates the search index.
*
* Logs current user out, and runs cron to make sure search content
* is indexed.
*/
public function doCronRun() {
// Make sure we are logged out before cron run.
if ($this->loggedInUser) {
$this
->drupalLogout();
}
$this
->drupalGet('cron/' . \Drupal::state()
->get('system.cron_key'));
}
/**
* Returns the unique role ID created by the testing framework.
*/
public function getNewRoleID($account) {
$roles = $account->roles;
$allroles = user_roles();
foreach ($roles as $id => $name) {
if ($id != AccountInterface::ANONYMOUS_ROLE && $id != AccountInterface::AUTHENTICATED_ROLE) {
$this
->assertTrue(isset($allroles[$id]) && $allroles[$id], 'Found a role for new user and it exists');
return $id;
}
}
$this
->fail('Did not find a new role for new user');
return AccountInterface::ANONYMOUS_ROLE;
}
/**
* Helper function: returns an array of last index times for items.
*
* @return
* Array whose keys are the internal Search by Page IDs for the items,
* and whose values are the last time the item was indexed by Search by
* Page.
*/
public function getIndexTimes() {
$times = [];
return \Drupal::database()
->query('SELECT pid, last_index_time FROM {search_by_page_path}')
->fetchAllKeyed();
}
/**
* Helper function: Verifies reindexing.
*
* Tests that one item was reindexed, and it was the oldest item, given
* previous and current output from getIndexTimes(). Assumes, but does
* not explicitly check, that the array keys are the same for the two
* arrays.
*
* @param $prev
* Previous index times array.
* @param $curr
* Current index times array.
*/
public function verifyIndexCycling($prev, $curr) {
$keys = array_keys($prev);
$min_val = min($prev);
$count = 0;
foreach ($keys as $key) {
if ($prev[$key] != $curr[$key]) {
$count++;
$this
->assertEqual($prev[$key], $min_val, "Item indexed was the oldest");
}
}
$this
->assertEqual($count, 1, "Exactly one item was indexed");
}
/**
* Implements hook_search_by_page_excerpt_match().
*
* For testing of the search_by_page_excerpt() function.
*/
public function search_by_page_test_search_by_page_excerpt_match($key, $text, $offset, $boundary) {
// Find the root form of the keyword -- in this simple example,
// all but the last 3 characters.
$key = mb_substr($key, 0, -3);
if (mb_strlen($key) < 3) {
return FALSE;
}
// Look for this modified key at the start of a word.
$match = array();
if (!preg_match('/' . $boundary . $key . '/iu', $text, $match, PREG_OFFSET_CAPTURE, $offset)) {
// didn't match our modified key.
return FALSE;
}
// If we get here, we have a match. Find the end of the word we
// actually matched, so it can be highlighted.
$pos = $match[0][1];
if (preg_match('/' . $boundary . '/iu', $text, $match, PREG_OFFSET_CAPTURE, $pos + mb_strlen($key))) {
$keyfound = mb_substr($text, $pos, $match[0][1] - $pos);
}
return array(
'where' => $pos,
'keyword' => $keyfound,
);
}
/**
* Implements hook_permission().
*
* Sets up permissions for this module.
*/
public function search_by_page_test_permission() {
return array(
'view test private content' => array(
'title' => 'View test private content',
),
);
}
/**
* Implements hook_node_access_records().
*
* Sets up node access for testing purposes. If a node is marked
* "test_private", it is only available to users with
* 'view test private content' permissions. Code taken form the
* Node Access Example module on drupal.org.
*/
public function search_by_page_test_node_access_records($node) {
// Only restrict access to nodes marked private
if ($node->test_private) {
$grants = array();
$grants[] = array(
'realm' => 'search_by_page_test',
'gid' => 888,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 999,
);
return $grants;
}
}
/**
* Implements hook_node_grants().
*
* Sets up node access for testing purposes. If a node is marked
* "test_private", it is only available to users with
* 'view test private content' permissions. Code taken form the
* Node Access Example module on drupal.org.
*/
public function serch_by_page_test_node_grants($account, $op) {
$grants = array();
if ($op == 'view' && user_access('view test private content', $account)) {
$grants['search_by_page_test'] = array(
888,
);
}
return $grants;
}
/**
* Implements hook_node_load().
*
* Manage the "private" flag on nodes. Code taken form the
* Node Access Example module on drupal.org.
*/
public function search_by_page_test_node_load($nodes, $types) {
$result = \Drupal::database()
->query('SELECT * FROM {search_by_page_test_access} WHERE nid IN(:nids)', array(
':nids' => array_keys($nodes),
))
->fetchAll();
foreach ($result as $item) {
$nodes[$item->nid]->test_private = $item->private;
}
}
/**
* Implements hook_node_delete().
*
* Manage the "private" flag on nodes. Code taken form the
* Node Access Example module on drupal.org.
*/
public function search_by_page_test_node_delete($node) {
\Drupal::database()
->delete('search_by_page_test_access')
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_node_insert().
*
* Manage the "private" flag on nodes. Code taken form the
* Node Access Example module on drupal.org.
*/
public function search_by_page_test_node_insert($node) {
// Make sure we are saving an integer for this field!
if (isset($node->test_private) && $node->test_private) {
$node->test_private = 1;
}
else {
$node->test_private = 0;
}
\Drupal::database()
->insert('search_by_page_test_access')
->fields(array(
'nid' => $node->nid,
'private' => $node->test_private,
))
->execute();
}
/**
* Implements hook_node_update().
*
* Manage the "private" flag on nodes. Code taken form the
* Node Access Example module on drupal.org.
*/
public function search_by_page_test_node_update($node) {
// Make sure we are saving an integer for this field!
if (isset($node->test_private) && $node->test_private) {
$node->test_private = 1;
}
else {
$node->test_private = 0;
}
\Drupal::database()
->update('search_by_page_test_access')
->fields(array(
'private' => $node->test_private,
))
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_node_info().
*
* Defines two node types, so that they can be set up for searching or not
* searching.
*/
public function search_by_page_test_node_info() {
return array(
'search_by_page_indexed' => array(
'name' => t('SBP Indexed'),
'base' => 'search_by_page_test',
'description' => t('Content to be indexed for searching in tests'),
'has_title' => TRUE,
),
'search_by_page_hidden' => array(
'name' => t('SBP Hidden'),
'base' => 'search_by_page_test',
'description' => t('Content not to be indexed for searching in tests'),
'has_title' => TRUE,
),
);
}
/**
* Implements hook_form().
*/
public function search_by_page_test_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_menu().
*
* Sets up pages that can be rendered and searched.
*/
public function search_by_page_test_menu() {
$items = array();
$items['search_by_page_test_priv_page'] = array(
'title' => 'Search by Page private page',
'description' => 'Test content',
'page callback' => 'search_by_page_test_make_priv_page',
'access arguments' => array(
'view test private content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['search_by_page_test_pub_page'] = array(
'title' => 'Search by Page public page',
'description' => 'Test content',
'page callback' => 'search_by_page_test_make_pub_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['search_by_page_test_another_page'] = array(
'title' => 'Search by Page another Page',
'description' => 'Test content',
'page callback' => 'search_by_page_test_make_another_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['search_by_page_test_queried_page'] = array(
'title' => 'Search by Page queried Page',
'description' => 'Test content',
'page callback' => 'search_by_page_test_make_queried_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['search_by_page_test_exclude_page'] = array(
'title' => 'Search by Page exclude Page',
'description' => 'Test content',
'page callback' => 'search_by_page_test_make_exclude_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['search_by_page_test_output_page'] = array(
'title' => 'Search by Page output Page',
'description' => 'Test content',
'page callback' => 'search_by_page_test_make_output_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
/**
* Page callback function for path 'search_by_page_test_priv_page'.
*/
public function search_by_page_test_make_priv_page() {
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Pine') . '</li>' . '<li>' . t('Spruce') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'search_by_page_test_pub_page'.
*/
public function search_by_page_test_make_pub_page() {
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Oak') . '</li>' . '<li>' . t('Maple') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'search_by_page_test_another_page'.
*/
public function search_by_page_test_make_another_page() {
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Apple') . '</li>' . '<li>' . t('Pear') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'search_by_page_test_output_page'.
*
* This page intentionally does some things designed to screw up the
* Search by Page output:
* - Sets the page title.
* - Sets the breadcrumb.
* - Sets the active trail.
* - Prints some output instead of just returning output.
*/
public function search_by_page_test_make_output_page() {
drupal_set_title('wrong page title');
print "wrong information printed";
drupal_set_breadcrumb(array(
l('wrong breadcrumb', '<front>'),
l('wrong breadcrumb 2', 'search_by_page_test_pub_page'),
));
$trail = menu_get_active_trail();
$i = 1;
foreach ($trail as $item) {
$item['title'] = "wrong trail {$i}";
$i++;
}
menu_set_active_trail($trail);
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Apple') . '</li>' . '<li>' . t('Pear') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'search_by_page_test_queried_page'.
*/
public function search_by_page_test_make_queried_page() {
// Print out different text depending on the value of the query arg.
$value = $_GET['a'];
$text = $value == 3 ? t('three') : t('four');
return '<p>' . $text . '</p>' . '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Walnut') . '</li>' . '<li>' . t('Hazelnut') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'search_by_page_test_exclude_page'.
*/
public function search_by_page_test_make_exclude_page() {
return '<h2>' . t('Trees') . '</h2>' . '<tagtoexclude><ul>' . '<li>' . t('Poplar') . '</li>' . '</ul></tagtoexclude><p>Cottonwood</p>';
}
/**
* Implements MODULE_preprocess_HOOK() for search-result.tpl.php.
*
* Makes sure the 'type' is displayed, so that the internationalization
* test can verify it's translated.
*/
public function search_by_page_test_preprocess_search_result(&$variables) {
if (isset($variables['result']['type'])) {
$variables['info_split']['type'] = Html::escape($variables['result']['type']);
$variables['info'] = implode(' - ', $variables['info_split']);
}
}
/**
* Implements hook_block_info().
*
* Sets up two test blocks.
*/
public function search_by_page_test_block_info() {
return array(
'cat' => array(
'info' => t('Pet block 1'),
),
'dog' => array(
'info' => t('Pet block 2'),
),
);
}
/**
* Implements hook_block_view().
*
* @see search_by_page_test_block_info()
*/
public function search_by_page_test_block_view($delta = '') {
$text = Html::escape($delta);
return array(
'subject' => ucfirst($text) . ' title',
'content' => $text . ' block body',
);
}
}
Classes
Name | Description |
---|---|
SearchByPageTesterTest | Base class for testing Search by Page. |