You are here

class OgAdminRoutesControllerTest in Organic groups 8

Tests the OG admin routes overview route.

@group og @coversDefaultClass \Drupal\og\Controller\OgAdminRoutesController

Hierarchy

Expanded class hierarchy of OgAdminRoutesControllerTest

File

tests/src/Unit/OgAdminRoutesControllerTest.php, line 26

Namespace

Drupal\Tests\og\Unit
View source
class OgAdminRoutesControllerTest extends UnitTestCase {

  /**
   * The access manager service.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $accessManager;

  /**
   * Route provider object.
   *
   * @var \Drupal\Core\Routing\RouteProvider|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $routeProvider;

  /**
   * The route service.
   *
   * @var \Symfony\Component\Routing\Route|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $route;

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $routeMatch;

  /**
   * The event dispatcher service.
   *
   * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $eventDispatcher;

  /**
   * The group entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $group;

  /**
   * The OG admin route event.
   *
   * @var \Drupal\og\Event\OgAdminRoutesEvent
   */
  protected $event;

  /**
   * The entity type ID of the group entity.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The routes info as returned from the event subscribers.
   *
   * @var array
   */
  protected $routesInfo;

  /**
   * The Url object.
   *
   * @var \Drupal\Core\Url
   */
  protected $url;

  /**
   * The entity ID.
   *
   * @var int
   */
  protected $entityId;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->accessManager = $this
      ->prophesize(AccessManagerInterface::class);
    $this->routeMatch = $this
      ->prophesize(RouteMatchInterface::class);
    $this->group = $this
      ->prophesize(EntityInterface::class);
    $this->event = $this
      ->prophesize(OgAdminRoutesEvent::class);
    $this->eventDispatcher = $this
      ->prophesize(ContainerAwareEventDispatcher::class);
    $this->route = $this
      ->prophesize(Route::class);
    $this->entityTypeId = $this
      ->randomMachineName();
    $this->entityId = rand(20, 30);
    $this->url = $this
      ->prophesize(Url::class);
    $this->routesInfo = [
      $this
        ->randomMachineName() => [
        'title' => $this
          ->randomMachineName(),
        'description' => $this
          ->randomMachineName(),
      ],
      $this
        ->randomMachineName() => [
        'title' => $this
          ->randomMachineName(),
        'description' => $this
          ->randomMachineName(),
      ],
    ];
    $this->routeMatch
      ->getRouteObject()
      ->willReturn($this->route);
    $parameter_name = $this
      ->randomMachineName();
    $this->route
      ->getOption('_og_entity_type_id')
      ->willReturn($parameter_name);
    $this->routeMatch
      ->getParameter($parameter_name)
      ->willReturn($this->group
      ->reveal());
    $this->group
      ->getEntityTypeId()
      ->willReturn($this->entityTypeId);
    $this->group
      ->id()
      ->willReturn($this->entityId);
    $this->eventDispatcher
      ->dispatch(OgAdminRoutesEventInterface::EVENT_NAME, Argument::type(OgAdminRoutesEvent::class))
      ->willReturn($this->event
      ->reveal())
      ->shouldBeCalled();
    $this->event
      ->getRoutes($this->entityTypeId)
      ->willReturn($this->routesInfo)
      ->shouldBeCalled();

    // Set the container for the string translation service.
    $translation = $this
      ->getStringTranslationStub();
    $container = new ContainerBuilder();
    $container
      ->set('access_manager', $this->accessManager
      ->reveal());
    $container
      ->set('string_translation', $translation);
    \Drupal::setContainer($container);
  }

  /**
   * Tests overview with non-accessible routes.
   *
   * @covers ::overview
   */
  public function testRoutesWithNoAccess() {
    $result = $this
      ->getRenderElementResult(FALSE);
    $this
      ->assertEquals('You do not have any administrative items.', $result['#markup']);
  }

  /**
   * Tests overview with accessible routes.
   *
   * @covers ::overview
   */
  public function testRoutesWithAccess() {
    $result = $this
      ->getRenderElementResult(TRUE);
    foreach ($result['og_admin_routes']['#content'] as $key => $value) {
      $this
        ->assertEquals($this->routesInfo[$key]['title'], $value['title']);
      $this
        ->assertEquals($this->routesInfo[$key]['description'], $value['description']);
    }
  }

  /**
   * Return the render array from calling the "overview" method.
   *
   * @param bool $allow_access
   *   Indicate if access to the routes should be given.
   *
   * @return array
   *   The render array.
   */
  protected function getRenderElementResult($allow_access) {
    $parameters = [
      $this->entityTypeId => $this->entityId,
    ];
    foreach (array_keys($this->routesInfo) as $name) {
      $route_name = "entity.{$this->entityTypeId}.og_admin_routes.{$name}";
      $this->accessManager
        ->checkNamedRoute($route_name, $parameters)
        ->willReturn($allow_access);
    }
    $og_admin_routes_controller = new OgAdminRoutesController($this->eventDispatcher
      ->reveal(), $this->accessManager
      ->reveal());
    return $og_admin_routes_controller
      ->overview($this->routeMatch
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgAdminRoutesControllerTest::$accessManager protected property The access manager service.
OgAdminRoutesControllerTest::$entityId protected property The entity ID.
OgAdminRoutesControllerTest::$entityTypeId protected property The entity type ID of the group entity.
OgAdminRoutesControllerTest::$event protected property The OG admin route event.
OgAdminRoutesControllerTest::$eventDispatcher protected property The event dispatcher service.
OgAdminRoutesControllerTest::$group protected property The group entity.
OgAdminRoutesControllerTest::$route protected property The route service.
OgAdminRoutesControllerTest::$routeMatch protected property The route match service.
OgAdminRoutesControllerTest::$routeProvider protected property Route provider object.
OgAdminRoutesControllerTest::$routesInfo protected property The routes info as returned from the event subscribers.
OgAdminRoutesControllerTest::$url protected property The Url object.
OgAdminRoutesControllerTest::getRenderElementResult protected function Return the render array from calling the "overview" method.
OgAdminRoutesControllerTest::setUp protected function Overrides UnitTestCase::setUp
OgAdminRoutesControllerTest::testRoutesWithAccess public function Tests overview with accessible routes.
OgAdminRoutesControllerTest::testRoutesWithNoAccess public function Tests overview with non-accessible routes.
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.