assertMenuLink example

// Add menu links.     $item1 = $this->addMenuLink('', '/node/' . $node1->id()$menu_name, TRUE);
    $item2 = $this->addMenuLink($item1->getPluginId(), '/node/' . $node2->id()$menu_name, FALSE);
    $item3 = $this->addMenuLink($item2->getPluginId(), '/node/' . $node3->id()$menu_name);

    // Hierarchy     // <$menu_name>     // - item1     // -- item2     // --- item3
    $this->assertMenuLink([
      'children' => [$item2->getPluginId()$item3->getPluginId()],
      'parents' => [$item1->getPluginId()],
      // We assert the language code here to make sure that the language       // selection element degrades gracefully without the Language module.       'langcode' => 'en',
    ]$item1->getPluginId());
    $this->assertMenuLink([
      'children' => [$item3->getPluginId()],
      'parents' => [$item2->getPluginId()$item1->getPluginId()],
      // See above.       'langcode' => 'en',
    ],
// Add a menu link.     $link_title = $this->randomString();
    $edit = [
      'title[0][value]' => $link_title,
      'link[0][uri]' => $link_path,
    ];
    $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
    $this->submitForm($edit, 'Save');
    // Check the link was added with the correct menu link default language.     $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => $link_title]);
    $menu_link = reset($menu_links);
    $this->assertMenuLink([
      'menu_name' => $menu_name,
      'route_name' => '<front>',
      'langcode' => 'bb',
    ]$menu_link->getPluginId());

    // Edit menu link default, changing it to cc.     ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
      ->setDefaultLangcode('cc')
      ->setLanguageAlterable(TRUE)
      ->save();

    
// Add menu link.     $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('The menu link has been saved.');

    $storage = $this->container->get('entity_type.manager')->getStorage('menu_link_content');
    $menu_links = $storage->loadByProperties(['title' => $title]);
    $menu_link = reset($menu_links);

    // Check that the stored menu link meeting the expectations.     $this->assertNotNull($menu_link);
    $this->assertMenuLink([
      'menu_name' => $menu_id,
      'children' => [],
      'parent' => $parent,
    ]$menu_link->getPluginId());

    return $menu_link;
  }

}
/** * Tests with a simple linear hierarchy. */
  public function testSimpleHierarchy() {
    // Add some links with parent on the previous one and test some values.     // <tools>     // - test1     // -- test2     // --- test3     $this->addMenuLink('test1', '');
    $this->assertMenuLink('test1', ['has_children' => 0, 'depth' => 1]);

    $this->addMenuLink('test2', 'test1');
    $this->assertMenuLink('test1', ['has_children' => 1, 'depth' => 1][]['test2']);
    $this->assertMenuLink('test2', ['has_children' => 0, 'depth' => 2]['test1']);

    $this->addMenuLink('test3', 'test2');
    $this->assertMenuLink('test1', ['has_children' => 1, 'depth' => 1][]['test2', 'test3']);
    $this->assertMenuLink('test2', ['has_children' => 1, 'depth' => 2]['test1']['test3']);
    $this->assertMenuLink('test3', ['has_children' => 0, 'depth' => 3]['test2', 'test1']);
  }

  
Home | Imprint | This part of the site doesn't use cookies.