basic.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. require_once ABSPATH . 'wp-admin/includes/admin.php';
  3. require_once ABSPATH . WPINC . '/class-IXR.php';
  4. require_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php';
  5. /**
  6. * @group xmlrpc
  7. */
  8. class Tests_XMLRPC_Basic extends WP_XMLRPC_UnitTestCase {
  9. function test_enabled() {
  10. $result = $this->myxmlrpcserver->wp_getOptions( array( 1, 'username', 'password' ) );
  11. $this->assertIXRError( $result );
  12. // If disabled, 405 would result.
  13. $this->assertSame( 403, $result->code );
  14. }
  15. function test_disabled() {
  16. add_filter( 'xmlrpc_enabled', '__return_false' );
  17. $result = $this->myxmlrpcserver->wp_getOptions( array( 1, 'username', 'password' ) );
  18. $this->assertIXRError( $result );
  19. $this->assertSame( 405, $result->code );
  20. }
  21. function test_login_pass_ok() {
  22. $user_id = $this->make_user_by_role( 'subscriber' );
  23. $this->assertTrue( $this->myxmlrpcserver->login_pass_ok( 'subscriber', 'subscriber' ) );
  24. $this->assertInstanceOf( 'WP_User', $this->myxmlrpcserver->login( 'subscriber', 'subscriber' ) );
  25. }
  26. function test_login_pass_bad() {
  27. $user_id = $this->make_user_by_role( 'subscriber' );
  28. $this->assertFalse( $this->myxmlrpcserver->login_pass_ok( 'username', 'password' ) );
  29. $this->assertFalse( $this->myxmlrpcserver->login( 'username', 'password' ) );
  30. // The auth will still fail due to authentication blocking after the first failed attempt.
  31. $this->assertFalse( $this->myxmlrpcserver->login_pass_ok( 'subscriber', 'subscriber' ) );
  32. }
  33. /**
  34. * @ticket 34336
  35. */
  36. function test_multicall_invalidates_all_calls_after_invalid_call() {
  37. $editor_id = $this->make_user_by_role( 'editor' );
  38. $post_id = self::factory()->post->create(
  39. array(
  40. 'post_author' => $editor_id,
  41. )
  42. );
  43. $method_calls = array(
  44. // Valid login.
  45. array(
  46. 'methodName' => 'wp.editPost',
  47. 'params' => array(
  48. 0,
  49. 'editor',
  50. 'editor',
  51. $post_id,
  52. array(
  53. 'title' => 'Title 1',
  54. ),
  55. ),
  56. ),
  57. // *Invalid* login.
  58. array(
  59. 'methodName' => 'wp.editPost',
  60. 'params' => array(
  61. 0,
  62. 'editor',
  63. 'password',
  64. $post_id,
  65. array(
  66. 'title' => 'Title 2',
  67. ),
  68. ),
  69. ),
  70. // Valid login.
  71. array(
  72. 'methodName' => 'wp.editPost',
  73. 'params' => array(
  74. 0,
  75. 'editor',
  76. 'editor',
  77. $post_id,
  78. array(
  79. 'title' => 'Title 3',
  80. ),
  81. ),
  82. ),
  83. );
  84. $this->myxmlrpcserver->callbacks = $this->myxmlrpcserver->methods;
  85. $result = $this->myxmlrpcserver->multiCall( $method_calls );
  86. $this->assertArrayNotHasKey( 'faultCode', $result[0] );
  87. $this->assertArrayHasKey( 'faultCode', $result[1] );
  88. $this->assertArrayHasKey( 'faultCode', $result[2] );
  89. }
  90. /**
  91. * @ticket 36586
  92. */
  93. function test_isStruct_on_non_numerically_indexed_array() {
  94. $value = new IXR_Value( array( '0.0' => 100 ) );
  95. $return = "<struct>\n";
  96. $return .= " <member><name>0.0</name><value><int>100</int></value></member>\n";
  97. $return .= '</struct>';
  98. $this->assertXmlStringEqualsXmlString( $return, $value->getXML() );
  99. }
  100. }