Pull merge.
[yaffs-website] / web / core / modules / workspaces / tests / src / Kernel / EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Kernel;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\entity_test\Entity\EntityTestMulRevPub;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\Tests\user\Traits\UserCreationTrait;
10
11 /**
12  * @coversDefaultClass \Drupal\workspaces\Plugin\Validation\Constraint\EntityReferenceSupportedNewEntitiesConstraintValidator
13  * @group workspaces
14  */
15 class EntityReferenceSupportedNewEntitiesConstraintValidatorTest extends KernelTestBase {
16
17   use UserCreationTrait;
18   use WorkspaceTestTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   protected static $modules = [
24     'system',
25     'user',
26     'workspaces',
27     'entity_test',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     $this->installEntitySchema('user');
37     $this->installSchema('system', ['sequences']);
38     $this->createUser();
39
40     $fields['supported_reference'] = BaseFieldDefinition::create('entity_reference')->setSetting('target_type', 'entity_test_mulrevpub');
41     $fields['unsupported_reference'] = BaseFieldDefinition::create('entity_reference')->setSetting('target_type', 'entity_test');
42     $this->container->get('state')->set('entity_test_mulrevpub.additional_base_field_definitions', $fields);
43
44     $this->installEntitySchema('entity_test_mulrevpub');
45     $this->initializeWorkspacesModule();
46   }
47
48   /**
49    * @covers ::validate
50    */
51   public function testNewEntitiesAllowedInDefaultWorkspace() {
52     $entity = EntityTestMulRevPub::create([
53       'unsupported_reference' => [
54         'entity' => EntityTest::create([]),
55       ],
56       'supported_reference' => [
57         'entity' => EntityTest::create([]),
58       ],
59     ]);
60     $this->assertCount(0, $entity->validate());
61   }
62
63   /**
64    * @covers ::validate
65    */
66   public function testNewEntitiesForbiddenInNonDefaultWorkspace() {
67     $this->switchToWorkspace('stage');
68     $entity = EntityTestMulRevPub::create([
69       'unsupported_reference' => [
70         'entity' => EntityTest::create([]),
71       ],
72       'supported_reference' => [
73         'entity' => EntityTestMulRevPub::create([]),
74       ],
75     ]);
76     $violations = $entity->validate();
77     $this->assertCount(1, $violations);
78     $this->assertEquals('<em class="placeholder">Test entity entities</em> can only be created in the default workspace.', $violations[0]->getMessage());
79   }
80
81 }