UserUnauthorizedTest.php in Freelinking 4.0.x
File
tests/src/Unit/Plugin/freelinking/UserUnauthorizedTest.php
View source
<?php
namespace Drupal\Tests\freelinking\Unit\Plugin\freelinking;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\freelinking\Plugin\freelinking\User;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class UserUnauthorizedTest extends UnitTestCase {
use ProphecyTrait;
protected $translationInterfaceMock;
protected $plugin;
protected $container;
protected function setUp() : void {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Unauthorized to view user profile.');
$this->translationInterfaceMock = $tProphet
->reveal();
$currentUserProphet = $this
->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
$currentUserProphet
->hasPermission('access user profiles')
->willReturn(FALSE);
$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);
}
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));
}
}