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

Drupal example source code file (xmlsitemap_custom.test)

This example Drupal source code file (xmlsitemap_custom.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, custom, directory, edit, file, for, function, link, links, loc, php, saved, system/files, users

The xmlsitemap_custom.test Drupal example source code

<?php
// $Id: xmlsitemap_custom.test,v 1.1.2.9 2010/11/13 04:37:36 davereid Exp $

/**
 * @file
 * Unit tests for the xmlsitemap_custom module.
 */

class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap custom interface tests',
      'description' => 'Functional tests for the XML sitemap custom module.',
      'group' => 'XML sitemap',
    );
  }

  function setUp($modules = array()) {
    $modules[] = 'xmlsitemap_custom';
    $modules[] = 'path';
    parent::setUp($modules);

    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer xmlsitemap'));
    $this->drupalLogin($this->admin_user);
  }

  function testCustomLinks() {
    // Set a path alias for the node page.
    path_set_alias('system/files', 'public-files');

    $this->drupalGet('admin/settings/xmlsitemap/custom');
    $this->clickLink(t('Add custom link'));

    // Test an invalid path.
    $edit['loc'] = 'invalid-testing-path';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test a path not accessible to anonymous user.
    $edit['loc'] = 'admin/user/user';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test that the current page, which should not give a false positive for
    // $menu_item['access'] since the result has been cached already.
    $edit['loc'] = 'admin/settings/xmlsitemap/custom/add';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Add an aliased path with padded spaces.
    $edit['loc'] = ' public-files ';
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText('The custom link for system/files was saved');
    $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => 'system/files'));
    $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
    $link = reset($links);
    $this->assertSitemapLinkValues('custom', $link['id'], array('priority' => 0.5, 'changefreq' => 0, 'access' => 1, 'status' => 1));

    $this->clickLink('Edit');
    $edit = array(
      'priority' => 0.1,
      'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS,
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertText('The custom link for system/files was saved');
    $this->assertSitemapLinkValues('custom', $link['id'], array('priority' => 0.1, 'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS, 'access' => 1, 'status' => 1));

    $this->clickLink('Delete');
    $this->drupalPost(NULL, array(), t('Delete'));
    $this->assertText('The custom link for system/files has been deleted.');
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => 'system/files'));
  }

  /**
   * Test adding files as custom links.
   */
  function testCustomFileLinks() {
    // Test an invalid file.
    $edit['loc'] = $this->randomName();
    $this->drupalPost('admin/settings/xmlsitemap/custom/add', $edit, t('Save'));
    $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test an unaccessible file .
    //$edit['loc'] = '.htaccess';
    //$this->drupalPost('admin/settings/xmlsitemap/custom/add', $edit, t('Save'));
    //$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
    //$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));

    // Test a valid file.
    $edit['loc'] = 'misc/drupal.js';
    $this->drupalPost('admin/settings/xmlsitemap/custom/add', $edit, t('Save'));
    $this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
    $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
    $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));

    // Test a valid folder.
    $edit['loc'] = 'misc';
    $this->drupalPost('admin/settings/xmlsitemap/custom/add', $edit, t('Save'));
    $this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
    $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
    $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
  }
}

Other Drupal examples (source code examples)

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