Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zaerl committed Jan 8, 2025
1 parent b11fe9b commit 9d19eb9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public function import_term( $data ) {
}

$original_id = isset( $data['id'] ) ? (int) $data['id'] : 0;
$parent = isset( $data['parent'] ) ? $data['parent'] : null;
$parent_id = isset( $data['parent'] ) ? (int) $data['parent'] : 0;

$mapping_key = sha1( $data['taxonomy'] . ':' . $data['slug'] );
$existing = $this->term_exists( $data );
if ( $existing ) {
Expand Down Expand Up @@ -306,11 +307,11 @@ public function import_term( $data ) {
'parent' => true,
);

// Map the parent term, or mark it as one we need to fix
if ( $parent ) {
// TODO: add parent mapping and remapping
// $requires_remapping = false;
/*if ( isset( $this->mapping['term'][ $parent_id ] ) ) {
// Map the parent comment, or mark it as one we need to fix
// TODO: add parent mapping and remapping
/*$requires_remapping = false;
if ( $parent_id ) {
if ( isset( $this->mapping['term'][ $parent_id ] ) ) {
$data['parent'] = $this->mapping['term'][ $parent_id ];
} else {
// Prepare for remapping later
Expand All @@ -319,30 +320,9 @@ public function import_term( $data ) {
// Wipe the parent for now
$data['parent'] = 0;
}*/
$parent_term = term_exists( $parent, $data['taxonomy'] );

if ( $parent_term ) {
$data['parent'] = $parent_term['term_id'];
} else {
// It can happens that the parent term is not imported yet in manually created WXR files.
$parent_term = wp_insert_term( $parent, $data['taxonomy'] );

if ( is_wp_error( $parent_term ) ) {
$this->logger->error(
sprintf(
/* translators: %s: taxonomy name */
__( 'Failed to import parent term for "%s"', 'wordpress-importer' ),
$data['taxonomy']
)
);
} else {
$data['parent'] = $parent_term['term_id'];
}
}
}
}*/

// Filter the term data to only include allowed keys.
foreach ( $data as $key => $value ) {
if ( ! isset( $allowed[ $key ] ) ) {
continue;
Expand All @@ -351,17 +331,7 @@ public function import_term( $data ) {
$termdata[ $key ] = $data[ $key ];
}

$term = term_exists( $data['slug'], $data['taxonomy'] );
$result = null;

if ( is_array( $term ) ) {
// Update the existing term.
$result = wp_update_term( $term['term_id'], $data['taxonomy'], $termdata );
} else {
// Create a new term.
$result = wp_insert_term( $data['name'], $data['taxonomy'], $termdata );
}

$result = wp_insert_term( $data['name'], $data['taxonomy'], $termdata );
if ( is_wp_error( $result ) ) {
$this->logger->warning(
sprintf(
Expand Down
105 changes: 105 additions & 0 deletions packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,111 @@ public function test_small_import() {
$this->assertEquals( 0, (int) $count );
}

public function test_small_import_right_order_of_import() {
global $wpdb;

$file_path = __DIR__ . '/wxr/small-export.xml';
$importer = $this->import_wxr_file( $file_path );
$count = 0;
$imported_ids = array(
'category' => array(),
'post' => array(),
'post_tag' => array(),
'unknown' => array(),
);
$expected_ids = array(
'category' => array(
'alpha',
'bar',
'beta',
'chi',
'delta',
'epsilon',
'eta',
'foo',
'foo-bar',
'gamma',
'iota',
'kappa',
'lambda',
'mu',
'nu',
'omega',
'omicron',
'phi',
'pi',
'psi',
'rho',
'sigma',
'tau',
'theta',
'uncategorized',
'unused-category',
'upsilon',
'xi',
'zeta',
'eternity',
),
'post' => array(
'http://127.0.0.1:9400/?p=1',
'http://127.0.0.1:9400/?page_id=2',
'http://127.0.0.1:9400/?page_id=4',
'http://127.0.0.1:9400/?page_id=6',
'http://127.0.0.1:9400/?page_id=9',
'http://127.0.0.1:9400/?page_id=11',
'http://127.0.0.1:9400/?p=13',
'http://127.0.0.1:9400/?p=15',
'http://127.0.0.1:9400/?p=17',
'http://127.0.0.1:9400/?p=19',
'http://127.0.0.1:9400/?p=22',
),
'post_tag' => array(
'tag1',
'tag2',
'tag3',
),
'unknown' => array(),
);

$import_fn = function ( $data, $id = null ) use ( &$imported_ids, &$count ) {
if ( array_key_exists( 'post_id', $data ) ) {
$imported_ids['post'][] = $data['guid'];
} elseif ( array_key_exists( 'taxonomy', $data ) ) {
$imported_ids[ $data['taxonomy'] ][] = $data['slug'];
} else {
$imported_ids['unknown'][] = $data;
}

++$count;

return $data;
};

add_filter( 'wxr_importer_pre_process_post', $import_fn, 10, 2 );
add_filter( 'wxr_importer_pre_process_term', $import_fn );

do {
while ( $importer->next_step() ) {
// noop
}
} while ( $importer->advance_to_next_stage() );

$this->assertEquals( $expected_ids, $imported_ids );

$categories = get_terms(array(
'taxonomy' => 'category',
'hide_empty' => false,
));

$this->assertEquals( $expected_ids['category'], $imported_ids['category'] );
// $this->assertEquals( 1, 2 );

remove_filter( 'wxr_importer_pre_process_post', $import_fn );
remove_filter( 'wxr_importer_pre_process_term', $import_fn );

$this->assertEquals( 44, $count );
}

private function small_import_counts() {
$types = WP_WXR_Sorted_Reader::ENTITY_TYPES;

Expand Down

0 comments on commit 9d19eb9

Please sign in to comment.