xmlrpc_example.test 5.7 KB

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