home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (admin_menu.test)

This example Drupal source code file (admin_menu.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

access, admin-menu, administration, array, file, found, function, links, menu, permissions, php, title, url

The admin_menu.test Drupal example source code

<?php
// $Id: admin_menu.test,v 1.15 2010/02/20 02:17:55 sun Exp $

/**
 * @file
 * Administration menu functionality tests.
 */

/**
 * Base class for all administration menu web test cases.
 */
class AdminMenuWebTestCase extends DrupalWebTestCase {
  function setUp() {
    // Enable admin menu module and any other modules.
    $args = func_get_args();
    call_user_func_array(array($this, 'parent::setUp'), array_merge(array('admin_menu'), $args));

    // Disable client-side caching.
    variable_set('admin_menu_cache_client', FALSE);
  }

  /**
   * Check that an element exists in HTML markup.
   *
   * @param $xpath
   *   An XPath expression.
   * @param $message
   *   The message to display along with the assertion.
   * @param $group
   *   The type of assertion - examples are "Browser", "PHP".
   * @return
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertElementByXPath($xpath, $message, $group = 'Other') {
    return $this->assertTrue($this->xpath($xpath), $message, $group);
  }

  /**
   * Check that an element does not exist in HTML markup.
   *
   * @param $xpath
   *   An XPath expression.
   * @param $message
   *   The message to display along with the assertion.
   * @param $group
   *   The type of assertion - examples are "Browser", "PHP".
   * @return
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertNoElementByXPath($xpath, $message, $group = 'Other') {
    return $this->assertFalse($this->xpath($xpath), $message, $group);
  }
}

/**
 * Test menu links depending on user permissions.
 */
class AdminMenuPermissionsTestCase extends AdminMenuWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Menu link permissions',
      'description' => 'Verify that menu is displayed according to user permissions.',
      'group' => 'Administration menu',
    );
  }

  function setUp() {
    // Additionally enable contact module.
    parent::setUp('contact');
  }

  /**
   * Test that the links are added to the page (no JS testing).
   */
  function testPermissions() {
    // Anonymous users should not see the menu.
    $this->drupalGet('node');
    $this->assertNoElementByXPath('//div[@id="admin-menu"]', t('Admin menu not displayed to anonymous.'));

    // Create a new user who can access administration menu, but without the
    // permission 'display drupal links'.
    $permissions = array('administer site configuration', 'access administration pages', 'access content overview', 'bypass node access', 'access administration menu');
    // @see http://drupal.org/node/719964
    if (module_exists('dashboard')) {
      $permissions[] = 'access dashboard';
    }
    $admin_user = $this->drupalCreateUser($permissions);
    $this->drupalLogin($admin_user);

    // Check that the user can see the admin links, but not the drupal links.
    $this->assertElementByXPath('//div[@id="admin-menu"]', t('Administration menu is displayed.'));
    $this->drupalGet('node');
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, "/admin/content/node")]', t('Administer content link found.'));
    $this->assertNoElementByXPath('//div[@id="admin-menu"]//a[@href="http://drupal.org"]', t('Drupal links not found.'));
    $this->assertNoElementByXPath('//div[@id="admin-menu"]//a[contains(@href, "/admin/structure/contact")]', t('Contact module link not found.'));

    // Create a new user with the permission 'display drupal links'.
    $permissions = array('administer site configuration', 'access administration pages', 'access content overview', 'access administration menu', 'display drupal links', 'administer contact forms');
    // @see http://drupal.org/node/719964
    if (module_exists('dashboard')) {
      $permissions[] = 'access dashboard';
    }
    $admin_user2 = $this->drupalCreateUser($permissions);
    $this->drupalLogin($admin_user2);
    $this->drupalGet('node');
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[@href="http://drupal.org"]', t('Drupal links found.'));
    $this->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, "/admin/structure/contact")]', t('Contact module link found.'));
  }
}

/**
 * Test contained links in administration menu.
 */
class AdminMenuLinksTestCase extends AdminMenuWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Menu links',
      'description' => 'Verify that admin menu contains proper links.',
      'group' => 'Administration menu',
    );
  }

  function setUp() {
    parent::setUp();

    // Create and log in a full-blown administrative user.
    $permissions = module_invoke_all('permission');
    $admin_user = $this->drupalCreateUser(array_keys($permissions));
    $this->admin_user = $this->drupalLogin($admin_user);
  }

  /**
   * Test link contents.
   *
   * @todo Disabled due to latest changes in HEAD.
   *   @see http://drupal.org/node/420816
   */
  function DISABLEDtestLinkContents() {
    // Create a content-type with special characters.
    $info = array(
      'type' => 'special',
      'name' => 'Cool & Special',
      'base' => 'node_content',
    );
    $info = (object) node_type_set_defaults($info);
    node_type_save($info);
    drupal_flush_all_caches();

    // Fetch a page.
    $this->drupalGet('node');
    $this->assertElementByXPath('//div[@id="admin-menu"]', t('Administration menu is displayed.'));

    // Verify that proper links are displayed.
    // We are explicitly NOT using t() here, since the purpose is to test our
    // custom link titles and 't' option.
    $links = array(
      '/admin/structure/types/article' => t('Article'),
      '/admin/structure/types/special' => t('Cool & Special'),
    );
    foreach ($links as $url => $title) {
      $this->assertFieldByXPath('//div[@id="admin-menu"]//a[contains(@href, "' . $url . '")]', $title, t('!link-title content-type link found.', array('!link-title' => $title)));
    }
    $links = array(
      '/node/add/article' => t('Article'),
      '/node/add/special' => t('Cool & Special'),
    );
    foreach ($links as $url => $title) {
      $this->assertFieldByXPath('//div[@id="admin-menu"]//a[contains(@href, "' . $url . '")]', $title, t('Create content &raquo; !link-title link found.', array('!link-title' => $title)));
    }
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal admin_menu.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.