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

Drupal example source code file (page_title.test)

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

array, content, current-page:page-title, forum, function, node, page, php, section, taxonomy, term, test, title, type

The page_title.test Drupal example source code

// $Id: page_title.test,v 2010/11/01 23:18:35 njt1982 Exp $

 * @file
 * Test cases for the Page Title module.

class PageTitleTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Page Title',
      'description' => 'Ensure that Page Title functions correctly',
      'group' => 'Page Title',
      'dependencies' => array('token'),

  public function setUp() {
    parent::setUp('token', 'page_title', 'forum', 'taxonomy');

    // Create an admin user
    $this->admin_user = $this->drupalCreateUser(array(
      'administer page titles',
      'administer taxonomy',
      'access content',
      'create page content',
      'edit any page content',
      'administer forums',
      'create forum content',
      'set page title')

  private function pageTitleCheck($path, $pattern, $tokens = array(), $section = '') {
    // Get the path...
    if (is_array($path)) {
      $this->drupalGet($path['path'], $path['options']);
    else {

    // Apply tokens to pattern
    $title = strtr($pattern, $tokens);

    // Do some verbose output
    $this->pass(t('Pattern: %pat', array('%pat' => '<title>'. $pattern .'</title>')));
    $this->pass(t('Looking for title: %title', array('%title' => '<title>'. $title .'</title>')));

    // Look for the title in the content
    $res = $this->assertRaw($title, t('Correct page title found in the <head> for: %section', array('%section' => $section)), 'Page Title');

    // If the search failed, do some more verbose debugging
    if (!$res) {
      preg_match('/<title>.*<\/title>/', $this->drupalGetContent(), $found_title);
      $this->pass(t('Found instead: %found', array('%found' => $found_title[0])));

  public function testPageTitleTest() {

    // Create a term
    $term = array(
      'name' => 'Test Term Foo',
    $this->drupalPost('admin/structure/taxonomy/tags/add', $term, t('Save'));

    // Define our settings
    $settings = array(
      'page_title_default' => '[current-page:page-title] - DEFAULT TEST',
      'page_title_front' => '[site:name]',
      'page_title_user' => 'Profile For [user]',
      'page_title_type_page' => 'PAGE NODE: [current-page:page-title]',
      'page_title_type_page_showfield' => 1,
      'page_title_type_forum' => 'Forum - [current-page:page-title]',
      'page_title_pager_pattern' => ' - page [current-page:page-number]',
      'page_title_vocab_1' => 'FORUM: [term:name]',
      'page_title_vocab_2' => 'TERM: [current-page:page-title]',
      'page_title_vocab_1_showfield' => 1,
      'page_title_vocab_2_showfield' => 1,
      'page_title_forum_root_title' => 'Welcome to [site:name] [current-page:page-title]',

    // Save the settings
    $this->drupalPost('admin/config/search/page-title', $settings, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('The configuration saved message was found'), 'Page Title');

     * Lets check the frontpage page title is working
    $this->pageTitleCheck('<front>', $settings['page_title_front'], array('[site:name]' => 'Drupal'), 'Frontpage Title');

     * Lets check a "default" page, such a the page title admin form
    $this->pageTitleCheck('admin/config/search/page-title', $settings['page_title_default'], array('[current-page:page-title]' => 'Page titles'), 'Admin Page');

     * Let's create a page node and check that

    //Create a basic page node
    $node = array(
      'type' => 'page',
      'title' => 'Test Page Node',
      'taxonomy' => array(2 => 1), // Set taxonomy for vocab 2 to term 1 See Hack/assumption above

    // Save the node
    $node = $this->drupalCreateNode($node);

    // Pass out a message to confirm the save
    $this->pass(t('Created Node !nid', array('!nid' => $node->nid)), 'Page Title');

    // Load the node page and check for the title in the head
    $this->pageTitleCheck('node/'. $node->nid, $settings['page_title_type_page'], array('[current-page:page-title]' => $node->title), 'Page Node Type');

    // Post a page_title into the node and reload the node
    $edit['page_title'] = 'I am a test Page Title field';
    $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
    $node = node_load($node->nid, NULL, TRUE);

    // Node load the node page and check for the title in the head
    $this->pageTitleCheck('node/'. $node->nid, $settings['page_title_type_page'], array('[current-page:page-title]' => $node->page_title), 'Page Node Type');

     *  TAXONOMY

    // Lets check a taxonomy/term/tid page (should be term from earlier!)
    // Load the term page and check for the title in the head
    $this->pageTitleCheck('taxonomy/term/2', $settings['page_title_vocab_2'], array('[current-page:page-title]' => $term['name']), 'Taxonomy Title');

    // Lets check the pagenation suffix is working but appending it to taxonomy/term/1.
    // This is a little messy - but it works for our purpose
    // Remember, the page value in the URL is zero indexed. This means page=1 in URL is Page 2 on the site
      array('path' => 'taxonomy/term/2', 'options' => array('query' => array('page' => 1))),
      $settings['page_title_vocab_2'] . $settings['page_title_pager_pattern'],
      array('[current-page:page-title]' => $term['name'], '[current-page:page-number]' => 2),
      'Taxonomy Title with pagenation suffix'

     *  FORUMS
    // Updating term 1 - The General Discussion forum
    #$forum_forum = array('page_title' => 'I AM A TEST FORUM');
    #$this->drupalPost('admin/structure/forum/edit/forum/1', $forum_forum, t('Save'));

    //Create a basic forum topic node
    $forum_node = array(
      'nid' => NULL,
      'type' => 'forum',
      'title' => 'Test Forum Node',
      'taxonomy_forums' => array('und' => array(0 => array('tid' => 1))), // Set taxonomy for vocab 1 (forum vocab) to term 1 (the General discussion forum)... See Hack/assumptions above

    // Save the node
    $forum_node = $this->drupalCreateNode($forum_node);

    // Node load the node page and check for the title in the head
    $this->pageTitleCheck('node/'. $forum_node->nid, $settings['page_title_type_forum'], array('[current-page:page-title]' => $forum_node->title), 'Forum Topic');

    //Now test the forum root...
    $this->pageTitleCheck('forum', $settings['page_title_forum_root_title'], array('[current-page:page-title]' => 'Forums', '[site:name]' => 'Drupal'), 'Forum Topic');

    //Now test the forum forum...
    $this->pageTitleCheck('forum/1', $settings['page_title_vocab_1'], array('[term:name]' => 'General discussion'), 'Forum Container');

Other Drupal examples (source code examples)

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