diff --git a/config/schema/structure_sync.schema.yml b/config/schema/structure_sync.schema.yml index 6df50623e88ec0dc005029441b1f63555caefb6d..ab2f5f1f1c08cf203256779979e524ff701bd27a 100644 --- a/config/schema/structure_sync.schema.yml +++ b/config/schema/structure_sync.schema.yml @@ -48,6 +48,13 @@ structure_sync_menu_item: type: string uuid: type: string + options: + type: sequence + label: 'Link options' + # Ignore the type of the items in the sequence, since we're not sure what + # they could be: integer, mapping, sequence, string, etc... + sequence: + type: ignore # Define a data type for taxonomies captured by structure_sync. structure_sync_taxonomy: diff --git a/src/Controller/MenuLinksController.php b/src/Controller/MenuLinksController.php index ca47075f35b8316cb83a24be249acee261f8bf02..012b53156658db9ddc340a8167aee80eee0fb9d9 100755 --- a/src/Controller/MenuLinksController.php +++ b/src/Controller/MenuLinksController.php @@ -104,6 +104,7 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta 'weight' => $menuLink->weight->value, 'langcode' => $menuLink->langcode->value, 'uuid' => $menuLink->uuid(), + 'options' => $menuLink->link->options, ]; StructureSyncHelper::logMessage('Exported "' . $menuLink->title->value . '" of menu "' . $menuLink->menu_name->value . '"'); @@ -378,6 +379,7 @@ public static function importMenuLinksFull($menus, &$context) { 'link' => [ 'uri' => $menuLink['uri'], 'title' => $menuLink['link_title'], + 'options' => $menuLink['options'], ], 'menu_name' => $menuLink['menu_name'], 'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE), @@ -399,6 +401,7 @@ public static function importMenuLinksFull($menus, &$context) { ->set('link', [ 'uri' => $menuLink['uri'], 'title' => $menuLink['link_title'], + 'options' => $menuLink['options'], ]) ->set('expanded', in_array($menuLink['expanded'], ['1', TRUE], TRUE)) ->set('enabled', in_array($menuLink['enabled'], ['1', TRUE], TRUE)) @@ -461,6 +464,7 @@ public static function importMenuLinksSafe($menus, &$context) { 'link' => [ 'uri' => $menuLink['uri'], 'title' => $menuLink['link_title'], + 'options' => $menuLink['options'], ], 'menu_name' => $menuLink['menu_name'], 'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE), @@ -503,6 +507,7 @@ public static function importMenuLinksForce($menus, &$context) { 'link' => [ 'uri' => $menuLink['uri'], 'title' => $menuLink['link_title'], + 'options' => $menuLink['options'], ], 'menu_name' => $menuLink['menu_name'], 'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE), diff --git a/tests/src/FunctionalJavascript/StructureSyncMenuLinksTest.php b/tests/src/FunctionalJavascript/StructureSyncMenuLinksTest.php index 9578bfdf8efab23c65624d6c47ea876899a8c8fb..060dd7bcf0eeadce850ef7fc79f9d1dddefdc1bf 100644 --- a/tests/src/FunctionalJavascript/StructureSyncMenuLinksTest.php +++ b/tests/src/FunctionalJavascript/StructureSyncMenuLinksTest.php @@ -57,6 +57,7 @@ protected function setUp(): void { public function testMenuLinksExportImportUsingAdmin(): void { $this->drupalGet('admin/structure/structure-sync/menu-links'); + // Test export of all the link items of all menus except 'Arc'. $page = $this->getSession()->getPage(); $page->uncheckField('Arc'); $this->click('#edit-export-menu-links'); @@ -69,15 +70,15 @@ public function testMenuLinksExportImportUsingAdmin(): void { $this->assertCount(5, $menus); - // Editing the "Oxygen" menu link. - $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); - reset($oxygenMenuLinks)->set('description', 'Oxygen')->save(); - - // Deleting the "Tig gas" menu link. - $tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); - $this->menuLinkStorage->delete($tigGasMenuLinkBefore); - $this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4'])); + // Update the link's 'class' and 'target' attributes of the tested links. + $updated_options = [ + 'attributes' => [ + 'class' => ['testing-class3 testing-class4'], + 'target' => '_blank', + ], + ]; + /* Test 'Safe' import. */ // Adding an "Electrode" menu link. MenuLinkContent::create([ 'title' => 'Electrode', @@ -86,47 +87,168 @@ public function testMenuLinksExportImportUsingAdmin(): void { 'uuid' => '9a49342a-45e0-4d15-a251-83d704371c79', ])->save(); + // Deleting the "Tig gas" menu link. + $tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); + $this->menuLinkStorage->delete($tigGasMenuLinkBefore); + $this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4'])); + + // Editing the "Oxygen" menu link of the 'tig' menu. + $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); + // Update the description of the "Oxygen" menu link. + /** @var array $oxygenMenuLinks */ + $oxygenMenuLink = reset($oxygenMenuLinks)->set('description', 'Oxygen'); + // The link item has to be set separately. + $oxygenMenuLink->link->get(0)->set('options', $updated_options); + // Save the "Oxygen" menu link after it was updated. + $oxygenMenuLink->save(); + + // Editing the "Wire rod" menu link of the 'mig' menu. + $wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']); + // Update the description of the "Wire rod" menu link. + /** @var array $wirerodMenuLinks */ + $wirerodMenuLink = reset($wirerodMenuLinks)->set('description', 'Wire rod'); + // The link item has to be set separately. + $wirerodMenuLink->link->get(0)->set('options', $updated_options); + // Save the "Wire rod" menu link after it was updated. + $wirerodMenuLink->save(); + + // Importing menus using 'safe' mode. $this->click('#edit-import-menus-safe'); // Wait for the import batch to complete so the buttons are visible again. $this->assertSession()->waitForElementVisible('css', '#edit-import-menus-full'); - // Deleted menu item should be imported. - $tigGasMenuLink = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); - $this->assertNotEmpty($tigGasMenuLink); + // "Electrode" menu link should not be deleted by a 'safe' import. + $electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']); + $this->assertNotEmpty($electrodeMenuLinks); - // "MIG" and "Arc" should not be deleted during the process. - $this->assertNotEmpty($this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779'])); - $this->assertNotEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7'])); + // Deleted "Tig gas" menu item should be imported. + $tigGasMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); + $this->assertNotEmpty($tigGasMenuLinks); + $tigGasMenuLink = reset($tigGasMenuLinks); + // Check whether exported menu link's options were imported correctly. + $this->assertEquals('tig-argon-gas-class1 tig-argon-gas-class2', $tigGasMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals('_blank', $tigGasMenuLink->link->options['attributes']['target']); - // "Oxygen" menu link should not be updated by a safe import. + // "Oxygen" menu link should not be updated by a 'safe' import. $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); - $this->assertEquals('Oxygen', reset($oxygenMenuLinks)->description->value); + $oxygenMenuLink = reset($oxygenMenuLinks); + $this->assertEquals('Oxygen', $oxygenMenuLink->description->value); + // Ensure the "Oxygen" menu link still has the updated options values. + $this->assertEquals($updated_options['attributes']['class'][0], $oxygenMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals($updated_options['attributes']['target'], $oxygenMenuLink->link->options['attributes']['target']); + + // "Wire rod" menu link should not be updated by a 'safe' import. + $wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']); + $this->assertNotEmpty($wirerodMenuLinks); + $wirerodMenuLink = reset($wirerodMenuLinks); + $this->assertEquals('Wire rod', $wirerodMenuLink->description->value); + // Ensure the "Wire rod" menu link still has the updated options values. + $this->assertEquals($updated_options['attributes']['class'][0], $wirerodMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals($updated_options['attributes']['target'], $wirerodMenuLink->link->options['attributes']['target']); + + // "Arc" should not be deleted during a 'safe' import. + $this->assertNotEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7'])); - // "Electrode" menu link should not be deleted by a safe import. - $electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']); - $this->assertNotEmpty($electrodeMenuLinks); + /* Test 'Full' import. */ + // Deleting the "Tig gas" menu link to prepare next import ('full'). + $tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); + $this->menuLinkStorage->delete($tigGasMenuLinkBefore); + $this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4'])); - // Importing menus using full mode. + // Importing menus using 'full' mode. $this->click('#edit-import-menus-full'); // Wait for the import batch to complete so the buttons are visible again. $this->assertSession()->waitForElementVisible('css', '#edit-import-menus-force'); - // "Electrode" menu link should be deleted by full import. + // "Electrode" menu link should be deleted by 'full' import. $electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']); $this->assertEmpty($electrodeMenuLinks); // "Tig gas" should be correctly imported. - $tigGasMenuLink = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); - $this->assertNotEmpty($tigGasMenuLink); + $tigGasMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); + $this->assertNotEmpty($tigGasMenuLinks); + $tigGasMenuLink = reset($tigGasMenuLinks); + // Check whether exported menu link's options were imported correctly. + $this->assertEquals('tig-argon-gas-class1 tig-argon-gas-class2', $tigGasMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals('_blank', $tigGasMenuLink->link->options['attributes']['target']); + + // "Oxygen" menu link should not be updated by a 'full' import. + $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); + $oxygenMenuLink = reset($oxygenMenuLinks); + $this->assertEquals('Oxygen', $oxygenMenuLink->description->value); + // Ensure the "Oxygen" menu link still has the updated options values. + $this->assertEquals($updated_options['attributes']['class'][0], $oxygenMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals($updated_options['attributes']['target'], $oxygenMenuLink->link->options['attributes']['target']); + + // "Wire rod" menu link should not be updated by a 'full' import. + $wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']); + $this->assertNotEmpty($wirerodMenuLinks); + $wirerodMenuLink = reset($wirerodMenuLinks); + $this->assertEquals('Wire rod', $wirerodMenuLink->description->value); + // Ensure the "Wire rod" menu link still has the updated options values. + $this->assertEquals($updated_options['attributes']['class'][0], $wirerodMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals($updated_options['attributes']['target'], $wirerodMenuLink->link->options['attributes']['target']); + + // "Arc" should be deleted during a 'full' import. + $this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7'])); + + /* Test 'Force' import. */ + // Adding an "Electrode" menu link back again for the 'force' tests. + MenuLinkContent::create([ + 'title' => 'Electrode', + 'link' => 'https://en.wikipedia.org/wiki/Electrode', + 'menu_name' => 'arc', + 'uuid' => '9a49342a-45e0-4d15-a251-83d704371c79', + ])->save(); + + // Deleting the "Tig gas" menu link to prepare next import ('force'). + $tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); + $this->menuLinkStorage->delete($tigGasMenuLinkBefore); + $this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4'])); + + // Adding an "Arc" menu link back again for the 'force' tests. + MenuLinkContent::create([ + 'title' => 'SMAW', + 'link' => 'https://en.wikipedia.org/wiki/Shielded_metal_arc_welding', + 'menu_name' => 'arc', + 'uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7', + ])->save(); - // Importing menus using full mode. + // Importing menus using 'force' mode. $this->click('#edit-import-menus-force'); // Wait for the import batch to complete so the buttons are visible again. $this->assertSession()->waitForElementVisible('css', '#edit-import-menus-force'); - // "Oxygen" menu link should be updated by a force import. + // "Electrode" menu link should be deleted by 'force' import. + $electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']); + $this->assertEmpty($electrodeMenuLinks); + + // "Tig gas" should be correctly imported. + $tigGasMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); + $this->assertNotEmpty($tigGasMenuLinks); + $tigGasMenuLink = reset($tigGasMenuLinks); + // Check whether exported menu link's options were imported correctly. + $this->assertEquals('tig-argon-gas-class1 tig-argon-gas-class2', $tigGasMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals('_blank', $tigGasMenuLink->link->options['attributes']['target']); + + // "Oxygen" menu link should be updated by a 'force' import. $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); - $this->assertEquals('Oxygen gas', reset($oxygenMenuLinks)->description->value); + $oxygenMenuLink = reset($oxygenMenuLinks); + $this->assertEquals('Oxygen gas', $oxygenMenuLink->description->value); + // Ensure the "Oxygen" menu link options were reset to their initial values. + $this->assertEquals('tig-oxygen-gas-class1 tig-oxygen-gas-class2', $oxygenMenuLink->link->options['attributes']['class'][0]); + $this->assertEquals('_self', $oxygenMenuLink->link->options['attributes']['target']); + + // "Wire rod" menu link should be updated by a 'force' import. + $wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']); + $this->assertNotEmpty($wirerodMenuLinks); + $wirerodMenuLink = reset($wirerodMenuLinks); + $this->assertEquals('Machine-fed wire rod', $wirerodMenuLink->description->value); + // Ensure "Wire rod" menu link options were reset to their initial values. + $this->assertEmpty($wirerodMenuLink->link->options); + + // "Arc" should be deleted during a 'force' import. + $this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7'])); } } diff --git a/tests/src/Traits/StructureSyncMenuLinksSetUpTrait.php b/tests/src/Traits/StructureSyncMenuLinksSetUpTrait.php index 745adf73d80d8dc9c9b847510bb0721ce4772bec..99d6462deac3cbe5f74fd6690fcd091d034061f3 100644 --- a/tests/src/Traits/StructureSyncMenuLinksSetUpTrait.php +++ b/tests/src/Traits/StructureSyncMenuLinksSetUpTrait.php @@ -73,7 +73,16 @@ public function createSetUpContent(): void { MenuLinkContent::create([ 'title' => 'Argon', 'description' => 'Argon gas', - 'link' => 'https://en.wikipedia.org/wiki/Argon', + 'link' => [ + 'uri' => 'https://en.wikipedia.org/wiki/Argon', + // Set the menu link 'class' and 'target' attributes. + 'options' => [ + 'attributes' => [ + 'class' => ['tig-argon-gas-class1 tig-argon-gas-class2'], + 'target' => '_blank', + ], + ], + ], 'weight' => 1, 'menu_name' => 'tig', 'uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4', @@ -85,7 +94,16 @@ public function createSetUpContent(): void { MenuLinkContent::create([ 'title' => 'Oxygen', 'description' => 'Oxygen gas', - 'link' => 'https://en.wikipedia.org/wiki/Oxygen', + 'link' => [ + 'uri' => 'https://en.wikipedia.org/wiki/Oxygen', + // Set the menu link 'class' and 'target' attributes. + 'options' => [ + 'attributes' => [ + 'class' => ['tig-oxygen-gas-class1 tig-oxygen-gas-class2'], + 'target' => '_self', + ], + ], + ], 'weight' => 2, 'menu_name' => 'tig', 'uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac',