og-8.x-1.x-dev/tests/src/Unit/OgLocalTaskTest.php

tests/src/Unit/OgLocalTaskTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\og\Unit;

use Prophecy\Prophecy\ObjectProphecy;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Routing\RouteProvider;
use Drupal\Tests\UnitTestCase;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\Plugin\Derivative\OgLocalTask;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Routing\Route;

/**
 * Tests OG local task definition.
 *
 * Assert that the "Group" tab is properly added.
 *
 * @group og
 * @coversDefaultClass \Drupal\og\Plugin\Derivative\OgLocalTask
 */
class OgLocalTaskTest extends UnitTestCase {

  use ProphecyTrait;

  /**
   * The group type manager.
   */
  protected GroupTypeManagerInterface|ObjectProphecy $groupTypeManager;

  /**
   * Route provider object.
   */
  protected RouteProvider|ObjectProphecy $routeProvider;

  /**
   * The route service.
   */
  protected Route|ObjectProphecy $route;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->groupTypeManager = $this->prophesize(GroupTypeManagerInterface::class);
    $this->routeProvider = $this->prophesize(RouteProvider::class);
    $this->route = $this->prophesize(Route::class);

    $container = new ContainerBuilder();
    $container->set('string_translation', $this->getStringTranslationStub());
    \Drupal::setContainer($container);
  }

  /**
   * Tests setting local task definitions.
   *
   * @covers ::getDerivativeDefinitions
   */
  public function testGetDerivativeDefinitions(): void {
    $entity_type_id1 = $this->randomMachineName();
    $entity_type_id2 = $this->randomMachineName();

    $group_map = [
      // We don't need to add the bundles data, as they are skipped by doing
      // array_keys() on the tested method.
      $entity_type_id1 => [],
      $entity_type_id2 => [],
    ];

    $this
      ->groupTypeManager
      ->getGroupMap()
      ->willReturn($group_map);

    foreach (array_keys($group_map) as $entity_type_id) {
      $route_name = "entity.$entity_type_id.og_admin_routes";

      $this
        ->routeProvider
        ->getRoutesByNames([$route_name])
        ->willReturn([
          $this->route->reveal(),
          $this->route->reveal(),
        ]);
    }

    $og_local_task = new OgLocalTask($this->groupTypeManager->reveal(), $this->routeProvider->reveal());
    $derivatives = $og_local_task->getDerivativeDefinitions([]);

    $this->assertCount(2, $derivatives);

  }

}

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

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