View source
<?php
namespace Drupal\Tests\entity_legal\Kernel;
use Drupal\entity_legal\Entity\EntityLegalDocument;
use Drupal\entity_legal\Entity\EntityLegalDocumentVersion;
use Drupal\KernelTests\KernelTestBase;
class SingleLegalDocumentPublishedVersionConstraintTest extends KernelTestBase {
public static $modules = [
'entity_legal',
'field',
'text',
];
public function testBundleUniqueFieldValue() {
$this
->installConfig([
'entity_legal',
]);
$this
->installEntitySchema('entity_legal_document_version');
EntityLegalDocument::create([
'id' => 'legal_notice',
'label' => 'Legal notice',
])
->save();
$doc_v1 = EntityLegalDocumentVersion::create([
'document_name' => 'legal_notice',
'label' => 'v1.0',
'acceptance_label' => 'Accept the legal notice',
]);
$violations = $doc_v1
->validate();
$this
->assertCount(0, $violations);
$doc_v1
->save();
$doc_v1 = EntityLegalDocumentVersion::load($doc_v1
->id());
$this
->assertFalse($doc_v1
->get('published')->value);
$doc_v1
->set('published', TRUE);
$violations = $doc_v1
->validate();
$this
->assertCount(0, $violations);
$doc_v1
->save();
$doc_v1 = EntityLegalDocumentVersion::load($doc_v1
->id());
$this
->assertTrue($doc_v1
->get('published')->value);
$doc_v2 = EntityLegalDocumentVersion::create([
'document_name' => 'legal_notice',
'label' => 'v2.0',
'acceptance_label' => 'Accept the legal notice v2',
'published' => TRUE,
]);
$violations = $doc_v2
->validate();
$this
->assertCount(1, $violations);
$violation_message = strip_tags($violations[0]
->getMessage());
$this
->assertEquals('A legal document can have only one published version. Legal notice v1.0 is already published and should be un-published before publishing this version.', $violation_message);
$doc_v2
->set('published', FALSE);
$violations = $doc_v2
->validate();
$this
->assertCount(0, $violations);
EntityLegalDocument::create([
'id' => 'privacy_policy',
'label' => 'Privacy policy',
])
->save();
$privacy_policy_v1 = EntityLegalDocumentVersion::create([
'document_name' => 'privacy_policy',
'label' => 'v1.0',
'acceptance_label' => 'Accept the privacy policy',
]);
$violations = $privacy_policy_v1
->validate();
$this
->assertCount(0, $violations);
}
}