getPost.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @group xmlrpc
  4. */
  5. class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
  6. protected static $post_id;
  7. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  8. self::$post_id = $factory->post->create(
  9. array(
  10. 'post_author' => $factory->user->create(
  11. array(
  12. 'user_login' => 'author',
  13. 'user_pass' => 'author',
  14. 'role' => 'author',
  15. )
  16. ),
  17. 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
  18. )
  19. );
  20. }
  21. function test_invalid_username_password() {
  22. $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'username', 'password' ) );
  23. $this->assertIXRError( $result );
  24. $this->assertSame( 403, $result->code );
  25. }
  26. function test_incapable_user() {
  27. $this->make_user_by_role( 'subscriber' );
  28. $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'subscriber', 'subscriber' ) );
  29. $this->assertIXRError( $result );
  30. $this->assertSame( 401, $result->code );
  31. }
  32. /**
  33. * @ticket 20336
  34. */
  35. function test_invalid_postid() {
  36. $result = $this->myxmlrpcserver->mw_getPost( array( 9999, 'author', 'author' ) );
  37. $this->assertIXRError( $result );
  38. $this->assertSame( 404, $result->code );
  39. }
  40. function test_valid_post() {
  41. add_theme_support( 'post-thumbnails' );
  42. $fields = array( 'post' );
  43. $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
  44. $this->assertNotIXRError( $result );
  45. // Check data types.
  46. $this->assertInternalType( 'string', $result['userid'] );
  47. $this->assertInternalType( 'int', $result['postid'] );
  48. $this->assertInternalType( 'string', $result['description'] );
  49. $this->assertInternalType( 'string', $result['title'] );
  50. $this->assertInternalType( 'string', $result['link'] );
  51. $this->assertInternalType( 'string', $result['permaLink'] );
  52. $this->assertInternalType( 'array', $result['categories'] );
  53. $this->assertInternalType( 'string', $result['mt_excerpt'] );
  54. $this->assertInternalType( 'string', $result['mt_text_more'] );
  55. $this->assertInternalType( 'string', $result['wp_more_text'] );
  56. $this->assertInternalType( 'int', $result['mt_allow_comments'] );
  57. $this->assertInternalType( 'int', $result['mt_allow_pings'] );
  58. $this->assertInternalType( 'string', $result['mt_keywords'] );
  59. $this->assertInternalType( 'string', $result['wp_slug'] );
  60. $this->assertInternalType( 'string', $result['wp_password'] );
  61. $this->assertInternalType( 'string', $result['wp_author_id'] );
  62. $this->assertInternalType( 'string', $result['wp_author_display_name'] );
  63. $this->assertInternalType( 'string', $result['post_status'] );
  64. $this->assertInternalType( 'array', $result['custom_fields'] );
  65. $this->assertInternalType( 'string', $result['wp_post_format'] );
  66. $this->assertInternalType( 'bool', $result['sticky'] );
  67. $post_data = get_post( self::$post_id );
  68. // Check expected values.
  69. $this->assertStringMatchesFormat( '%d', $result['userid'] );
  70. $this->assertSame( $post_data->post_title, $result['title'] );
  71. $this->assertSame( 'publish', $result['post_status'] );
  72. $this->assertStringMatchesFormat( '%d', $result['wp_author_id'] );
  73. $this->assertSame( $post_data->post_excerpt, $result['mt_excerpt'] );
  74. $this->assertSame( url_to_postid( $result['link'] ), self::$post_id );
  75. $this->assertSame( 0, $result['wp_post_thumbnail'] );
  76. remove_theme_support( 'post-thumbnails' );
  77. }
  78. function test_post_thumbnail() {
  79. add_theme_support( 'post-thumbnails' );
  80. // Create attachment.
  81. $filename = ( DIR_TESTDATA . '/images/a2-small.jpg' );
  82. $attachment_id = self::factory()->attachment->create_upload_object( $filename );
  83. set_post_thumbnail( self::$post_id, $attachment_id );
  84. $fields = array( 'post' );
  85. $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
  86. $this->assertNotIXRError( $result );
  87. $this->assertInternalType( 'int', $result['wp_post_thumbnail'] );
  88. $this->assertSame( $attachment_id, $result['wp_post_thumbnail'] );
  89. remove_theme_support( 'post-thumbnails' );
  90. }
  91. function test_date() {
  92. $fields = array( 'post' );
  93. $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
  94. $this->assertNotIXRError( $result );
  95. $this->assertInstanceOf( 'IXR_Date', $result['dateCreated'] );
  96. $this->assertInstanceOf( 'IXR_Date', $result['date_created_gmt'] );
  97. $this->assertInstanceOf( 'IXR_Date', $result['date_modified'] );
  98. $this->assertInstanceOf( 'IXR_Date', $result['date_modified_gmt'] );
  99. $post_data = get_post( self::$post_id );
  100. $this->assertSame( strtotime( $post_data->post_date ), $result['dateCreated']->getTimestamp() );
  101. $this->assertSame( strtotime( $post_data->post_date ), $result['date_modified']->getTimestamp() );
  102. $post_date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
  103. $post_modified_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
  104. $this->assertSame( $post_date_gmt, $result['date_created_gmt']->getTimestamp() );
  105. $this->assertSame( $post_modified_gmt, $result['date_modified_gmt']->getTimestamp() );
  106. }
  107. }