RoutingFixtures.php 6.53 KB
Newer Older
Crell's avatar
Crell committed
1 2
<?php

3
namespace Drupal\Tests\Core\Routing;
Crell's avatar
Crell committed
4 5 6 7

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

8 9
use Drupal\Core\Database\Connection;

Crell's avatar
Crell committed
10 11 12 13 14
/**
 * Utility methods to generate sample data, database configuration, etc.
 */
class RoutingFixtures {

15 16 17
  /**
   * Create the tables required for the sample data.
   *
18
   * @param \Drupal\Core\Database\Connection $connection
19 20
   *   The connection to use to create the tables.
   */
21 22 23 24 25 26 27 28 29 30
  public function createTables(Connection $connection) {
    $tables = $this->routingTableDefinition();
    $schema = $connection->schema();

    foreach ($tables as $name => $table) {
      $schema->dropTable($name);
      $schema->createTable($name, $table);
    }
  }

31 32 33
  /**
   * Drop the tables used for the sample data.
   *
34
   * @param \Drupal\Core\Database\Connection $connection
35 36
   *   The connection to use to drop the tables.
   */
37 38 39 40 41 42 43 44 45
  public function dropTables(Connection $connection) {
    $tables = $this->routingTableDefinition();
    $schema = $connection->schema();

    foreach ($tables as $name => $table) {
      $schema->dropTable($name);
    }
  }

46 47 48 49 50 51 52
  /**
   * Returns a static version of the routes.
   */
  public function staticSampleRouteCollection() {
    $routes = array();
    $routes['route_a'] = array(
      'path' => '/path/one',
53
      'methods' => array('GET'),
54 55 56
    );
    $routes['route_b'] = array(
      'path' => '/path/one',
57
      'methods' => array('PUT'),
58 59 60
    );
    $routes['route_c'] = array(
      'path' => '/path/two',
61
      'methods' => array('GET'),
62 63 64 65 66 67 68 69 70
      'requirements' => array(
        '_format' => 'json'
      ),
    );
    $routes['route_d'] = array(
      'path' => '/path/three',
    );
    $routes['route_e'] = array(
      'path' => '/path/two',
71
      'methods' => array('GET', 'HEAD'),
72 73 74 75 76 77 78 79
      'requirements' => array(
        '_format' => 'html'
      ),
    );

    return $routes;
  }

Crell's avatar
Crell committed
80 81 82 83 84 85 86 87 88
  /**
   * Returns a standard set of routes for testing.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   */
  public function sampleRouteCollection() {
    $collection = new RouteCollection();

    $route = new Route('path/one');
89
    $route->setMethods(['GET']);
Crell's avatar
Crell committed
90 91 92
    $collection->add('route_a', $route);

    $route = new Route('path/one');
93
    $route->setMethods(['PUT']);
Crell's avatar
Crell committed
94 95 96
    $collection->add('route_b', $route);

    $route = new Route('path/two');
97
    $route->setMethods(['GET']);
98
    $route->setRequirement('_format', 'json');
Crell's avatar
Crell committed
99 100 101 102 103 104
    $collection->add('route_c', $route);

    $route = new Route('path/three');
    $collection->add('route_d', $route);

    $route = new Route('path/two');
105
    $route->setMethods(['GET', 'HEAD']);
106
    $route->setRequirement('_format', 'html');
Crell's avatar
Crell committed
107 108 109 110 111
    $collection->add('route_e', $route);

    return $collection;
  }

112 113 114 115 116 117 118 119 120
  /**
   * Returns a complex set of routes for testing.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   */
  public function complexRouteCollection() {
    $collection = new RouteCollection();

    $route = new Route('/path/{thing}/one');
121
    $route->setMethods(['GET']);
122 123 124
    $collection->add('route_a', $route);

    $route = new Route('/path/{thing}/one');
125
    $route->setMethods(['PUT']);
126 127 128
    $collection->add('route_b', $route);

    $route = new Route('/somewhere/{item}/over/the/rainbow');
129
    $route->setMethods(['GET']);
130 131 132 133 134 135
    $collection->add('route_c', $route);

    $route = new Route('/another/{thing}/about/{item}');
    $collection->add('route_d', $route);

    $route = new Route('/path/add/one');
136
    $route->setMethods(['GET', 'HEAD']);
137 138 139 140 141
    $collection->add('route_e', $route);

    return $collection;
  }

142 143 144 145 146 147 148 149 150
  /**
   * Returns a Content-type restricted set of routes for testing.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   */
  public function contentRouteCollection() {
    $collection = new RouteCollection();

    $route = new Route('path/three');
151
    $route->setMethods(['POST']);
152 153 154 155
    $route->setRequirement('_content_type_format', 'json');
    $collection->add('route_f', $route);

    $route = new Route('path/three');
156
    $route->setMethods(['PATCH']);
157 158 159 160 161
    $route->setRequirement('_content_type_format', 'xml');
    $collection->add('route_g', $route);
    return $collection;
  }

162 163 164 165 166 167
  /**
   * Returns the table definition for the routing fixtures.
   *
   * @return array
   *   Table definitions.
   */
Crell's avatar
Crell committed
168 169 170 171 172 173 174
  public function routingTableDefinition() {

    $tables['test_routes'] = array(
      'description' => 'Maps paths to various callbacks (access, page and title)',
      'fields' => array(
        'name' => array(
          'description' => 'Primary Key: Machine name of this route',
175
          'type' => 'varchar_ascii',
Crell's avatar
Crell committed
176 177 178 179
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
180 181
        'path' => array(
          'description' => 'The path for this URI',
Crell's avatar
Crell committed
182 183 184 185 186 187 188 189 190 191 192 193
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'pattern_outline' => array(
          'description' => 'The pattern',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
194 195
        'provider' => array(
          'description' => 'The provider grouping to which a route belongs.',
Crell's avatar
Crell committed
196 197 198 199 200 201
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'access_callback' => array(
202
          'description' => 'The callback which determines the access to this router path. Defaults to \Drupal\Core\Session\AccountInterface::hasPermission.',
Crell's avatar
Crell committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'access_arguments' => array(
          'description' => 'A serialized array of arguments for the access callback.',
          'type' => 'blob',
          'not null' => FALSE,
        ),
        'fit' => array(
          'description' => 'A numeric representation of how specific the path is.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ),
        'number_parts' => array(
          'description' => 'Number of parts in this router path.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'size' => 'small',
        ),
        'route' => array(
          'description' => 'A serialized Route object',
          'type' => 'text',
        ),
      ),
      'indexes' => array(
        'fit' => array('fit'),
        'pattern_outline' => array('pattern_outline'),
234
        'provider' => array('provider'),
Crell's avatar
Crell committed
235 236 237 238 239 240
      ),
      'primary key' => array('name'),
    );

    return $tables;
  }
241

Crell's avatar
Crell committed
242
}