diff --git a/src/RemoteMenuLink.php b/src/RemoteMenuLink.php index f37fd3fb04277132c3bc62e3c3b1bd0b3550c6cd..6d92976553de626b68a699e34104647e4a8e43ff 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 f5aaa4be52ec34801eba6f7539483469e290976e..b064b1976cf39548b2918906fc7a54dbb105956d 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 0000000000000000000000000000000000000000..7367f1abae03a8b10f839de37c23852b074c82d8 --- /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 0000000000000000000000000000000000000000..fd19407c1720e9913ef84af439d7d2641c92bed3 --- /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" + ] + } + ] + } + ] +}