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
293 lines (272 loc) · 8.18 KB
/
exportWp.php
File metadata and controls
293 lines (272 loc) · 8.18 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
<?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'];
}
}