1 search.test SearchBlockTestCase::testBlock()

Test that the search block form works correctly.

File

core/modules/search/tests/search.test, line 565
Tests for search.module.

Class

SearchBlockTestCase
Tests the rendering of the search block.

Code

function testBlock() {
  // Enable the search block.
  $layout = layout_load('default');
  $layout->addBlock('search', 'form', 'content');
  $layout->save();

  // Test a normal search via the block form, from the front page.
  $terms = array('search_block_form' => 'test');
  $this->backdropPost('user', $terms, t('Search'));
  $this->assertText('Your search yielded no results');

  // Test a search from the block on a 404 page.
  $this->backdropGet('foo');
  $this->assertResponse(404);
  $this->backdropPost(NULL, $terms, t('Search'));
  $this->assertResponse(200);
  $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)), 
  'Redirected to correct url.'
  );

  // Test an empty search via the block form, from the front page.
  $terms = array('search_block_form' => '');
  $this->backdropPost('user', $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)), 
  'Redirected to correct url.'
  );

  // Test that after entering a too-short keyword in the form, you can then
  // search again with a longer keyword. First test using the block form.
  $terms = array('search_block_form' => 'a');
  $this->backdropPost('user', $terms, t('Search'));
  $this->assertText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
  $terms = array('search_block_form' => 'foo');
  $this->backdropPost(NULL, $terms, t('Search'));
  $this->assertNoText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
  $this->assertText('Your search yielded no results');

  // Same test again, using the search page form for the second search this time.
  $terms = array('search_block_form' => 'a');
  $this->backdropPost('user', $terms, t('Search'));
  $terms = array('keys' => 'foo');
  $this->backdropPost(NULL, $terms, t('Search'));
  $this->assertNoText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
  $this->assertText('Your search yielded no results');
}