CollectRoutesTest.php 4.62 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
<?php

/**
 * @file
 * Contains \Drupal\rest\Tests\CollectRoutesTest.
 */

namespace Drupal\rest\Tests;

use Drupal\Tests\UnitTestCase;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\rest\Plugin\views\display\RestExport;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Defines tests for the collectRoutes() method.
 */
class CollectRoutesTest extends UnitTestCase {

  /**
   * The REST export instance.
   *
   * @var \Drupal\rest\Plugin\views\display\RestExport
   */
  protected $restExport;

  /**
   * The RouteCollection.
   */
  protected $routes;

  public static function getInfo() {
    return array(
      'name' => 'routes: REST export plugin',
      'description' => 'Tests the REST export view plugin.',
      'group' => 'Views Plugins',
    );
  }

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

    $container = new ContainerBuilder();

    $content_negotiation = $this->getMockBuilder('\Drupal\Core\ContentNegotiation')
      ->disableOriginalConstructor()
      ->getMock();

    $request = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')
      ->disableOriginalConstructor()
      ->getMock();

    $container->set('content_negotiation', $content_negotiation);
    $container->set('request', $request);

    $this->view = $this->getMock('\Drupal\views\Entity\View', array('initHandlers'), array(
      array('id' => 'test_view'),
      'view',
    ));

    $view_executable = $this->getMock('\Drupal\views\ViewExecutable', array('initHandlers'), array(), '', FALSE);

    $view_executable->storage = $this->view;
    $view_executable->argument = array();

    $display_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $container->set('plugin.manager.views.display', $display_manager);

    $access_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $container->set('plugin.manager.views.access', $access_manager);

80 81 82 83 84
    $route_provider = $this->getMockBuilder('\Drupal\Core\Routing\RouteProviderInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $container->set('router.route_provider', $route_provider);

85
    $state = $this->getMock('\Drupal\Core\State\StateInterface');
86 87
    $container->set('state', $state);

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    $style_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $container->set('plugin.manager.views.style', $style_manager);

    \Drupal::setContainer($container);

    $this->restExport = RestExport::create($container, array(), "test_routes", array());
    $this->restExport->view = $view_executable;

    // Initialize a display.
    $this->restExport->display = array('id' => 'page_1');

    // Set the style option.
    $this->restExport->setOption('style', array('type' => 'serializer'));

    $display_manager->expects($this->once())
      ->method('getDefinition')
106
      ->will($this->returnValue(array('id' => 'test', 'provider' => 'test')));
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    $none = $this->getMockBuilder('\Drupal\views\Plugin\views\access\None')
      ->disableOriginalConstructor()
      ->getMock();

    $access_manager->expects($this->once())
      ->method('createInstance')
      ->will($this->returnValue($none));

    $style_plugin = $this->getMock('\Drupal\rest\Plugin\views\style\Serializer', array('getFormats', 'init'), array(), '', FALSE);

    $style_plugin->expects($this->once())
      ->method('getFormats')
      ->will($this->returnValue(array('json')));

    $style_plugin->expects($this->once())
      ->method('init')
      ->with($view_executable)
      ->will($this->returnValue(TRUE));

    $style_manager->expects($this->once())
      ->method('createInstance')
      ->will($this->returnValue($style_plugin));

    $this->routes = new RouteCollection();
    $this->routes->add('test_1', new Route('/test/1'));
    $this->routes->add('view.test_view.page_1', new Route('/test/2'));

    $this->view->addDisplay('page', NULL, 'page_1');
  }

  /**
   * Tests if adding a requirement to a route only modify one route.
   */
  public function testRoutesRequirements() {
    $this->restExport->collectRoutes($this->routes);

    $requirements_1 = $this->routes->get('test_1')->getRequirements();
    $requirements_2 = $this->routes->get('view.test_view.page_1')->getRequirements();

    $this->assertEquals(count($requirements_1), 0, 'First route has no requirement.');
    $this->assertEquals(count($requirements_2), 2, 'Views route with rest export had the format and method requirements added.');
  }
}