freelinking-8.x-3.x-dev/tests/src/Unit/Plugin/freelinking/UserUnauthorizedTest.php

tests/src/Unit/Plugin/freelinking/UserUnauthorizedTest.php
<?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;

/**
 * Tests the user plugin when the current user does not have access.
 *
 * @group freelinking
 */
class UserUnauthorizedTest extends UnitTestCase {

  use ProphecyTrait;

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

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

  /**
   * Container builder.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerBuilder
   */
  protected $container;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::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));
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc