forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.php
More file actions
300 lines (273 loc) · 8.22 KB
/
parser.php
File metadata and controls
300 lines (273 loc) · 8.22 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
<?php
require_once __DIR__ . '/base.php';
/**
* @group import
*/
class Tests_Import_Parser extends WP_Import_UnitTestCase {
function setUp() {
parent::setUp();
if ( ! defined( 'WP_IMPORTING' ) ) {
define( 'WP_IMPORTING', true );
}
if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) {
define( 'WP_LOAD_IMPORTERS', true );
}
if ( ! file_exists( DIR_TESTDATA . '/plugins/wordpress-importer/wordpress-importer.php' ) ) {
$this->fail( 'This test requires the WordPress Importer plugin to be installed in the test suite. See: https://make.wordpress.org/core/handbook/contribute/git/#unit-tests' );
}
require_once DIR_TESTDATA . '/plugins/wordpress-importer/wordpress-importer.php';
}
function test_malformed_wxr() {
$file = DIR_TESTDATA . '/export/malformed.xml';
// Regex based parser cannot detect malformed XML.
foreach ( array( 'WXR_Parser_SimpleXML', 'WXR_Parser_XML' ) as $p ) {
$parser = new $p;
$result = $parser->parse( $file );
$this->assertWPError( $result );
$this->assertSame( 'There was an error when reading this WXR file', $result->get_error_message() );
}
}
function test_invalid_wxr() {
$f1 = DIR_TESTDATA . '/export/missing-version-tag.xml';
$f2 = DIR_TESTDATA . '/export/invalid-version-tag.xml';
foreach ( array( 'WXR_Parser_SimpleXML', 'WXR_Parser_XML', 'WXR_Parser_Regex' ) as $p ) {
foreach ( array( $f1, $f2 ) as $file ) {
$parser = new $p;
$result = $parser->parse( $file );
$this->assertWPError( $result );
$this->assertSame( 'This does not appear to be a WXR file, missing/invalid WXR version number', $result->get_error_message() );
}
}
}
function test_wxr_version_1_1() {
$file = DIR_TESTDATA . '/export/valid-wxr-1.1.xml';
foreach ( array( 'WXR_Parser_SimpleXML', 'WXR_Parser_XML', 'WXR_Parser_Regex' ) as $p ) {
$message = $p . ' failed';
$parser = new $p;
$result = $parser->parse( $file );
$this->assertIsArray( $result, $message );
$this->assertSame( 'http://localhost/', $result['base_url'], $message );
$this->assertEquals(
array(
'author_id' => 2,
'author_login' => 'john',
'author_email' => 'johndoe@example.org',
'author_display_name' => 'John Doe',
'author_first_name' => 'John',
'author_last_name' => 'Doe',
),
$result['authors']['john'],
$message
);
$this->assertEquals(
array(
'term_id' => 3,
'category_nicename' => 'alpha',
'category_parent' => '',
'cat_name' => 'alpha',
'category_description' => 'The alpha category',
),
$result['categories'][0],
$message
);
$this->assertEquals(
array(
'term_id' => 22,
'tag_slug' => 'clippable',
'tag_name' => 'Clippable',
'tag_description' => 'The Clippable post_tag',
),
$result['tags'][0],
$message
);
$this->assertEquals(
array(
'term_id' => 40,
'term_taxonomy' => 'post_tax',
'slug' => 'bieup',
'term_parent' => '',
'term_name' => 'bieup',
'term_description' => 'The bieup post_tax',
),
$result['terms'][0],
$message
);
$this->assertCount( 2, $result['posts'], $message );
$this->assertCount( 19, $result['posts'][0], $message );
$this->assertCount( 18, $result['posts'][1], $message );
$this->assertEquals(
array(
array(
'name' => 'alpha',
'slug' => 'alpha',
'domain' => 'category',
),
array(
'name' => 'Clippable',
'slug' => 'clippable',
'domain' => 'post_tag',
),
array(
'name' => 'bieup',
'slug' => 'bieup',
'domain' => 'post_tax',
),
),
$result['posts'][0]['terms'],
$message
);
$this->assertSame(
array(
array(
'key' => '_wp_page_template',
'value' => 'default',
),
),
$result['posts'][1]['postmeta'],
$message
);
}
}
function test_wxr_version_1_0() {
$file = DIR_TESTDATA . '/export/valid-wxr-1.0.xml';
foreach ( array( 'WXR_Parser_SimpleXML', 'WXR_Parser_XML', 'WXR_Parser_Regex' ) as $p ) {
$message = $p . ' failed';
$parser = new $p;
$result = $parser->parse( $file );
$this->assertIsArray( $result, $message );
$this->assertSame( 'http://localhost/', $result['base_url'], $message );
$this->assertSame( $result['categories'][0]['category_nicename'], 'alpha', $message );
$this->assertSame( $result['categories'][0]['cat_name'], 'alpha', $message );
$this->assertSame( $result['categories'][0]['category_parent'], '', $message );
$this->assertSame( $result['categories'][0]['category_description'], 'The alpha category', $message );
$this->assertSame( $result['tags'][0]['tag_slug'], 'chicken', $message );
$this->assertSame( $result['tags'][0]['tag_name'], 'chicken', $message );
$this->assertCount( 6, $result['posts'], $message );
$this->assertCount( 19, $result['posts'][0], $message );
$this->assertCount( 18, $result['posts'][1], $message );
$this->assertEquals(
array(
array(
'name' => 'Uncategorized',
'slug' => 'uncategorized',
'domain' => 'category',
),
),
$result['posts'][0]['terms'],
$message
);
$this->assertEquals(
array(
array(
'name' => 'alpha',
'slug' => 'alpha',
'domain' => 'category',
),
array(
'name' => 'news',
'slug' => 'news',
'domain' => 'tag',
),
array(
'name' => 'roar',
'slug' => 'roar',
'domain' => 'tag',
),
),
$result['posts'][2]['terms'],
$message
);
$this->assertEquals(
array(
array(
'name' => 'chicken',
'slug' => 'chicken',
'domain' => 'tag',
),
array(
'name' => 'child',
'slug' => 'child',
'domain' => 'category',
),
array(
'name' => 'face',
'slug' => 'face',
'domain' => 'tag',
),
),
$result['posts'][3]['terms'],
$message
);
$this->assertSame(
array(
array(
'key' => '_wp_page_template',
'value' => 'default',
),
),
$result['posts'][1]['postmeta'],
$message
);
}
}
/**
* Test the WXR parser's ability to correctly retrieve content from CDATA
* sections that contain escaped closing tags ("]]>" -> "]]]]><![CDATA[>").
*
* @link https://core.trac.wordpress.org/ticket/15203
*/
function test_escaped_cdata_closing_sequence() {
$file = DIR_TESTDATA . '/export/crazy-cdata-escaped.xml';
foreach ( array( 'WXR_Parser_SimpleXML', 'WXR_Parser_XML', 'WXR_Parser_Regex' ) as $p ) {
$message = 'Parser ' . $p;
$parser = new $p;
$result = $parser->parse( $file );
$post = $result['posts'][0];
$this->assertSame( 'Content with nested <![CDATA[ tags ]]> :)', $post['post_content'], $message );
foreach ( $post['postmeta'] as $meta ) {
switch ( $meta['key'] ) {
case 'Plain string':
$value = 'Foo';
break;
case 'Closing CDATA':
$value = ']]>';
break;
case 'Alot of CDATA':
$value = 'This has <![CDATA[ opening and ]]> closing <![CDATA[ tags like this: ]]>';
break;
default:
$this->fail( sprintf( 'Unknown postmeta (%1$s) was parsed out by %2$s.', $meta['key'], $p ) );
}
$this->assertSame( $value, $meta['value'], $message );
}
}
}
/**
* Ensure that the regex parser can still parse invalid CDATA blocks (i.e. those
* with "]]>" unescaped within a CDATA section).
*/
function test_unescaped_cdata_closing_sequence() {
$file = DIR_TESTDATA . '/export/crazy-cdata.xml';
$parser = new WXR_Parser_Regex;
$result = $parser->parse( $file );
$post = $result['posts'][0];
$this->assertSame( 'Content with nested <![CDATA[ tags ]]> :)', $post['post_content'] );
foreach ( $post['postmeta'] as $meta ) {
switch ( $meta['key'] ) {
case 'Plain string':
$value = 'Foo';
break;
case 'Closing CDATA':
$value = ']]>';
break;
case 'Alot of CDATA':
$value = 'This has <![CDATA[ opening and ]]> closing <![CDATA[ tags like this: ]]>';
break;
default:
$this->fail( sprintf( 'Unknown postmeta (%1$s) was parsed out by %2$s.', $meta['key'], $p ) );
}
$this->assertSame( $value, $meta['value'] );
}
}
// Tags in CDATA #11574.
}