View source
<?php
namespace Drupal\Tests\entity_reference_revisions\Functional;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class EntityReferenceRevisionsNormalizerTest extends BrowserTestBase {
use FieldUiTestTrait;
public static $modules = array(
'node',
'field',
'entity_reference_revisions',
'field_ui',
'block',
'hal',
'serialization',
'rest',
);
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'entity_revisions',
'name' => 'Entity revisions',
));
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
$this
->drupalPlaceBlock('system_breadcrumb_block');
}
public function testEntityReferenceRevisions() {
$admin_user = $this
->drupalCreateUser(array(
'administer site configuration',
'administer nodes',
'create article content',
'administer content types',
'administer node fields',
'administer node display',
'administer node form display',
'edit any article content',
));
$this
->drupalLogin($admin_user);
static::fieldUIAddNewField('admin/structure/types/manage/entity_revisions', 'entity_reference_revisions', 'Entity reference revisions', 'entity_reference_revisions', array(
'settings[target_type]' => 'node',
'cardinality' => '-1',
), array(
'settings[handler_settings][target_bundles][article]' => TRUE,
));
$this
->assertText('Saved Entity reference revisions configuration.');
$title = $this
->randomMachineName();
$edit = array(
'title[0][value]' => $title,
'body[0][value]' => 'Revision 1',
);
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$this
->assertText($title);
$this
->assertText('Revision 1');
$node = $this
->drupalGetNodeByTitle($title);
$err_title = 'Entity reference revision content';
$edit = array(
'title[0][value]' => $err_title,
'field_entity_reference_revisions[0][target_id]' => $node
->label() . ' (' . $node
->id() . ')',
);
$this
->drupalGet('node/add/entity_revisions');
$this
->submitForm($edit, 'Save');
$this
->assertText('Entity revisions Entity reference revision content has been created.');
$err_node = $this
->drupalGetNodeByTitle($err_title);
$this
->assertText($err_title);
$this
->assertText($title);
$this
->assertText('Revision 1');
$edit = array(
'body[0][value]' => 'Revision 2',
'revision' => TRUE,
);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$serializer = $this->container
->get('serializer');
$normalized = $serializer
->normalize($err_node, 'hal_json');
$request = \Drupal::request();
$link_domain = $request
->getSchemeAndHttpHost() . $request
->getBasePath();
$this
->assertEqual($err_node->field_entity_reference_revisions->target_revision_id, $normalized['_embedded'][$link_domain . '/rest/relation/node/entity_revisions/field_entity_reference_revisions'][0]['target_revision_id']);
$new_err_node = $serializer
->denormalize($normalized, Node::class, 'hal_json');
$this
->assertEqual($err_node->field_entity_reference_revisions->target_revision_id, $new_err_node->field_entity_reference_revisions->target_revision_id);
}
}