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

Drupal example source code file (page_example.test)

This example Drupal source code file (page_example.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

argument, arguments, content, first, function, number, page, php, second, str, successfully, the, url, verified

The page_example.test Drupal example source code

<?php

/**
 * @file
 * Test case for Testing the page example module.
 *
 * This file contains the test cases to check if module is performing as
 * expected.
 *
 */
class PageExampleTestCase extends DrupalWebTestCase {
  protected $web_user;

  public static function getInfo() {
    return array(
      'name' => 'Page example functionality',
      'description' => 'Creates page and render the content based on the arguments passed in the URL.',
      'group' => 'Examples',
    );
  }

  /**
   * Enable modules and create user with specific permissions.
   */
  function setUp() {
    parent::setUp('page_example');
  }

  /**
   * Generates a random string of ASCII numeric characters (values 48 to 57).
   *
   * @param $length
   *   Length of random string to generate .
   * @return
   *   Randomly generated string.
   */
  private static function randomNumber($length = 8) {
    $str = '';
    for ($i = 0; $i < $length; $i++) {
      $str .= chr(mt_rand(48, 57));
    }
    return $str;
  }

  /**
   * Verify that current user has no access to page.
   *
   * @param $url
   *   URL to verify.
   */
  function pageExampleVerifyNoAccess($url) {
    // Test that page returns 403 Access Denied
    $this->drupalGet($url);
    $this->assertResponse(403);
  }

  /**
   * Login user, create an example node, and test blog functionality through the admin and user interfaces.
   */
  function testPageExampleBasic() {

    // Verify that anonymous user can't access the pages created by
    // page_example module
    $this->pageExampleVerifyNoAccess('examples/page_example/simple');
    $this->pageExampleVerifyNoAccess('examples/page_example/arguments/1/2');

    // Create a regular user and login.
    $this->web_user = $this->drupalCreateUser();
    $this->drupalLogin($this->web_user);

    // Verify that regular user can't access the pages created by
    // page_example module
    $this->pageExampleVerifyNoAccess('examples/page_example/simple');
    $this->pageExampleVerifyNoAccess('examples/page_example/arguments/1/2');

    // Create a user with permissions to access 'simple' page and login.
    $this->web_user = $this->drupalCreateUser(array('access simple page'));
    $this->drupalLogin($this->web_user);

    // Verify that user can access foo content
    $this->drupalGet('examples/page_example/simple');
    $this->assertResponse(200, t('Simple page content successfully accessed.'));
    $this->assertText(t('The quick brown fox jumps over the lazy dog.'), t('foo content successfully verified.'));

    // Check if user can't access arguments page
    $this->pageExampleVerifyNoAccess('examples/page_example/arguments/1/2');
    
    

    // Create a user with permissions to access 'arguments' page and login.
    $this->web_user = $this->drupalCreateUser(array('access arguments page'));
    $this->drupalLogin($this->web_user);
    
    // Verify that user can access foo content
    $first = $this->randomNumber(3);
    $second = $this->randomNumber(3);
    $this->drupalGet('examples/page_example/arguments/' . $first . '/' . $second);
    $this->assertResponse(200, t('arguments content successfully accessed.'));
    
    // Verify argument usage
    $this->assertRaw(t("First number was @number.", array('@number' => $first)), t('arguments first argument successfully verified.'));
    $this->assertRaw(t("Second number was @number.", array('@number' => $second)), t('arguments second argument successfully verified.'));
    $this->assertRaw(t('The total was @number.', array('@number' => $first + $second)), t('arguments content successfully verified.'));

    // Verify incomplete argument call to arguments content
    $this->drupalGet('examples/page_example/arguments/' . $first . '/');
    $this->assertText("provides two pages");

    // Verify invalid argument call to arguments content
    $this->drupalGet('examples/page_example/arguments/' . $first . '/' . $this->randomString());
    $this->assertResponse(403, t('Invalid argument for arguments content successfully verified'));

    // Verify invalid argument call to arguments content
    $this->drupalGet('examples/page_example/arguments/' . $this->randomString() . '/' . $second);
    $this->assertResponse(403, t('Invalid argument for arguments content successfully verified'));

    // Check if user can't access simple page
    $this->pageExampleVerifyNoAccess('examples/page_example/simple');
  }
}

Other Drupal examples (source code examples)

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