xmlrpc_example.test

  1. examples
    1. 6 xmlrpc_example/xmlrpc_example.test
    2. 7 xmlrpc_example/xmlrpc_example.test
    3. 8 xmlrpc_example/xmlrpc_example.test

Test case for the XML-RPC example module.

Classes

NameDescription
XmlrpcExampleTestCase@file Test case for the XML-RPC example module.

File

xmlrpc_example/xmlrpc_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Test case for the XML-RPC example module.
  5. */
  6. class XmlrpcExampleTestCase extends DrupalWebTestCase {
  7. protected $xmlrpc_url;
  8. public static function getInfo() {
  9. return array(
  10. 'name' => 'XMLRPC example functionality',
  11. 'description' => 'Test xmlrpc service implementation.',
  12. 'group' => 'Examples',
  13. );
  14. }
  15. /**
  16. * Enable module.
  17. */
  18. function setUp() {
  19. parent::setUp('xmlrpc_example');
  20. // Init common variables.
  21. global $base_url;
  22. $this->xmlrpc_url = url($GLOBALS['base_url'] . '/xmlrpc.php', array('external' => TRUE));
  23. }
  24. /**
  25. * Perform several calls to the XML-RPC interface to test the services.
  26. */
  27. function testXmlrpcExampleBasic() {
  28. // Unit test functionality.
  29. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.add' => array(3, 4)));
  30. $this->assertEqual($result, 7, t('Successfully added 3+4 = 7'));
  31. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.subtract' => array(4, 3)));
  32. $this->assertEqual($result, 1, t('Successfully subtracted 4-3 = 1'));
  33. // Make a multicall request
  34. $options = array(
  35. 'xmlrpc_example.add' => array(5, 2),
  36. 'xmlrpc_example.subtract' => array(5, 2),
  37. );
  38. $expected = array(7, 3);
  39. $result = xmlrpc($this->xmlrpc_url, $options);
  40. $this->assertEqual($result, $expected, t('Successfully called multicall request'));
  41. // Verify default limits
  42. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.subtract' => array(3, 4)));
  43. $this->assertEqual(xmlrpc_errno(), 10002, t('Results below minimum return custom error: 10002'));
  44. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.add' => array(7, 4)));
  45. $this->assertEqual(xmlrpc_errno(), 10001, t('Results beyond maximum return custom error: 10001'));
  46. }
  47. /**
  48. * Perform several calls using XML-RPC web client.
  49. */
  50. function testXmlrpcExampleClient() {
  51. // Now test the UI.
  52. // Add the integers.
  53. $edit = array('num1' => 3, 'num2' => 5);
  54. $this->drupalPost('examples/xmlrpc/client', $edit, t('Add the integers'));
  55. $this->assertText(t('The XML-RPC server returned this response: @num', array('@num' => 8)));
  56. // Subtract the integers.
  57. $edit = array('num1' => 8, 'num2' => 3);
  58. $result = $this->drupalPost('examples/xmlrpc/client', $edit, t('Subtract the integers'));
  59. $this->assertText(t('The XML-RPC server returned this response: @num', array('@num' => 5)));
  60. // Request available methods.
  61. $this->drupalPost('examples/xmlrpc/client', $edit, t('Request methods'));
  62. $this->assertText('xmlrpc_example.add', t('The XML-RPC Add method was found.'));
  63. $this->assertText('xmlrpc_example.subtract', t('The XML-RPC Subtract method was found.'));
  64. // Before testing multicall, verify that method exists
  65. $this->assertText('system.multicall', t('The XML-RPC Multicall method was found.'));
  66. // Verify multicall request
  67. $edit = array('num1' => 5, 'num2' => 2);
  68. $this->drupalPost('examples/xmlrpc/client', $edit, t('Add and Subtract'));
  69. $this->assertText('[0] =&gt; 7', t('The XML-RPC server returned the addition result.'));
  70. $this->assertText('[1] =&gt; 3', t('The XML-RPC server returned the subtraction result.'));
  71. }
  72. /**
  73. * Perform several XML-RPC requests with different server settings.
  74. */
  75. function testXmlrpcExampleServer() {
  76. // Set different minimum and maxmimum valuesI.
  77. $options = array('xmlrpc_example_server_min' => 3, 'xmlrpc_example_server_max' => 7);
  78. $this->drupalPost('examples/xmlrpc/server', $options, t('Save configuration'));
  79. $this->assertText(t('The configuration options have been saved'), t('Results limited to >= 3 and <= 7'));
  80. $edit = array('num1' => 8, 'num2' => 3);
  81. $this->drupalPost('examples/xmlrpc/client', $edit, t('Subtract the integers'));
  82. $this->assertText(t('The XML-RPC server returned this response: @num', array('@num' => 5)));
  83. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.add' => array(3, 4)));
  84. $this->assertEqual($result, 7, t('Successfully added 3+4 = 7'));
  85. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.subtract' => array(4, 3)));
  86. $this->assertEqual(xmlrpc_errno(), 10002, t('subtracting 4-3 = 1 returns custom error: 10002'));
  87. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.add' => array(7, 4)));
  88. $this->assertEqual(xmlrpc_errno(), 10001, t('Adding 7 + 4 = 11 returns custom error: 10001'));
  89. }
  90. /**
  91. * Perform several XML-RPC requests altering the server behaviour with
  92. * hook_xmlrpc_alter API
  93. */
  94. function testXmlrpcExampleAlter() {
  95. // Enable XML-RPC service altering functionality.
  96. $options = array('xmlrpc_example_alter_enabled' => TRUE);
  97. $this->drupalPost('examples/xmlrpc/alter', $options, t('Save configuration'));
  98. $this->assertText(t('The configuration options have been saved'), t('Results are not limited due to methods alteration'));
  99. // After altering the functionality, the add and subtract methods have no
  100. // limits and should not return any error.
  101. $edit = array('num1' => 80, 'num2' => 3);
  102. $this->drupalPost('examples/xmlrpc/client', $edit, t('Subtract the integers'));
  103. $this->assertText(t('The XML-RPC server returned this response: @num', array('@num' => 77)));
  104. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.add' => array(30, 4)));
  105. $this->assertEqual($result, 34, t('Successfully added 30+4 = 34'));
  106. $result = xmlrpc($this->xmlrpc_url, array('xmlrpc_example.subtract' => array(4, 30)));
  107. $this->assertEqual($result, -26, t('Successfully substracted 4-30 = -26'));
  108. }
  109. }
Login or register to post comments