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

Drupal example source code file (dashboard.test)

This example Drupal source code file (dashboard.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, array, blocks, comments, configuration, dashboard, disabled, elements, function, has, php, region, the, to

The dashboard.test Drupal example source code

<?php
// $Id: dashboard.test,v 1.8 2010/11/06 23:24:33 webchick Exp $

/**
 * @file
 * Tests for the dashboard module.
 */

class DashboardBlocksTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Dashboard blocks',
      'description' => 'Test blocks as used by the dashboard.',
      'group' => 'Dashboard',
    );
  }

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

    // Create and log in an administrative user having access to the dashboard.
    $admin_user = $this->drupalCreateUser(array('access dashboard', 'administer blocks', 'access administration pages', 'administer modules'));
    $this->drupalLogin($admin_user);

    // Make sure that the dashboard is using the same theme as the rest of the
    // site (and in particular, the same theme used on 403 pages). This forces
    // the dashboard blocks to be the same for an administrator as for a
    // regular user, and therefore lets us test that the dashboard blocks
    // themselves are specifically removed for a user who does not have access
    // to the dashboard page.
    theme_enable(array('stark'));
    variable_set('theme_default', 'stark');
    variable_set('admin_theme', 'stark');
  }

  /**
   * Test adding a block to the dashboard and checking access to it.
   */
  function testDashboardAccess() {
    // Add a new custom block to a dashboard region.
    $custom_block = array();
    $custom_block['info'] = $this->randomName(8);
    $custom_block['title'] = $this->randomName(8);
    $custom_block['body[value]'] = $this->randomName(32);
    $custom_block['regions[stark]'] = 'dashboard_main';
    $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));

    // Ensure admin access.
    $this->drupalGet('admin/dashboard');
    $this->assertResponse(200, t('Admin has access to the dashboard.'));
    $this->assertRaw($custom_block['title'], t('Admin has access to a dashboard block.'));

    // Ensure non-admin access is denied.
    $normal_user = $this->drupalCreateUser();
    $this->drupalLogin($normal_user);
    $this->drupalGet('admin/dashboard');
    $this->assertResponse(403, t('Non-admin has no access to the dashboard.'));
    $this->assertNoText($custom_block['title'], t('Non-admin has no access to a dashboard block.'));
  }

  /**
   * Test that dashboard regions are displayed or hidden properly.
   */
  function testDashboardRegions() {
    $dashboard_regions = dashboard_region_descriptions();

    // Ensure blocks can be placed in dashboard regions.
    $this->drupalGet('admin/dashboard/configure');
    foreach ($dashboard_regions as $region => $description) {
      $elements = $this->xpath('//option[@value=:region]', array(':region' => $region));
      $this->assertTrue(!empty($elements), t('%region is an available choice on the dashboard block configuration page.', array('%region' => $region)));
    }

    // Ensure blocks cannot be placed in dashboard regions on the standard
    // blocks configuration page.
    $this->drupalGet('admin/structure/block');
    foreach ($dashboard_regions as $region => $description) {
      $elements = $this->xpath('//option[@value=:region]', array(':region' => $region));
      $this->assertTrue(empty($elements), t('%region is not an available choice on the block configuration page.', array('%region' => $region)));
    }
  }

  /**
   * Test that the dashboard module can be disabled and enabled again,
   * retaining its blocks.
   */
  function testDisableEnable() {
    // Add a new custom block to a dashboard region.
    $custom_block = array();
    $custom_block['info'] = $this->randomName(8);
    $custom_block['title'] = $this->randomName(8);
    $custom_block['body[value]'] = $this->randomName(32);
    $custom_block['regions[stark]'] = 'dashboard_main';
    $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
    $this->drupalGet('admin/dashboard');
    $this->assertRaw($custom_block['title'], t('Block appears on the dashboard.'));

    $edit = array();
    $edit['modules[Core][dashboard][enable]'] = FALSE;
    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
    $this->assertNoRaw('assigned to the invalid region', t('Dashboard blocks gracefully disabled.'));
    module_list(TRUE);
    $this->assertFalse(module_exists('dashboard'), t('Dashboard disabled.'));

    $edit['modules[Core][dashboard][enable]'] = 'dashboard';
    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
    module_list(TRUE);
    $this->assertTrue(module_exists('dashboard'), t('Dashboard enabled.'));

    $this->drupalGet('admin/dashboard');
    $this->assertRaw($custom_block['title'], t('Block still appears on the dashboard.'));
  }

  /**
   * Test that defining a block with ['properties']['administrative'] = TRUE
   * adds it as an available block for the dashboard.
   */
  function testBlockAvailability() {
    // Test "Recent comments", which should be available (defined as
    // "administrative") but not enabled.
    $this->drupalGet('admin/dashboard');
    $this->assertNoText(t('Recent comments'), t('"Recent comments" not on dashboard.'));
    $this->drupalGet('admin/dashboard/drawer');
    $this->assertText(t('Recent comments'), t('Drawer of disabled blocks includes a block defined as "administrative".'));
    $this->assertNoText(t('Syndicate'), t('Drawer of disabled blocks excludes a block not defined as "administrative".'));
    $this->drupalGet('admin/dashboard/configure');
    $elements = $this->xpath('//select[@id=:id]//option[@selected="selected"]', array(':id' => 'edit-blocks-comment-recent-region'));
    $this->assertTrue($elements[0]['value'] == 'dashboard_inactive', t('A block defined as "administrative" defaults to dashboard_inactive.'));

    // Now enable the block on the dashboard.
    $values = array();
    $values['blocks[comment_recent][region]'] = 'dashboard_main';
    $this->drupalPost('admin/dashboard/configure', $values, t('Save blocks'));
    $this->drupalGet('admin/dashboard');
    $this->assertText(t('Recent comments'), t('"Recent comments" was placed on dashboard.'));
    $this->drupalGet('admin/dashboard/drawer');
    $this->assertNoText(t('Recent comments'), t('Drawer of disabled blocks excludes enabled blocks.'));
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal dashboard.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.