Commit c787f37a authored by catch's avatar catch

Issue #1594006 by aspilicious: Convert search tests to PSR-0.

parent 5fc51d33
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchAdvancedSearchFormTest.
*/
namespace Drupal\search\Tests;
class SearchAdvancedSearchFormTest extends SearchTestBase {
protected $node;
public static function getInfo() {
return array(
'name' => 'Advanced search form',
'description' => 'Indexes content and tests the advanced search form.',
'group' => 'Search',
);
}
function setUp() {
parent::setUp();
// Create and login user.
$test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes'));
$this->drupalLogin($test_user);
// Create initial node.
$node = $this->drupalCreateNode();
$this->node = $this->drupalCreateNode();
// First update the index. This does the initial processing.
node_update_index();
// Then, run the shutdown function. Testing is a unique case where indexing
// and searching has to happen in the same request, so running the shutdown
// function manually is needed to finish the indexing process.
search_update_totals();
}
/**
* Test using the search form with GET and POST queries.
* Test using the advanced search form to limit search to nodes of type "Basic page".
*/
function testNodeType() {
$this->assertTrue($this->node->type == 'page', t('Node type is Basic page.'));
// Assert that the dummy title doesn't equal the real title.
$dummy_title = 'Lorem ipsum';
$this->assertNotEqual($dummy_title, $this->node->title, t("Dummy title doesn't equal node title"));
// Search for the dummy title with a GET query.
$this->drupalGet('search/node/' . $dummy_title);
$this->assertNoText($this->node->title, t('Basic page node is not found with dummy title.'));
// Search for the title of the node with a GET query.
$this->drupalGet('search/node/' . $this->node->title);
$this->assertText($this->node->title, t('Basic page node is found with GET query.'));
// Search for the title of the node with a POST query.
$edit = array('or' => $this->node->title);
$this->drupalPost('search/node', $edit, t('Advanced search'));
$this->assertText($this->node->title, t('Basic page node is found with POST query.'));
// Advanced search type option.
$this->drupalPost('search/node', array_merge($edit, array('type[page]' => 'page')), t('Advanced search'));
$this->assertText($this->node->title, t('Basic page node is found with POST query and type:page.'));
$this->drupalPost('search/node', array_merge($edit, array('type[article]' => 'article')), t('Advanced search'));
$this->assertText('bike shed', t('Article node is not found with POST query and type:article.'));
}
}
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchBlockTest.
*/
namespace Drupal\search\Tests;
class SearchBlockTest extends SearchTestBase {
public static function getInfo() {
return array(
'name' => 'Block availability',
'description' => 'Check if the search form block is available.',
'group' => 'Search',
);
}
function setUp() {
parent::setUp(array('block'));
// Create and login user
$admin_user = $this->drupalCreateUser(array('administer blocks', 'search content'));
$this->drupalLogin($admin_user);
}
function testSearchFormBlock() {
// Set block title to confirm that the interface is available.
$this->drupalPost('admin/structure/block/manage/search/form/configure', array('title' => $this->randomName(8)), t('Save block'));
$this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
// Set the block to a region to confirm block is available.
$edit = array();
$edit['blocks[search_form][region]'] = 'footer';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
}
/**
* Test that the search block form works correctly.
*/
function testBlock() {
// Enable the block, and place it in the 'content' region so that it isn't
// hidden on 404 pages.
$edit = array('blocks[search_form][region]' => 'content');
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
// Test a normal search via the block form, from the front page.
$terms = array('search_block_form' => 'test');
$this->drupalPost('node', $terms, t('Search'));
$this->assertText('Your search yielded no results');
// Test a search from the block on a 404 page.
$this->drupalGet('foo');
$this->assertResponse(404);
$this->drupalPost(NULL, $terms, t('Search'));
$this->assertResponse(200);
$this->assertText('Your search yielded no results');
// Test a search from the block when it doesn't appear on the search page.
$edit = array('pages' => 'search');
$this->drupalPost('admin/structure/block/manage/search/form/configure', $edit, t('Save block'));
$this->drupalPost('node', $terms, t('Search'));
$this->assertText('Your search yielded no results');
// Confirm that the user is redirected to the search page.
$this->assertEqual(
$this->getUrl(),
url('search/node/' . $terms['search_block_form'], array('absolute' => TRUE)),
t('Redirected to correct url.')
);
// Test an empty search via the block form, from the front page.
$terms = array('search_block_form' => '');
$this->drupalPost('node', $terms, t('Search'));
$this->assertText('Please enter some keywords');
// Confirm that the user is redirected to the search page, when form is submitted empty.
$this->assertEqual(
$this->getUrl(),
url('search/node/', array('absolute' => TRUE)),
t('Redirected to correct url.')
);
}
}
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchCommentCountToggleTest.
*/
namespace Drupal\search\Tests;
/**
* Tests that comment count display toggles properly on comment status of node
*
* Issue 537278
*
* - Nodes with comment status set to Open should always how comment counts
* - Nodes with comment status set to Closed should show comment counts
* only when there are comments
* - Nodes with comment status set to Hidden should never show comment counts
*/
class SearchCommentCountToggleTest extends SearchTestBase {
// Requires node types, comment config, filter formats.
protected $profile = 'standard';
protected $searching_user;
protected $searchable_nodes;
public static function getInfo() {
return array(
'name' => 'Comment count toggle',
'description' => 'Verify that comment count display toggles properly on comment status of node.',
'group' => 'Search',
);
}
function setUp() {
parent::setUp(array('comment'));
// Create searching user.
$this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
// Create initial nodes.
$node_params = array('type' => 'article', 'body' => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 'SearchCommentToggleTestCase'))));
$this->searchable_nodes['1 comment'] = $this->drupalCreateNode($node_params);
$this->searchable_nodes['0 comments'] = $this->drupalCreateNode($node_params);
// Login with sufficient privileges.
$this->drupalLogin($this->searching_user);
// Create a comment array
$edit_comment = array();
$edit_comment['subject'] = $this->randomName();
$edit_comment['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]'] = $this->randomName();
$filtered_html_format_id = 'filtered_html';
$edit_comment['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][format]'] = $filtered_html_format_id;
// Post comment to the test node with comment
$this->drupalPost('comment/reply/' . $this->searchable_nodes['1 comment']->nid, $edit_comment, t('Save'));
// First update the index. This does the initial processing.
node_update_index();
// Then, run the shutdown function. Testing is a unique case where indexing
// and searching has to happen in the same request, so running the shutdown
// function manually is needed to finish the indexing process.
search_update_totals();
}
/**
* Verify that comment count display toggles properly on comment status of node
*/
function testSearchCommentCountToggle() {
// Search for the nodes by string in the node body.
$edit = array(
'search_block_form' => "'SearchCommentToggleTestCase'",
);
// Test comment count display for nodes with comment status set to Open
$this->drupalPost('', $edit, t('Search'));
$this->assertText(t('0 comments'), t('Empty comment count displays for nodes with comment status set to Open'));
$this->assertText(t('1 comment'), t('Non-empty comment count displays for nodes with comment status set to Open'));
// Test comment count display for nodes with comment status set to Closed
$this->searchable_nodes['0 comments']->comment = COMMENT_NODE_CLOSED;
node_save($this->searchable_nodes['0 comments']);
$this->searchable_nodes['1 comment']->comment = COMMENT_NODE_CLOSED;
node_save($this->searchable_nodes['1 comment']);
$this->drupalPost('', $edit, t('Search'));
$this->assertNoText(t('0 comments'), t('Empty comment count does not display for nodes with comment status set to Closed'));
$this->assertText(t('1 comment'), t('Non-empty comment count displays for nodes with comment status set to Closed'));
// Test comment count display for nodes with comment status set to Hidden
$this->searchable_nodes['0 comments']->comment = COMMENT_NODE_HIDDEN;
node_save($this->searchable_nodes['0 comments']);
$this->searchable_nodes['1 comment']->comment = COMMENT_NODE_HIDDEN;
node_save($this->searchable_nodes['1 comment']);
$this->drupalPost('', $edit, t('Search'));
$this->assertNoText(t('0 comments'), t('Empty comment count does not display for nodes with comment status set to Hidden'));
$this->assertNoText(t('1 comment'), t('Non-empty comment count does not display for nodes with comment status set to Hidden'));
}
}
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchCommentTest.
*/
namespace Drupal\search\Tests;
/**
* Test integration searching comments.
*/
class SearchCommentTest extends SearchTestBase {
protected $profile = 'standard';
protected $admin_user;
public static function getInfo() {
return array(
'name' => 'Comment Search tests',
'description' => 'Verify text formats and filters used elsewhere.',
'group' => 'Search',
);
}
function setUp() {
parent::setUp(array('comment'));
// Create and log in an administrative user having access to the Full HTML
// text format.
$full_html_format = filter_format_load('full_html');
$permissions = array(
'administer filters',
filter_permission_name($full_html_format),
'administer permissions',
'create page content',
'skip comment approval',
'access comments',
);
$this->admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->admin_user);
}
/**
* Verify that comments are rendered using proper format in search results.
*/
function testSearchResultsComment() {
$comment_body = 'Test comment body';
variable_set('comment_preview_article', DRUPAL_OPTIONAL);
// Enable check_plain() for 'Filtered HTML' text format.
$filtered_html_format_id = 'filtered_html';
$edit = array(
'filters[filter_html_escape][status]' => TRUE,
);
$this->drupalPost('admin/config/content/formats/' . $filtered_html_format_id, $edit, t('Save configuration'));
// Allow anonymous users to search content.
$edit = array(
DRUPAL_ANONYMOUS_RID . '[search content]' => 1,
DRUPAL_ANONYMOUS_RID . '[access comments]' => 1,
DRUPAL_ANONYMOUS_RID . '[post comments]' => 1,
);
$this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
// Create a node.
$node = $this->drupalCreateNode(array('type' => 'article'));
// Post a comment using 'Full HTML' text format.
$edit_comment = array();
$edit_comment['subject'] = 'Test comment subject';
$edit_comment['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
$full_html_format_id = 'full_html';
$edit_comment['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][format]'] = $full_html_format_id;
$this->drupalPost('comment/reply/' . $node->nid, $edit_comment, t('Save'));
// Invoke search index update.
$this->drupalLogout();
$this->cronRun();
// Search for the comment subject.
$edit = array(
'search_block_form' => "'" . $edit_comment['subject'] . "'",
);
$this->drupalPost('', $edit, t('Search'));
$this->assertText($node->title, t('Node found in search results.'));
$this->assertText($edit_comment['subject'], t('Comment subject found in search results.'));
// Search for the comment body.
$edit = array(
'search_block_form' => "'" . $comment_body . "'",
);
$this->drupalPost('', $edit, t('Search'));
$this->assertText($node->title, t('Node found in search results.'));
// Verify that comment is rendered using proper format.
$this->assertText($comment_body, t('Comment body text found in search results.'));
$this->assertNoRaw(t('n/a'), t('HTML in comment body is not hidden.'));
$this->assertNoRaw(check_plain($edit_comment['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]']), t('HTML in comment body is not escaped.'));
// Hide comments.
$this->drupalLogin($this->admin_user);
$node->comment = 0;
$node->save();
// Invoke search index update.
$this->drupalLogout();
$this->cronRun();
// Search for $title.
$this->drupalPost('', $edit, t('Search'));
$this->assertNoText($comment_body, t('Comment body text not found in search results.'));
}
/**
* Verify access rules for comment indexing with different permissions.
*/
function testSearchResultsCommentAccess() {
$comment_body = 'Test comment body';
$this->comment_subject = 'Test comment subject';
$this->admin_role = $this->admin_user->roles;
unset($this->admin_role[DRUPAL_AUTHENTICATED_RID]);
$this->admin_role = key($this->admin_role);
// Create a node.
variable_set('comment_preview_article', DRUPAL_OPTIONAL);
$this->node = $this->drupalCreateNode(array('type' => 'article'));
// Post a comment using 'Full HTML' text format.
$edit_comment = array();
$edit_comment['subject'] = $this->comment_subject;
$edit_comment['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
$this->drupalPost('comment/reply/' . $this->node->nid, $edit_comment, t('Save'));
$this->drupalLogout();
$this->setRolePermissions(DRUPAL_ANONYMOUS_RID);
$this->checkCommentAccess('Anon user has search permission but no access comments permission, comments should not be indexed');
$this->setRolePermissions(DRUPAL_ANONYMOUS_RID, TRUE);
$this->checkCommentAccess('Anon user has search permission and access comments permission, comments should be indexed', TRUE);
$this->drupalLogin($this->admin_user);
$this->drupalGet('admin/people/permissions');
// Disable search access for authenticated user to test admin user.
$this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, FALSE, FALSE);
$this->setRolePermissions($this->admin_role);
$this->checkCommentAccess('Admin user has search permission but no access comments permission, comments should not be indexed');
$this->setRolePermissions($this->admin_role, TRUE);
$this->checkCommentAccess('Admin user has search permission and access comments permission, comments should be indexed', TRUE);
$this->setRolePermissions(DRUPAL_AUTHENTICATED_RID);
$this->checkCommentAccess('Authenticated user has search permission but no access comments permission, comments should not be indexed');
$this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE);
$this->checkCommentAccess('Authenticated user has search permission and access comments permission, comments should be indexed', TRUE);
// Verify that access comments permission is inherited from the
// authenticated role.
$this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE, FALSE);
$this->setRolePermissions($this->admin_role);
$this->checkCommentAccess('Admin user has search permission and no access comments permission, but comments should be indexed because admin user inherits authenticated user\'s permission to access comments', TRUE);
// Verify that search content permission is inherited from the authenticated
// role.
$this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE, TRUE);
$this->setRolePermissions($this->admin_role, TRUE, FALSE);
$this->checkCommentAccess('Admin user has access comments permission and no search permission, but comments should be indexed because admin user inherits authenticated user\'s permission to search', TRUE);
}
/**
* Set permissions for role.
*/
function setRolePermissions($rid, $access_comments = FALSE, $search_content = TRUE) {
$permissions = array(
'access comments' => $access_comments,
'search content' => $search_content,
);
user_role_change_permissions($rid, $permissions);
}
/**
* Update search index and search for comment.
*/
function checkCommentAccess($message, $assume_access = FALSE) {
// Invoke search index update.
search_touch_node($this->node->nid);
$this->cronRun();
// Search for the comment subject.
$edit = array(
'search_block_form' => "'" . $this->comment_subject . "'",
);
$this->drupalPost('', $edit, t('Search'));
$method = $assume_access ? 'assertText' : 'assertNoText';
$verb = $assume_access ? 'found' : 'not found';
$this->{$method}($this->node->title, "Node $verb in search results: " . $message);
$this->{$method}($this->comment_subject, "Comment subject $verb in search results: " . $message);
}
/**
* Verify that 'add new comment' does not appear in search results or index.
*/
function testAddNewComment() {
// Create a node with a short body.
$settings = array(
'type' => 'article',
'title' => 'short title',
'body' => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 'short body text'))),
);
$user = $this->drupalCreateUser(array('search content', 'create article content', 'access content'));
$this->drupalLogin($user);
$node = $this->drupalCreateNode($settings);
// Verify that if you view the node on its own page, 'add new comment'
// is there.
$this->drupalGet('node/' . $node->nid);
$this->assertText(t('Add new comment'), t('Add new comment appears on node page'));
// Run cron to index this page.
$this->drupalLogout();
$this->cronRun();
// Search for 'comment'. Should be no results.
$this->drupalLogin($user);
$this->drupalPost('search/node', array('keys' => 'comment'), t('Search'));
$this->assertText(t('Your search yielded no results'), t('No results searching for the word comment'));
// Search for the node title. Should be found, and 'Add new comment' should
// not be part of the search snippet.
$this->drupalPost('search/node', array('keys' => 'short'), t('Search'));
$this->assertText($node->title, t('Search for keyword worked'));
$this->assertNoText(t('Add new comment'), t('Add new comment does not appear on search results page'));
}
}
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchConfigSettingsFormTest.
*/
namespace Drupal\search\Tests;
/**
* Test config page.
*/
class SearchConfigSettingsFormTest extends SearchTestBase {
public $search_user;
public $search_node;
public static function getInfo() {
return array(
'name' => 'Config settings form',
'description' => 'Verify the search config settings form.',
'group' => 'Search',
);
}
function setUp() {
parent::setUp(array('block', 'search_extra_type'));
// Login as a user that can create and search content.
$this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks'));
$this->drupalLogin($this->search_user);
// Add a single piece of content and index it.
$node = $this->drupalCreateNode();
$this->search_node = $node;
// Link the node to itself to test that it's only indexed once. The content
// also needs the word "pizza" so we can use it as the search keyword.
$langcode = LANGUAGE_NOT_SPECIFIED;
$body_key = "body[$langcode][0][value]";
$edit[$body_key] = l($node->title, 'node/' . $node->nid) . ' pizza sandwich';
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
node_update_index();
search_update_totals();
// Enable the search block.
$edit = array();
$edit['blocks[search_form][region]'] = 'content';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
}
/**
* Verify the search settings form.
*/
function testSearchSettingsPage() {
// Test that the settings form displays the correct count of items left to index.
$this->drupalGet('admin/config/search/settings');
$this->assertText(t('There are @count items left to index.', array('@count' => 0)));
// Test the re-index button.
$this->drupalPost('admin/config/search/settings', array(), t('Re-index site'));
$this->assertText(t('Are you sure you want to re-index the site'));
$this->drupalPost('admin/config/search/settings/reindex', array(), t('Re-index site'));
$this->assertText(t('The index will be rebuilt'));
$this->drupalGet('admin/config/search/settings');
$this->assertText(t('There is 1 item left to index.'));
// Test that the form saves with the default values.
$this->drupalPost('admin/config/search/settings', array(), t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), 'Form saves with the default values.');
// Test that the form does not save with an invalid word length.
$edit = array(