From 5633ac7e43c69e3379c032ad89fc1e7a810eba4c Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Mon, 23 Dec 2024 07:48:15 +1000 Subject: [PATCH] Issue #3495429 by nikolay shapovalov: Move helpers in options_test.module and delete it --- core/.phpstan-baseline.php | 12 ---- core/modules/options/options.api.php | 4 +- core/modules/options/options.module | 2 +- .../tests/options_test/options_test.module | 58 ------------------ .../options_test/src/OptionsAllowedValues.php | 60 +++++++++++++++++++ .../OptionsDynamicValuesTestBase.php | 4 +- .../src/Functional/OptionsWidgetsTest.php | 4 +- .../Kernel/OptionsDynamicValuesApiTest.php | 6 +- .../OptionsDynamicValuesValidationTest.php | 4 +- .../tests/src/Kernel/Views/ViewsDataTest.php | 2 +- 10 files changed, 73 insertions(+), 83 deletions(-) delete mode 100644 core/modules/options/tests/options_test/options_test.module create mode 100644 core/modules/options/tests/options_test/src/OptionsAllowedValues.php diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php index b14a8730b684..f9a888c25d27 100644 --- a/core/.phpstan-baseline.php +++ b/core/.phpstan-baseline.php @@ -30918,18 +30918,6 @@ 'count' => 1, 'path' => __DIR__ . '/modules/options/src/Plugin/views/filter/ListField.php', ]; -$ignoreErrors[] = [ - // identifier: missingType.return - 'message' => '#^Function options_test_allowed_values_callback\\(\\) has no return type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/modules/options/tests/options_test/options_test.module', -]; -$ignoreErrors[] = [ - // identifier: missingType.return - 'message' => '#^Function options_test_dynamic_values_callback\\(\\) has no return type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/modules/options/tests/options_test/options_test.module', -]; $ignoreErrors[] = [ // identifier: missingType.return 'message' => '#^Method Drupal\\\\Tests\\\\options\\\\FunctionalJavascript\\\\OptionsFieldUIAllowedValuesTest\\:\\:assertNodeFormOrder\\(\\) has no return type specified\\.$#', diff --git a/core/modules/options/options.api.php b/core/modules/options/options.api.php index 97641b1f5290..15367cddaba8 100644 --- a/core/modules/options/options.api.php +++ b/core/modules/options/options.api.php @@ -77,8 +77,8 @@ function hook_options_list_alter(array &$options, array $context) { * * @ingroup callbacks * @see options_allowed_values() - * @see options_test_allowed_values_callback() - * @see options_test_dynamic_values_callback() + * @see \Drupal\options_test\OptionsAllowedValues::simpleValues() + * @see \Drupal\options_test\OptionsAllowedValues::dynamicValues() */ function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) { if (isset($entity) && ($entity->bundle() == 'not_a_programmer')) { diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 7c648a5c8e4c..e7cd0e7fe616 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -41,7 +41,7 @@ function options_allowed_values(FieldStorageDefinitionInterface $definition, ?Fi if (!isset($allowed_values[$cache_id])) { $function = $definition->getSetting('allowed_values_function'); // If $cacheable is FALSE, then the allowed values are not statically - // cached. See options_test_dynamic_values_callback() for an example of + // cached. See OptionsAllowedValues::dynamicValues() for an example of // generating dynamic and uncached values. $cacheable = TRUE; if (!empty($function)) { diff --git a/core/modules/options/tests/options_test/options_test.module b/core/modules/options/tests/options_test/options_test.module deleted file mode 100644 index 6138d62f005d..000000000000 --- a/core/modules/options/tests/options_test/options_test.module +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -/** - * @file - * Helper module for the List module tests. - */ - -declare(strict_types=1); - -use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\Core\Field\FieldStorageDefinitionInterface; - -/** - * Implements callback_allowed_values_function(). - * - * @see options_allowed_values() - */ -function options_test_allowed_values_callback(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL) { - $values = [ - 'Group 1' => [ - 0 => 'Zero', - ], - 1 => 'One', - 'Group 2' => [ - 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>', - ], - 'More <script>dangerous</script> markup' => [ - 3 => 'Three', - ], - ]; - - return $values; -} - -/** - * Implements callback_allowed_values_function(). - * - * @todo This function violates the recommendation in options_allowed_values() - * to return a list of all possible values in any context when $items is - * NULL. Since this is not yet used for testing Views integration, that is - * alright for now. Fix this in https://www.drupal.org/node/2012130. - * - * @see options_allowed_values() - */ -function options_test_dynamic_values_callback(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL, &$cacheable = NULL) { - $values = []; - if (isset($entity)) { - $cacheable = FALSE; - $values = [ - $entity->label(), - $entity->toUrl()->toString(), - $entity->uuid(), - $entity->bundle(), - ]; - } - // We need the values of the entity as keys. - return array_combine($values, $values); -} diff --git a/core/modules/options/tests/options_test/src/OptionsAllowedValues.php b/core/modules/options/tests/options_test/src/OptionsAllowedValues.php new file mode 100644 index 000000000000..c3a17fd2a5ad --- /dev/null +++ b/core/modules/options/tests/options_test/src/OptionsAllowedValues.php @@ -0,0 +1,60 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\options_test; + +use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Field\FieldStorageDefinitionInterface; + +/** + * Provide allowed values callback. + */ +class OptionsAllowedValues { + + /** + * Implements callback_allowed_values_function(). + * + * @see options_allowed_values() + */ + public static function simpleValues(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL): array { + return [ + 'Group 1' => [ + 0 => 'Zero', + ], + 1 => 'One', + 'Group 2' => [ + 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>', + ], + 'More <script>dangerous</script> markup' => [ + 3 => 'Three', + ], + ]; + } + + /** + * Implements callback_allowed_values_function(). + * + * @todo This function violates the recommendation in options_allowed_values() + * to return a list of all possible values in any context when $items is + * NULL. Since this is not yet used for testing Views integration, that is + * alright for now. Fix this in https://www.drupal.org/node/2012130. + * + * @see options_allowed_values() + */ + public static function dynamicValues(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL, &$cacheable = NULL): array { + $values = []; + if (isset($entity)) { + $cacheable = FALSE; + $values = [ + $entity->label(), + $entity->toUrl()->toString(), + $entity->uuid(), + $entity->bundle(), + ]; + } + // We need the values of the entity as keys. + return array_combine($values, $values); + } + +} diff --git a/core/modules/options/tests/src/Functional/OptionsDynamicValuesTestBase.php b/core/modules/options/tests/src/Functional/OptionsDynamicValuesTestBase.php index e74ff9aa13a7..c8d21bdd423a 100644 --- a/core/modules/options/tests/src/Functional/OptionsDynamicValuesTestBase.php +++ b/core/modules/options/tests/src/Functional/OptionsDynamicValuesTestBase.php @@ -58,7 +58,7 @@ protected function setUp(): void { 'type' => 'list_string', 'cardinality' => 1, 'settings' => [ - 'allowed_values_function' => 'options_test_dynamic_values_callback', + 'allowed_values_function' => '\Drupal\options_test\OptionsAllowedValues::dynamicValues', ], ]); $this->fieldStorage->save(); @@ -77,7 +77,7 @@ protected function setUp(): void { ->save(); // Create an entity and prepare test data that will be used by - // options_test_dynamic_values_callback(). + // \Drupal\options_test\OptionsAllowedValues::dynamicValues(). $values = [ 'user_id' => mt_rand(1, 10), 'name' => $this->randomMachineName(), diff --git a/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php b/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php index c3b38e0ad943..99543ffe90cf 100644 --- a/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php +++ b/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php @@ -344,7 +344,7 @@ public function testSelectListSingle(): void { // Test optgroups. $this->card1->setSetting('allowed_values', []); - $this->card1->setSetting('allowed_values_function', 'options_test_allowed_values_callback'); + $this->card1->setSetting('allowed_values_function', '\Drupal\options_test\OptionsAllowedValues::simpleValues'); $this->card1->save(); // Display form: with no field data, nothing is selected @@ -505,7 +505,7 @@ public function testSelectListMultiple(): void { // Use a callback function defining optgroups. $this->card2->setSetting('allowed_values', []); - $this->card2->setSetting('allowed_values_function', 'options_test_allowed_values_callback'); + $this->card2->setSetting('allowed_values_function', '\Drupal\options_test\OptionsAllowedValues::simpleValues'); $this->card2->save(); $field->setRequired(FALSE); $field->save(); diff --git a/core/modules/options/tests/src/Kernel/OptionsDynamicValuesApiTest.php b/core/modules/options/tests/src/Kernel/OptionsDynamicValuesApiTest.php index 483381e719da..f0d94b6e7193 100644 --- a/core/modules/options/tests/src/Kernel/OptionsDynamicValuesApiTest.php +++ b/core/modules/options/tests/src/Kernel/OptionsDynamicValuesApiTest.php @@ -46,7 +46,7 @@ protected function setUp(): void { 'type' => 'list_string', 'cardinality' => 1, 'settings' => [ - 'allowed_values_function' => 'options_test_dynamic_values_callback', + 'allowed_values_function' => '\Drupal\options_test\OptionsAllowedValues::dynamicValues', ], ]); $this->fieldStorage->save(); @@ -65,7 +65,7 @@ protected function setUp(): void { ->save(); // Create an entity and prepare test data that will be used by - // options_test_dynamic_values_callback(). + // \Drupal\options_test\OptionsAllowedValues::dynamicValues(). $values = [ 'user_id' => 2, 'name' => $this->randomMachineName(), @@ -77,7 +77,7 @@ protected function setUp(): void { /** * Tests options_allowed_values(). * - * @see options_test_dynamic_values_callback() + * @see \Drupal\options_test\OptionsAllowedValues::dynamicValues() */ public function testOptionsAllowedValues(): void { // Test allowed values without passed $items. diff --git a/core/modules/options/tests/src/Kernel/OptionsDynamicValuesValidationTest.php b/core/modules/options/tests/src/Kernel/OptionsDynamicValuesValidationTest.php index 4922ed1193d0..77b2003796c9 100644 --- a/core/modules/options/tests/src/Kernel/OptionsDynamicValuesValidationTest.php +++ b/core/modules/options/tests/src/Kernel/OptionsDynamicValuesValidationTest.php @@ -51,7 +51,7 @@ protected function setUp(): void { 'type' => 'list_string', 'cardinality' => 1, 'settings' => [ - 'allowed_values_function' => 'options_test_dynamic_values_callback', + 'allowed_values_function' => '\Drupal\options_test\OptionsAllowedValues::dynamicValues', ], ])->save(); @@ -69,7 +69,7 @@ protected function setUp(): void { ->save(); // Create an entity and prepare test data that will be used by - // options_test_dynamic_values_callback(). + // \Drupal\options_test\OptionsAllowedValues::dynamicValues(). $values = [ 'user_id' => 2, 'name' => $this->randomMachineName(), diff --git a/core/modules/options/tests/src/Kernel/Views/ViewsDataTest.php b/core/modules/options/tests/src/Kernel/Views/ViewsDataTest.php index f1eb578fe33d..20dcb7a4f8ef 100644 --- a/core/modules/options/tests/src/Kernel/Views/ViewsDataTest.php +++ b/core/modules/options/tests/src/Kernel/Views/ViewsDataTest.php @@ -50,7 +50,7 @@ protected function setUp($import_test_views = TRUE): void { 'type' => 'list_string', 'cardinality' => 1, 'settings' => [ - 'allowed_values_function' => 'options_test_dynamic_values_callback', + 'allowed_values_function' => '\Drupal\options_test\OptionsAllowedValues::dynamicValues', ], ]); $this->fieldStorage->save(); -- GitLab