You are here

class GoogleSearchTest in Freelinking 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/Plugin/freelinking/GoogleSearchTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\GoogleSearchTest

Tests the google search plugin.

@group freelinking

Hierarchy

Expanded class hierarchy of GoogleSearchTest

File

tests/src/Unit/Plugin/freelinking/GoogleSearchTest.php, line 17

Namespace

Drupal\Tests\freelinking\Unit\Plugin\freelinking
View source
class GoogleSearchTest extends UnitTestCase {

  /**
   * Search plugin.
   *
   * @var \Drupal\freelinking\Plugin\freelinking\Search
   */
  protected $plugin;

  /**
   * Translation interface mock.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationInterfaceMock;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {

    // Mock the string translation.
    $tProphet = $this
      ->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $tProphet
      ->translateString(Argument::any())
      ->willReturn('Search google for the specified terms.');
    $this->translationInterfaceMock = $tProphet
      ->reveal();

    // Mock path validator.
    $pathValidatorProphet = $this
      ->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $this->translationInterfaceMock);
    $container
      ->set('path.validator', $pathValidatorProphet
      ->reveal());
    \Drupal::setContainer($container);
    $plugin_definition = [
      'id' => 'google',
      'title' => 'Google Search',
      'hidden' => FALSE,
      'weight' => 1,
    ];
    $this->plugin = new GoogleSearch([], 'google', $plugin_definition);
  }

  /**
   * Assert that indicator functions for both google and core search.
   *
   * @param string $indicator
   *   The indicator string.
   * @param int $expected
   *   The expected output from preg_match.
   *
   * @dataProvider indicatorProvider
   */
  public function testGetIndicator($indicator, $expected) {
    $this
      ->assertEquals($expected, preg_match($this->plugin
      ->getIndicator(), $indicator));
  }

  /**
   * Asserts that the appropriate tip is returned.
   */
  public function testGetTip() {
    $this
      ->assertEquals('Search google for the specified terms.', $this->plugin
      ->getTip());
  }

  /**
   * Asserts the failover functionality for the google search plugin.
   */
  public function testBuildLink() {
    $expected = [
      '#type' => 'link',
      '#title' => 'Google Search Test Search',
      '#url' => Url::fromUri('https://google.com/search', [
        'absolute' => TRUE,
        'query' => [
          'q' => 'Test+Search',
          'hl' => 'en',
        ],
        'language' => NULL,
      ]),
      '#attributes' => [
        'title' => new TranslatableMarkup('Search google for the specified terms.', [], [], $this->translationInterfaceMock),
      ],
    ];
    $target = [
      'target' => 'google:Test Search|Test Search',
      'dest' => 'Test Search',
      'text' => 'Test Search',
      'language' => NULL,
    ];
    $this
      ->assertEquals($expected, $this->plugin
      ->buildLink($target));
  }

  /**
   * Provides test parameters for ::testGetIndicator.
   *
   * @return array
   *   An array of test parameters.
   */
  public function indicatorProvider() {
    return [
      [
        'nomatch',
        0,
      ],
      [
        'google',
        1,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleSearchTest::$plugin protected property Search plugin.
GoogleSearchTest::$translationInterfaceMock protected property Translation interface mock.
GoogleSearchTest::indicatorProvider public function Provides test parameters for ::testGetIndicator.
GoogleSearchTest::setUp protected function Overrides UnitTestCase::setUp
GoogleSearchTest::testBuildLink public function Asserts the failover functionality for the google search plugin.
GoogleSearchTest::testGetIndicator public function Assert that indicator functions for both google and core search.
GoogleSearchTest::testGetTip public function Asserts that the appropriate tip is returned.
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.
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.