sbp_test.module in Search by Page 7
Same filename and directory in other branches
Module file for Search by Page testing.
This module does the following:
- Implements a couple of hooks, for unit testing of functions in the main Search by Page API infrastructure.
- Sets up a couple of content types and a "private" flag for content, for functional testing of the Search by Page Nodes module.
- Sets up paths that contain content, for functional testing of the Search by Page Paths module.
File
tests/sbp_test.moduleView source
<?php
/**
* @file
* Module file for Search by Page testing.
*
* This module does the following:
* - Implements a couple of hooks, for unit testing of functions in the main
* Search by Page API infrastructure.
* - Sets up a couple of content types and a "private" flag for content,
* for functional testing of the Search by Page Nodes module.
* - Sets up paths that contain content, for functional testing of the
* Search by Page Paths module.
*
* @ingroup search_by_page
*/
/**
* Implements hook_sbp_excerpt_match().
*
* For testing of the search_by_page_excerpt() function.
*/
function sbp_test_sbp_excerpt_match($key, $text, $offset, $boundary) {
// Find the root form of the keyword -- in this simple example,
// all but the last 3 characters.
$key = drupal_substr($key, 0, -3);
if (drupal_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 + drupal_strlen($key))) {
$keyfound = drupal_substr($text, $pos, $match[0][1] - $pos);
}
return array(
'where' => $pos,
'keyword' => $keyfound,
);
}
/**
* Implements hook_permission().
*
* Sets up permissions for this module.
*/
function sbp_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.
*/
function sbp_test_node_access_records($node) {
// Only restrict access to nodes marked private
if ($node->test_private) {
$grants = array();
$grants[] = array(
'realm' => 'sbp_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.
*/
function sbp_test_node_grants($account, $op) {
$grants = array();
if ($op == 'view' && user_access('view test private content', $account)) {
$grants['sbp_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.
*/
function sbp_test_node_load($nodes, $types) {
$result = db_query('SELECT * FROM {sbp_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.
*/
function sbp_test_node_delete($node) {
db_delete('sbp_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.
*/
function sbp_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;
}
db_insert('sbp_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.
*/
function sbp_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;
}
db_update('sbp_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.
*/
function sbp_test_node_info() {
return array(
'sbp_indexed' => array(
'name' => t('SBP Indexed'),
'base' => 'sbp_test',
'description' => t('Content to be indexed for searching in tests'),
'has_title' => TRUE,
),
'sbp_hidden' => array(
'name' => t('SBP Hidden'),
'base' => 'sbp_test',
'description' => t('Content not to be indexed for searching in tests'),
'has_title' => TRUE,
),
);
}
/**
* Implements hook_form().
*/
function sbp_test_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_menu().
*
* Sets up pages that can be rendered and searched.
*/
function sbp_test_menu() {
$items = array();
$items['sbp_test_priv_page'] = array(
'title' => 'Search by Page private page',
'description' => 'Test content',
'page callback' => 'sbp_test_make_priv_page',
'access arguments' => array(
'view test private content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['sbp_test_pub_page'] = array(
'title' => 'Search by Page public page',
'description' => 'Test content',
'page callback' => 'sbp_test_make_pub_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['sbp_test_another_page'] = array(
'title' => 'Search by Page another Page',
'description' => 'Test content',
'page callback' => 'sbp_test_make_another_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['sbp_test_queried_page'] = array(
'title' => 'Search by Page queried Page',
'description' => 'Test content',
'page callback' => 'sbp_test_make_queried_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['sbp_test_exclude_page'] = array(
'title' => 'Search by Page exclude Page',
'description' => 'Test content',
'page callback' => 'sbp_test_make_exclude_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['sbp_test_output_page'] = array(
'title' => 'Search by Page output Page',
'description' => 'Test content',
'page callback' => 'sbp_test_make_output_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
/**
* Page callback function for path 'sbp_test_priv_page'.
*/
function sbp_test_make_priv_page() {
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Pine') . '</li>' . '<li>' . t('Spruce') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'sbp_test_pub_page'.
*/
function sbp_test_make_pub_page() {
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Oak') . '</li>' . '<li>' . t('Maple') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'sbp_test_another_page'.
*/
function sbp_test_make_another_page() {
return '<h2>' . t('Trees') . '</h2>' . '<ul>' . '<li>' . t('Apple') . '</li>' . '<li>' . t('Pear') . '</li>' . '</ul>';
}
/**
* Page callback function for path 'sbp_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.
*/
function sbp_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', 'sbp_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 'sbp_test_queried_page'.
*/
function sbp_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 'sbp_test_exclude_page'.
*/
function sbp_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.
*/
function sbp_test_preprocess_search_result(&$variables) {
if (isset($variables['result']['type'])) {
$variables['info_split']['type'] = check_plain($variables['result']['type']);
$variables['info'] = implode(' - ', $variables['info_split']);
}
}
/**
* Implements hook_block_info().
*
* Sets up two test blocks.
*/
function sbp_test_block_info() {
return array(
'cat' => array(
'info' => t('Pet block 1'),
),
'dog' => array(
'info' => t('Pet block 2'),
),
);
}
/**
* Implements hook_block_view().
*
* @see sbp_test_block_info()
*/
function sbp_test_block_view($delta = '') {
$text = check_plain($delta);
return array(
'subject' => ucfirst($text) . ' title',
'content' => $text . ' block body',
);
}