From 5d7528b1294e99f41f29bdc7c5b0fa9e3a9e2dfa Mon Sep 17 00:00:00 2001
From: Alec Smrekar <alec.smrekar@gmail.com>
Date: Tue, 13 Dec 2022 17:39:20 +0100
Subject: [PATCH] Add tests

---
 src/RemoteMenuLink.php                |   1 +
 src/RemoteMenuParser.php              |   2 +-
 tests/src/Unit/ParserTest.php         | 156 +++++++++++++++++++++++++
 tests/src/Unit/fixtures/example1.json | 159 ++++++++++++++++++++++++++
 4 files changed, 317 insertions(+), 1 deletion(-)
 create mode 100644 tests/src/Unit/ParserTest.php
 create mode 100644 tests/src/Unit/fixtures/example1.json

diff --git a/src/RemoteMenuLink.php b/src/RemoteMenuLink.php
index f37fd3f..6d92976 100644
--- a/src/RemoteMenuLink.php
+++ b/src/RemoteMenuLink.php
@@ -35,6 +35,7 @@ class RemoteMenuLink extends MenuLinkBase {
         'menu_name' => 'remote',
         'parent' => '',
         'url' => 'https://cancer.ddev.site' . $link,
+        'options' => [],
         'title' => $title,
         'class' => MenuLinkDefault::class,
         'provider' => 'remote_menu_link',
diff --git a/src/RemoteMenuParser.php b/src/RemoteMenuParser.php
index f5aaa4b..b064b19 100644
--- a/src/RemoteMenuParser.php
+++ b/src/RemoteMenuParser.php
@@ -136,7 +136,7 @@ class RemoteMenuParser {
    * @return array
    *   The adjusted tree.
    */
-  public static function insertLink(string $location, string $link, string $title, array $data): array {
+  protected static function insertLink(string $location, string $link, string $title, array $data): array {
     $pointer = &$data;
     $fragments = array_filter(explode('.', $location));
     // Traverse the tree.
diff --git a/tests/src/Unit/ParserTest.php b/tests/src/Unit/ParserTest.php
new file mode 100644
index 0000000..7367f1a
--- /dev/null
+++ b/tests/src/Unit/ParserTest.php
@@ -0,0 +1,156 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\Tests\menu_parser_php\Unit;
+
+use Drupal\Core\Menu\MenuLinkTreeElement;
+use Drupal\menu_parser_php\RemoteMenuParser;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Unit test class base.
+ *
+ * @coversDefaultClass \Drupal\menu_parser_php\RemoteMenuParser
+ */
+class ParserTest extends UnitTestCase {
+
+  /**
+   * @covers \Drupal\menu_parser_php\RemoteMenuParser::insertLink
+   */
+  public function testInsertLinks(): void {
+    $call_method = static function ($location, $link, $title, $data): array {
+      $class = new \ReflectionClass(RemoteMenuParser::class);
+      $method = $class->getMethod('insertLink');
+      $method->setAccessible(TRUE);
+      return $method->invokeArgs(NULL, [$location, $link, $title, $data]);
+    };
+
+    $data = [];
+
+    // Insert and check a top level item.
+    $data = $call_method('.000', '/test0', 'Title .000', $data);
+    static::assertArrayHasKey('000', $data);
+    $expected = [
+      'link' => '/test0',
+      'title' => 'Title .000',
+      'location' => '.000',
+    ];
+    static::assertEqualsCanonicalizing($expected, $data['000']);
+
+    // Insert and check another top level item.
+    $data = $call_method('.001', '/test1', 'Title .001', $data);
+    static::assertArrayHasKey('001', $data);
+    $expected = [
+      'link' => '/test1',
+      'title' => 'Title .001',
+      'location' => '.001',
+    ];
+    static::assertEqualsCanonicalizing($expected, $data['001']);
+
+    // Insert and check a second level item.
+    $data = $call_method('.002.000', '/test2', 'Title .002.000', $data);
+    static::assertArrayHasKey('002', $data);
+    static::assertArrayHasKey('subtree', $data['002']);
+    static::assertArrayHasKey('000', $data['002']['subtree']);
+    $expected = [
+      'link' => '/test2',
+      'title' => 'Title .002.000',
+      'location' => '.002.000',
+    ];
+    static::assertEqualsCanonicalizing($expected, $data['002']['subtree']['000']);
+
+    // Insert and check another top level item.
+    $data = $call_method('.002', '/test3', 'Title .002', $data);
+    $expected = [
+      'link' => '/test3',
+      'title' => 'Title .002',
+      'location' => '.002',
+      'subtree' => [
+        '000' => $expected,
+      ],
+    ];
+    static::assertEqualsCanonicalizing($expected, $data['002']);
+
+    // Insert and check a second level item.
+    $data = $call_method('.000.000', '/test4', 'Title .000.000', $data);
+    static::assertArrayHasKey('subtree', $data['000']);
+    static::assertArrayHasKey('000', $data['000']['subtree']);
+    $expected = [
+      'link' => '/test0',
+      'title' => 'Title .000',
+      'location' => '.000',
+      'subtree' => [
+        '000' => [
+          'link' => '/test4',
+          'title' => 'Title .000.000',
+          'location' => '.000.000',
+        ],
+      ],
+    ];
+    static::assertEqualsCanonicalizing($expected, $data['000']);
+  }
+
+  /**
+   * @covers \Drupal\menu_parser_php\RemoteMenuParser::convertToObjects
+   */
+  public static function testObjectCreation(): void {
+    // Create some sample output as ::insertLink would return it.
+    $data = [
+      '000' => [
+        'link' => '/test0',
+        'title' => 'Title .000',
+        'location' => '.000',
+      ],
+      '001' => [
+        'link' => '/test1',
+        'title' => 'Title .001',
+        'location' => '.001',
+        'subtree' => [
+          '000' => [
+            'link' => '/test0',
+            'title' => 'Title .001.000',
+            'location' => '.001.000',
+          ],
+          '001' => [
+            'link' => '/test2',
+            'title' => 'Title .001.001',
+            'location' => '.001.001',
+            'subtree' => [
+              '000' => [
+                'link' => '/test3',
+                'title' => 'Title .001.001.000',
+                'location' => '.001.001.000',
+              ],
+            ],
+          ],
+        ],
+      ],
+    ];
+    // Call ::convertToObjects
+    $class = new \ReflectionClass(RemoteMenuParser::class);
+    $method = $class->getMethod('convertToObjects');
+    $method->setAccessible(TRUE);
+    $method->invokeArgs(NULL, [&$data]);
+
+    // Check the top level items.
+    static::assertArrayHasKey('000', $data);
+    static::assertArrayHasKey('001', $data);
+    static::assertCount(2, $data);
+
+    // Check second level children.
+    static::assertEquals(1, $data['001']->depth);
+    static::assertTrue($data['001']->hasChildren);
+    static::assertArrayHasKey('000', $data['001']->subtree);
+    static::assertArrayHasKey('001', $data['001']->subtree);
+
+    // Check third level item.
+    static::assertArrayHasKey('000',  $data['001']->subtree['001']->subtree);
+    $deepest_item = $data['001']->subtree['001']->subtree['000'];
+    static::assertInstanceOf(MenuLinkTreeElement::class, $deepest_item);
+    static::assertEquals(3, $deepest_item->depth);
+    $url = $deepest_item->link->getUrlObject()->toUriString();
+    static::assertStringEndsWith('/test3', $url);
+  }
+
+}
diff --git a/tests/src/Unit/fixtures/example1.json b/tests/src/Unit/fixtures/example1.json
new file mode 100644
index 0000000..fd19407
--- /dev/null
+++ b/tests/src/Unit/fixtures/example1.json
@@ -0,0 +1,159 @@
+{
+  "linkset": [
+    {
+      "anchor": "\/system\/menu\/menu-id\/linkset",
+      "item": [
+        {
+          "href": "\/top-0",
+          "title": "Title .000",
+          "drupal-menu-hierarchy": [
+            ".000"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-0",
+          "title": "Title .000.000",
+          "drupal-menu-hierarchy": [
+            ".000.000"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-0\/bottom-0",
+          "title": "Title .000.000.000",
+          "drupal-menu-hierarchy": [
+            ".000.000.000"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-0\/bottom-1",
+          "title": "Title .000.000.001",
+          "drupal-menu-hierarchy": [
+            ".000.000.001"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-1",
+          "title": "Brain, Spine \u0026 Central Nervous System Cancers",
+          "drupal-menu-hierarchy": [
+            ".000.001"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-1\/bottom-0",
+          "title": "Title .000.001.000",
+          "drupal-menu-hierarchy": [
+            ".000.001.000"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-1\/bottom-1",
+          "title": "Title .000.001.001",
+          "drupal-menu-hierarchy": [
+            ".000.001.001"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-0\/mid-2",
+          "title": "Title .000.002",
+          "drupal-menu-hierarchy": [
+            ".000.002"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-1",
+          "title": "Title .001",
+          "drupal-menu-hierarchy": [
+            ".001"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-1\/mid-0",
+          "title": "Title .001.000",
+          "drupal-menu-hierarchy": [
+            ".001.000"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-1\/mid-1",
+          "title": "Title .001.001",
+          "drupal-menu-hierarchy": [
+            ".001.001"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-1\/mid-1\/bottom-0",
+          "title": "Title .001.001.000",
+          "drupal-menu-hierarchy": [
+            ".001.001.000"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-1\/mid-1\/bottom-1",
+          "title": "Title .001.001.001",
+          "drupal-menu-hierarchy": [
+            ".001.001.001"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-1\/mid-2",
+          "title": "Title .001.002",
+          "drupal-menu-hierarchy": [
+            ".001.002"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        },
+        {
+          "href": "\/top-2",
+          "title": "Title .002",
+          "drupal-menu-hierarchy": [
+            ".002"
+          ],
+          "drupal-menu-machine-name": [
+            "menu-id"
+          ]
+        }
+      ]
+    }
+  ]
+}
-- 
GitLab