function StyleSerializerTest::testFieldapiField

Same name in other branches
  1. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testFieldapiField()

Tests the field row style using fieldapi fields.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php, line 654

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testFieldapiField() {
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    $node = $this->drupalCreateNode();
    $result = Json::decode($this->drupalGet('test/serialize/node-field', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $this->assertEqual($result[0]['nid'], $node->id());
    $this->assertEqual($result[0]['body'], $node->body->processed);
    // Make sure that serialized fields are not exposed to XSS.
    $node = $this->drupalCreateNode();
    $node->body = [
        'value' => '<script type="text/javascript">alert("node-body");</script>' . $this->randomMachineName(32),
        'format' => filter_default_format(),
    ];
    $node->save();
    $result = Json::decode($this->drupalGet('test/serialize/node-field', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $this->assertEqual($result[1]['nid'], $node->id());
    $this->assertStringNotContainsString("<script", $this->getSession()
        ->getPage()
        ->getContent(), "No script tag is present in the raw page contents.");
    $this->drupalLogin($this->adminUser);
    // Add an alias and make the output raw.
    $row_options = 'admin/structure/views/nojs/display/test_serializer_node_display_field/rest_export_1/row_options';
    // Test an empty string for an alias, this should not be used. This also
    // tests that the form can be submitted with no aliases.
    $this->drupalPostForm($row_options, [
        'row_options[field_options][title][raw_output]' => '1',
    ], t('Apply'));
    $this->drupalPostForm(NULL, [], t('Save'));
    $view = Views::getView('test_serializer_node_display_field');
    $view->setDisplay('rest_export_1');
    $this->executeView($view);
    // Test the raw 'created' value against each row.
    foreach (Json::decode($this->drupalGet('test/serialize/node-field', [
        'query' => [
            '_format' => 'json',
        ],
    ])) as $index => $values) {
        $this->assertIdentical($values['title'], $view->result[$index]->_entity->title->value, 'Expected raw title value found.');
    }
    // Test that multiple raw body fields are shown.
    // Set the body field to unlimited cardinality.
    $storage_definition = $node->getFieldDefinition('body')
        ->getFieldStorageDefinition();
    $storage_definition->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $storage_definition->save();
    $this->drupalPostForm($row_options, [
        'row_options[field_options][body][raw_output]' => '1',
    ], t('Apply'));
    $this->drupalPostForm(NULL, [], t('Save'));
    $node = $this->drupalCreateNode();
    $body = [
        'value' => '<script type="text/javascript">alert("node-body");</script>' . $this->randomMachineName(32),
        'format' => filter_default_format(),
    ];
    // Add two body items.
    $node->body = [
        $body,
        $body,
    ];
    $node->save();
    $view = Views::getView('test_serializer_node_display_field');
    $view->setDisplay('rest_export_1');
    $this->executeView($view);
    $result = Json::decode($this->drupalGet('test/serialize/node-field', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $this->assertEqual(count($result[2]['body']), $node->body
        ->count(), 'Expected count of values');
    $this->assertEqual($result[2]['body'], array_map(function ($item) {
        return $item['value'];
    }, $node->body
        ->getValue()), 'Expected raw body values found.');
}

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