View source  
  <?php
namespace Drupal\admin_menu\Tests;
class AdminMenuCustomizedTest extends AdminMenuTestBase {
  public static $modules = [
    'menu',
  ];
  public static function getInfo() {
    return [
      'name' => 'Customized links',
      'description' => 'Tests customized menu links.',
      'group' => 'Administration menu',
    ];
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalLogin($this->root_user);
  }
  
  function testCustomDisabled() {
    $type = $this
      ->drupalCreateContentType();
    $node = $this
      ->drupalCreateNode([
      'type' => $type->type,
    ]);
    $text = $this
      ->randomName();
    $xpath = $this
      ->buildXPathQuery('//div[@id=:id]//a[contains(text(), :text)]', [
      ':id' => 'admin-menu',
      ':text' => $text,
    ]);
    
    $this
      ->drupalGet('node');
    $elements = $this
      ->xpath($xpath);
    $this
      ->assertFalse($elements, 'Custom link not found.');
    
    $edit = [
      'link_path' => 'node/' . $node->nid,
      'link_title' => $text,
      'parent' => 'admin:' . $this
        ->queryMlidByPath('admin'),
    ];
    $this
      ->drupalPost('admin/structure/menu/manage/admin/add', $edit, t('Save'));
    
    $this
      ->drupalGet('node');
    $elements = $this
      ->xpath($xpath);
    $this
      ->assertTrue($elements, 'Custom link found.');
    
    $edit = [
      'enabled' => FALSE,
    ];
    $this
      ->drupalPost('admin/structure/menu/item/' . $this
      ->queryMlidByPath('node/' . $node->nid) . '/edit', $edit, t('Save'));
    
    $this
      ->drupalGet('node');
    $elements = $this
      ->xpath($xpath);
    $this
      ->assertFalse($elements, 'Disabled custom link not found.');
  }
  
  function testCustomExternal() {
    
    $edit = [
      'link_path' => 'http://example.com',
      'link_title' => 'Example',
      'parent' => 'admin:' . $this
        ->queryMlidByPath('admin'),
    ];
    $this
      ->drupalPost('admin/structure/menu/manage/admin/add', $edit, t('Save'));
    
    $this
      ->drupalGet('');
    $elements = $this
      ->xpath('//div[@id=:id]//a[@href=:href and contains(text(), :text)]', [
      ':id' => 'admin-menu',
      ':href' => $edit['link_path'],
      ':text' => $edit['link_title'],
    ]);
    $this
      ->assertTrue($elements, 'External link found.');
  }
  
  protected function queryMlidByPath($path) {
    return \Drupal::database()
      ->query('SELECT mlid FROM {menu_links} WHERE menu_name = :menu AND link_path = :path', [
      ':menu' => 'admin',
      ':path' => $path,
    ])
      ->fetchField();
  }
}