Commit faeacbea authored by catch's avatar catch
Browse files

Issue #3260805 by andypost: Remove deprecated code from core/lib/Drupal/Core/Routing

parent 4fa26fd9
Loading
Loading
Loading
Loading
+0 −48
Original line number Diff line number Diff line
@@ -110,54 +110,6 @@ public function getPatternOutline() {
    return $this->patternOutline;
  }

  /**
   * Returns the options.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3159706
   *
   * @return array
   *   The options.
   */
  public function getOptions() {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706', E_USER_DEPRECATED);
    return $this->route->getOptions();
  }

  /**
   * Returns the defaults.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3159706
   *
   * @return array
   *   The defaults.
   */
  public function getDefaults() {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706', E_USER_DEPRECATED);
    return $this->route->getDefaults();
  }

  /**
   * Returns the requirements.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3159706
   *
   * @return array
   *   The requirements.
   */
  public function getRequirements() {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706', E_USER_DEPRECATED);
    return $this->route->getRequirements();
  }

  /**
   * {@inheritdoc}
   */
+0 −53
Original line number Diff line number Diff line
@@ -429,59 +429,6 @@ public static function getSubscribedEvents(): array {
    return $events;
  }

  /**
   * Returns a chunk of routes.
   *
   * Should only be used in conjunction with an iterator.
   *
   * @param int $offset
   *   The query offset.
   * @param int $length
   *   The number of records.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   Routes keyed by the route name.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3151009
   */
  public function getRoutesPaged($offset, $length = NULL) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3151009', E_USER_DEPRECATED);
    $select = $this->connection->select($this->tableName, 'router')
      ->fields('router', ['name', 'route']);

    if (isset($length)) {
      $select->range($offset, $length);
    }

    $routes = $select->execute()->fetchAllKeyed();

    $result = [];
    foreach ($routes as $name => $route) {
      $result[$name] = unserialize($route);
    }

    return $result;
  }

  /**
   * Gets the total count of routes provided by the router.
   *
   * @return int
   *   Number of routes.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3151009
   */
  public function getRoutesCount() {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3151009', E_USER_DEPRECATED);
    return $this->connection->query("SELECT COUNT(*) FROM {" . $this->connection->escapeTable($this->tableName) . "}")->fetchField();
  }

  /**
   * {@inheritdoc}
   */
+0 −39
Original line number Diff line number Diff line
@@ -119,45 +119,6 @@ public function reset() {
    return $this->routeProvider->reset();
  }

  /**
   * Returns a chunk of routes.
   *
   * Should only be used in conjunction with an iterator.
   *
   * @param int $offset
   *   The query offset.
   * @param int $length
   *   The number of records.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   Routes keyed by the route name.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3151009
   */
  public function getRoutesPaged($offset, $length = NULL) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3151009', E_USER_DEPRECATED);
    return $this->getRouteProvider()->getRoutesPaged($offset, $length);
  }

  /**
   * Gets the total count of routes provided by the router.
   *
   * @return int
   *   Number of routes.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct
   *   replacement is provided.
   *
   * @see https://www.drupal.org/node/3151009
   */
  public function getRoutesCount() {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3151009', E_USER_DEPRECATED);
    return $this->getRouteProvider()->getRoutesCount();
  }

  /**
   * Determines if the router has been rebuilt.
   *
+0 −47
Original line number Diff line number Diff line
@@ -731,53 +731,6 @@ public function testGetRoutesByPatternWithLongPatterns() {
    $this->assertCount(7, $candidates);
  }

  /**
   * Tests getRoutesPaged().
   *
   * @group legacy
   */
  public function testGetRoutesPaged() {
    $this->expectDeprecation('Drupal\Core\Routing\RouteProvider::getRoutesPaged() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3151009');
    $connection = Database::getConnection();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');

    $this->fixtures->createTables($connection);
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
    $dumper->dump();

    $fixture_routes = $this->fixtures->staticSampleRouteCollection();

    // Query all the routes.
    $routes = $provider->getRoutesPaged(0);
    $this->assertEquals(array_keys($fixture_routes), array_keys($routes));

    // Query non routes.
    $routes = $provider->getRoutesPaged(0, 0);
    $this->assertEquals([], array_keys($routes));

    // Query a limited sets of routes.
    $routes = $provider->getRoutesPaged(1, 2);
    $this->assertEquals(array_slice(array_keys($fixture_routes), 1, 2), array_keys($routes));
  }

  /**
   * Tests getRoutesCount().
   *
   * @group legacy
   */
  public function testGetRoutesCount() {
    $this->expectDeprecation('Drupal\Core\Routing\RouteProvider::getRoutesCount() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3151009');
    $connection = Database::getConnection();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');

    $this->fixtures->createTables($connection);
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
    $dumper->dump();
    $this->assertEquals(5, $provider->getRoutesCount());
  }

}

class TestRouteProvider extends RouteProvider {
+0 −137
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Core\Routing;

use Drupal\Core\Routing\CompiledRoute;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\Core\Routing\CompiledRoute
 * @group Routing
 * @group legacy
 */
class CompiledRouteLegacyTest extends UnitTestCase {

  /**
   * A compiled route object for testing purposes.
   *
   * @var \Drupal\Core\Routing\CompiledRoute
   */
  private $compiled_route;

  /**
   * @var \Symfony\Component\Routing\Route
   *   A mocked Route object.
   */
  private $mocked_route;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    // We can pass in dummy values because we're not doing anything in this
    // test except for verifying the deprecation notices are being thrown.
    $this->compiled_route = new CompiledRoute(0, "", 0, "", "", [], []);
    $this->mocked_route = $this->createMock('Symfony\Component\Routing\Route');

    $this->mocked_route->expects($this->any())
      ->method('getDefaults')
      ->willReturn([]);

    $this->mocked_route->expects($this->any())
      ->method('getRequirements')
      ->willReturn([]);

    $this->mocked_route->expects($this->any())
      ->method('getOptions')
      ->willReturn([]);

  }

  /**
   * Tests for deprecated message and no PHP error.
   *
   * @covers ::getOptions
   */
  public function testOptionsDeprecated() {
    $this->expectDeprecation('Drupal\Core\Routing\CompiledRoute::getOptions() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706');
    if (PHP_VERSION_ID >= 80000) {
      $this->expectWarning();
      $this->expectWarningMessage('Undefined property: Drupal\Core\Routing\CompiledRoute::$route');
    }
    else {
      $this->expectNotice();
      $this->expectNoticeMessage('Undefined property: Drupal\Core\Routing\CompiledRoute::$route');
    }
    $this->compiled_route->getOptions();
  }

  /**
   * Tests to make sure we get an array when dynamically setting.
   *
   * @covers ::getOptions
   */
  public function testOptionsDynamicallySet() {
    $this->expectDeprecation('Drupal\Core\Routing\CompiledRoute::getOptions() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706');
    $this->compiled_route->route = $this->mocked_route;
    $this->compiled_route->getOptions();
  }

  /**
   * Tests for deprecated message and no PHP error.
   *
   * @covers ::getDefaults
   */
  public function testDefaultsDeprecated() {
    $this->expectDeprecation('Drupal\Core\Routing\CompiledRoute::getDefaults() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706');
    if (PHP_VERSION_ID >= 80000) {
      $this->expectWarning();
      $this->expectWarningMessage('Undefined property: Drupal\Core\Routing\CompiledRoute::$route');
    }
    else {
      $this->expectNotice();
      $this->expectNoticeMessage('Undefined property: Drupal\Core\Routing\CompiledRoute::$route');
    }
    $this->compiled_route->getDefaults();
  }

  /**
   * Tests to make sure we get an array when dynamically setting.
   *
   * @covers ::getDefaults
   */
  public function testDefaultsDynamicallySet() {
    $this->expectDeprecation('Drupal\Core\Routing\CompiledRoute::getDefaults() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706');
    $this->compiled_route->route = $this->mocked_route;
    $this->compiled_route->getDefaults();
  }

  /**
   * @covers ::getRequirements
   */
  public function testRequirementsDeprecated() {
    $this->expectDeprecation('Drupal\Core\Routing\CompiledRoute::getRequirements() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706');
    if (PHP_VERSION_ID >= 80000) {
      $this->expectWarning();
      $this->expectWarningMessage('Undefined property: Drupal\Core\Routing\CompiledRoute::$route');
    }
    else {
      $this->expectNotice();
      $this->expectNoticeMessage('Undefined property: Drupal\Core\Routing\CompiledRoute::$route');
    }
    $this->compiled_route->getRequirements();
  }

  /**
   * Tests to make sure we get an array when dynamically setting.
   *
   * @covers ::getRequirements
   */
  public function testRequirementsDynamicallySet() {
    $this->expectDeprecation('Drupal\Core\Routing\CompiledRoute::getRequirements() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. No direct replacement is provided. See https://www.drupal.org/node/3159706');
    $this->compiled_route->route = $this->mocked_route;
    $this->compiled_route->getRequirements();
  }

}