You are here

xmlrpc_test.module in xmlrpc 8

Test module to support XmlRpcMessagesTest and XmlRpcValidatorTest.

File

tests/modules/xmlrpc_test/xmlrpc_test.module
View source
<?php

/**
 * @file
 * Test module to support XmlRpcMessagesTest and XmlRpcValidatorTest.
 */

/**
 * Test function counting elements occurrences.
 *
 * @param array $array
 *   A hash of values, containing sub-arrays with repeated keys.
 *
 * @return int
 *   The count of values for a repeated key.
 */
function xmlrpc_test_array_of_structs_test(array $array) {
  $sum = 0;
  foreach ($array as $struct) {
    if (isset($struct['curly'])) {
      $sum += $struct['curly'];
    }
  }
  return $sum;
}

/**
 * Test function checking entities in string arguments.
 *
 * @param string $string
 *   A string containing escaped characters.
 *
 * @return array
 *   A hash of the count of occurrences of escapes by type.
 */
function xmlrpc_test_count_the_entities($string) {
  return [
    'ctLeftAngleBrackets' => substr_count($string, '<'),
    'ctRightAngleBrackets' => substr_count($string, '>'),
    'ctAmpersands' => substr_count($string, '&'),
    'ctApostrophes' => substr_count($string, "'"),
    'ctQuotes' => substr_count($string, '"'),
  ];
}

/**
 * Test function building an array from hash/struct values.
 *
 * @param array $array
 *   A hash of values of any type.
 *
 * @return mixed
 *   An array of values from the source hash.
 */
function xmlrpc_test_easy_struct_test(array $array) {
  return $array["curly"] + $array["moe"] + $array["larry"];
}

/**
 * Test function accepting an array and returning it.
 *
 * @param array $array
 *   A source array.
 *
 * @return array
 *   The same array.
 */
function xmlrpc_test_echo_struct_test(array $array) {
  return $array;
}

/**
 * Test function accepting multiple parameters of different types.
 *
 * @param int $number
 *   An integer value.
 * @param bool $boolean
 *   A boolean value.
 * @param string $string
 *   A string value.
 * @param float $double
 *   A floating-point value.
 * @param object $date_time
 *   A XML-RPC date/time object.
 * @param string $base64
 *   A XML-RPC base64 object.
 *
 * @return array
 *   An array combining the arguments.
 */
function xmlrpc_test_many_types_test($number, $boolean, $string, $double, $date_time, $base64) {
  $timestamp = gmmktime($date_time->hour, $date_time->minute, $date_time->second, $date_time->month, $date_time->day, $date_time->year);
  return [
    $number,
    $boolean,
    $string,
    $double,
    xmlrpc_date($timestamp),
    xmlrpc_Base64($base64),
  ];
}

/**
 * Test function accepting an array.
 *
 * @param array $array
 *   The source array to operate on.
 *
 * @return string
 *   The first and last values of the array, concatenated.
 */
function xmlrpc_test_moderate_size_array_check(array $array) {
  return array_shift($array) . array_pop($array);
}

/**
 * Test function returning a nested hash/struct.
 *
 * @param array $array
 *   A source data hash.
 *
 * @return mixed
 *   A nested hash combining parts of the source data.
 */
function xmlrpc_test_nested_struct_test(array $array) {
  return $array["2000"]["04"]["01"]["larry"] + $array["2000"]["04"]["01"]["moe"] + $array["2000"]["04"]["01"]["curly"];
}

/**
 * Test function returning a simple hash/struct.
 *
 * @param int $number
 *   A single number argument.
 *
 * @return array
 *   The incoming number, multiplied by 10, 100, 1000 as hash/struct values.
 */
function xmlrpc_test_simple_struct_return_test($number) {
  return [
    "times10" => $number * 10,
    "times100" => $number * 100,
    "times1000" => $number * 1000,
  ];
}

/**
 * Echoes http headers to XML-RPC clients.
 */
function xmlrpc_test_header_echo() {
  return getallheaders();
}

/**
 * Implements hook_xmlrpc().
 */
function xmlrpc_test_xmlrpc() {
  return [
    'validator1.arrayOfStructsTest' => 'xmlrpc_test_array_of_structs_test',
    'validator1.countTheEntities' => 'xmlrpc_test_count_the_entities',
    'validator1.easyStructTest' => 'xmlrpc_test_easy_struct_test',
    'validator1.echoStructTest' => 'xmlrpc_test_echo_struct_test',
    'validator1.manyTypesTest' => 'xmlrpc_test_many_types_test',
    'validator1.moderateSizeArrayCheck' => 'xmlrpc_test_moderate_size_array_check',
    'validator1.nestedStructTest' => 'xmlrpc_test_nested_struct_test',
    'validator1.simpleStructReturnTest' => 'xmlrpc_test_simple_struct_return_test',
    'messages.messageSizedInKB' => 'xmlrpc_test_message_sized_in_kb',
    'test.headerEcho' => 'xmlrpc_test_header_echo',
  ];
}

/**
 * Implements hook_xmlrpc_alter().
 *
 * Hide (or not) the system.methodSignature() service depending on a variable.
 */
function xmlrpc_test_xmlrpc_alter(&$services) {
  $xmlprc_alter = \Drupal::state()
    ->get('xmlrpc_test.alter') ?: FALSE;
  if ($xmlprc_alter) {
    $remove = NULL;
    foreach ($services as $key => $value) {
      if (!is_array($value)) {
        continue;
      }
      if ($value[0] == 'system.methodSignature') {
        $remove = $key;
        break;
      }
    }
    if (isset($remove)) {
      unset($services[$remove]);
    }
  }
}

/**
 * Created a message of the desired size in KB.
 *
 * @param int $size
 *   Message size in KB.
 *
 * @return array
 *   Generated message structure.
 */
function xmlrpc_test_message_sized_in_kb($size) {
  $message = [];
  $line = [];
  $word = 'abcdefg';

  // Create a ~1KB sized struct.
  for ($i = 0; $i < 128; $i++) {
    $line['word_' . $i] = $word;
  }
  for ($i = 0; $i < $size; $i++) {
    $message['line_' . $i] = $line;
  }
  return $message;
}

Functions

Namesort descending Description
xmlrpc_test_array_of_structs_test Test function counting elements occurrences.
xmlrpc_test_count_the_entities Test function checking entities in string arguments.
xmlrpc_test_easy_struct_test Test function building an array from hash/struct values.
xmlrpc_test_echo_struct_test Test function accepting an array and returning it.
xmlrpc_test_header_echo Echoes http headers to XML-RPC clients.
xmlrpc_test_many_types_test Test function accepting multiple parameters of different types.
xmlrpc_test_message_sized_in_kb Created a message of the desired size in KB.
xmlrpc_test_moderate_size_array_check Test function accepting an array.
xmlrpc_test_nested_struct_test Test function returning a nested hash/struct.
xmlrpc_test_simple_struct_return_test Test function returning a simple hash/struct.
xmlrpc_test_xmlrpc Implements hook_xmlrpc().
xmlrpc_test_xmlrpc_alter Implements hook_xmlrpc_alter().