Same name and namespace in other branches
  1. 8.9.x core/modules/rest/tests/src/Unit/CollectRoutesTest.php \Drupal\Tests\rest\Unit\CollectRoutesTest
  2. 9 core/modules/rest/tests/src/Unit/CollectRoutesTest.php \Drupal\Tests\rest\Unit\CollectRoutesTest

Tests the REST export view plugin.

@group rest

Hierarchy

Expanded class hierarchy of CollectRoutesTest

File

core/modules/rest/tests/src/Unit/CollectRoutesTest.php, line 19

Namespace

Drupal\Tests\rest\Unit
View source
class CollectRoutesTest extends UnitTestCase {

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

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $container = new ContainerBuilder();
    $request = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Request')
      ->disableOriginalConstructor()
      ->getMock();
    $view = new View([
      'id' => 'test_view',
    ], 'view');
    $view_executable = $this
      ->getMockBuilder('\\Drupal\\views\\ViewExecutable')
      ->onlyMethods([
      'initHandlers',
      'getTitle',
    ])
      ->disableOriginalConstructor()
      ->getMock();
    $view_executable
      ->expects($this
      ->any())
      ->method('getTitle')
      ->willReturn('View title');
    $view_executable->storage = $view;
    $view_executable->argument = [];
    $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);
    $route_provider = $this
      ->getMockBuilder('\\Drupal\\Core\\Routing\\RouteProviderInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $container
      ->set('router.route_provider', $route_provider);
    $container
      ->setParameter('authentication_providers', [
      'basic_auth' => 'basic_auth',
    ]);
    $state = $this
      ->createMock('\\Drupal\\Core\\State\\StateInterface');
    $container
      ->set('state', $state);
    $style_manager = $this
      ->getMockBuilder('\\Drupal\\views\\Plugin\\ViewsPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $container
      ->set('plugin.manager.views.style', $style_manager);
    $container
      ->set('renderer', $this
      ->createMock('Drupal\\Core\\Render\\RendererInterface'));
    $authentication_collector = $this
      ->createMock('\\Drupal\\Core\\Authentication\\AuthenticationCollectorInterface');
    $container
      ->set('authentication_collector', $authentication_collector);
    $authentication_collector
      ->expects($this
      ->any())
      ->method('getSortedProviders')
      ->willReturn([
      'basic_auth' => 'data',
      'cookie' => 'data',
    ]);
    $container
      ->setParameter('serializer.format_providers', [
      'json',
    ]);
    \Drupal::setContainer($container);
    $this->restExport = RestExport::create($container, [], "test_routes", []);
    $this->restExport->view = $view_executable;

    // Initialize a display.
    $this->restExport->display = [
      'id' => 'page_1',
    ];

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

    // Set the auth option.
    $this->restExport
      ->setOption('auth', [
      'basic_auth',
    ]);
    $display_manager
      ->expects($this
      ->once())
      ->method('getDefinition')
      ->willReturn([
      'id' => 'test',
      'provider' => 'test',
    ]);
    $none = $this
      ->getMockBuilder('\\Drupal\\views\\Plugin\\views\\access\\None')
      ->disableOriginalConstructor()
      ->getMock();
    $access_manager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->willReturn($none);
    $style_plugin = $this
      ->getMockBuilder('\\Drupal\\rest\\Plugin\\views\\style\\Serializer')
      ->onlyMethods([
      'getFormats',
      'init',
    ])
      ->disableOriginalConstructor()
      ->getMock();
    $style_plugin
      ->expects($this
      ->once())
      ->method('getFormats')
      ->willReturn([
      'json',
    ]);
    $style_plugin
      ->expects($this
      ->once())
      ->method('init')
      ->with($view_executable)
      ->willReturn(TRUE);
    $style_manager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->willReturn($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'));
    $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
      ->assertCount(0, $requirements_1, 'First route has no requirement.');
    $this
      ->assertCount(1, $requirements_2, 'Views route with rest export had the format requirement added.');

    // Check auth options.
    $auth = $this->routes
      ->get('view.test_view.page_1')
      ->getOption('_auth');
    $this
      ->assertCount(1, $auth, 'View route with rest export has an auth option added');
    $this
      ->assertEquals('basic_auth', $auth[0], 'View route with rest export has the correct auth option added');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CollectRoutesTest::$restExport protected property The REST export instance.
CollectRoutesTest::$routes protected property The RouteCollection.
CollectRoutesTest::setUp protected function Overrides UnitTestCase::setUp
CollectRoutesTest::testRoutesRequirements public function Tests if adding a requirement to a route only modify one route.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function