onAlterRoutes example

class AdminRouteSubscriberTest extends UnitTestCase {

  /** * @covers ::alterRoutes * @covers ::isHtmlRoute * * @dataProvider providerTestAlterRoutes */
  public function testAlterRoutes(Route $route$is_admin) {
    $collection = new RouteCollection();
    $collection->add('the_route', $route);
    (new AdminRouteSubscriber())->onAlterRoutes(new RouteBuildEvent($collection));

    $this->assertSame($is_admin$route->getOption('_admin_route'));
  }

  public function providerTestAlterRoutes() {
    $data = [];
    $data['non-admin'] = [
      new Route('/foo'),
      NULL,
    ];
    $data['admin prefix'] = [
      

  public function testSetLatestRevisionFlag($defaults$parameters$expected_parameters = FALSE) {
    $route = new Route('/foo/{entity_test}', $defaults[][
      'parameters' => $parameters,
    ]);

    $route_collection = new RouteCollection();
    $route_collection->add('test', $route);
    $event = new RouteBuildEvent($route_collection);
    $this->routeSubscriber->onAlterRoutes($event);

    // If expected parameters have not been provided, assert they are unchanged.     $this->assertEquals($expected_parameters ?: $parameters$route->getOption('parameters'));
  }

}

/** * A concrete entity. */
class TestEntity extends EntityBase {
}
->getMock();
    $route_collection = new RouteCollection();
    $route_collection->add('test', new Route('/admin/foo', ['_controller' => 'Drupal\ExampleController']));
    $route_collection->add('test2', new Route('/admin/bar', ['_controller' => 'Drupal\ExampleController']));
    $event->expects($this->once())
      ->method('getRouteCollection')
      ->willReturn($route_collection);

    $this->state->expects($this->once())
      ->method('set')
      ->with('routing.non_admin_routes', []);
    $this->preloader->onAlterRoutes($event);
    $this->preloader->onFinishedRoutes(new Event());
  }

  /** * Tests onAlterRoutes with "admin" appearing in the path. */
  public function testOnAlterRoutesWithAdminPathNoAdminRoute() {
    $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $route_collection = new RouteCollection();
    

  public function testOnRouteBuildingValidVariables(Route $route) {
    $route_collection = new RouteCollection();
    $route_collection->add('test', $route);

    $event = new RouteBuildEvent($route_collection);
    $subscriber = new SpecialAttributesRouteSubscriber();
    $this->assertNull($subscriber->onAlterRoutes($event));
  }

  /** * Tests the onAlterRoutes method for invalid variables. * * @param \Symfony\Component\Routing\Route $route * The route to check. * * @dataProvider providerTestOnRouteBuildingInvalidVariables * @covers ::onAlterRoutes */
  
$collection->add('test_route2', new Route('/test/path2'));
    });

    $this->sectionStorageManager->loadEmpty('first')->willReturn($section_storage_first->reveal());
    $this->sectionStorageManager->loadEmpty('second')->willReturn($section_storage_second->reveal());
    $definitions['first'] = new SectionStorageDefinition();
    $definitions['second'] = new SectionStorageDefinition();
    $this->sectionStorageManager->getDefinitions()->willReturn($definitions);

    $collection = new RouteCollection();
    $event = new RouteBuildEvent($collection);
    $this->routeBuilder->onAlterRoutes($event);
    $this->assertEquals($expected$collection->all());
    $this->assertSame(array_keys($expected)array_keys($collection->all()));
  }

}
return ['test_id.page_2' => 'views.test_id.page_2'];
      });
    $display_2->expects($this->once())
      ->method('alterRoutes')
      ->willReturn([]);

    // Ensure that even both the collectRoutes() and alterRoutes() methods     // are called on the displays, we ensure that the route first defined by     // views is dropped.
    $this->routeSubscriber->routes();
    $this->assertNull($this->routeSubscriber->onAlterRoutes($route_event));

    $this->state->expects($this->once())
      ->method('set')
      ->with('views.view_route_names', ['test_id.page_1' => 'test_route', 'test_id.page_2' => 'views.test_id.page_2']);

    $collection = $route_event->getRouteCollection();
    $this->assertEquals(['test_route', 'test_route_2', 'views.test_id.page_2']array_keys($collection->all()));

    $this->routeSubscriber->routeRebuildFinished();
  }

  
$updated_routes->add($field_ui_base_route$this->collection->get($field_ui_base_route));
      $event = new RouteBuildEvent($updated_routes);

      // Apply route subscribers that add routes based on field_ui_base_route,       // in the order of their weights.       $subscribers = [
        'field_ui' => 'field_ui.subscriber',
        'content_translation' => 'content_translation.subscriber',
      ];
      foreach ($subscribers as $module_name => $service_name) {
        if ($this->moduleHandler->moduleExists($module_name)) {
          \Drupal::service($service_name)->onAlterRoutes($event);
        }
      }

      $updated_routes->remove($field_ui_base_route);
      $route_names = array_merge($route_namesarray_keys($updated_routes->all()));
      $route_names = array_merge($route_names[
        // @see \Drupal\config_translation\Routing\RouteSubscriber::alterRoutes()         "config_translation.item.add.{$field_ui_base_route}",
        "config_translation.item.edit.{$field_ui_base_route}",
        "config_translation.item.delete.{$field_ui_base_route}",
      ]);
    }

  public function testRemoveRoute($route_name, array $requirements$removed) {
    $collection = new RouteCollection();
    $route = new Route('', []$requirements);
    $collection->add($route_name$route);

    $event = new RouteBuildEvent($collection);
    $route_subscriber = new ModuleRouteSubscriber($this->moduleHandler);
    $route_subscriber->onAlterRoutes($event);

    if ($removed) {
      $this->assertNull($collection->get($route_name));
    }
    else {
      $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get($route_name));
    }
  }

  /** * Data provider for testRemoveRoute(). */
Home | Imprint | This part of the site doesn't use cookies.