You are here

class UserUnauthorizedTest in Freelinking 8.3

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

Tests the user plugin when the current user does not have access.

@group freelinking

Hierarchy

Expanded class hierarchy of UserUnauthorizedTest

File

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

Namespace

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

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

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

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

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

    // Mock current user interface.
    $currentUserProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $currentUserProphet
      ->hasPermission('access user profiles')
      ->willReturn(FALSE);

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

  /**
   * Assert the buildLink method returns correct render array.
   */
  public function testBuildLink() {
    $expected = [
      '#theme' => 'freelink_error',
      '#plugin' => 'user',
      '#message' => new TranslatableMarkup('Unauthorized to view user profile.', [], [], $this->translationInterfaceMock),
    ];
    $target = [
      'target' => 'uid:2|Some user',
      'dest' => '2',
      'language' => NULL,
    ];
    $this
      ->assertEquals($expected, $this->plugin
      ->buildLink($target));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
UserUnauthorizedTest::$plugin protected property The plugin to test.
UserUnauthorizedTest::$translationInterfaceMock protected property The translation interface.
UserUnauthorizedTest::setUp protected function Overrides UnitTestCase::setUp
UserUnauthorizedTest::testBuildLink public function Assert the buildLink method returns correct render array.