You are here

class PathAliasTest in Freelinking 8.3

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

Tests the path_alias plugin.

@group freelinking

Hierarchy

Expanded class hierarchy of PathAliasTest

File

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

Namespace

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

  /**
   * The container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * String translation 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::any())
      ->willReturn('Click to view a local node.');
    $this->translationInterfaceMock = $tProphet
      ->reveal();

    // Mock module handler service.
    $moduleHandlerProphet = $this
      ->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $moduleHandlerProphet
      ->moduleExists('search')
      ->willReturn(FALSE);

    // Path alias manager service.
    $aliasManagerProphet = $this
      ->prophesize('\\Drupal\\path_alias\\AliasManagerInterface');
    $aliasManagerProphet
      ->getPathByAlias(Argument::any(), Argument::any())
      ->will(function ($args) {
      return $args[0] === '/validalias' ? '/node/1' : '/invalidalias';
    });

    // Path validator service.
    $pathValidatorProphet = $this
      ->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
    $this->container = new ContainerBuilder();
    $this->container
      ->set('string_translation', $this->translationInterfaceMock);
    $this->container
      ->set('module_handler', $moduleHandlerProphet
      ->reveal());
    $this->container
      ->set('path_alias.manager', $aliasManagerProphet
      ->reveal());
    $this->container
      ->set('path.validator', $pathValidatorProphet
      ->reveal());
    \Drupal::setContainer($this->container);
  }

  /**
   * Get plugin instance.
   *
   * @param string $failoverOption
   *   The failover option.
   *
   * @return \Drupal\freelinking\Plugin\freelinking\PathAlias
   *   A plugin instance.
   */
  protected function getPlugin($failoverOption = 'search') {
    $configuration = [
      'settings' => [
        'failover' => $failoverOption,
      ],
    ];
    $plugin_definition = [
      'id' => 'path_alias',
      'title' => 'Path Alias',
      'hidden' => FALSE,
      'weight' => 0,
    ] + $configuration;
    return PathAlias::create($this->container, $configuration, 'path_alias', $plugin_definition);
  }

  /**
   * Asserts that the indicator is functional.
   *
   * @param string $indicator
   *   The indicator string.
   * @param int $expected
   *   The expected value from preg_match.
   *
   * @dataProvider indicatorProvider
   */
  public function testGetIndicator($indicator, $expected) {
    $plugin = $this
      ->getPlugin();
    $this
      ->assertEquals($expected, preg_match($plugin
      ->getIndicator(), $indicator));
  }

  /**
   * Asserts that getTip is functional.
   */
  public function testGetTip() {
    $plugin = $this
      ->getPlugin();
    $this
      ->assertEquals('Click to view a local node.', $plugin
      ->getTip()
      ->render());
  }

  /**
   * Asserts that defaultConfiguration provides the correct settings.
   */
  public function testDefaultConfiguration() {
    $plugin = $this
      ->getPlugin();
    $this
      ->assertEquals([
      'settings' => [
        'failover' => 'search',
      ],
    ], $plugin
      ->defaultConfiguration());
  }

  /**
   * Asserts that buildLink is functional for valid and invalid path aliases.
   *
   * @param string $alias
   *   The alias string to test.
   * @param array $expected
   *   The expected render array without container dependencies.
   *
   * @dataProvider buildLinkProvider
   */
  public function testBuildLink($alias, array $expected) {
    $target = [
      'text' => 'A valid path alias',
      'dest' => $alias,
      'target' => 'alias:' . $alias . '|A valid path alias',
      'language' => NULL,
    ];
    $plugin = $this
      ->getPlugin('error');
    if ($alias === 'validalias') {
      $expected['#url'] = Url::fromUri('base:node/1', [
        'language' => NULL,
      ]);
      $expected['#attributes'] = [
        'title' => new TranslatableMarkup('Click to view a local node.', [], [], $this->translationInterfaceMock),
      ];
    }
    else {
      $expected['#message'] = new TranslatableMarkup('path “%path” not found', [
        '%path' => '/invalidalias',
      ], [], $this->translationInterfaceMock);
    }
    $this
      ->assertEquals($expected, $plugin
      ->buildLink($target));
  }

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

  /**
   * Provider test parameters for ::testBuildLink.
   *
   * @return array
   *   An array of test parameters.
   */
  public function buildLinkProvider() {
    $validExpected = [
      '#type' => 'link',
      '#title' => 'A valid path alias',
    ];
    $invalidExpected = [
      '#theme' => 'freelink_error',
      '#plugin' => 'path_alias',
    ];
    return [
      [
        'validalias',
        $validExpected,
      ],
      [
        'invalidalias',
        $invalidExpected,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathAliasTest::$container protected property The container.
PathAliasTest::$translationInterfaceMock protected property String translation mock.
PathAliasTest::buildLinkProvider public function Provider test parameters for ::testBuildLink.
PathAliasTest::getPlugin protected function Get plugin instance.
PathAliasTest::indicatorProvider public function Provide test parameters for ::testGetIndicator.
PathAliasTest::setUp protected function Overrides UnitTestCase::setUp
PathAliasTest::testBuildLink public function Asserts that buildLink is functional for valid and invalid path aliases.
PathAliasTest::testDefaultConfiguration public function Asserts that defaultConfiguration provides the correct settings.
PathAliasTest::testGetIndicator public function Asserts that the indicator is functional.
PathAliasTest::testGetTip public function Asserts that getTip is functional.
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.