View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class ContentModerationSyncingTest extends KernelTestBase {
use ContentModerationTestTrait;
protected static $modules = [
'user',
'workflows',
'content_moderation',
'entity_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('workflow');
$this
->installEntitySchema('content_moderation_state');
$this
->installEntitySchema('entity_test_mulrevpub');
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
$workflow
->save();
}
public function testNoRevisionForcedDuringSync() {
$entity = EntityTestMulRevPub::create([
'moderation_state' => 'draft',
'name' => 'foo',
]);
$entity
->save();
$initial_revision_id = $entity
->getRevisionId();
$entity
->setSyncing(TRUE);
$entity->name = 'bar';
$entity
->save();
$this
->assertEquals($entity
->getRevisionId(), $initial_revision_id);
}
public function testSingleRevisionStateChangedDuringSync() {
$entity = EntityTestMulRevPub::create([
'moderation_state' => 'published',
'name' => 'foo',
]);
$entity
->save();
$initial_revision_id = $entity
->getRevisionId();
$this
->assertTrue($entity
->isDefaultRevision());
$this
->assertTrue($entity
->isPublished());
$entity
->setSyncing(TRUE);
$entity->moderation_state = 'draft';
$entity
->save();
$this
->assertEquals($initial_revision_id, $entity
->getRevisionId());
$this
->assertFalse($entity
->isPublished());
$this
->assertTrue($entity
->isDefaultRevision());
$this
->assertEquals('draft', $entity->moderation_state->value);
}
public function testMultipleRevisionStateChangedDuringSync() {
$entity = EntityTestMulRevPub::create([
'moderation_state' => 'published',
'name' => 'foo',
]);
$entity
->save();
$entity->name = 'bar';
$entity
->save();
$latest_revision_id = $entity
->getRevisionId();
$entity
->setSyncing(TRUE);
$entity->moderation_state = 'draft';
$entity
->save();
$this
->assertEquals($latest_revision_id, $entity
->getRevisionId());
$this
->assertEquals('draft', $entity->moderation_state->value);
$this
->assertEquals('bar', $entity->name->value);
$this
->assertFalse($entity
->isPublished());
$this
->assertTrue($entity
->isDefaultRevision());
}
public function testUpdatingPreviousRevisionDuringSync() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mulrevpub');
$entity = EntityTestMulRevPub::create([
'moderation_state' => 'published',
'name' => 'foo',
]);
$entity
->save();
$original_revision_id = $entity
->getRevisionId();
$entity->name = 'bar';
$entity
->save();
$original_revision = $storage
->loadRevision($original_revision_id);
$original_revision
->setSyncing(TRUE);
$original_revision->name = 'baz';
$original_revision
->save();
$this
->assertEquals([
'baz',
'bar',
], $this
->getAllRevisionNames($entity));
}
public function testStateChangedPreviousRevisionDuringSync() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mulrevpub');
$entity = EntityTestMulRevPub::create([
'moderation_state' => 'published',
'name' => 'foo',
]);
$entity
->save();
$entity->moderation_state = 'draft';
$entity->name = 'bar';
$entity
->save();
$draft_revision_id = $entity
->getRevisionId();
$entity->name = 'baz';
$entity->moderation_state = 'published';
$entity
->save();
$default_revision_id = $entity
->getRevisionId();
$draft_revision = $storage
->loadRevision($draft_revision_id);
$draft_revision
->setSyncing(TRUE);
$draft_revision->name = 'qux';
$draft_revision->moderation_state = 'published';
$draft_revision
->save();
$reloaded_default_revision = $storage
->load($entity
->id());
$this
->assertEquals($default_revision_id, $reloaded_default_revision
->getRevisionId());
$this
->assertEquals([
'foo',
'qux',
'baz',
], $this
->getAllRevisionNames($reloaded_default_revision));
}
protected function getAllRevisionNames(EntityTestMulRevPub $entity) {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mulrevpub');
return array_map(function ($revision_id) use ($storage) {
return $storage
->loadRevision($revision_id)->name->value;
}, array_keys($storage
->getQuery()
->accessCheck(FALSE)
->allRevisions()
->condition('id', $entity
->id())
->sort('revision_id', 'ASC')
->execute()));
}
}