View source
<?php
namespace Drupal\Tests\freelinking\Unit\Plugin\freelinking;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\freelinking\User;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class UserTest extends UnitTestCase {
protected $translationInterfaceMock;
protected $plugin;
protected function setUp() {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to view user profile.');
$this->translationInterfaceMock = $tProphet
->reveal();
$currentUserProphet = $this
->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
$currentUserProphet
->hasPermission('access user profiles')
->willReturn(TRUE);
$userProphet = $this
->prophesize('\\Drupal\\user\\Entity\\User');
$userProphet
->id()
->willReturn(1);
$userProphet
->getDisplayName()
->willReturn('admin');
$userStorageProphet = $this
->prophesize('\\Drupal\\user\\UserStorageInterface');
$userStorageProphet
->load(1)
->willReturn($userProphet
->reveal());
$userStorageProphet
->load(2)
->willReturn(NULL);
$userStorageProphet
->loadByProperties([
'name' => 'admin',
])
->willReturn([
$userProphet
->reveal(),
]);
$entityManagerProphet = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entityManagerProphet
->getStorage('user')
->willReturn($userStorageProphet
->reveal());
$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',
'hidden' => FALSE,
'weight' => 0,
'settings' => [],
];
$this->plugin = User::create($container, [], 'user', $plugin_definition);
}
public function testGetIndicator($indicator, $expected) {
$this
->assertEquals($expected, preg_match($this->plugin
->getIndicator(), $indicator));
}
public function testGetTip() {
$this
->assertEquals('Click to view user profile.', $this->plugin
->getTip()
->render());
}
public function testBuildLink(array $target, array $expected, $shouldFailover = FALSE) {
if ($shouldFailover) {
$defaultMessage = $expected['#message'];
$expected['#message'] = new TranslatableMarkup($defaultMessage, [
'%user' => '2',
], [], $this->translationInterfaceMock);
}
else {
$expected['#url'] = Url::fromRoute('entity.user.canonical', [
'user' => 1,
], [
'language' => NULL,
]);
$expected['#attributes']['title'] = new TranslatableMarkup('Click to view user profile.', [], [], $this->translationInterfaceMock);
}
$this
->assertEquals($expected, $this->plugin
->buildLink($target));
}
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'u',
1,
],
[
'user',
1,
],
[
'username',
1,
],
[
'uid',
1,
],
[
'userid',
1,
],
];
}
public function buildLinkProvider() {
$failoverTarget = [
'target' => 'uid:2',
'dest' => '2',
'language' => NULL,
];
$failoverExpected = [
'#theme' => 'freelink_error',
'#plugin' => 'user',
'#message' => 'User %user not found',
];
$successTarget = [
'target' => 'uid:1',
'dest' => '1',
'language' => NULL,
];
$successExpected = [
'#type' => 'link',
'#title' => 'admin',
'#attributes' => [
'title' => 'Click to view user profile.',
],
];
$userNameTarget = [
'target' => 'user:admin',
'dest' => 'admin',
'language' => NULL,
];
$userNameExpected = [
'#type' => 'link',
'#title' => 'admin',
'#attributes' => [
'title' => 'Click to view user profile.',
],
];
return [
[
$failoverTarget,
$failoverExpected,
TRUE,
],
[
$successTarget,
$successExpected,
FALSE,
],
[
$userNameTarget,
$userNameExpected,
FALSE,
],
];
}
}