| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group date |
| | 5 | * @group datetime |
| | 6 | * @group xmlrpc |
| | 7 | */ |
| | 8 | class Tests_Xmlrpc extends WP_XMLRPC_UnitTestCase { |
| | 9 | |
| | 10 | /** |
| | 11 | * @ticket 30429 |
| | 12 | */ |
| | 13 | public function test_date_new_post() { |
| | 14 | $timezone = 'Europe/Kiev'; |
| | 15 | update_option( 'timezone_string', $timezone ); |
| | 16 | |
| | 17 | $datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) ); |
| | 18 | $datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); |
| | 19 | |
| | 20 | $this->make_user_by_role( 'editor' ); |
| | 21 | |
| | 22 | $post = get_post( $this->myxmlrpcserver->mw_newPost( array( |
| | 23 | 1, |
| | 24 | 'editor', |
| | 25 | 'editor', |
| | 26 | array( |
| | 27 | 'title' => 'test', |
| | 28 | 'post_content' => 'test', |
| | 29 | 'dateCreated' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s\Z' ) ) |
| | 30 | ) |
| | 31 | ) ) ); |
| | 32 | |
| | 33 | $this->assertEquals( |
| | 34 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 35 | $post->post_date, |
| | 36 | 'UTC time with explicit time zone into mw_newPost' |
| | 37 | ); |
| | 38 | |
| | 39 | $post = get_post( $this->myxmlrpcserver->mw_newPost( array( |
| | 40 | 1, |
| | 41 | 'editor', |
| | 42 | 'editor', |
| | 43 | array( |
| | 44 | 'title' => 'test', |
| | 45 | 'post_content' => 'test', |
| | 46 | 'dateCreated' => new IXR_Date( $datetime->format( 'Ymd\TH:i:s' ) ) |
| | 47 | ) |
| | 48 | ) ) ); |
| | 49 | |
| | 50 | $this->assertEquals( |
| | 51 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 52 | $post->post_date, |
| | 53 | 'Local time w/o time zone into mw_newPost' |
| | 54 | ); |
| | 55 | |
| | 56 | $post = get_post( $this->myxmlrpcserver->mw_newPost( array( |
| | 57 | 1, |
| | 58 | 'editor', |
| | 59 | 'editor', |
| | 60 | array( |
| | 61 | 'title' => 'test', |
| | 62 | 'post_content' => 'test', |
| | 63 | 'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ) |
| | 64 | ) |
| | 65 | ) ) ); |
| | 66 | |
| | 67 | $this->assertEquals( |
| | 68 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 69 | $post->post_date, |
| | 70 | 'UTC time into mw_newPost' |
| | 71 | ); |
| | 72 | |
| | 73 | $post = get_post( $this->myxmlrpcserver->wp_newPost( array( |
| | 74 | 1, |
| | 75 | 'editor', |
| | 76 | 'editor', |
| | 77 | array( |
| | 78 | 'title' => 'test', |
| | 79 | 'post_content' => 'test', |
| | 80 | 'post_date' => $datetime->format( 'Ymd\TH:i:s' ) |
| | 81 | ) |
| | 82 | ) ) ); |
| | 83 | |
| | 84 | $this->assertEquals( |
| | 85 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 86 | $post->post_date, |
| | 87 | 'Local time into wp_newPost' |
| | 88 | ); |
| | 89 | |
| | 90 | $post = get_post( $this->myxmlrpcserver->wp_newPost( array( |
| | 91 | 1, |
| | 92 | 'editor', |
| | 93 | 'editor', |
| | 94 | array( |
| | 95 | 'title' => 'test', |
| | 96 | 'post_content' => 'test', |
| | 97 | 'post_date_gmt' => $datetimeutc->format( 'Ymd\TH:i:s' ) |
| | 98 | ) |
| | 99 | ) ) ); |
| | 100 | |
| | 101 | $this->assertEquals( |
| | 102 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 103 | $post->post_date, |
| | 104 | 'UTC time into wp_newPost' |
| | 105 | ); |
| | 106 | } |
| | 107 | |
| | 108 | /** |
| | 109 | * @ticket 30429 |
| | 110 | */ |
| | 111 | public function test_date_edit_post() { |
| | 112 | $timezone = 'Europe/Kiev'; |
| | 113 | update_option( 'timezone_string', $timezone ); |
| | 114 | |
| | 115 | $datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) ); |
| | 116 | $datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); |
| | 117 | |
| | 118 | $editor_id = $this->make_user_by_role( 'editor' ); |
| | 119 | |
| | 120 | $post_id = self::factory()->post->create( array( |
| | 121 | 'post_author' => $editor_id, |
| | 122 | 'post_date' => $datetime->modify( '-1 hour' )->format( 'Y-m-d H:i:s' ), |
| | 123 | ) ); |
| | 124 | |
| | 125 | $result = $this->myxmlrpcserver->mw_editPost( array( |
| | 126 | $post_id, |
| | 127 | 'editor', |
| | 128 | 'editor', |
| | 129 | array( |
| | 130 | 'dateCreated' => new IXR_Date( $datetime->format( 'Ymd\TH:i:s' ) ), |
| | 131 | ) |
| | 132 | ) ); |
| | 133 | $fetched_post = get_post( $post_id ); |
| | 134 | |
| | 135 | $this->assertTrue( $result ); |
| | 136 | $this->assertEquals( |
| | 137 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 138 | $fetched_post->post_date, |
| | 139 | 'Local time into mw_editPost' |
| | 140 | ); |
| | 141 | |
| | 142 | $post_id = self::factory()->post->create( array( |
| | 143 | 'post_author' => $editor_id, |
| | 144 | 'post_date' => $datetime->modify( '-1 hour' )->format( 'Y-m-d H:i:s' ), |
| | 145 | ) ); |
| | 146 | |
| | 147 | $result = $this->myxmlrpcserver->mw_editPost( array( |
| | 148 | $post_id, |
| | 149 | 'editor', |
| | 150 | 'editor', |
| | 151 | array( |
| | 152 | 'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ), |
| | 153 | ) |
| | 154 | ) ); |
| | 155 | $fetched_post = get_post( $post_id ); |
| | 156 | |
| | 157 | $this->assertTrue( $result ); |
| | 158 | $this->assertEquals( |
| | 159 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 160 | $fetched_post->post_date, |
| | 161 | 'UTC time into mw_editPost' |
| | 162 | ); |
| | 163 | } |
| | 164 | |
| | 165 | /** |
| | 166 | * @ticket 30429 |
| | 167 | */ |
| | 168 | function test_date_edit_comment() { |
| | 169 | $timezone = 'Europe/Kiev'; |
| | 170 | update_option( 'timezone_string', $timezone ); |
| | 171 | |
| | 172 | $datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) ); |
| | 173 | $datetime = $datetime->modify( '-1 hour' ); |
| | 174 | $datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); |
| | 175 | |
| | 176 | $this->make_user_by_role( 'administrator' ); |
| | 177 | $post_id = $this->factory->post->create(); |
| | 178 | |
| | 179 | $comment_data = array( |
| | 180 | 'comment_post_ID' => $post_id, |
| | 181 | 'comment_author' => 'Test commenter', |
| | 182 | 'comment_author_url' => 'http://example.com/', |
| | 183 | 'comment_author_email' => 'example@example.com', |
| | 184 | 'comment_content' => rand_str( 100 ), |
| | 185 | 'comment_approved' => '1', |
| | 186 | ); |
| | 187 | $comment_id = wp_insert_comment( $comment_data ); |
| | 188 | |
| | 189 | $result = $this->myxmlrpcserver->wp_editComment( array( |
| | 190 | 1, |
| | 191 | 'administrator', |
| | 192 | 'administrator', |
| | 193 | $comment_id, |
| | 194 | array( |
| | 195 | 'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ) |
| | 196 | ) |
| | 197 | ) |
| | 198 | ); |
| | 199 | |
| | 200 | $fetched_comment = get_comment( $comment_id ); |
| | 201 | |
| | 202 | $this->assertTrue( $result ); |
| | 203 | $this->assertEquals( |
| | 204 | $datetime->format( 'Y-m-d H:i:s' ), |
| | 205 | $fetched_comment->comment_date, |
| | 206 | 'UTC time into wp_editComment' |
| | 207 | ); |
| | 208 | } |
| | 209 | } |