function StyleSerializerEntityTest::testSerializerResponses

Same name in other branches
  1. 10 core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerEntityTest::testSerializerResponses()

Checks the behavior of the Serializer callback paths and row plugins.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php, line 99

Class

StyleSerializerEntityTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testSerializerResponses() : void {
    // Test the serialize callback.
    $view = Views::getView('test_serializer_display_field');
    $view->initDisplay();
    $this->executeView($view);
    $actual_json = $this->drupalGet('test/serialize/field', [
        'query' => [
            '_format' => 'json',
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertCacheTags($view->getCacheTags());
    $this->assertCacheContexts([
        'languages:language_interface',
        'theme',
        'request_format',
    ]);
    // @todo Due to https://www.drupal.org/node/2352009 we can't yet test the
    // propagation of cache max-age.
    // Test the http Content-type.
    $headers = $this->getSession()
        ->getResponseHeaders();
    $this->assertSame([
        'application/json',
    ], $headers['Content-Type']);
    $expected = [];
    foreach ($view->result as $row) {
        $expected_row = [];
        foreach ($view->field as $id => $field) {
            $expected_row[$id] = $field->render($row);
        }
        $expected[] = $expected_row;
    }
    $this->assertSame(json_encode($expected), $actual_json, 'The expected JSON output was found.');
    // Test that the rendered output and the preview output are the same.
    $view->destroy();
    $view->setDisplay('rest_export_1');
    // Mock the request content type by setting it on the display handler.
    $view->display_handler
        ->setContentType('json');
    $output = $view->preview();
    $this->assertSame((string) $this->renderer
        ->renderRoot($output), $actual_json, 'The expected JSON preview output was found.');
    // Test a 403 callback.
    $this->drupalGet('test/serialize/denied', [
        'query' => [
            '_format' => 'json',
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(403);
    // Test the entity rows.
    $view = Views::getView('test_serializer_display_entity');
    $view->initDisplay();
    $this->executeView($view);
    // Get the serializer service.
    $serializer = $this->container
        ->get('serializer');
    $entities = [];
    foreach ($view->result as $row) {
        $entities[] = $row->_entity;
    }
    $expected = $serializer->serialize($entities, 'json');
    $actual_json = $this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'json',
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSame($expected, $actual_json, 'The expected JSON output was found.');
    $expected_cache_tags = $view->getCacheTags();
    $expected_cache_tags[] = 'entity_test_list';
    
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    foreach ($entities as $entity) {
        $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $entity->getCacheTags());
    }
    $this->assertCacheTags($expected_cache_tags);
    $this->assertCacheContexts([
        'languages:language_interface',
        'theme',
        'entity_test_view_grants',
        'request_format',
    ]);
    // Change the format to xml.
    $view->setDisplay('rest_export_1');
    $view->getDisplay()
        ->setOption('style', [
        'type' => 'serializer',
        'options' => [
            'uses_fields' => FALSE,
            'formats' => [
                'xml' => 'xml',
            ],
        ],
    ]);
    $view->save();
    $expected = $serializer->serialize($entities, 'xml');
    $actual_xml = $this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'xml',
        ],
    ]);
    $this->assertSame(trim($expected), $actual_xml);
    $this->assertCacheContexts([
        'languages:language_interface',
        'theme',
        'entity_test_view_grants',
        'request_format',
    ]);
    // Allow multiple formats.
    $view->setDisplay('rest_export_1');
    $view->getDisplay()
        ->setOption('style', [
        'type' => 'serializer',
        'options' => [
            'uses_fields' => FALSE,
            'formats' => [
                'xml' => 'xml',
                'json' => 'json',
            ],
        ],
    ]);
    $view->save();
    $expected = $serializer->serialize($entities, 'json');
    $actual_json = $this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'json',
        ],
    ]);
    $this->assertSame($expected, $actual_json, 'The expected JSON output was found.');
    $expected = $serializer->serialize($entities, 'xml');
    $actual_xml = $this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'xml',
        ],
    ]);
    $this->assertSame(trim($expected), $actual_xml);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.