You are here

class SearchByPageUnitTest in Search by Page 8

Unit tests for Search by Page functions.

@group search_by_page

Hierarchy

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\Unit
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SearchByPageUnitTest::$modules public static property
SearchByPageUnitTest::getInfo public static function
SearchByPageUnitTest::LANGUAGE_NONE constant
SearchByPageUnitTest::testExcerpt function Tests the search_by_page_excerpt() function.
SearchByPageUnitTest::testPathParts function Tests the search_by_page_path_parts() function.
SearchByPageUnitTest::testStripTags function Tests the search_by_page_strip_tags function.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340