From a798064dacad0595a5e142ac1909a6037f2f90c3 Mon Sep 17 00:00:00 2001 From: catch <6915-catch@users.noreply.drupalcode.org> Date: Thu, 13 Feb 2025 13:03:01 +0000 Subject: [PATCH] Issue #3495963 by nikolay shapovalov, berdir: Move helpers in entity_test.module --- core/.phpstan-baseline.php | 18 ----- .../modules/entity_test/entity_test.module | 66 ------------------- .../modules/entity_test/src/Callbacks.php | 30 +++++++++ .../src/Entity/EntityTestDefaultValue.php | 35 +++++++++- .../src/Entity/EntityTestMulDefaultValue.php | 2 +- .../entity_test/src/Hook/EntityTestHooks.php | 63 +++++++++++------- .../Entity/EntityFieldDefaultValueTest.php | 2 +- .../Core/Entity/EntityTranslationTest.php | 4 +- 8 files changed, 108 insertions(+), 112 deletions(-) create mode 100644 core/modules/system/tests/modules/entity_test/src/Callbacks.php diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php index 0d1515b32436..17228344117e 100644 --- a/core/.phpstan-baseline.php +++ b/core/.phpstan-baseline.php @@ -35163,12 +35163,6 @@ 'count' => 1, 'path' => __DIR__ . '/modules/system/tests/modules/entity_schema_test/src/Hook/EntitySchemaTestHooks.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Function _entity_test_record_hooks\\(\\) has no return type specified\\.$#', - 'identifier' => 'missingType.return', - 'count' => 1, - 'path' => __DIR__ . '/modules/system/tests/modules/entity_test/entity_test.module', -]; $ignoreErrors[] = [ 'message' => '#^Function entity_test_create_bundle\\(\\) has no return type specified\\.$#', 'identifier' => 'missingType.return', @@ -35181,18 +35175,6 @@ 'count' => 1, 'path' => __DIR__ . '/modules/system/tests/modules/entity_test/entity_test.module', ]; -$ignoreErrors[] = [ - 'message' => '#^Function entity_test_form_entity_test_form_validate\\(\\) has no return type specified\\.$#', - 'identifier' => 'missingType.return', - 'count' => 1, - 'path' => __DIR__ . '/modules/system/tests/modules/entity_test/entity_test.module', -]; -$ignoreErrors[] = [ - 'message' => '#^Function entity_test_form_entity_test_form_validate_check\\(\\) has no return type specified\\.$#', - 'identifier' => 'missingType.return', - 'count' => 1, - 'path' => __DIR__ . '/modules/system/tests/modules/entity_test/entity_test.module', -]; $ignoreErrors[] = [ 'message' => '#^Method Drupal\\\\entity_test\\\\Controller\\\\EntityTestController\\:\\:testAdmin\\(\\) has no return type specified\\.$#', 'identifier' => 'missingType.return', diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 30e55b6145e2..6ee66367027d 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -7,9 +7,6 @@ declare(strict_types=1); -use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Form\FormStateInterface; use Drupal\entity_test\EntityTestHelper; /** @@ -52,66 +49,3 @@ function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') { @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use \Drupal\entity_test\EntityTestHelper::deleteBundle() instead. See https://www.drupal.org/node/3497049', E_USER_DEPRECATED); EntityTestHelper::deleteBundle($bundle, $entity_type); } - -/** - * Validation handler for the entity_test entity form. - */ -function entity_test_form_entity_test_form_validate(array &$form, FormStateInterface $form_state) { - $form['#entity_test_form_validate'] = TRUE; -} - -/** - * Validation handler for the entity_test entity form. - */ -function entity_test_form_entity_test_form_validate_check(array &$form, FormStateInterface $form_state) { - if (!empty($form['#entity_test_form_validate'])) { - \Drupal::state()->set('entity_test.form.validate.result', TRUE); - } -} - -/** - * Field default value callback. - * - * @param \Drupal\Core\Entity\FieldableEntityInterface $entity - * The entity being created. - * @param \Drupal\Core\Field\FieldDefinitionInterface $definition - * The field definition. - * - * @return array - * An array of default values, in the same format as the $default_value - * property. - * - * @see \Drupal\field\Entity\FieldConfig::$default_value - */ -function entity_test_field_default_value(FieldableEntityInterface $entity, FieldDefinitionInterface $definition) { - // Include the field name and entity language in the generated values to check - // that they are correctly passed. - $string = $definition->getName() . '_' . $entity->language()->getId(); - // Return a "default value" with multiple items. - return [ - [ - 'shape' => "shape:0:$string", - 'color' => "color:0:$string", - ], - [ - 'shape' => "shape:1:$string", - 'color' => "color:1:$string", - ], - ]; -} - -/** - * Helper function to be used to record hook invocations. - * - * @param string $hook - * The hook name. - * @param mixed $data - * Arbitrary data associated with the hook invocation. - */ -function _entity_test_record_hooks($hook, $data) { - $state = \Drupal::state(); - $key = 'entity_test.hooks'; - $hooks = $state->get($key); - $hooks[$hook] = $data; - $state->set($key, $hooks); -} diff --git a/core/modules/system/tests/modules/entity_test/src/Callbacks.php b/core/modules/system/tests/modules/entity_test/src/Callbacks.php new file mode 100644 index 000000000000..9919fa4ed8f0 --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Callbacks.php @@ -0,0 +1,30 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\entity_test; + +use Drupal\Core\Form\FormStateInterface; + +/** + * Simple object with callbacks. + */ +class Callbacks { + + /** + * Validation handler for the entity_test entity form. + */ + public static function entityTestFormValidate(array &$form, FormStateInterface $form_state): void { + $form['#entity_test_form_validate'] = TRUE; + } + + /** + * Validation handler for the entity_test entity form. + */ + public static function entityTestFormValidateCheck(array &$form, FormStateInterface $form_state): void { + if (!empty($form['#entity_test_form_validate'])) { + \Drupal::state()->set('entity_test.form.validate.result', TRUE); + } + } + +} diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestDefaultValue.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestDefaultValue.php index 3d5aaca9080c..d47363dee3ba 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestDefaultValue.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestDefaultValue.php @@ -5,6 +5,8 @@ namespace Drupal\entity_test\Entity; use Drupal\Core\Entity\Attribute\ContentEntityType; +use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -33,9 +35,40 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['description'] = BaseFieldDefinition::create('shape') ->setLabel(t('Some custom description')) - ->setDefaultValueCallback('entity_test_field_default_value'); + ->setDefaultValueCallback(static::class . '::descriptionDefaultValue'); return $fields; } + /** + * Field default value callback. + * + * @param \Drupal\Core\Entity\FieldableEntityInterface $entity + * The entity being created. + * @param \Drupal\Core\Field\FieldDefinitionInterface $definition + * The field definition. + * + * @return array + * An array of default values, in the same format as the $default_value + * property. + * + * @see \Drupal\field\Entity\FieldConfig::$default_value + */ + public static function descriptionDefaultValue(FieldableEntityInterface $entity, FieldDefinitionInterface $definition): array { + // Include the field name and entity language in the generated values to + // check that they are correctly passed. + $string = $definition->getName() . '_' . $entity->language()->getId(); + // Return a "default value" with multiple items. + return [ + [ + 'shape' => "shape:0:$string", + 'color' => "color:0:$string", + ], + [ + 'shape' => "shape:1:$string", + 'color' => "color:1:$string", + ], + ]; + } + } diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php index 828032e8682c..30245cf42773 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulDefaultValue.php @@ -57,7 +57,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['description'] = BaseFieldDefinition::create('shape') ->setLabel(t('Some custom description')) ->setTranslatable(TRUE) - ->setDefaultValueCallback('entity_test_field_default_value'); + ->setDefaultValueCallback(EntityTestDefaultValue::class . '::descriptionDefaultValue'); return $fields; } diff --git a/core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php b/core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php index d665a70186cd..19d38732f4dc 100644 --- a/core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php +++ b/core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php @@ -22,6 +22,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Hook\Attribute\Hook; use Drupal\entity_test\EntityTestHelper; +use Drupal\entity_test\Callbacks; use Drupal\entity_test\Entity\EntityTest; /** @@ -242,12 +243,12 @@ public function entityExtraFieldInfo(): array { public function formEntityTestFormAlter(&$form) : void { switch (\Drupal::state()->get('entity_test.form.validate.test')) { case 'form-level': - $form['#validate'][] = 'entity_test_form_entity_test_form_validate'; - $form['#validate'][] = 'entity_test_form_entity_test_form_validate_check'; + $form['#validate'][] = [Callbacks::class, 'entityTestFormValidate']; + $form['#validate'][] = [Callbacks::class, 'entityTestFormValidateCheck']; break; case 'button-level': - $form['actions']['submit']['#validate'][] = 'entity_test_form_entity_test_form_validate'; + $form['actions']['submit']['#validate'][] = [Callbacks::class, 'entityTestFormValidateCheck']; } } @@ -410,7 +411,7 @@ public function entityOperationAlter(array &$operations, EntityInterface $entity */ #[Hook('entity_translation_create')] public function entityTranslationCreate(EntityInterface $translation) { - _entity_test_record_hooks('entity_translation_create', $translation->language()->getId()); + $this->recordHooks('entity_translation_create', $translation->language()->getId()); } /** @@ -418,7 +419,7 @@ public function entityTranslationCreate(EntityInterface $translation) { */ #[Hook('entity_translation_insert')] public function entityTranslationInsert(EntityInterface $translation) { - _entity_test_record_hooks('entity_translation_insert', $translation->language()->getId()); + $this->recordHooks('entity_translation_insert', $translation->language()->getId()); } /** @@ -426,7 +427,7 @@ public function entityTranslationInsert(EntityInterface $translation) { */ #[Hook('entity_translation_delete')] public function entityTranslationDelete(EntityInterface $translation) { - _entity_test_record_hooks('entity_translation_delete', $translation->language()->getId()); + $this->recordHooks('entity_translation_delete', $translation->language()->getId()); } /** @@ -434,7 +435,7 @@ public function entityTranslationDelete(EntityInterface $translation) { */ #[Hook('entity_test_mul_translation_create')] public function entityTestMulTranslationCreate(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_translation_create', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_translation_create', $translation->language()->getId()); } /** @@ -442,7 +443,7 @@ public function entityTestMulTranslationCreate(EntityInterface $translation) { */ #[Hook('entity_test_mul_translation_insert')] public function entityTestMulTranslationInsert(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_translation_insert', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_translation_insert', $translation->language()->getId()); } /** @@ -450,7 +451,7 @@ public function entityTestMulTranslationInsert(EntityInterface $translation) { */ #[Hook('entity_test_mul_translation_delete')] public function entityTestMulTranslationDelete(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_translation_delete', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_translation_delete', $translation->language()->getId()); } /** @@ -458,7 +459,7 @@ public function entityTestMulTranslationDelete(EntityInterface $translation) { */ #[Hook('entity_test_mul_changed_translation_create')] public function entityTestMulChangedTranslationCreate(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_changed_translation_create', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_changed_translation_create', $translation->language()->getId()); } /** @@ -466,7 +467,7 @@ public function entityTestMulChangedTranslationCreate(EntityInterface $translati */ #[Hook('entity_test_mul_changed_translation_insert')] public function entityTestMulChangedTranslationInsert(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_changed_translation_insert', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_changed_translation_insert', $translation->language()->getId()); } /** @@ -474,7 +475,7 @@ public function entityTestMulChangedTranslationInsert(EntityInterface $translati */ #[Hook('entity_test_mul_changed_translation_delete')] public function entityTestMulChangedTranslationDelete(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_changed_translation_delete', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_changed_translation_delete', $translation->language()->getId()); } /** @@ -482,7 +483,7 @@ public function entityTestMulChangedTranslationDelete(EntityInterface $translati */ #[Hook('entity_test_mulrev_translation_create')] public function entityTestMulrevTranslationCreate(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mulrev_translation_create', $translation->language()->getId()); + $this->recordHooks('entity_test_mulrev_translation_create', $translation->language()->getId()); } /** @@ -490,7 +491,7 @@ public function entityTestMulrevTranslationCreate(EntityInterface $translation) */ #[Hook('entity_test_mulrev_translation_insert')] public function entityTestMulrevTranslationInsert(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mulrev_translation_insert', $translation->language()->getId()); + $this->recordHooks('entity_test_mulrev_translation_insert', $translation->language()->getId()); } /** @@ -498,7 +499,7 @@ public function entityTestMulrevTranslationInsert(EntityInterface $translation) */ #[Hook('entity_test_mulrev_translation_delete')] public function entityTestMulrevTranslationDelete(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mulrev_translation_delete', $translation->language()->getId()); + $this->recordHooks('entity_test_mulrev_translation_delete', $translation->language()->getId()); } /** @@ -506,7 +507,7 @@ public function entityTestMulrevTranslationDelete(EntityInterface $translation) */ #[Hook('entity_test_mulrev_changed_translation_create')] public function entityTestMulrevChangedTranslationCreate(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mulrev_changed_translation_create', $translation->language()->getId()); + $this->recordHooks('entity_test_mulrev_changed_translation_create', $translation->language()->getId()); } /** @@ -514,7 +515,7 @@ public function entityTestMulrevChangedTranslationCreate(EntityInterface $transl */ #[Hook('entity_test_mulrev_changed_translation_insert')] public function entityTestMulrevChangedTranslationInsert(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mulrev_changed_translation_insert', $translation->language()->getId()); + $this->recordHooks('entity_test_mulrev_changed_translation_insert', $translation->language()->getId()); } /** @@ -522,7 +523,7 @@ public function entityTestMulrevChangedTranslationInsert(EntityInterface $transl */ #[Hook('entity_test_mulrev_changed_translation_delete')] public function entityTestMulrevChangedTranslationDelete(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mulrev_changed_translation_delete', $translation->language()->getId()); + $this->recordHooks('entity_test_mulrev_changed_translation_delete', $translation->language()->getId()); } /** @@ -530,7 +531,7 @@ public function entityTestMulrevChangedTranslationDelete(EntityInterface $transl */ #[Hook('entity_test_mul_langcode_key_translation_create')] public function entityTestMulLangcodeKeyTranslationCreate(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_langcode_key_translation_create', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_langcode_key_translation_create', $translation->language()->getId()); } /** @@ -538,7 +539,7 @@ public function entityTestMulLangcodeKeyTranslationCreate(EntityInterface $trans */ #[Hook('entity_test_mul_langcode_key_translation_insert')] public function entityTestMulLangcodeKeyTranslationInsert(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_langcode_key_translation_insert', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_langcode_key_translation_insert', $translation->language()->getId()); } /** @@ -546,7 +547,7 @@ public function entityTestMulLangcodeKeyTranslationInsert(EntityInterface $trans */ #[Hook('entity_test_mul_langcode_key_translation_delete')] public function entityTestMulLangcodeKeyTranslationDelete(EntityInterface $translation) { - _entity_test_record_hooks('entity_test_mul_langcode_key_translation_delete', $translation->language()->getId()); + $this->recordHooks('entity_test_mul_langcode_key_translation_delete', $translation->language()->getId()); } /** @@ -554,7 +555,7 @@ public function entityTestMulLangcodeKeyTranslationDelete(EntityInterface $trans */ #[Hook('entity_revision_create')] public function entityRevisionCreate(EntityInterface $new_revision, EntityInterface $entity, $keep_untranslatable_fields) { - _entity_test_record_hooks('entity_revision_create', [ + $this->recordHooks('entity_revision_create', [ 'new_revision' => $new_revision, 'entity' => $entity, 'keep_untranslatable_fields' => $keep_untranslatable_fields, @@ -569,7 +570,7 @@ public function entityTestMulrevRevisionCreate(EntityInterface $new_revision, En if ($new_revision->get('name')->value == 'revision_create_test_it') { $new_revision->set('name', 'revision_create_test_it_altered'); } - _entity_test_record_hooks('entity_test_mulrev_revision_create', [ + $this->recordHooks('entity_test_mulrev_revision_create', [ 'new_revision' => $new_revision, 'entity' => $entity, 'keep_untranslatable_fields' => $keep_untranslatable_fields, @@ -726,4 +727,20 @@ public function entityTestDuplicate(EntityTest $duplicate, EntityTest $entity) : } } + /** + * Helper function to be used to record hook invocations. + * + * @param string $hook + * The hook name. + * @param mixed $data + * Arbitrary data associated with the hook invocation. + */ + protected function recordHooks($hook, $data): void { + $state = \Drupal::state(); + $key = 'entity_test.hooks'; + $hooks = $state->get($key); + $hooks[$hook] = $data; + $state->set($key, $hooks); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php index 08af66e967a8..58555850f7f9 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php @@ -65,7 +65,7 @@ protected function assertDefaultValues(string $entity_type_id): void { public function testDefaultValueCallback(): void { $entity = $this->entityTypeManager->getStorage('entity_test_default_value')->create(); // The description field has a default value callback for testing, see - // entity_test_field_default_value(). + // \Drupal\entity_test\Entity\EntityTestDefaultValue::descriptionDefaultValue(). $string = 'description_' . $entity->language()->getId(); $expected = [ [ diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php index e09aafb52ee0..243d5682d8ed 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php @@ -549,8 +549,8 @@ protected function doTestEntityTranslationAPI($entity_type): void { // Check that per-language defaults are properly populated. The // 'entity_test_mul_default_value' entity type is translatable and uses - // entity_test_field_default_value() as a "default value callback" for its - // 'description' field. + // \Drupal\entity_test\Entity\EntityTestDefaultValue::descriptionDefaultValue() + // as a "default value callback" for its 'description' field. $entity = $this->entityTypeManager ->getStorage('entity_test_mul_default_value') ->create(['name' => $this->randomMachineName(), 'langcode' => $langcode]); -- GitLab