clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
<?php
namespace Drupal\Tests\entity_reference_validators\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* Tests circular entity reference validation.
*
* @group entity_reference
*/
class CircularEntityReferenceTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use EntityReferenceTestTrait;
/**
* The entity reference field under test.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'entity_reference_validators',
'node',
'field_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Sets up the test.
*/
protected function setUp(): void {
parent::setUp();
$this->createContentType(['type' => 'test']);
$this->createEntityReferenceField(
'node',
'test',
'field_entity_ref_test',
'Test circular entity reference',
'node'
);
$this->createEntityReferenceField(
'node',
'test',
'field_entity_ref_other_test',
'Test other entity type reference',
'node_type'
);
$admin_user = $this->drupalCreateUser(['administer node fields']);
$this->drupalLogin($admin_user);
}
/**
* Tests circular references in field UI.
*/
public function testCircularReference() {
// Ensure that entity reference fields that reference the same entity type
// can configure the circular reference validator.
$this->drupalGet(
'admin/structure/types/manage/test/fields/node.test.field_entity_ref_test'
);
$this->assertSession()->pageTextContains(t('Reference validators'));
$this->assertSession()->pageTextContains(t('Prevent circular references'));
$this->assertSession()->pageTextContains(
t('Recursively check circular references')
);
$edit = [
'settings[handler_settings][target_bundles][test]' => TRUE,
'third_party_settings[entity_reference_validators][circular_reference]' => TRUE,
];
$this->submitForm($edit, t('Save settings'), 'field-config-edit-form');
$this->assertTrue(
FieldConfig::loadByName('node', 'test', 'field_entity_ref_test')
->getThirdPartySetting(
'entity_reference_validators',
'circular_reference',
FALSE
)
);
$this->assertFalse(
FieldConfig::loadByName('node', 'test', 'field_entity_ref_test')
->getThirdPartySetting(
'entity_reference_validators',
'circular_reference_deep',
FALSE
)
);
$this->drupalGet(
'admin/structure/types/manage/test/fields/node.test.field_entity_ref_test'
);
$this->assertSession()->pageTextContains(
t('Recursively check circular references')
);
$edit = [
'third_party_settings[entity_reference_validators][circular_reference_deep]' => TRUE,
];
$this->submitForm($edit, t('Save settings'), 'field-config-edit-form');
$this->assertTrue(
FieldConfig::loadByName('node', 'test', 'field_entity_ref_test')
->getThirdPartySetting(
'entity_reference_validators',
'circular_reference_deep',
FALSE
)
);
// Ensure that setting the circular_reference setting to FALSE also sets the
// circular_reference_deep setting to FALSE.
$this->drupalGet(
'admin/structure/types/manage/test/fields/node.test.field_entity_ref_test'
);
$this->assertSession()->pageTextContains(
t('Recursively check circular references')
);
$edit = [
'third_party_settings[entity_reference_validators][circular_reference]' => FALSE,
];
$this->submitForm($edit, t('Save settings'), 'field-config-edit-form');
$this->assertFalse(
FieldConfig::loadByName('node', 'test', 'field_entity_ref_test')
->getThirdPartySetting(
'entity_reference_validators',
'circular_reference',
FALSE
)
);
$this->assertFalse(
FieldConfig::loadByName('node', 'test', 'field_entity_ref_test')
->getThirdPartySetting(
'entity_reference_validators',
'circular_reference_deep',
FALSE
)
);
// Ensure that entity reference fields that reference other entity types
// cannot configure the circular reference validator.
$this->drupalGet(
'admin/structure/types/manage/test/fields/node.test.field_entity_ref_other_test'
);
$this->assertSession()->pageTextNotContains(
t('Prevent circular references')
);
// Just check something positive so we know we're on the proper page.
$this->assertSession()->pageTextContains(t('Reference type'));
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Drupal\Tests\entity_reference_validators\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* Tests duplicates of entity reference validation.
*
* @group entity_reference
*/
class DuplicateEntityReferenceTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use EntityReferenceTestTrait;
/**
* The entity reference field under test.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'entity_reference_validators',
'node',
'field_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Sets up the test.
*/
protected function setUp(): void {
parent::setUp();
$this->createContentType(['type' => 'test']);
$this->createEntityReferenceField(
'node',
'test',
'field_entity_ref_test',
'Test duplicate entity reference',
'node'
);
$admin_user = $this->drupalCreateUser(['administer node fields']);
$this->drupalLogin($admin_user);
}
/**
* Tests duplicate references in field UI.
*/
public function testDuplicateReference() {
// Ensure that the user can't create entity reference to the same entity
// multiple times.
$this->drupalGet(
'admin/structure/types/manage/test/fields/node.test.field_entity_ref_test'
);
$this->assertSession()->pageTextContains(t('Reference validators'));
$this->assertSession()->pageTextContains(
t('Prevent entity from referencing duplicates')
);
}
}

View File

@@ -0,0 +1,209 @@
<?php
namespace Drupal\Tests\entity_reference_validators\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
/**
* Tests circular entity reference validation.
*
* @group entity_reference
*/
class CircularEntityReferenceTest extends FieldKernelTestBase {
use EntityReferenceTestTrait;
/**
* Modules to install.
*
* @var array
*/
public static $modules = ['entity_reference_validators'];
/**
* Tests circular references.
*/
public function testCircularReference() {
$this->createEntityReferenceField(
'entity_test',
'entity_test',
'field_circular_entity_test',
'Circular field',
'entity_test'
);
$circularField = FieldConfig::loadByName(
'entity_test',
'entity_test',
'field_circular_entity_test'
);
$circularField->setThirdPartySetting(
'entity_reference_validators',
'circular_reference',
TRUE
)->save();
$this->createEntityReferenceField(
'entity_test',
'entity_test',
'field_not_circular_entity_test',
'Not circular field',
'entity_test'
);
$notCircularField = FieldConfig::loadByName(
'entity_test',
'entity_test',
'field_not_circular_entity_test'
);
$notCircularField->setThirdPartySetting(
'entity_reference_validators',
'circular_reference',
FALSE
)->save();
// Create a test entity.
$entity1 = EntityTest::create();
$entity1->save();
// Create a test entity that references the first entity.
$entity2 = EntityTest::create();
$entity2->field_circular_entity_test->target_id = $entity1->id();
$entity2->save();
$errors = $entity2->validate();
$this->assertCount(0, $errors);
// Make the first entity created reference itself.
$entity1->field_circular_entity_test->target_id = $entity1->id();
$errors = $entity1->validate();
$this->assertCount(1, $errors);
$this->assertEquals(
new FormattableMarkup(
'This entity (%type: %id) cannot be referenced.',
['%type' => 'entity_test', '%id' => $entity1->id()]
),
$errors[0]->getMessage()
);
$this->assertEquals(
'field_circular_entity_test.0.target_id',
$errors[0]->getPropertyPath()
);
// Reload the entity refresh field definitions.
$entity1 = EntityTest::load($entity1->id());
$entity1->field_not_circular_entity_test->target_id = $entity1->id();
$errors = $entity1->validate();
$this->assertCount(0, $errors);
}
/**
* Tests deep circular references.
*/
public function testDeepCircularReference() {
$this->createEntityReferenceField(
'entity_test',
'entity_test',
'field_deep_circular_entity_test',
'Deep circular field',
'entity_test',
'default',
[],
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
);
$circularField = FieldConfig::loadByName(
'entity_test',
'entity_test',
'field_deep_circular_entity_test'
);
$circularField
->setThirdPartySetting(
'entity_reference_validators',
'circular_reference',
TRUE
)
->setThirdPartySetting(
'entity_reference_validators',
'circular_reference_deep',
TRUE
)
->save();
// Create a test entity.
$entity1 = EntityTest::create();
$entity1->save();
// Create a second test entity that references the first entity.
$entity2 = EntityTest::create();
$entity2->field_deep_circular_entity_test->target_id = $entity1->id();
$entity2->save();
$errors = $entity2->validate();
$this->assertCount(0, $errors);
// Create a third test entity that references the second entity.
$entity3 = EntityTest::create();
$entity3->field_deep_circular_entity_test->target_id = $entity2->id();
$entity3->save();
$errors = $entity3->validate();
$this->assertCount(0, $errors);
// Make the third entity created reference the first.
$entity1->field_deep_circular_entity_test->target_id = $entity3->id();
$errors = $entity1->validate();
$this->assertCount(1, $errors);
$this->assertEquals(
new FormattableMarkup(
'This entity (%type: %id) cannot be referenced.',
['%type' => 'entity_test', '%id' => $entity3->id()]
),
$errors[0]->getMessage()
);
$this->assertEquals(
'field_deep_circular_entity_test.0.target_id',
$errors[0]->getPropertyPath()
);
// Make another entity to test multiple cardinalities.
$entity4 = EntityTest::create();
$entity4->save();
$entity2->field_deep_circular_entity_test = [$entity4, $entity1];
$errors = $entity1->validate();
$this->assertCount(1, $errors);
$this->assertEquals(
new FormattableMarkup(
'This entity (%type: %id) cannot be referenced.',
['%type' => 'entity_test', '%id' => $entity3->id()]
),
$errors[0]->getMessage()
);
$this->assertEquals(
'field_deep_circular_entity_test.0.target_id',
$errors[0]->getPropertyPath()
);
// Test the property path is correct.
$entity1->field_deep_circular_entity_test = [$entity4, $entity3];
$errors = $entity1->validate();
$this->assertCount(1, $errors);
$this->assertEquals(
new FormattableMarkup(
'This entity (%type: %id) cannot be referenced.',
['%type' => 'entity_test', '%id' => $entity3->id()]
),
$errors[0]->getMessage()
);
$this->assertEquals(
'field_deep_circular_entity_test.1.target_id',
$errors[0]->getPropertyPath()
);
// Break the chain.
$entity2->field_deep_circular_entity_test = [];
$entity2->save();
$errors = $entity1->validate();
$this->assertCount(0, $errors);
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace Drupal\Tests\entity_reference_validators\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
/**
* Tests duplicates of entity reference validation.
*
* @group entity_reference
*/
class DuplicateEntityReferenceTest extends FieldKernelTestBase {
use EntityReferenceTestTrait;
/**
* The entity reference field under test.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* Modules to install.
*
* @var array
*/
public static $modules = ['entity_reference_validators'];
/**
* Sets up the test.
*/
protected function setUp():void {
parent::setUp();
// Use the util to create an instance.
$this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_entity_test', 'Test entity reference', 'entity_test', 'default', [], -1);
$this->field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_entity_test');
$this->field->setThirdPartySetting('entity_reference_validators', 'duplicate_reference', TRUE)->save();
}
/**
* Tests duplicate references.
*/
public function testDuplicateReference() {
// Create test entities.
$entity1 = EntityTest::create();
$entity1->save();
$entity2 = EntityTest::create();
$entity2->save();
// Create a test entity that references other entities.
$entity3 = EntityTest::create();
$entity3->field_test_entity_test[] = [
['target_id' => $entity1->id()],
['target_id' => $entity2->id()],
];
$entity3->save();
$errors = $entity3->validate();
$this->assertCount(0, $errors);
// Make the third entity reference the same entity twice.
$entity3 = EntityTest::create();
$entity3->field_test_entity_test = [
['target_id' => $entity2->id()],
['target_id' => $entity2->id()],
];
$entity3->save();
$errors = $entity3->validate();
$this->assertCount(2, $errors);
$this->assertEquals(new FormattableMarkup('The value %label has been entered multiple times.', ['%label' => EntityAutocomplete::getEntityLabels([$entity2])]), $errors[0]->getMessage());
$this->assertEquals('field_test_entity_test.0.target_id', $errors[0]->getPropertyPath());
// Ensure the validator is configurable.
$this->field->setThirdPartySetting('entity_reference_validators', 'duplicate_reference', FALSE)->save();
// Reload the entity refresh field definitions.
$entity3 = EntityTest::create();
$entity3->field_test_entity_test = [
['target_id' => $entity2->id()],
['target_id' => $entity2->id()],
];
$errors = $entity3->validate();
$this->assertCount(0, $errors);
}
}