View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class ContentModerationStateStorageSchemaTest extends KernelTestBase {
use ContentModerationTestTrait;
protected static $modules = [
'node',
'content_moderation',
'user',
'system',
'text',
'workflows',
'entity_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('node');
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('user');
$this
->installEntitySchema('content_moderation_state');
$this
->installConfig('content_moderation');
NodeType::create([
'type' => 'example',
])
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
}
public function testUniqueKeys() {
$node = Node::create([
'title' => 'Test title',
'type' => 'example',
'moderation_state' => 'draft',
]);
$node
->save();
$this
->assertStorageException([
'content_entity_type_id' => $node
->getEntityTypeId(),
'content_entity_id' => $node
->id(),
'content_entity_revision_id' => $node
->getRevisionId(),
], TRUE);
$this
->assertStorageException([
'content_entity_type_id' => $node
->getEntityTypeId(),
'content_entity_id' => $node
->id(),
'content_entity_revision_id' => $node
->getRevisionId(),
'langcode' => 'de',
], FALSE);
$this
->assertStorageException([
'content_entity_type_id' => $node
->getEntityTypeId(),
'content_entity_id' => $node
->id(),
'content_entity_revision_id' => $node
->getRevisionId(),
'workflow' => 'foo',
], FALSE);
$this
->assertStorageException([
'content_entity_type_id' => 'entity_test',
'content_entity_id' => $node
->id(),
'content_entity_revision_id' => $node
->getRevisionId(),
], FALSE);
$this
->assertStorageException([
'content_entity_type_id' => $node
->getEntityTypeId(),
'content_entity_id' => 9999,
'content_entity_revision_id' => 9999,
], FALSE);
$old_revision_id = $node
->getRevisionId();
$node
->setNewRevision(TRUE);
$node->title = 'Updated title';
$node->moderation_state = 'published';
$node
->save();
$this
->assertStorageException([
'content_entity_type_id' => $node
->getEntityTypeId(),
'content_entity_id' => $node
->id(),
'content_entity_revision_id' => $old_revision_id,
], TRUE);
}
protected function assertStorageException(array $values, bool $has_exception) : void {
$defaults = [
'moderation_state' => 'draft',
'workflow' => 'editorial',
];
$entity = ContentModerationState::create($values + $defaults);
$exception_triggered = FALSE;
try {
ContentModerationState::updateOrCreateFromEntity($entity);
} catch (\Exception $e) {
$exception_triggered = TRUE;
}
$this
->assertEquals($has_exception, $exception_triggered);
}
}