SearchByPageUnitTest.php in Search by Page 8
Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com
Namespace
Drupal\Tests\search_by_page\UnitFile
tests/src/Unit/SearchByPageUnitTest.phpView 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\Unit;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
/**
* Unit tests for Search by Page functions.
*
* @group search_by_page
*/
class SearchByPageUnitTest extends UnitTestCase {
const LANGUAGE_NONE = Language::LANGCODE_NOT_SPECIFIED;
public static $modules = [
'search',
'search_by_page',
];
public static function getInfo() {
return [
'name' => t('Search by Page Unit Tests'),
'description' => t('Test individual functions in search_by_page.module'),
'group' => t('Search by Page'),
'dependencies' => self::$modules,
];
}
/**
* Tests the search_by_page_path_parts() function.
*/
function testPathParts() {
$path = "abc/def?a=b&c=f";
$res = \Drupal::service('search_by_page.settings')
->explodePathParts($path);
$this
->assertEquals(count($res), 2, "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right count in results");
$this
->assertEquals($res[0], 'abc/def', "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right base path");
$this
->assertEquals($res[1], 'a=b&c=f', "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right query part");
$path = "abc/def";
$res = \Drupal::service('search_by_page.settings')
->explodePathParts($path);
$this
->assertEquals(count($res), 1, "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right count in results");
$this
->assertEquals($res[0], 'abc/def', "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right base path");
$path = "abc/def&a=b&c=f";
$res = \Drupal::service('search_by_page.settings')
->explodePathParts($path);
$this
->assertEquals(count($res), 2, "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right count in results");
$this
->assertEquals($res[0], 'abc/def', "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right base path");
$this
->assertEquals($res[1], 'a=b&c=f', "\\Drupal::service('search_by_page.settings')->explodePathParts() returns right query part");
}
/**
* Tests the search_by_page_excerpt() function.
*/
function testExcerpt() {
// Our test class should have made the 'walking' key match walks, walker,
// walking, and walked. They should now be highlighted with <strong> HTML
// tags.
$result = \Drupal::service('search_by_page.settings')
->excerpt('walking', 'He walks through the forest');
$this
->assertTrue(strpos($result, '<strong>walks</strong>') > 0, "Excerpt highlights walks");
$result = \Drupal::service('search_by_page.settings')
->excerpt('walking', 'He is a walker through the forest');
$this
->assertTrue(strpos($result, '<strong>walker</strong>') > 0, "Excerpt highlights walker");
$result = \Drupal::service('search_by_page.settings')
->excerpt('walking', 'He is walking through the forest');
$this
->assertTrue(strpos($result, '<strong>walking</strong>') > 0, "Excerpt highlights walking");
$result = \Drupal::service('search_by_page.settings')
->excerpt('walking', 'He walked through the forest');
$this
->assertTrue(strpos($result, '<strong>walked</strong>') > 0, "Excerpt highlights walked");
// Verify that an exact match after a stemmed match highlights the stemmed.
$result = \Drupal::service('search_by_page.settings')
->excerpt('walking', 'He walked on the long walk in town');
$this
->assertTrue(strpos($result, '<strong>walked</strong>') > 0, "Excerpt highlights walked");
}
/**
* Tests the search_by_page_strip_tags function.
*/
function testStripTags() {
$env = 1;
$test_string = '<div>This is a test. <tag1>This should be removed</tag1> <tag2>This should also be removed</tag2> This should be left. <tag1>This should not be here</tag1><tag1 some="attribute">This although with attributes, should also be removed</tag1></div>';
// Try excluding some tags.
\Drupal::service('search_by_page.settings')
->setSetting('exclude_tags', $env, 'tag1 tag2');
$result = search_by_page_strip_tags($test_string, $env);
$this
->assertEquals($result, '<div>This is a test. This should be left. </div>', 'search_by_page_strip_tags() is working correctly.');
// Try not excluding any tags.
\Drupal::service('search_by_page.settings')
->setSetting('exclude_tags', $env, '');
$result = search_by_page_strip_tags($test_string, $env);
$this
->assertEquals($result, $test_string, 'search_by_page_strip_tags() with no tags to strip is working correctly.');
}
}
Classes
Name | Description |
---|---|
SearchByPageUnitTest | Unit tests for Search by Page functions. |