SpecialAttributesRouteSubscriberTest.php 3.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 77 78 79 80 81 82 83 84 85
<?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;

/**
 * Tests the special attributes route subscriber.
 *
 * @see \Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber
 */
class SpecialAttributesRouteSubscriberTest extends UnitTestCase {

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

  public static function getInfo() {
    return array(
      'name' => 'Special attributes route subscriber',
      'description' => 'Tests the special attributes route subscriber.',
      'group' => 'System'
    );
  }

  /**
   * {@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;
  }

  /**
86
   * Tests the onAlterRoutes method for valid variables.
87 88 89 90 91 92 93 94 95 96
   *
   * @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');
97
    $this->assertTrue($this->specialAttributesRouteSubscriber->onAlterRoutes($event));
98 99 100
  }

  /**
101
   * Tests the onAlterRoutes method for invalid variables.
102 103 104 105 106 107 108 109 110 111
   *
   * @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');
112
    $this->assertFalse($this->specialAttributesRouteSubscriber->onAlterRoutes($event));
113 114 115 116 117 118 119 120
  }

}

}

namespace {
  if (!function_exists('watchdog')) {
121
    function watchdog($type, $message, array $args = array()) {
122 123 124 125 126 127 128
    }
  }
  if (!function_exists('drupal_set_message')) {
    function drupal_set_message($type = NULL, $message = '') {
    }
  }
}