From d14562be6bf4fd4b265e56e5fe42586d380b01ee Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Mon, 27 Feb 2023 06:43:46 +1000
Subject: [PATCH] Issue #3185183 by philipnorton42, larowlan, bruno.bicudo,
 longwave, xjm, quietone: Refactor ContextualUnitTest to use a data provider

(cherry picked from commit 950e7ff9b8a5eb43c1a7f1ed8599f3ee88ab5e64)
---
 .../tests/src/Kernel/ContextualUnitTest.php   | 91 +++++++++----------
 1 file changed, 45 insertions(+), 46 deletions(-)

diff --git a/core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php b/core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php
index 9e088aefb70a..dc46bef32a44 100644
--- a/core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php
+++ b/core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php
@@ -5,8 +5,7 @@
 use Drupal\KernelTests\KernelTestBase;
 
 /**
- * Tests all edge cases of converting from #contextual_links to ids and vice
- * versa.
+ * Tests edge cases for converting between contextual links and IDs.
  *
  * @group contextual
  */
@@ -23,14 +22,13 @@ class ContextualUnitTest extends KernelTestBase {
    * Provides testcases for both test functions.
    *
    * Used in testContextualLinksToId() and testContextualIdToLinks().
+   *
+   * @return array[]
+   *   Test cases.
    */
-  public function _contextual_links_id_testcases() {
-    // Test branch conditions:
-    // - one group.
-    // - one dynamic path argument.
-    // - no metadata.
-    $tests[] = [
-      'links' => [
+  public function contextualLinksDataProvider(): array {
+    $tests['one group, one dynamic path argument, no metadata'] = [
+      [
         'node' => [
           'route_parameters' => [
             'node' => '14031991',
@@ -38,33 +36,25 @@ public function _contextual_links_id_testcases() {
           'metadata' => ['langcode' => 'en'],
         ],
       ],
-      'id' => 'node:node=14031991:langcode=en',
+      'node:node=14031991:langcode=en',
     ];
 
-    // Test branch conditions:
-    // - one group.
-    // - multiple dynamic path arguments.
-    // - no metadata.
-    $tests[] = [
-      'links' => [
+    $tests['one group, multiple dynamic path arguments, no metadata'] = [
+      [
         'foo' => [
           'route_parameters' => [
-            'bar',
+            0 => 'bar',
             'key' => 'baz',
-            'qux',
+            1 => 'qux',
           ],
           'metadata' => ['langcode' => 'en'],
         ],
       ],
-      'id' => 'foo:0=bar&key=baz&1=qux:langcode=en',
+      'foo:0=bar&key=baz&1=qux:langcode=en',
     ];
 
-    // Test branch conditions:
-    // - one group.
-    // - one dynamic path argument.
-    // - metadata.
-    $tests[] = [
-      'links' => [
+    $tests['one group, one dynamic path argument, metadata'] = [
+      [
         'views_ui_edit' => [
           'route_parameters' => [
             'view' => 'frontpage',
@@ -76,14 +66,11 @@ public function _contextual_links_id_testcases() {
           ],
         ],
       ],
-      'id' => 'views_ui_edit:view=frontpage:location=page&display=page_1&langcode=en',
+      'views_ui_edit:view=frontpage:location=page&display=page_1&langcode=en',
     ];
 
-    // Test branch conditions:
-    // - multiple groups.
-    // - multiple dynamic path arguments.
-    $tests[] = [
-      'links' => [
+    $tests['multiple groups, multiple dynamic path arguments'] = [
+      [
         'node' => [
           'route_parameters' => [
             'node' => '14031991',
@@ -92,9 +79,9 @@ public function _contextual_links_id_testcases() {
         ],
         'foo' => [
           'route_parameters' => [
-            'bar',
+            0 => 'bar',
             'key' => 'baz',
-            'qux',
+            1 => 'qux',
           ],
           'metadata' => ['langcode' => 'en'],
         ],
@@ -103,30 +90,42 @@ public function _contextual_links_id_testcases() {
           'metadata' => ['langcode' => 'en'],
         ],
       ],
-      'id' => 'node:node=14031991:langcode=en|foo:0=bar&key=baz&1=qux:langcode=en|edge:0=20011988:langcode=en',
+      'node:node=14031991:langcode=en|foo:0=bar&key=baz&1=qux:langcode=en|edge:0=20011988:langcode=en',
     ];
 
     return $tests;
   }
 
   /**
-   * Tests _contextual_links_to_id().
+   * Tests the conversion from contextual links to IDs.
+   *
+   * @param array $links
+   *   The #contextual_links property value array.
+   * @param string $id
+   *   The serialized representation of the passed links.
+   *
+   * @covers ::_contextual_links_to_id
+   *
+   * @dataProvider contextualLinksDataProvider
    */
-  public function testContextualLinksToId() {
-    $tests = $this->_contextual_links_id_testcases();
-    foreach ($tests as $test) {
-      $this->assertSame($test['id'], _contextual_links_to_id($test['links']));
-    }
+  public function testContextualLinksToId(array $links, string $id) {
+    $this->assertSame($id, _contextual_links_to_id($links));
   }
 
   /**
-   * Tests _contextual_id_to_links().
+   * Tests the conversion from contextual ID to links.
+   *
+   * @param array $links
+   *   The #contextual_links property value array.
+   * @param string $id
+   *   The serialized representation of the passed links.
+   *
+   * @covers ::_contextual_id_to_links
+   *
+   * @dataProvider contextualLinksDataProvider
    */
-  public function testContextualIdToLinks() {
-    $tests = $this->_contextual_links_id_testcases();
-    foreach ($tests as $test) {
-      $this->assertSame($test['links'], _contextual_id_to_links($test['id']));
-    }
+  public function testContextualIdToLinks(array $links, string $id) {
+    $this->assertSame($links, _contextual_id_to_links($id));
   }
 
 }
-- 
GitLab