View source
<?php
namespace Drupal\Tests\multiversion\Kernel;
use Drupal\Core\Entity\EntityInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\multiversion\Entity\Workspace;
class ParagraphsTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
protected $paragraphStorage;
protected $nodeStorage;
public static $modules = [
'system',
'field',
'key_value',
'user',
'serialization',
'paragraphs',
'multiversion_test_paragraphs',
'node',
'multiversion',
'entity_reference_revisions',
'file',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installEntitySchema('node');
$this
->installEntitySchema('paragraph');
$this
->installEntitySchema('user');
$this
->installEntitySchema('workspace');
$this
->installConfig([
'multiversion',
'multiversion_test_paragraphs',
]);
$this
->installSchema('node', 'node_access');
$this
->installSchema('key_value', 'key_value_sorted');
$this
->installSchema('file', 'file_usage');
$multiversion_manager = $this->container
->get('multiversion.manager');
$multiversion_manager
->enableEntityTypes();
$workspace = Workspace::create([
'machine_name' => 'live',
'label' => 'Live',
'type' => 'basic',
]);
$workspace
->save();
$this->nodeStorage = $this->container
->get('entity_type.manager')
->getStorage('node');
$this->paragraphStorage = $this->container
->get('entity_type.manager')
->getStorage('paragraph');
}
public function testDefaultParagraphsBehaviour() {
$paragraph = $this->paragraphStorage
->create([
'title' => 'Stub of real paragraph',
'type' => 'test_paragraph_type',
'field_test_field' => 'First revision title',
]);
$node = $this->nodeStorage
->create([
'type' => 'paragraphs_node_type',
'title' => 'Test node',
'field_paragraph' => $paragraph,
]);
$node
->save();
$node_revision_id = $node
->getRevisionId();
$paragraph_entity_id = $node->field_paragraph->target_id;
$paragraph_entity = $this->paragraphStorage
->load($paragraph_entity_id);
list($i, $hash) = explode('-', $paragraph_entity->_rev->value);
$this
->assertEquals($i, '1', 'After saving new node with paragraph we have new paragraph with one revision.');
$paragraph->field_test_field = 'Second revision title';
$node->field_paragraph = $paragraph;
$node
->save();
$paragraph_entity_revision_id = $node->field_paragraph->target_revision_id;
$paragraph_entity = $this->paragraphStorage
->loadRevision($paragraph_entity_revision_id);
$this
->assertRevNumber($paragraph_entity, 2);
$this
->assertEquals($paragraph_entity->field_test_field->value, 'Second revision title');
$node_first_revision = $this->nodeStorage
->loadRevision($node_revision_id);
$paragraph_entity_revision_id = $node_first_revision->field_paragraph->target_revision_id;
$paragraph_entity = $this->paragraphStorage
->loadRevision($paragraph_entity_revision_id);
$this
->assertEquals($paragraph_entity->field_test_field->value, 'First revision title');
}
public function testParagraphStubCreatedAfterParent() {
$paragraph_stub = $this->paragraphStorage
->create([
'title' => 'Stub of real paragraph',
'type' => 'test_paragraph_type',
]);
$paragraph_stub->_rev->is_stub = TRUE;
$node = $this->nodeStorage
->create([
'type' => 'paragraphs_node_type',
'title' => 'Test node',
'field_paragraph' => $paragraph_stub,
]);
$node
->save();
$paragraph_stub_entity_id = $node->field_paragraph->target_id;
$paragraph_stub_entity = $this->paragraphStorage
->load($paragraph_stub_entity_id);
$this
->assertRevNumber($paragraph_stub_entity, 0);
$paragraph_real = $this->paragraphStorage
->create([
'type' => 'test_paragraph_type',
'id' => $paragraph_stub_entity
->id(),
]);
$paragraph_real
->enforceIsNew(FALSE);
$paragraph_real->_rev->is_stub = FALSE;
$paragraph_real
->save();
$this
->assertEquals($paragraph_real
->id(), $paragraph_stub_entity_id);
$this
->assertRevNumber($paragraph_real, 1);
}
public function testParagraphStubCreatedBeforeParent() {
$paragraph = $this->paragraphStorage
->create([
'title' => 'Real paragraph',
'type' => 'test_paragraph_type',
]);
$paragraph
->save();
$this
->assertRevNumber($paragraph, 1);
$paragraph_stub_in_node = $this->paragraphStorage
->create([
'type' => 'test_paragraph_type',
'uuid' => $paragraph
->uuid(),
]);
$paragraph_stub_in_node->_rev->is_stub = TRUE;
$node = $this->nodeStorage
->create([
'type' => 'paragraphs_node_type',
'title' => 'Test node',
'field_paragraph' => $paragraph_stub_in_node,
]);
$node
->save();
$paragraph_entity_id_from_node = $node->field_paragraph->target_id;
$this
->assertEquals($paragraph_entity_id_from_node, $paragraph
->id());
$paragraph_entity = $this->paragraphStorage
->load($paragraph
->id());
$this
->assertRevNumber($paragraph_entity, 1);
}
protected function assertRevNumber(EntityInterface $entity, $expected_rev_number) {
list($rev_number) = explode('-', $entity->_rev->value);
$this
->assertEquals($expected_rev_number, $rev_number);
}
}