Skip to content
179 changes: 179 additions & 0 deletions features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1402,3 +1402,182 @@ Feature: Export content.
"""
<wp:meta_value><![CDATA[term_metavalue]]></wp:meta_value>
"""

Scenario: Export attaches terms, meta and comments to the right posts across batches
Given a WP install
And I run `wp site empty --yes`
And I run `wp post generate --count=150`
And I run `wp post create --post_title='Last post' --post_status=publish --tags_input='Zoo,apple,beta' --porcelain`
And save STDOUT as {LAST_POST_ID}
And I run `wp term create category Zoo --slug=zoo-category`
And I run `wp post term add {LAST_POST_ID} category zoo-category`
And I run `wp post meta add {LAST_POST_ID} my_meta_key my_meta_value`
And I run `wp post meta add {LAST_POST_ID} _edit_lock 123:1`
And I run `wp comment create --comment_post_ID={LAST_POST_ID} --comment_content='Approved comment' --comment_approved=1 --porcelain`
And save STDOUT as {COMMENT_ID}
And I run `wp comment meta add {COMMENT_ID} my_comment_meta_key my_comment_meta_value`
And I run `wp comment create --comment_post_ID={LAST_POST_ID} --comment_content='Spam comment' --comment_approved=spam`
And a wp-content/mu-plugins/skip-meta.php file:
"""
<?php
// Only export meta while the post it belongs to is the current post.
add_filter(
'wxr_export_skip_postmeta',
function ( $skip, $meta_key, $meta ) {
return $skip || (int) $meta->post_id !== get_the_ID();
},
10,
3
);
"""
And a check-export.php file:
"""
<?php
$xml = simplexml_load_file( $args[0] );
$found = 0;
foreach ( $xml->channel->item as $item ) {
$wp = $item->children( 'http://wordpress.org/export/1.2/' );
$tags = array();
$terms = array();
foreach ( $item->category as $category ) {
if ( 'post_tag' === (string) $category['domain'] ) {
$tags[] = (string) $category['nicename'];
}
$terms[] = (string) $category;
}
$meta = array();
foreach ( $wp->postmeta as $postmeta ) {
if ( in_array( (string) $postmeta->meta_key, array( '_pingme', '_encloseme' ), true ) ) {
continue;
}
$meta[] = (string) $postmeta->meta_key . '=' . (string) $postmeta->meta_value;
}
$comments = array();
foreach ( $wp->comment as $comment ) {
$comment_meta = array();
foreach ( $comment->commentmeta as $commentmeta ) {
$comment_meta[] = (string) $commentmeta->meta_key . '=' . (string) $commentmeta->meta_value;
}
$comments[] = (string) $comment->comment_content . ' [' . implode( ',', $comment_meta ) . ']';
}
if ( (string) $wp->post_id === '{LAST_POST_ID}' ) {
++$found;
// The terms should be in the same order as the database returns them for the post. Compare
// their names, as the database can return terms with the same name in any order.
$expected = wp_list_pluck( wp_get_object_terms( (int) $wp->post_id, get_object_taxonomies( 'post' ) ), 'name' );
echo 'terms in database order: ' . ( $terms === $expected ? 'yes' : 'no' ) . "\n";
sort( $tags );
echo 'tags: ' . implode( ',', $tags ) . "\n";
echo 'meta: ' . implode( ',', $meta ) . "\n";
echo 'comments: ' . implode( ',', $comments ) . "\n";
} elseif ( $tags || $meta || $comments || array( 'Uncategorized' ) !== $terms ) {
echo 'unexpected data on post ' . $wp->post_id . "\n";
}
}
echo "found: $found\n";
"""

When I run `wp export --post_type=post`
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}

When I run `wp eval-file check-export.php {EXPORT_FILE}`
Then STDOUT should be:
"""
terms in database order: yes
tags: apple,beta,zoo
meta: my_meta_key=my_meta_value
comments: Approved comment [my_comment_meta_key=my_comment_meta_value]
found: 1
"""

# Duplicate IDs in --post__in leave gaps in the keys of the list of post IDs.
When I run `wp post list --post_type=post --orderby=ID --order=ASC --format=ids`
Then save STDOUT as {POST_IDS}

When I run `wp eval 'echo implode( ",", array_merge( ...array_map( function ( $id ) { return array( $id, $id ); }, explode( " ", "{POST_IDS}" ) ) ) );'`
Then save STDOUT as {DUPLICATED_POST_IDS}

Given a wp-content/mu-plugins/count-batches.php file:
"""
<?php
// Count the queries that load the post meta of a batch of posts.
$GLOBALS['batch_loads'] = 0;
add_filter(
'query',
function ( $query ) {
if ( false !== strpos( $query, 'postmeta WHERE post_id IN (' ) ) {
++$GLOBALS['batch_loads'];
}
return $query;
}
);
register_shutdown_function(
function () {
if ( $GLOBALS['batch_loads'] ) {
file_put_contents( ABSPATH . 'batch-loads.txt', $GLOBALS['batch_loads'] );
}
}
);
"""

When I run `wp export --post__in={DUPLICATED_POST_IDS} --filename_format=duplicated.xml`
Then save STDOUT 'Writing to file %s' as {DUPLICATED_EXPORT_FILE}
And the batch-loads.txt file should be:
"""
2
"""

When I run `wp eval-file check-export.php {DUPLICATED_EXPORT_FILE}`
Then STDOUT should be:
"""
terms in database order: yes
tags: apple,beta,zoo
meta: my_meta_key=my_meta_value
comments: Approved comment [my_comment_meta_key=my_comment_meta_value]
found: 1
"""

Scenario: Export the terms of taxonomies registered with arguments for querying terms
Given a WP install
And a wp-content/mu-plugins/taxonomies.php file:
"""
<?php
// These arguments override the ones passed to wp_get_object_terms().
add_action(
'init',
function () {
register_taxonomy( 'limited', 'post', array( 'args' => array( 'number' => 1 ) ) );
register_taxonomy( 'plain', 'post', array( 'args' => array( 'fields' => 'all' ) ) );
}
);
"""
And I run `wp term create limited Alpha`
And I run `wp term create limited Beta`
And I run `wp term create plain Gamma`
And I run `wp term create plain Delta`
And I run `wp post create --post_title='First post' --post_status=publish --porcelain`
And save STDOUT as {FIRST_POST_ID}
And I run `wp post term add {FIRST_POST_ID} limited alpha`
And I run `wp post term add {FIRST_POST_ID} plain gamma`
And I run `wp post create --post_title='Second post' --post_status=publish --porcelain`
And save STDOUT as {SECOND_POST_ID}
And I run `wp post term add {SECOND_POST_ID} limited beta`
And I run `wp post term add {SECOND_POST_ID} plain delta`

When I run `wp export --post_type=post --stdout`
Then STDOUT should contain:
"""
<category domain="limited" nicename="alpha"><![CDATA[Alpha]]></category>
"""
And STDOUT should contain:
"""
<category domain="limited" nicename="beta"><![CDATA[Beta]]></category>
"""
And STDOUT should contain:
"""
<category domain="plain" nicename="gamma"><![CDATA[Gamma]]></category>
"""
And STDOUT should contain:
"""
<category domain="plain" nicename="delta"><![CDATA[Delta]]></category>
"""
Loading