You are here

class PanelsEverywhereRouteSubscriberTest in Panels Everywhere 8.4

@coversDefaultClass \Drupal\panels_everywhere\Routing\PanelsEverywhereRouteSubscriber @group panels_everywhere

Hierarchy

Expanded class hierarchy of PanelsEverywhereRouteSubscriberTest

File

tests/src/Unit/Routing/PanelsEverywhereRouteSubscriberTest.php, line 22

Namespace

Drupal\Tests\panels_everywhere\Unit\Routing
View source
class PanelsEverywhereRouteSubscriberTest extends UnitTestCase {

  /**
   * Tests onAlterRoutes.
   *
   * Specifically that PanelsEverywhereRouteSubscriber does nothing if there are no
   * page entities.
   */
  public function testSubscriberDoesNothingForNoPageEntities() {

    // Given.
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeCollection = new RouteCollection();
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertEmpty($routeCollection
      ->all());
  }

  /**
   * Tests onAlterRoutes.
   *
   * Specifically that PanelsEverywhereRouteSubscriber does nothing if there are no
   * enabled page entities.
   */
  public function testSubscriberDoesNothingForNoEnabledPageEntity() {

    // Given.
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->status()
      ->willReturn(FALSE);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeCollection = new RouteCollection();
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertEmpty($routeCollection
      ->all());
  }

  /**
   * Tests onAlterRoutes.
   *
   * Specifically that PanelsEverywhereRouteSubscriber does nothing if there
   * are no variants on page entity.
   */
  public function testSubscriberDoesNothingForNoVariantsOnPageEntity() {

    // Given.
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->status()
      ->willReturn(TRUE);
    $pageEntity
      ->getVariants()
      ->willReturn([]);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeCollection = new RouteCollection();
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertEmpty($routeCollection
      ->all());
  }

  /**
   * Tests onAlterRoutes.
   *
   * Specifically that PanelsEverywhereRouteSubscriber does nothing if there
   * are no variants of plugin-id 'panels_everywhere_variant' on page entity.
   */
  public function testSubscriberDoesNothingForNoPanelsEveryWhereVariantOnPageEntity() {
    $pageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $pageVariant
      ->getVariantPluginId()
      ->willReturn('not_panels_everywhere_variant');
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->status()
      ->willReturn(TRUE);
    $pageEntity
      ->getVariants()
      ->willReturn([
      'some_variant_id' => $pageVariant
        ->reveal(),
    ]);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeCollection = new RouteCollection();
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertEmpty($routeCollection
      ->all());
  }

  /**
   * Tests onAlterRoutes.
   *
   * Specifically that PanelsEverywhereRouteSubscriber does nothing if the
   * corresponding route for the 'panels_everywhere_variant' is not in
   * the route collection.
   */
  public function testSubscriberDoesNothingForNoVariantRouteInCollection() {
    $page_id = 'some_page_id';
    $variant_id = 'some_variant_id';
    $pageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $pageVariant
      ->id()
      ->willReturn($variant_id);
    $pageVariant
      ->getVariantPluginId()
      ->willReturn('panels_everywhere_variant');
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->id()
      ->willReturn($page_id);
    $pageEntity
      ->status()
      ->willReturn(TRUE);
    $pageEntity
      ->getVariants()
      ->willReturn([
      'some_variant_id' => $pageVariant
        ->reveal(),
    ]);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeCollection = new RouteCollection();
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertEmpty($routeCollection
      ->all());
  }

  /**
   * Test onAlterRoutes.
   *
   * Specifically that the page-manager variant route-override is and
   * the panels_everywhere_page_id is set on the original route.
   */
  public function testSubscriberRemovesVariantRouteAndSetsPanelsEverywherePageIdForOriginalRouteInCollection() {
    $page_id = 'some_page_id';
    $variant_id = 'some_variant_id';
    $route_name_variant = "page_manager.page_view_{$page_id}_{$variant_id}";
    $route_name_original = 'original.route_name';
    $variantPlugin = $this
      ->prophesize(PanelsEverywhereDisplayVariant::class);
    $variantPlugin
      ->isRouteOverrideEnabled()
      ->willReturn(FALSE);
    $pageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $pageVariant
      ->id()
      ->willReturn($variant_id);
    $pageVariant
      ->getVariantPluginId()
      ->willReturn('panels_everywhere_variant');
    $pageVariant
      ->getVariantPlugin()
      ->willReturn($variantPlugin
      ->reveal());
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->id()
      ->willReturn($page_id);
    $pageEntity
      ->status()
      ->willReturn(TRUE);
    $pageEntity
      ->getVariants()
      ->willReturn([
      $variant_id => $pageVariant
        ->reveal(),
    ]);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeOriginal = new Route('/some-path');
    $routeVariant = new Route('/some-path');
    $routeVariant
      ->setDefault('overridden_route_name', $route_name_original);
    $routeCollection = new RouteCollection();
    $routeCollection
      ->add($route_name_original, $routeOriginal);
    $routeCollection
      ->add($route_name_variant, $routeVariant);
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertNull($routeCollection
      ->get($route_name_variant));
    self::assertEquals($page_id, $routeOriginal
      ->getDefault('page_id'));
  }

  /**
   * Test onAlterRoutes.
   *
   * Specifically that the page-manager variant route-override is not removed
   * when the removal of route-overrides is disables.
   */
  public function testSubscriberDoesNotRemoveVariantRouteAndSetsPanelsEverywherePageIdOnItForDisabledRouteOverrideRemovalBehaviour() {
    $page_id = 'some_page_id';
    $variant_id = 'some_variant_id';
    $route_name_variant = "page_manager.page_view_{$page_id}_{$variant_id}";
    $route_name_original = 'original.route_name';
    $variantPlugin = $this
      ->prophesize(PanelsEverywhereDisplayVariant::class);
    $variantPlugin
      ->isRouteOverrideEnabled()
      ->willReturn(TRUE);
    $pageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $pageVariant
      ->id()
      ->willReturn($variant_id);
    $pageVariant
      ->getVariantPluginId()
      ->willReturn('panels_everywhere_variant');
    $pageVariant
      ->getVariantPlugin()
      ->willReturn($variantPlugin
      ->reveal());
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->id()
      ->willReturn($page_id);
    $pageEntity
      ->status()
      ->willReturn(TRUE);
    $pageEntity
      ->getVariants()
      ->willReturn([
      $variant_id => $pageVariant
        ->reveal(),
    ]);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeOriginal = new Route('/some-path');
    $routeVariant = new Route('/some-path');
    $routeVariant
      ->setDefault('overridden_route_name', $route_name_original);
    $routeCollection = new RouteCollection();
    $routeCollection
      ->add($route_name_original, $routeOriginal);
    $routeCollection
      ->add($route_name_variant, $routeVariant);
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertNotNull($routeCollection
      ->get($route_name_variant));
    self::assertEquals($page_id, $routeVariant
      ->getDefault('page_id'));
  }
  public function testSubscriberWillSetPanelsEverywherePageIdForOtherVariantsOnThePageIfOverrideDisabledAndRemoveOverriddenRoute() {
    $page_id = 'some_page_id';
    $variant_id = 'some_variant_id';
    $other_variant_id = 'some_other_variant_id';
    $route_name_variant = "page_manager.page_view_{$page_id}_{$variant_id}";
    $route_name_other_variant = "page_manager.page_view_{$page_id}_{$other_variant_id}";
    $variantPlugin = $this
      ->prophesize(PanelsEverywhereDisplayVariant::class);
    $variantPlugin
      ->isRouteOverrideEnabled()
      ->willReturn(FALSE);
    $pageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $pageVariant
      ->id()
      ->willReturn($variant_id);
    $pageVariant
      ->getVariantPluginId()
      ->willReturn('panels_everywhere_variant');
    $pageVariant
      ->getVariantPlugin()
      ->willReturn($variantPlugin
      ->reveal());
    $otherPageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $otherPageVariant
      ->id()
      ->willReturn($other_variant_id);
    $otherPageVariant
      ->getVariantPluginId()
      ->willReturn('other_variant');
    $pageEntity = $this
      ->prophesize(PageInterface::class);
    $pageEntity
      ->id()
      ->willReturn($page_id);
    $pageEntity
      ->status()
      ->willReturn(TRUE);
    $pageEntity
      ->getVariants()
      ->willReturn([
      $variant_id => $pageVariant
        ->reveal(),
      $other_variant_id => $otherPageVariant
        ->reveal(),
    ]);
    $pageStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $pageStorage
      ->loadMultiple()
      ->willReturn([
      $pageEntity
        ->reveal(),
    ]);
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('page')
      ->willReturn($pageStorage
      ->reveal());
    $cacheTagsInvalidator = $this
      ->prophesize(CacheTagsInvalidatorInterface::class);
    $routeVariant = new Route('/some-path');
    $routeOtherVariant = new Route('/some-path');
    $routeCollection = new RouteCollection();
    $routeCollection
      ->add($route_name_variant, $routeVariant);
    $routeCollection
      ->add($route_name_other_variant, $routeOtherVariant);
    $event = new RouteBuildEvent($routeCollection);

    // When.
    $subscriber = new PanelsEverywhereRouteSubscriber($entityTypeManager
      ->reveal(), $cacheTagsInvalidator
      ->reveal());
    $subscriber
      ->onAlterRoutes($event);

    // Then.
    self::assertNull($routeCollection
      ->get($route_name_variant));
    self::assertEquals($page_id, $routeOtherVariant
      ->getDefault('page_id'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PanelsEverywhereRouteSubscriberTest::testSubscriberDoesNothingForNoEnabledPageEntity public function Tests onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberDoesNothingForNoPageEntities public function Tests onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberDoesNothingForNoPanelsEveryWhereVariantOnPageEntity public function Tests onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberDoesNothingForNoVariantRouteInCollection public function Tests onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberDoesNothingForNoVariantsOnPageEntity public function Tests onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberDoesNotRemoveVariantRouteAndSetsPanelsEverywherePageIdOnItForDisabledRouteOverrideRemovalBehaviour public function Test onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberRemovesVariantRouteAndSetsPanelsEverywherePageIdForOriginalRouteInCollection public function Test onAlterRoutes.
PanelsEverywhereRouteSubscriberTest::testSubscriberWillSetPanelsEverywherePageIdForOtherVariantsOnThePageIfOverrideDisabledAndRemoveOverriddenRoute public function
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.
UnitTestCase::setUp protected function 340