PathChangedHelper example


  public function testPathChangedHelper() {
    $route = \Drupal::service('router.route_provider')->getRouteByName('path.changed.bc');
    $raw_parameters = [
      'block_type' => 'test_block_type',
    ];
    $query = [
      'destination' => 'admin/structure/block',
      'plugin_id' => 'some_block_config',
    ];
    $helper = new PathChangedHelper(
      new RouteMatch('path.changed.bc', $route[]$raw_parameters),
      new Request($query)
    );

    // Assert that oldPath() returns the internal path for path.changed.bc.     $this->assertEquals('old/path/test_block_type', $helper->oldPath());
    // Assert that newPath() returns the internal path for path.changed.     $this->assertEquals('new/path/test_block_type', $helper->newPath());
    // Assert that redirect() returns a RedirectResponse for the absolute URL of     // path.changed, and the query string comes from the Request object with the     // destination parameter removed.

  public function blockContentTypeRedirect(RouteMatchInterface $route_match, Request $request): RedirectResponse {
    @trigger_error('The path /admin/structure/block/block-content/types is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use /admin/structure/block-content. See https://www.drupal.org/node/3320855.', E_USER_DEPRECATED);
    $helper = new PathChangedHelper($route_match$request);
    $params = [
      '%old_path' => $helper->oldPath(),
      '%new_path' => $helper->newPath(),
      '%change_record' => 'https://www.drupal.org/node/3320855',
    ];
    $warning_message = $this->t('You have been redirected from %old_path. Update links, shortcuts, and bookmarks to use %new_path.', $params);
    $this->messenger()->addWarning($warning_message);
    $this->getLogger('block_content')->warning('A user was redirected from %old_path. This redirect will be removed in a future version of Drupal. Update links, shortcuts, and bookmarks to use %new_path. See %change_record for more information.', $params);

    return $helper->redirect();
  }

  
/** * Tests that the constructor validates its arguments. * * @covers ::__construct */
  public function testPathChangedHelperException() {
    $route_match = $this->prophesize(RouteMatchInterface::class);
    $route_match->getRouteName()->willReturn('path.changed.not-bc');

    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Drupal\Core\Routing\PathChangedHelper expects a route name that ends with ".bc".');
    new PathChangedHelper($route_match->reveal()new Request());
  }

}
Home | Imprint | This part of the site doesn't use cookies.