SpecialAttributesRouteSubscriberTest.php 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\EventSubscriber\SpecialAttributesRouteSubscriberTest.
 */

namespace Drupal\Tests\Core\EventSubscriber {

use Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
18 19
 * @coversDefaultClass \Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber
 * @group EventSubscriber
20 21 22 23 24 25 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
 */
class SpecialAttributesRouteSubscriberTest extends UnitTestCase {

  /**
   * The tested route subscriber.
   *
   * @var \Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber
   */
  protected  $specialAttributesRouteSubscriber;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->specialAttributesRouteSubscriber = new SpecialAttributesRouteSubscriber();
  }

  /**
   * Provides a list of routes with invalid route variables.
   *
   * @return array
   *   An array of invalid routes.
   */
  public function providerTestOnRouteBuildingInvalidVariables() {
    $routes = array();
    $routes[] = array(new Route('/test/{system_path}'));
    $routes[] = array(new Route('/test/{_maintenance}'));
    $routes[] = array(new Route('/test/{_legacy}'));
    $routes[] = array(new Route('/test/{_authentication_provider}'));
    $routes[] = array(new Route('/test/{' . RouteObjectInterface::ROUTE_OBJECT . '}'));
    $routes[] = array(new Route('/test/{' . RouteObjectInterface::ROUTE_NAME . '}'));
    $routes[] = array(new Route('/test/{_content}'));
    $routes[] = array(new Route('/test/{_form}'));
    $routes[] = array(new Route('/test/{_raw_variables}'));

    return $routes;
  }

  /**
   * Provides a list of routes with valid route variables.
   *
   * @return array
   *   An array of valid routes.
   */
  public function providerTestOnRouteBuildingValidVariables() {
    $routes = array();
    $routes[] = array(new Route('/test/{account}'));
    $routes[] = array(new Route('/test/{node}'));
    $routes[] = array(new Route('/test/{user}'));
    $routes[] = array(new Route('/test/{entity_test}'));

    return $routes;
  }

  /**
77
   * Tests the onAlterRoutes method for valid variables.
78 79 80 81 82 83 84 85 86 87
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check.
   *
   * @dataProvider providerTestOnRouteBuildingValidVariables
   */
  public function testOnRouteBuildingValidVariables(Route $route) {
    $route_collection = new RouteCollection();
    $route_collection->add('test', $route);
    $event = new RouteBuildEvent($route_collection, 'test');
88
    $this->assertTrue($this->specialAttributesRouteSubscriber->onAlterRoutes($event));
89 90 91
  }

  /**
92
   * Tests the onAlterRoutes method for invalid variables.
93 94 95 96 97 98 99 100 101 102
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check.
   *
   * @dataProvider providerTestOnRouteBuildingInvalidVariables
   */
  public function testOnRouteBuildingInvalidVariables(Route $route) {
    $route_collection = new RouteCollection();
    $route_collection->add('test', $route);
    $event = new RouteBuildEvent($route_collection, 'test');
103
    $this->assertFalse($this->specialAttributesRouteSubscriber->onAlterRoutes($event));
104 105 106 107 108 109 110 111
  }

}

}

namespace {
  if (!function_exists('watchdog')) {
112
    function watchdog($type, $message, array $args = array()) {
113 114 115 116 117 118 119
    }
  }
  if (!function_exists('drupal_set_message')) {
    function drupal_set_message($type = NULL, $message = '') {
    }
  }
}