Issue #3449259 by narendraR, alexpott, catch, Wim Leers: Add validation constraints to system.action.*

merge-requests/5838/merge
Alex Pott 2024-07-04 00:32:25 +01:00
parent a0466c55cf
commit 9ddbc2c784
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
6 changed files with 63 additions and 3 deletions

View File

@ -286,6 +286,8 @@ system.menu.*:
system.action.*:
type: config_entity
label: 'System action'
constraints:
FullyValidatable: ~
mapping:
id:
type: machine_name
@ -302,6 +304,12 @@ system.action.*:
type:
type: string
label: 'Type'
# Action can be specified without type.
# @see \Drupal\action_test\Plugin\Action\NoType
nullable: true
constraints:
NotBlank:
allowNull: true
plugin:
type: string
label: 'Plugin'

View File

@ -19,7 +19,10 @@ interface ActionConfigEntityInterface extends ConfigEntityInterface {
/**
* Returns the operation type.
*
* @return string
* The operation type can be NULL if no type is specified.
*
* @return string|null
* The operation type, or NULL if no type is specified.
*/
public function getType();

View File

@ -57,9 +57,9 @@ class Action extends ConfigEntityBase implements ActionConfigEntityInterface, En
/**
* The action type.
*
* @var string
* @var string|null
*/
protected $type;
protected $type = NULL;
/**
* The configuration of the action.

View File

@ -0,0 +1,6 @@
# Schema for the configuration files of the Action test module.
action.configuration.action_test_no_type:
type: mapping
label: 'Configuration for action_test_no_type plugin'
mapping: {}

View File

@ -101,4 +101,26 @@ class ActionTest extends KernelTestBase {
$this->assertSame($expected, $action->calculateDependencies()->getDependencies());
}
/**
* Tests no type specified action.
*/
public function testNoTypeAction(): void {
// Create an action config entity using the action_test_no_type plugin.
$action = Action::create([
'id' => 'action_test_no_type_action',
'label' => 'Test Action with No Type',
'plugin' => 'action_test_no_type',
]);
$action->save();
// Reload the action to ensure it's saved correctly.
$action = Action::load('action_test_no_type_action');
// Assert that the action was saved and loaded correctly.
$this->assertNotNull($action, 'The action config entity was saved and loaded correctly.');
$this->assertSame('action_test_no_type_action', $action->id(), 'The action ID is correct.');
$this->assertSame('Test Action with No Type', $action->label(), 'The action label is correct.');
$this->assertNull($action->getType(), 'The action type is correctly set to NULL.');
}
}

View File

@ -14,6 +14,11 @@ use Drupal\system\Entity\Action;
*/
class ActionValidationTest extends ConfigEntityValidationTestBase {
/**
* {@inheritdoc}
*/
protected static array $propertiesWithOptionalValues = ['type'];
/**
* {@inheritdoc}
*/
@ -53,4 +58,20 @@ class ActionValidationTest extends ConfigEntityValidationTestBase {
]);
}
/**
* {@inheritdoc}
*/
public function testImmutableProperties(array $valid_values = []): void {
$valid_values['id'] = 'test_changed';
parent::testImmutableProperties($valid_values);
}
/**
* {@inheritdoc}
*/
public function testLabelValidation(): void {
static::setLabel($this->entity, "Multi\nLine");
$this->assertValidationErrors(['label' => "Labels are not allowed to span multiple lines or contain control characters."]);
}
}