class SearchByPageUnitTest in Search by Page 8
Unit tests for Search by Page functions.
@group search_by_page
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_by_page\Unit\SearchByPageUnitTest
Expanded class hierarchy of SearchByPageUnitTest
File
- tests/
src/ Unit/ SearchByPageUnitTest.php, line 19 - Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com
Namespace
Drupal\Tests\search_by_page\UnitView source
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.');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SearchByPageUnitTest:: |
public static | property | ||
SearchByPageUnitTest:: |
public static | function | ||
SearchByPageUnitTest:: |
constant | |||
SearchByPageUnitTest:: |
function | Tests the search_by_page_excerpt() function. | ||
SearchByPageUnitTest:: |
function | Tests the search_by_page_path_parts() function. | ||
SearchByPageUnitTest:: |
function | Tests the search_by_page_strip_tags function. | ||
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |