You are here

class NodeTest in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/Plugin/freelinking/NodeTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\NodeTest

Tests the nid plugin.

@group freelinking

Hierarchy

Expanded class hierarchy of NodeTest

File

tests/src/Unit/Plugin/freelinking/NodeTest.php, line 16

Namespace

Drupal\Tests\freelinking\Unit\Plugin\freelinking
View source
class NodeTest extends NodeTestBase {

  /**
   * The translation interface.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationInterfaceMock;

  /**
   * The plugin to test.
   *
   * @var \Drupal\freelinking\Plugin\freelinking\Node
   */
  protected $plugin;

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

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

    // Mock string translation interface.
    $tProphet = $this
      ->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $tProphet
      ->translateString(Argument::any())
      ->willReturn('Click to view a local node.');
    $this->translationInterfaceMock = $tProphet
      ->reveal();

    // Mock node entity.
    $nodeProphet = $this
      ->prophesize('\\Drupal\\node\\Entity\\Node');
    $nodeProphet
      ->id()
      ->willReturn(1);
    $nodeProphet
      ->label()
      ->willReturn('Test Node');
    $nodeProphet
      ->language()
      ->willReturn(self::getDefaultLanguage());

    // Mock node storage interface.
    $nodeStorageProphet = $this
      ->prophesize('\\Drupal\\node\\NodeStorageInterface');
    $nodeStorageProphet
      ->load(1)
      ->willReturn($nodeProphet
      ->reveal());
    $nodeStorageProphet
      ->load(2)
      ->willReturn(NULL);

    // Mock entity type manager.
    $entityManagerProphet = $this
      ->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $entityManagerProphet
      ->getStorage('node')
      ->willReturn($nodeStorageProphet
      ->reveal());
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entityManagerProphet
      ->reveal());
    $container
      ->set('string_translation', $this->translationInterfaceMock);
    \Drupal::setContainer($container);
    $this->container = $container;
    $plugin_definition = [
      'id' => 'nid',
      'title' => 'Node ID',
      'hidden' => FALSE,
      'weight' => 0,
      'settings' => [],
    ];
    $this->plugin = Node::create($container, [], 'nid', $plugin_definition);
  }

  /**
   * Assert that getIndicator method returns correct value.
   *
   * @param string $indicator
   *   The indicator string to test.
   * @param int $expected
   *   The expected result from preg_match().
   *
   * @dataProvider indicatorProvider
   */
  public function testGetIndicator($indicator, $expected) {
    $this
      ->assertEquals($expected, preg_match($this->plugin
      ->getIndicator(), $indicator));
  }

  /**
   * Assert the getTip method returns correct value.
   */
  public function testGetTip() {
    $this
      ->assertEquals('Click to view a local node.', $this->plugin
      ->getTip()
      ->render());
  }

  /**
   * Assert the buildLink method returns render array.
   *
   * @param array $target
   *   The target array.
   * @param array $expected
   *   The expected render array.
   * @param bool $shouldFailover
   *   Test logic to place container-dependent render array items.
   *
   * @dataProvider buildLinkProvider
   */
  public function testBuildLink(array $target, array $expected, $shouldFailover = FALSE) {
    $language = self::getDefaultLanguage();
    if ($shouldFailover) {
      $expected['#message'] = new TranslatableMarkup($expected['#message'], [
        '@nid' => '2',
      ], [], $this->translationInterfaceMock);
    }
    else {
      $expected['#url'] = Url::fromRoute('entity.node.canonical', [
        'node' => 1,
      ], [
        'language' => $language,
      ]);
      $expected['#attributes']['title'] = new TranslatableMarkup('Click to view a local node', [], [], $this->translationInterfaceMock);
    }
    $this
      ->assertEquals($expected, $this->plugin
      ->buildLink($target));
  }

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

  /**
   * Provide test parameters for ::testBuildLink.
   *
   * @return array
   *   An array of test parameters.
   */
  public function buildLinkProvider() {
    $language = self::getDefaultLanguage();
    $failoverTarget = [
      'target' => 'nid:2',
      'dest' => '2',
      'language' => $language,
    ];
    $failoverExpected = [
      '#theme' => 'freelink_error',
      '#plugin' => 'nid',
      '#message' => 'Invalid node ID @nid',
    ];
    $successTarget = [
      'target' => 'nid:1',
      'dest' => '1',
      'language' => $language,
    ];
    $successExpected = [
      '#type' => 'link',
      '#title' => 'Test Node',
      '#attributes' => [
        'title' => 'Click to view a local node',
      ],
    ];
    return [
      [
        $failoverTarget,
        $failoverExpected,
        TRUE,
      ],
      [
        $successTarget,
        $successExpected,
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeTest::$container protected property A mock container.
NodeTest::$plugin protected property The plugin to test.
NodeTest::$translationInterfaceMock protected property The translation interface.
NodeTest::buildLinkProvider public function Provide test parameters for ::testBuildLink.
NodeTest::indicatorProvider public function Provide test parameters for ::testGetIndicator.
NodeTest::setUp protected function Overrides UnitTestCase::setUp
NodeTest::testBuildLink public function Assert the buildLink method returns render array.
NodeTest::testGetIndicator public function Assert that getIndicator method returns correct value.
NodeTest::testGetTip public function Assert the getTip method returns correct value.
NodeTestBase::getDefaultLanguage public static function Get a language object to pass into the plugin.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::setUpBeforeClass public static function