Skip to content
Snippets Groups Projects
Verified Commit 7b657ee6 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3440329 by phenaproxima: Expose ConfigEntityBase::setThirdPartySetting() as an action method

(cherry picked from commit a23d2ff720a0874c31ab31d6c48cf82a35d23fb1)
parent 64899341
No related branches found
No related tags found
1 merge request!7908Recipes API on 10.3.x
Showing
with 148 additions and 59 deletions
......@@ -4,6 +4,7 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Action\Attribute\ActionMethod;
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Config\ConfigDuplicateUUIDException;
......@@ -12,6 +13,7 @@
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Entity\SynchronizableEntityTrait;
use Drupal\Core\Plugin\PluginDependencyTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a base configuration entity class.
......@@ -502,6 +504,7 @@ protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_typ
/**
* {@inheritdoc}
*/
#[ActionMethod(adminLabel: new TranslatableMarkup('Set third-party setting'))]
public function setThirdPartySetting($module, $key, $value) {
$this->third_party_settings[$module][$key] = $value;
return $this;
......
......@@ -4,12 +4,11 @@
namespace Drupal\Tests\ckeditor5\Kernel\ConfigAction;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Recipe\InvalidConfigException;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\editor\Entity\Editor;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
/**
......@@ -19,6 +18,8 @@
*/
class AddItemToToolbarConfigActionTest extends KernelTestBase {
use RecipeTestTrait;
/**
* {@inheritdoc}
*/
......@@ -122,21 +123,4 @@ public function testActionRequiresCKEditor5(): void {
RecipeRunner::processRecipe($this->createRecipe($recipe));
}
/**
* @param string|array<mixed> $contents
* The contents of recipe.yml, as either a YAML string or an array to encode
* to YAML.
*
* @return \Drupal\Core\Recipe\Recipe
*/
private function createRecipe(string|array $contents): Recipe {
if (is_array($contents)) {
$contents = Yaml::encode($contents);
}
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $contents);
return Recipe::createFromDirectory($dir);
}
}
......@@ -8,6 +8,7 @@
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
......@@ -22,6 +23,9 @@
class AddModerationConfigActionTest extends KernelTestBase {
use ContentTypeCreationTrait;
use RecipeTestTrait {
createRecipe as traitCreateRecipe;
}
use TaxonomyTestTrait;
/**
......@@ -88,9 +92,6 @@ public function testDeriverAdminLabel(): void {
}
private function createRecipe(string $config_name): Recipe {
$dir = uniqid('public://');
mkdir($dir);
$recipe = <<<YAML
name: 'Add entity types and bundles to workflow'
recipes:
......@@ -103,8 +104,7 @@ private function createRecipe(string $config_name): Recipe {
- b
addTaxonomyVocabularies: '*'
YAML;
file_put_contents($dir . '/recipe.yml', $recipe);
return Recipe::createFromDirectory($dir);
return $this->traitCreateRecipe($recipe);
}
}
......@@ -8,9 +8,9 @@
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\field\Entity\FieldConfig;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
......@@ -22,6 +22,8 @@
*/
class AddToAllBundlesConfigActionTest extends KernelTestBase {
use RecipeTestTrait;
/**
* {@inheritdoc}
*/
......@@ -139,11 +141,7 @@ private function applyAction(string $config_name, bool $fail_if_exists = FALSE):
description: Set by config actions.
fail_if_exists: $fail_if_exists
YAML;
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $contents);
$recipe = Recipe::createFromDirectory($dir);
$recipe = $this->createRecipe($contents);
RecipeRunner::processRecipe($recipe);
}
......
......@@ -4,6 +4,8 @@
namespace Drupal\FunctionalTests\Core\Recipe;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Recipe\Recipe;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
......@@ -13,6 +15,27 @@
*/
trait RecipeTestTrait {
/**
* Creates a recipe in a temporary directory.
*
* @param string|array<mixed> $data
* The contents of recipe.yml. If passed as an array, will be encoded to
* YAML.
*
* @return \Drupal\Core\Recipe\Recipe
* The recipe object.
*/
protected function createRecipe(string|array $data): Recipe {
if (is_array($data)) {
$data = Yaml::encode($data);
}
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $data);
return Recipe::createFromDirectory($dir);
}
/**
* Applies a recipe to the site.
*
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeFileException;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
/**
......@@ -16,6 +17,8 @@
*/
class ConfigActionValidationTest extends KernelTestBase {
use RecipeTestTrait;
/**
* {@inheritdoc}
*/
......@@ -78,11 +81,7 @@ public function testConfigActionsAreValidated(string $entity_type_id): void {
$label_key: ''
YAML;
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $recipe_data);
$recipe = Recipe::createFromDirectory($dir);
$recipe = $this->createRecipe($recipe_data);
try {
RecipeRunner::processRecipe($recipe);
$this->fail('An exception should have been thrown.');
......@@ -123,11 +122,8 @@ public function testConfigActionMissingDependency(): void {
label: ''
YAML;
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $recipe_data);
try {
Recipe::createFromDirectory($dir);
$this->createRecipe($recipe_data);
$this->fail('An exception should have been thrown.');
}
catch (RecipeFileException $e) {
......
<?php
declare(strict_types=1);
namespace Drupal\KernelTests\Core\Recipe;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* @group Recipe
*/
class EntityMethodConfigActionsTest extends KernelTestBase {
use ContentTypeCreationTrait;
use RecipeTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'field',
'layout_builder',
'layout_discovery',
'node',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('node');
$this->createContentType(['type' => 'test']);
$this->container->get(EntityDisplayRepositoryInterface::class)
->getViewDisplay('node', 'test', 'full')
->save();
}
public function testSetSingleThirdPartySetting(): void {
$recipe = <<<YAML
name: Third-party setting
config:
actions:
core.entity_view_display.node.test.full:
setThirdPartySetting:
module: layout_builder
key: enabled
value: true
YAML;
$recipe = $this->createRecipe($recipe);
RecipeRunner::processRecipe($recipe);
/** @var \Drupal\Core\Config\Entity\ThirdPartySettingsInterface $display */
$display = $this->container->get(EntityDisplayRepositoryInterface::class)
->getViewDisplay('node', 'test', 'full');
$this->assertTrue($display->getThirdPartySetting('layout_builder', 'enabled'));
}
public function testSetMultipleThirdPartySettings(): void {
$recipe = <<<YAML
name: Third-party setting
config:
actions:
core.entity_view_display.node.test.full:
setThirdPartySettings:
-
module: layout_builder
key: enabled
value: true
-
module: layout_builder
key: allow_custom
value: true
YAML;
$recipe = $this->createRecipe($recipe);
RecipeRunner::processRecipe($recipe);
/** @var \Drupal\Core\Config\Entity\ThirdPartySettingsInterface $display */
$display = $this->container->get(EntityDisplayRepositoryInterface::class)
->getViewDisplay('node', 'test', 'full');
$this->assertTrue($display->getThirdPartySetting('layout_builder', 'enabled'));
$this->assertTrue($display->getThirdPartySetting('layout_builder', 'allow_custom'));
}
}
......@@ -7,8 +7,8 @@
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
......@@ -27,6 +27,7 @@ class PermissionsPerBundleTest extends KernelTestBase {
use ContentTypeCreationTrait;
use MediaTypeCreationTrait;
use RecipeTestTrait;
use TaxonomyTestTrait;
use UserCreationTrait;
......@@ -223,10 +224,7 @@ public function testInvalidValue(mixed $value): void {
* The contents of `recipe.yml`.
*/
private function applyRecipeFromString(string $recipe_data): void {
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $recipe_data);
$recipe = Recipe::createFromDirectory($dir);
$recipe = $this->createRecipe($recipe_data);
RecipeRunner::processRecipe($recipe);
}
......
......@@ -8,6 +8,7 @@
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipePreExistingConfigException;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\views\Entity\View;
......@@ -18,6 +19,8 @@
*/
class RecipeRunnerTest extends KernelTestBase {
use RecipeTestTrait;
public function testModuleInstall(): void {
// Test the state prior to applying the recipe.
$this->assertFalse($this->container->get('module_handler')->moduleExists('filter'), 'The filter module is not installed');
......@@ -209,11 +212,7 @@ public function testInvalidConfigAction() :void {
setBody: 'Description set by recipe'
YAML;
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $recipe_data);
$recipe = Recipe::createFromDirectory($dir);
$recipe = $this->createRecipe($recipe_data);
$this->expectException(PluginNotFoundException::class);
$this->expectExceptionMessage('The "setBody" plugin does not exist.');
RecipeRunner::processRecipe($recipe);
......
......@@ -6,11 +6,11 @@
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
......@@ -22,6 +22,7 @@
class WildcardConfigActionsTest extends KernelTestBase {
use ContentTypeCreationTrait;
use RecipeTestTrait;
/**
* {@inheritdoc}
......@@ -96,10 +97,7 @@ public function testTargetEntitiesByWildcards(string $expression, array $expecte
setLabel: 'Changed by config action'
YAML;
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $contents);
$recipe = Recipe::createFromDirectory($dir);
$recipe = $this->createRecipe($contents);
RecipeRunner::processRecipe($recipe);
$changed = $this->container->get(EntityTypeManagerInterface::class)
......@@ -127,11 +125,7 @@ public function testInvalidExpression(string $expression, string $expected_excep
simple_config_update:
label: 'Changed by config action'
YAML;
$dir = uniqid('public://');
mkdir($dir);
file_put_contents($dir . '/recipe.yml', $contents);
$recipe = Recipe::createFromDirectory($dir);
$recipe = $this->createRecipe($contents);
$this->expectException(ConfigActionException::class);
$this->expectExceptionMessage($expected_exception_message);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment