EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php

Same filename and directory in other branches
  1. 9 core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php
  2. 8.9.x core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php
  3. 10 core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php

Namespace

Drupal\Tests\workspaces\Kernel

File

core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\workspaces\Kernel;

use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
 * @coversDefaultClass \Drupal\workspaces\Plugin\Validation\Constraint\EntityReferenceSupportedNewEntitiesConstraintValidator
 * @group workspaces
 */
class EntityReferenceSupportedNewEntitiesConstraintValidatorTest extends KernelTestBase {
  use UserCreationTrait;
  use WorkspaceTestTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'workspaces',
    'entity_test',
  ];
  
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected EntityTypeManager $entityTypeManager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('user');
    $this->createUser();
    $fields['supported_reference'] = BaseFieldDefinition::create('entity_reference')->setSetting('target_type', 'entity_test_mulrevpub');
    $fields['unsupported_reference'] = BaseFieldDefinition::create('entity_reference')->setSetting('target_type', 'entity_test');
    $this->container
      ->get('state')
      ->set('entity_test_mulrevpub.additional_base_field_definitions', $fields);
    $this->installEntitySchema('entity_test_mulrevpub');
    $this->initializeWorkspacesModule();
  }
  
  /**
   * @covers ::validate
   */
  public function testNewEntitiesAllowedInDefaultWorkspace() : void {
    $entity = EntityTestMulRevPub::create([
      'unsupported_reference' => [
        'entity' => EntityTest::create([]),
      ],
      'supported_reference' => [
        'entity' => EntityTest::create([]),
      ],
    ]);
    $this->assertCount(0, $entity->validate());
  }
  
  /**
   * @covers ::validate
   */
  public function testNewEntitiesForbiddenInNonDefaultWorkspace() : void {
    $this->switchToWorkspace('stage');
    $entity = EntityTestMulRevPub::create([
      'unsupported_reference' => [
        'entity' => EntityTest::create([]),
      ],
      'supported_reference' => [
        'entity' => EntityTestMulRevPub::create([]),
      ],
    ]);
    $violations = $entity->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('Test entity entities can only be created in the default workspace.', $violations[0]->getMessage());
  }

}

Classes

Title Deprecated Summary
EntityReferenceSupportedNewEntitiesConstraintValidatorTest @coversDefaultClass \Drupal\workspaces\Plugin\Validation\Constraint\EntityReferenceSupportedNewEntitiesConstraintValidator[[api-linebreak]] @group workspaces

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.