You are here

class BuiltinTest in Freelinking 8.3

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

Tests the freelinking builtin plugin.

@group freelinking

Hierarchy

Expanded class hierarchy of BuiltinTest

File

tests/src/Unit/Plugin/freelinking/BuiltinTest.php, line 15

Namespace

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

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

  /**
   * Plugin configuration.
   *
   * @var array
   */
  protected $configuration = [
    'settings' => [],
  ];

  /**
   * Plugin ID.
   *
   * @var string
   */
  protected $id = 'builtin';

  /**
   * Plugin definition.
   *
   * @var array
   */
  protected $definition = [
    'id' => 'builtin',
    'title' => 'Built-in',
    'weight' => -1,
    'hidden' => TRUE,
    'settings' => [],
  ];

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

    // Mock string translation service.
    $tProphet = $this
      ->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $tProphet
      ->translateString(Argument::any())
      ->willReturn('Redact, show text only, or display the indicator');
    $this->translationInterfaceMock = $tProphet
      ->reveal();
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $tProphet
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * Asserts that getTip returns the correct string.
   */
  public function testGetTip() {
    $accountProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $plugin = new Builtin($this->configuration, $this->id, $this->definition, $accountProphet
      ->reveal());
    $this
      ->assertEquals('Redact, show text only, or display the indicator', $plugin
      ->getTip()
      ->render());
  }

  /**
   * Asserts that getIndicator pattern is functional for all patterns.
   *
   * @param string $indicator
   *   The indicator to test.
   * @param int $expected
   *   The expected output from preg_match.
   *
   * @dataProvider getIndicatorProvider
   */
  public function testGetIndicator($indicator, $expected) {
    $accountProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $plugin = new Builtin($this->configuration, $this->id, $this->definition, $accountProphet
      ->reveal());
    $this
      ->assertEquals($expected, preg_match($plugin
      ->getIndicator(), $indicator));
  }

  /**
   * Asserts that buildLink is functional for all patterns.
   *
   * @param string $indicator
   *   The indicator to test.
   * @param string $dest
   *   The target destination text.
   * @param bool $isAuthenticated
   *   The return value for isAuthenticated method.
   * @param string $text
   *   The expected text to find.
   *
   * @dataProvider buildLinkProvider
   */
  public function testBuildLink($indicator, $dest, $isAuthenticated, $text) {
    $accountProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $accountProphet
      ->isAuthenticated()
      ->willReturn($isAuthenticated);
    $target = [
      'indicator' => $indicator,
      'target' => '[[' . $indicator . '|' . $dest . ']]',
      'dest' => $dest,
      'text' => NULL,
    ];
    $expected = [
      '#markup' => $text,
    ];
    $plugin = new Builtin($this->configuration, $this->id, $this->definition, $accountProphet
      ->reveal());
    $this
      ->assertEquals($expected, $plugin
      ->buildLink($target));
  }

  /**
   * Asserts that isHidden method returns TRUE.
   */
  public function testIsHidden() {
    $accountProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $plugin = new Builtin($this->configuration, $this->id, $this->definition, $accountProphet
      ->reveal());
    $this
      ->assertTrue($plugin
      ->isHidden());
  }

  /**
   * Provides test parameters for ::testBuildLink().
   *
   * @return array
   *   An array of test parameters.
   */
  public function buildLinkProvider() {
    return [
      [
        'showtext',
        'Show Text',
        FALSE,
        'Show Text',
      ],
      [
        'redact',
        'Non Redacted',
        FALSE,
        '******',
      ],
      [
        'redact',
        'Non Redacted',
        TRUE,
        'Non Redacted',
      ],
      [
        'nowiki',
        'No Wiki',
        FALSE,
        '[[No Wiki]]',
      ],
    ];
  }

  /**
   * Provides test parameters for ::testGetIndicators().
   *
   * @return array
   *   An array of test parameters.
   */
  public function getIndicatorProvider() {
    return [
      [
        'nomatch',
        0,
      ],
      [
        'showtext',
        1,
      ],
      [
        'nowiki',
        1,
      ],
      [
        'redact',
        1,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BuiltinTest::$configuration protected property Plugin configuration.
BuiltinTest::$definition protected property Plugin definition.
BuiltinTest::$id protected property Plugin ID.
BuiltinTest::$translationInterfaceMock protected property Translation Interface mock.
BuiltinTest::buildLinkProvider public function Provides test parameters for ::testBuildLink().
BuiltinTest::getIndicatorProvider public function Provides test parameters for ::testGetIndicators().
BuiltinTest::setUp protected function Overrides UnitTestCase::setUp
BuiltinTest::testBuildLink public function Asserts that buildLink is functional for all patterns.
BuiltinTest::testGetIndicator public function Asserts that getIndicator pattern is functional for all patterns.
BuiltinTest::testGetTip public function Asserts that getTip returns the correct string.
BuiltinTest::testIsHidden public function Asserts that isHidden method returns TRUE.
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.