/** * 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.'); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->view = $this->getMockBuilder(ViewExecutable::class)->disableOriginalConstructor()->getMock(); // Make the view result empty so we don't have to mock the row plugin render // call. $this->view->result = []; $this->displayHandler = $this->getMockBuilder(RestExport::class)->disableOriginalConstructor()->getMock(); $this->displayHandler->expects($this->any())->method('getContentType')->willReturn('json'); }
/** * 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.'); // Check auth options. $auth = $this->routes->get('view.test_view.page_1')->getOption('_auth'); $this->assertEquals(count($auth), 1, 'View route with rest export has an auth option added'); $this->assertEquals($auth[0], 'basic_auth', 'View route with rest export has the correct auth option added'); }
/** * @covers ::buildResponse */ public function testBuildResponse() { /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_serializer_display_entity'); $display =& $view->getDisplay('rest_export_1'); $display['display_options']['defaults']['style'] = FALSE; $display['display_options']['style']['type'] = 'serializer'; $display['display_options']['style']['options']['formats'] = ['json', 'xml']; $view->save(); // No custom header should be set yet. $response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []); $this->assertFalse($response->headers->get('Custom-Header')); // Clear render cache. /** @var \Drupal\Core\Cache\MemoryBackend $render_cache */ $render_cache = $this->container->get('cache_factory')->get('render'); $render_cache->deleteAll(); // A custom header should now be added. // @see rest_test_views_views_post_execute() $header = $this->randomString(); $this->container->get('state')->set('rest_test_views_set_header', $header); $response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []); $this->assertEquals($header, $response->headers->get('Custom-Header')); }