forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportWp.php
More file actions
477 lines (430 loc) · 12.7 KB
/
exportWp.php
File metadata and controls
477 lines (430 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* @group admin
* @group export
*
* @covers ::export_wp
*
* Tests run in a separate process to prevent "headers already sent" error.
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class Tests_Admin_ExportWp extends WP_UnitTestCase {
/**
* Post IDs for posts, pages, and attachments.
*
* The structure is shown for understanding how to
* lookup / reference the information within it.
*
* IDs will be created in this order.
*
* @var array {
* @type array $data {
* Data for each post, page, or attachment.
*
* @type int $post_id The ID for the post, page, or attachment.
* @type int $post_author The author's ID.
* @type int $xml_item_index The XML item index for this post, page, or attachment.
* This number is based upon all of the posts, pages, and attachments
* in the self::$post_ids static property.
* }
* }
*/
private static $post_ids = array(
'post 1' => array(),
'attachment for post 1' => array(),
'post 2' => array(),
'attachment for post 2' => array(),
'page 1' => array(),
'attachment for page 1' => array(),
'page 2' => array(),
'attachment for page 2' => array(),
);
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
require_once ABSPATH . 'wp-admin/includes/export.php';
$file = DIR_TESTDATA . '/images/test-image.jpg';
$dataset = array(
'post 1' => array(
'post_title' => 'Test Post 1',
'post_type' => 'post',
),
'post 2' => array(
'post_title' => 'Test Post 2',
'post_type' => 'post',
),
'page 1' => array(
'post_title' => 'Test Page 1',
'post_type' => 'page',
),
'page 2' => array(
'post_title' => 'Test Page 2',
'post_type' => 'page',
),
);
$xml_item_index = -1;
foreach ( $dataset as $post_key => $post_data ) {
$attachment_key = "attachment for $post_key";
$post_data['post_author'] = $factory->user->create( array( 'role' => 'editor' ) );
$post_id = $factory->post->create( $post_data );
$attachment_id = $factory->attachment->create_upload_object( $file, $post_id );
set_post_thumbnail( $post_id, $attachment_id );
self::$post_ids[ $post_key ] = array(
'post_id' => $post_id,
'post_author' => $post_data['post_author'],
'xml_item_index' => ++$xml_item_index,
);
self::$post_ids[ $attachment_key ] = array(
'post_id' => $attachment_id,
'post_author' => $post_data['post_author'],
'xml_item_index' => ++$xml_item_index,
);
}
}
/**
* @dataProvider data_should_include_attachments
*
* @ticket 17379
*
* @param array $args Arguments to pass to export_wp().
* @param array $expected {
* The expected data.
*
* @type array $items {
* The expected XML items count assertion arguments.
*
* @type int $number_of_items The expected number of XML items.
* @type string $message The assertion failure message.
* }
* @type array $ids A list of self::$post_ids keys.
*/
public function test_should_include_attachments( array $args, array $expected ) {
$this->populate_args_post_authors( $args, $expected['ids'] );
$xml = $this->get_the_export( $args );
$expected_number_of_items = $expected['items']['number_of_items'];
$this->assertCount( $expected_number_of_items, $xml->channel->item, $expected['items']['message'] );
// Test each XML item's post ID to valid the post, page, and attachment (when appropriate) were exported.
foreach ( $expected['ids'] as $post_ids_key ) {
$xml_item = $this->get_xml_item( $xml, $post_ids_key, $expected_number_of_items );
$this->assertSame(
$this->get_expected_id( $post_ids_key ),
(int) $xml_item->post_id,
"In the XML, the {$post_ids_key}'s ID should match the expected content"
);
}
}
/**
* Data provider.
*
* @return array
*/
public function data_should_include_attachments() {
return array(
'for all content' => array(
'args' => array(
'content' => 'all',
),
'expected' => array(
'items' => array(
'number_of_items' => 8,
'message' => 'The number of items should be 8 = 2 pages, 2 posts and 4 attachments',
),
'ids' => array(
'post 1',
'post 2',
'page 1',
'page 2',
'attachment for page 1',
'attachment for post 2',
'attachment for page 1',
'attachment for page 2',
),
),
),
'for all posts' => array(
'args' => array(
'content' => 'post',
),
'expected' => array(
'items' => array(
'number_of_items' => 4,
'message' => 'The number of items should be 4 = 2 posts and 2 attachments',
),
'ids' => array(
'post 1',
'post 2',
'attachment for post 1',
'attachment for post 2',
),
),
),
'for all pages' => array(
'args' => array(
'content' => 'page',
),
'expected' => array(
'items' => array(
'number_of_items' => 4,
'message' => 'The number of items should be 4 = 2 pages and 2 attachments',
),
'ids' => array(
'page 1',
'attachment for page 1',
'page 2',
'attachment for page 2',
),
),
),
'for specific author posts' => array(
'args' => array(
'content' => 'post',
'author' => '', // The test will populate the author's ID.
),
'expected' => array(
'items' => array(
'number_of_items' => 2,
'message' => 'The number of items should be 2 = 1 post and 1 attachment',
),
'ids' => array(
'post 1',
'attachment for post 1',
),
),
),
'for specific author pages' => array(
'args' => array(
'content' => 'page',
'author' => '', // The test will populate the author's ID.
),
'expected' => array(
'items' => array(
'number_of_items' => 2,
'message' => 'The number of items should be 2 = 1 page and 1 attachment',
),
'ids' => array(
'page 2',
'attachment for page 2',
),
),
),
);
}
/**
* Gets the export results.
*
* @since 6.5.0
*
* @param array $args Arguments to pass to export_wp().
* @return SimpleXMLElement|false Returns the XML object on success, otherwise false is returned.
*/
private function get_the_export( $args ) {
ob_start();
export_wp( $args );
$results = ob_get_clean();
return simplexml_load_string( $results );
}
/**
* Gets the expected ID.
*
* @since 6.5.0
*
* @param string $post_ids_key The key to lookup in the $post_ids static property.
* @return int Expected ID.
*/
private function get_expected_id( $post_ids_key ) {
$post_info = self::$post_ids[ $post_ids_key ];
return $post_info['post_id'];
}
/**
* Gets the XML item for the given post or attachment in the self::$post_ids.
*
* @since 6.5.0
*
* @param SimpleXMLElement $xml XML object.
* @param string $post_ids_key The key to lookup in the $post_ids static property.
* @param int $number_of_items The number of expected XML items.
* @return SimpleXMLElement The XML item.
*/
private function get_xml_item( $xml, $post_ids_key, $number_of_items ) {
$post_info = self::$post_ids[ $post_ids_key ];
if ( $post_info['xml_item_index'] < $number_of_items ) {
$xml_item_index = $post_info['xml_item_index'];
} elseif ( 2 === $number_of_items ) {
$xml_item_index = 0 === $post_info['xml_item_index'] % 2 ? 0 : 1;
} else {
$xml_item_index = $post_info['xml_item_index'] - $number_of_items;
}
return $xml->channel->item[ $xml_item_index ]->children( 'wp', true );
}
/**
* Populates the post author in the given args.
*
* @since 6.5.0
*
* @param array $args Passed by reference. export_wp() arguments to process.
*/
private function populate_args_post_authors( array &$args, $expected_ids ) {
if ( ! isset( $args['author'] ) ) {
return;
}
$post_ids_key = $expected_ids[0];
$args['author'] = self::$post_ids[ $post_ids_key ]['post_author'];
}
/**
* @ticket 61244
*/
public function test_export_wp_should_not_include_empty_comments_when_filtered() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
self::factory()->comment->create_post_comments( $post_id, 3 );
// Add filter to make get_comment return null.
add_action(
'export_wp',
static function () {
add_filter( 'get_comment', '__return_null' );
}
);
$xml_obj = $this->get_the_export( array() );
$comment_tags = $xml_obj->xpath( '//wp:comment' );
$this->assertCount( 0, $comment_tags, 'No <wp:comment> tags should be present when comments are filtered out.' );
}
/**
* @ticket 61244
*/
public function test_export_wp_includes_comments_when_not_filtered() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
$comment_count = 3;
self::factory()->comment->create_post_comments( $post_id, $comment_count );
$xml_obj = $this->get_the_export( array() );
$comment_tags = $xml_obj->xpath( '//wp:comment' );
$this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' );
}
/**
* Tests that export handles posts with NULL postmeta values without fatal errors.
*
* @ticket 64347
*/
public function test_export_with_null_postmeta_values() {
global $wpdb;
$post_id = self::factory()->post->create(
array(
'post_title' => 'Test Post with NULL Meta',
'post_content' => 'Test content',
'post_type' => 'post',
)
);
// Add multiple types of postmeta values.
add_post_meta( $post_id, 'string_meta', 'normal string' );
add_post_meta( $post_id, 'numeric_string_meta', 123 );
add_post_meta( $post_id, 'empty_string_meta', '' );
add_post_meta(
$post_id,
'array_meta',
array(
'key' => 'value',
)
);
// Directly insert NULL and non-string values into postmeta.
$wpdb->insert(
$wpdb->postmeta,
array(
'post_id' => $post_id,
'meta_key' => 'null_meta',
'meta_value' => null,
),
array( '%d', '%s', '%s' )
);
$xml = $this->get_the_export(
array(
'content' => 'post',
)
);
$this->assertNotFalse( $xml, 'Export should not fail with NULL postmeta values' );
$this->assertGreaterThan( 0, count( $xml->channel->item ), 'Export should contain items' );
// Post should be present in export.
$found_post = false;
foreach ( $xml->channel->item as $item ) {
$wp_item = $item->children( 'wp', true );
if ( (int) $wp_item->post_id === $post_id ) {
$found_post = true;
break;
}
}
$this->assertTrue( $found_post, 'Post with NULL metadata should be included in export' );
}
/**
* Tests that export handles comments with NULL values without fatal errors.
*
* @ticket 64347
*/
public function test_export_with_null_comment_values() {
global $wpdb;
$post_id = self::factory()->post->create(
array(
'post_title' => 'Test Post for Comments',
'post_type' => 'post',
)
);
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_content' => 'Test comment',
)
);
// Insert NULL comment meta.
$wpdb->insert(
$wpdb->commentmeta,
array(
'comment_id' => $comment_id,
'meta_key' => 'null_comment_meta',
'meta_value' => null,
),
array( '%d', '%s', '%s' )
);
$xml = $this->get_the_export(
array(
'content' => 'post',
)
);
$this->assertNotFalse( $xml, 'Export should not fail with NULL comment meta values' );
$this->assertGreaterThan( 0, count( $xml->channel->item ), 'Export should contain items' );
}
/**
* Tests that export handles term meta with NULL values without fatal errors.
*
* @ticket 64347
*/
public function test_export_with_null_term_meta_values() {
global $wpdb;
// Create term.
$term = self::factory()->term->create(
array(
'taxonomy' => 'category',
'name' => 'Test Category',
)
);
$post_id = self::factory()->post->create(
array(
'post_title' => 'Test Post with Category',
'post_type' => 'post',
'post_status' => 'publish',
)
);
wp_set_object_terms( $post_id, $term, 'category' );
// Insert NULL term meta.
$wpdb->insert(
$wpdb->termmeta,
array(
'term_id' => $term,
'meta_key' => 'null_term_meta',
'meta_value' => null,
),
array( '%d', '%s', '%s' )
);
$xml = $this->get_the_export(
array(
'content' => 'all',
)
);
$this->assertNotFalse( $xml, 'Export should not fail with NULL term meta values' );
$this->assertGreaterThan( 0, count( $xml->channel->item ), 'Export should contain items' );
}
}