From bbf50c928b64b57bbfc15c22826decc8fc68deee Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Tue, 17 Nov 2020 15:33:35 +0000
Subject: [PATCH] Issue #3182891 by alexpott: The variables_required setting is
 a tricky name

---
 .../src/Plugin/migrate/source/Variable.php    | 39 ++++++++-----------
 .../Plugin/migrate/source/VariableTest.php    | 24 ++++++------
 2 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
index 320fc236376b..e52f74f02955 100644
--- a/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
+++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
@@ -13,9 +13,10 @@
  * Depending on the configuration, this returns zero or a single row and as such
  * is not a good example for any normal source class returning multiple rows.
  *
- * The configuration may contain optional and required variable names. If any of
- * the required variables is missing in the source, then the source will return
- * zero rows.
+ * The configuration may contain two lists of variable names, variables and
+ * variables_no_row_if_missing. If any of the variables listed in
+ * variables_no_row_if_missing is missing in the source, then the source will
+ * return zero rows.
  *
  * With this configuration, the source will return one row even when the
  * "filter_fallback_format" variable isn't available:
@@ -31,12 +32,12 @@
  * @code
  * source:
  *   plugin: variable
- *   variables_required:
+ *   variables_no_row_if_missing:
  *     - filter_fallback_format
  * @endcode
  *
- * The optional and the required variable names are always merged together. All
- * of the following configurations are valid:
+ * The variables and the variables_no_row_if_missing lists are always merged
+ * together. All of the following configurations are valid:
  * @code
  * source:
  *   plugin: variable
@@ -44,7 +45,7 @@
  *     - book_child_type
  *     - book_block_mode
  *     - book_allowed_types
- *   variables_required:
+ *   variables_no_row_if_missing:
  *     - book_child_type
  *     - book_block_mode
  *     - book_allowed_types
@@ -54,12 +55,12 @@
  *   variables:
  *     - book_child_type
  *     - book_block_mode
- *   variables_required:
+ *   variables_no_row_if_missing:
  *     - book_allowed_types
  *
  * source:
  *   plugin: variable
- *   variables_required:
+ *   variables_no_row_if_missing:
  *     - book_child_type
  *     - book_block_mode
  *     - book_allowed_types
@@ -80,28 +81,20 @@ class Variable extends DrupalSqlBase {
   protected $variables;
 
   /**
-   * The optional variables.
+   * The variables that result in no row if any are missing from the source.
    *
    * @var array
    */
-  protected $optionalVariables;
-
-  /**
-   * The required variables.
-   *
-   * @var array
-   */
-  protected $requiredVariables;
+  protected $variablesNoRowIfMissing;
 
   /**
    * {@inheritdoc}
    */
   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager) {
     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
-    $this->requiredVariables = $this->configuration['variables_required'] ?? [];
+    $this->variablesNoRowIfMissing = $this->configuration['variables_no_row_if_missing'] ?? [];
     $variables = $this->configuration['variables'] ?? [];
-    $this->variables = array_unique(array_merge(array_values($variables), array_values($this->requiredVariables)));
-    $this->optionalVariables = array_diff($this->variables, $this->requiredVariables);
+    $this->variables = array_unique(array_merge(array_values($variables), array_values($this->variablesNoRowIfMissing)));
   }
 
   /**
@@ -134,12 +127,12 @@ protected function values() {
    * {@inheritdoc}
    */
   public function count($refresh = FALSE) {
-    if (empty($this->requiredVariables)) {
+    if (empty($this->variablesNoRowIfMissing)) {
       return 1;
     }
     $variable_names = array_keys($this->query()->execute()->fetchAllAssoc('name'));
 
-    if (!empty(array_diff($this->requiredVariables, $variable_names))) {
+    if (!empty(array_diff($this->variablesNoRowIfMissing, $variable_names))) {
       return 0;
     }
     return 1;
diff --git a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/VariableTest.php b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/VariableTest.php
index 3e3623eaa828..ab31ea15a776 100644
--- a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/VariableTest.php
+++ b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/VariableTest.php
@@ -93,8 +93,8 @@ public function providerSource() {
       ],
     ];
 
-    // Test cases with only 'variables_required' configuration.
-    $variables_required_tests = [
+    // Test cases with only 'variables_no_row_if_missing' configuration.
+    $variables_no_row_if_missing_tests = [
       'Two required variables, all of them are available' => [
         'source_data' => $source_data,
         'expected_data' => [
@@ -106,7 +106,7 @@ public function providerSource() {
         ],
         'expected_count' => 1,
         'configuration' => [
-          'variables_required' => [
+          'variables_no_row_if_missing' => [
             'foo',
             'bar',
           ],
@@ -117,7 +117,7 @@ public function providerSource() {
         'expected_data' => [],
         'expected_count' => 0,
         'configuration' => [
-          'variables_required' => [
+          'variables_no_row_if_missing' => [
             'foo',
             'bar0',
           ],
@@ -133,7 +133,7 @@ public function providerSource() {
         ],
         'expected_count' => 1,
         'configuration' => [
-          'variables_required' => [
+          'variables_no_row_if_missing' => [
             'baz',
           ],
         ],
@@ -143,12 +143,12 @@ public function providerSource() {
         'expected_data' => [],
         'expected_count' => 0,
         'configuration' => [
-          'variables_required' => [
+          'variables_no_row_if_missing' => [
             'bar0',
           ],
         ],
       ],
-      // Test cases with both 'variables' and 'variables_required'
+      // Test cases with both 'variables' and 'variables_no_row_if_missing'
       // configuration.
       'One optional and two required variables, all of them are available' => [
         'source_data' => $source_data,
@@ -163,7 +163,7 @@ public function providerSource() {
         'expected_count' => 1,
         'configuration' => [
           'variables' => ['foo'],
-          'variables_required' => ['bar', 'baz'],
+          'variables_no_row_if_missing' => ['bar', 'baz'],
         ],
       ],
       'One optional and two required variables, only one required is available' => [
@@ -172,7 +172,7 @@ public function providerSource() {
         'expected_count' => 0,
         'configuration' => [
           'variables' => ['foo'],
-          'variables_required' => ['bar', 'foobar'],
+          'variables_no_row_if_missing' => ['bar', 'foobar'],
         ],
       ],
       'Two optional and one required and available variable, every optional is missing' => [
@@ -186,7 +186,7 @@ public function providerSource() {
         'expected_count' => 1,
         'configuration' => [
           'variables' => ['qux', 'waldo'],
-          'variables_required' => ['bar'],
+          'variables_no_row_if_missing' => ['bar'],
         ],
       ],
       'Two available optional and a required, but missing variable' => [
@@ -195,14 +195,14 @@ public function providerSource() {
         'expected_count' => 0,
         'configuration' => [
           'variables' => ['baz', 'foo'],
-          'variables_required' => [
+          'variables_no_row_if_missing' => [
             'foo_bar_baz',
           ],
         ],
       ],
     ];
 
-    return $tests + $variables_required_tests;
+    return $tests + $variables_no_row_if_missing_tests;
   }
 
 }
-- 
GitLab