You are here

class FreelinkingTest in Freelinking 8.3

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

Tests the freelinking plugin.

@group freelinking

Hierarchy

Expanded class hierarchy of FreelinkingTest

File

tests/src/Unit/Plugin/Filter/FreelinkingTest.php, line 17

Namespace

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

  /**
   * Freelinking filter plugin.
   *
   * @var \Drupal\freelinking\Plugin\Filter\Freelinking
   */
  protected $filter;

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

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

    // Mock string translation service.
    $tProphet = $this
      ->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $tProphet
      ->translateString(Argument::type('string'))
      ->will(function ($args) {
      return $args[0];
    });
    $this->translationInterfaceMock = $tProphet
      ->reveal();

    // Create a freelinking plugin mock.
    $pluginProphet = $this
      ->prophesize('\\Drupal\\freelinking\\Plugin\\FreelinkingPluginInterface');
    $pluginProphet
      ->getPluginDefinition()
      ->willReturn([
      'title' => 'Dummy',
    ]);
    $pluginProphet
      ->getIndicator()
      ->willReturn('indicator');
    $pluginProphet
      ->getTip()
      ->willReturn('tip');
    $mockPlugin = $pluginProphet
      ->reveal();

    // Create a mock of the freelinking plugin manager.
    $managerProphet = $this
      ->prophesize('\\Drupal\\freelinking\\FreelinkingManagerInterface');
    $managerProphet
      ->createInstance(Argument::type('string'), Argument::type('array'))
      ->willReturn($mockPlugin);

    // Create a mock of the current user.
    $userProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $this->translationInterfaceMock);
    $container
      ->set('freelinking.manager', $managerProphet
      ->reveal());
    $container
      ->set('current_user', $userProphet
      ->reveal());
    \Drupal::setContainer($container);
    $definition = [
      'id' => 'freelinking',
      'title' => 'Freelinking',
      'description' => 'Allowms for a flexible format for linking content.',
      'type' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'provider' => 'freelinking',
      'status' => FALSE,
      'settings' => [
        'default' => 'nodetitle',
        'global_options' => [
          'ignore_upi' => FALSE,
        ],
        'plugins' => [],
        'external_http_request' => FALSE,
      ],
      'weight' => 0,
    ];
    $configuration = [
      'settings' => [
        'plugins' => [
          'dummy' => [
            'plugin' => 'dummy',
            'enabled' => TRUE,
          ],
        ],
      ],
      'weight' => 0,
      'status' => TRUE,
    ];
    $this->filter = new Freelinking($configuration, 'freelinking', $definition, $container
      ->get('freelinking.manager'), $container
      ->get('current_user'));
  }

  /**
   * Asserts that a short tip is returned.
   */
  public function testShortTip() {
    $expected = new TranslatableMarkup('Freelinking helps you easily create HTML links. Links take the form of <code>[[indicator:target|Title]].</code>', [], [], $this->translationInterfaceMock);
    $this
      ->assertEquals($expected, $this->filter
      ->tips());
  }

  /**
   * Asserts that a long tip is returned.
   */
  public function testLongTip() {
    $expectedText = <<<EOF
<p>Freelinking helps you easily create HTML links. Links take the form of <code>[[indicator:target|Title]].</code><br />
Below is a list of available types of freelinks you may use, organized as <strong>Plugin Name</strong>: [<em>indicator</em>].</p>
<ul><li><strong>Dummy</strong> [<em>indicator</em>]: tip</li></ul>
EOF;
    $expected = new TranslatableMarkup($expectedText, [], [], $this->translationInterfaceMock);
    $this
      ->assertEquals($expected, $this->filter
      ->tips(TRUE));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FreelinkingTest::$filter protected property Freelinking filter plugin.
FreelinkingTest::$translationInterfaceMock protected property Translation interface mock.
FreelinkingTest::setUp protected function Overrides UnitTestCase::setUp
FreelinkingTest::testLongTip public function Asserts that a long tip is returned.
FreelinkingTest::testShortTip public function Asserts that a short 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.