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

Drupal example source code file (xmlrpc_example.test)

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

10001, 10002, edit, function, num2, options, php, response, result, returned, rpc, the, xml, xmlrpc

The xmlrpc_example.test Drupal example source code

<?php

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

  public static function getInfo() {
    return array(
      'name' => 'XMLRPC example functionality',
      'description' => 'Test xmlrpc service implementation.',
      'group' => 'Examples',
    );
  }

  /**
   * Enable module.
   */
  function setUp() {
    parent::setUp('xmlrpc_example');

    // Init common variables.
    global $base_url;
    $this->xmlrpc_url = url($base_url . '/xmlrpc.php', array('external' => TRUE));
  }

  /**
   * Perform several calls the xmlrpc inteface to test the services.
   */
  function testXmlrpcExampleBasic() {
    // Unit test functionality.
    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.add', 3, 4);
    $this->assertEqual($result, 7, t('Successfully added 3+4 = 7'));

    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.subtract', 4, 3);
    $this->assertEqual($result, 1, t('Successfully subtracted 4-3 = 1'));

    // Verify default limits
    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.subtract', 3, 4);
    $this->assertEqual(xmlrpc_errno(), 10002, t('Results below minimum return error: 10002'));

    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.add', 7, 4);
    $this->assertEqual(xmlrpc_errno(), 10001, t('Results beyond maximum return error: 10001'));
  }


  /**
   * Perform several calls using xmlrpc UI client
   */
  function testXmlrpcExampleClient() {
    // Now test the UI.
    $edit = array('num1' => 3, 'num2' => 5);
    $this->drupalPost('examples/xmlrpc_client', $edit, t('Add the integers'));
    $this->assertText(t("The XMLRPC server returned this response: @num", array('@num' => 8)));

    $edit = array('num1' => 8, 'num2' => 3);
    $this->drupalPost('examples/xmlrpc_client', $edit, t('Subtract the integers'));
    $this->assertText(t("The XMLRPC server returned this response: @num", array('@num' => 5)));
  }

  /**
   * Perform several xmlrpc requests changing server settings
   */
  function testXmlrpcExampleServer() {
    // Now test the UI.
    $options = array('xmlrpc_example_server_min' => 3, 'xmlrpc_example_server_max' => 7);
    $this->drupalPost('examples/xmlrpc_server', $options, t('Save configuration'));
    $this->assertText(t("The configuration options have been saved"), t("Results limited to >= 3 and <= 7"));

    $edit = array('num1' => 8, 'num2' => 3);
    $this->drupalPost('examples/xmlrpc_client', $edit, t('Subtract the integers'));
    $this->assertText(t("The XMLRPC server returned this response: @num", array('@num' => 5)));

    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.add', 3, 4);
    $this->assertEqual($result, 7, t('Successfully added 3+4 = 7'));

    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.subtract', 4, 3);
    $this->assertEqual(xmlrpc_errno(), 10002, t('subtracting 4-3 = 1 returns: 10002'));

    $result = xmlrpc($this->xmlrpc_url, 'xmlrpc_example.add', 7, 4);
    $this->assertEqual(xmlrpc_errno(), 10001, t('Adding 7 + 4 = 11 returns: 10001'));
  }
}

Other Drupal examples (source code examples)

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