UrlMatcherDumperTest.php 6.32 KB
Newer Older
Crell's avatar
Crell committed
1
2
3
4
<?php

/**
 * @file
Crell's avatar
Crell committed
5
 * Definition of Drupal\system\Tests\Routing\UrlMatcherDumperTest.
Crell's avatar
Crell committed
6
7
 */

Crell's avatar
Crell committed
8
9
namespace Drupal\system\Tests\Routing;

Crell's avatar
Crell committed
10
11
12
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

Crell's avatar
Crell committed
13
use Drupal\simpletest\UnitTestBase;
Crell's avatar
Crell committed
14
15
16
17
18
19
use Drupal\Core\Database\Database;
use Drupal\Core\Routing\UrlMatcherDumper;

/**
 * Basic tests for the UrlMatcherDumper.
 */
Crell's avatar
Crell committed
20
class UrlMatcherDumperTest extends UnitTestBase {
Crell's avatar
Crell committed
21
22
  public static function getInfo() {
    return array(
Crell's avatar
Crell committed
23
      'name' => 'Dumper tests',
Crell's avatar
Crell committed
24
      'description' => 'Confirm that the matcher dumper is functioning properly.',
Crell's avatar
Crell committed
25
      'group' => 'Routing',
Crell's avatar
Crell committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    );
  }

  function setUp() {
    parent::setUp();
  }

  /**
   * Confirms that the dumper can be instantiated successfuly.
   */
  function testCreate() {
    $connection = Database::getConnection();
    $dumper= new UrlMatcherDumper($connection);

    $class_name = 'Drupal\Core\Routing\UrlMatcherDumper';
    $this->assertTrue($dumper instanceof $class_name, t('Dumper created successfully'));
  }

  /**
   * Confirms that we can add routes to the dumper.
   */
  function testAddRoutes() {
    $connection = Database::getConnection();
    $dumper= new UrlMatcherDumper($connection);

    $route = new Route('test');
    $collection = new RouteCollection();
    $collection->add('test_route', $route);

    $dumper->addRoutes($collection);

    $dumper_routes = $dumper->getRoutes()->all();
    $collection_routes = $collection->all();

    foreach ($dumper_routes as $name => $route) {
      $this->assertEqual($route->getPattern(), $collection_routes[$name]->getPattern(), t('Routes match'));
    }
  }

  /**
   * Confirms that we can add routes to the dumper when it already has some.
   */
  function testAddAdditionalRoutes() {
    $connection = Database::getConnection();
    $dumper= new UrlMatcherDumper($connection);

    $route = new Route('test');
    $collection = new RouteCollection();
    $collection->add('test_route', $route);
    $dumper->addRoutes($collection);

    $route = new Route('test2');
    $collection2 = new RouteCollection();
    $collection2->add('test_route2', $route);
    $dumper->addRoutes($collection2);

    // Merge the two collections together so we can test them.
    $collection->addCollection(clone $collection2);

    $dumper_routes = $dumper->getRoutes()->all();
    $collection_routes = $collection->all();

    $success = TRUE;
    foreach ($collection_routes as $name => $route) {
      if (empty($dumper_routes[$name])) {
        $success = FALSE;
        $this->fail(t('Not all routes found in the dumper.'));
      }
    }

    if ($success) {
      $this->pass('All routes found in the dumper.');
    }
  }

Crell's avatar
Crell committed
101
  /**
102
   * Confirm that we can dump a route collection to the database.
Crell's avatar
Crell committed
103
   */
104
  public function testDump() {
Crell's avatar
Crell committed
105
    $connection = Database::getConnection();
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    $dumper= new UrlMatcherDumper($connection, 'test_routes');

    $route = new Route('/test/{my}/path');
    $route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
    $collection = new RouteCollection();
    $collection->add('test_route', $route);

    $dumper->addRoutes($collection);

    $this->prepareTables($connection);

    $dumper->dump(array('route_set' => 'test'));

    $record = $connection->query("SELECT * FROM {test_routes} WHERE name= :name", array(':name' => 'test_route'))->fetchObject();

    $loaded_route = unserialize($record->route);

    $this->assertEqual($record->name, 'test_route', t('Dumped route has correct name.'));
    $this->assertEqual($record->pattern, '/test/{my}/path', t('Dumped route has correct pattern.'));
    $this->assertEqual($record->pattern_outline, '/test/%/path', t('Dumped route has correct pattern outline.'));
    $this->assertEqual($record->fit, 5 /* 101 in binary */, t('Dumped route has correct fit.'));
    $this->assertTrue($loaded_route instanceof Route, t('Route object retrieved successfully.'));

  }

  /**
   * Creates a test database table just for unit testing purposes.
   */
  protected function prepareTables($connection) {
Crell's avatar
Crell committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

    $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',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'pattern' => array(
          'description' => 'The path pattern for this URI',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
        'pattern_outline' => array(
          'description' => 'The pattern',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
160
161
162
163
164
165
166
        'route_set' => array(
          'description' => 'The route set grouping to which a route belongs.',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ),
Crell's avatar
Crell committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        'access_callback' => array(
          'description' => 'The callback which determines the access to this router path. Defaults to user_access.',
          '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'),
200
        'route_set' => array('route_set'),
Crell's avatar
Crell committed
201
202
203
204
205
206
207
208
209
210
211
      ),
      'primary key' => array('name'),
    );


    $schema = $connection->schema();
    $schema->dropTable('test_routes');
    $schema->createTable('test_routes', $tables['test_routes']);
  }

}