Skip to content

Commit 9d19eb9

Browse files
committed
Add unit test
1 parent b11fe9b commit 9d19eb9

File tree

2 files changed

+114
-39
lines changed

2 files changed

+114
-39
lines changed

packages/playground/data-liberation/src/import/WP_Entity_Importer.php

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ public function import_term( $data ) {
276276
}
277277

278278
$original_id = isset( $data['id'] ) ? (int) $data['id'] : 0;
279-
$parent = isset( $data['parent'] ) ? $data['parent'] : null;
279+
$parent_id = isset( $data['parent'] ) ? (int) $data['parent'] : 0;
280+
280281
$mapping_key = sha1( $data['taxonomy'] . ':' . $data['slug'] );
281282
$existing = $this->term_exists( $data );
282283
if ( $existing ) {
@@ -306,11 +307,11 @@ public function import_term( $data ) {
306307
'parent' => true,
307308
);
308309

309-
// Map the parent term, or mark it as one we need to fix
310-
if ( $parent ) {
311-
// TODO: add parent mapping and remapping
312-
// $requires_remapping = false;
313-
/*if ( isset( $this->mapping['term'][ $parent_id ] ) ) {
310+
// Map the parent comment, or mark it as one we need to fix
311+
// TODO: add parent mapping and remapping
312+
/*$requires_remapping = false;
313+
if ( $parent_id ) {
314+
if ( isset( $this->mapping['term'][ $parent_id ] ) ) {
314315
$data['parent'] = $this->mapping['term'][ $parent_id ];
315316
} else {
316317
// Prepare for remapping later
@@ -319,30 +320,9 @@ public function import_term( $data ) {
319320
320321
// Wipe the parent for now
321322
$data['parent'] = 0;
322-
}*/
323-
$parent_term = term_exists( $parent, $data['taxonomy'] );
324-
325-
if ( $parent_term ) {
326-
$data['parent'] = $parent_term['term_id'];
327-
} else {
328-
// It can happens that the parent term is not imported yet in manually created WXR files.
329-
$parent_term = wp_insert_term( $parent, $data['taxonomy'] );
330-
331-
if ( is_wp_error( $parent_term ) ) {
332-
$this->logger->error(
333-
sprintf(
334-
/* translators: %s: taxonomy name */
335-
__( 'Failed to import parent term for "%s"', 'wordpress-importer' ),
336-
$data['taxonomy']
337-
)
338-
);
339-
} else {
340-
$data['parent'] = $parent_term['term_id'];
341-
}
342323
}
343-
}
324+
}*/
344325

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

354-
$term = term_exists( $data['slug'], $data['taxonomy'] );
355-
$result = null;
356-
357-
if ( is_array( $term ) ) {
358-
// Update the existing term.
359-
$result = wp_update_term( $term['term_id'], $data['taxonomy'], $termdata );
360-
} else {
361-
// Create a new term.
362-
$result = wp_insert_term( $data['name'], $data['taxonomy'], $termdata );
363-
}
364-
334+
$result = wp_insert_term( $data['name'], $data['taxonomy'], $termdata );
365335
if ( is_wp_error( $result ) ) {
366336
$this->logger->warning(
367337
sprintf(

packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,111 @@ public function test_small_import() {
9393
$this->assertEquals( 0, (int) $count );
9494
}
9595

96+
public function test_small_import_right_order_of_import() {
97+
global $wpdb;
98+
99+
$file_path = __DIR__ . '/wxr/small-export.xml';
100+
$importer = $this->import_wxr_file( $file_path );
101+
$count = 0;
102+
$imported_ids = array(
103+
'category' => array(),
104+
'post' => array(),
105+
'post_tag' => array(),
106+
'unknown' => array(),
107+
);
108+
$expected_ids = array(
109+
'category' => array(
110+
'alpha',
111+
'bar',
112+
'beta',
113+
'chi',
114+
'delta',
115+
'epsilon',
116+
'eta',
117+
'foo',
118+
'foo-bar',
119+
'gamma',
120+
'iota',
121+
'kappa',
122+
'lambda',
123+
'mu',
124+
'nu',
125+
'omega',
126+
'omicron',
127+
'phi',
128+
'pi',
129+
'psi',
130+
'rho',
131+
'sigma',
132+
'tau',
133+
'theta',
134+
'uncategorized',
135+
'unused-category',
136+
'upsilon',
137+
'xi',
138+
'zeta',
139+
'eternity',
140+
),
141+
'post' => array(
142+
'http://127.0.0.1:9400/?p=1',
143+
'http://127.0.0.1:9400/?page_id=2',
144+
'http://127.0.0.1:9400/?page_id=4',
145+
'http://127.0.0.1:9400/?page_id=6',
146+
'http://127.0.0.1:9400/?page_id=9',
147+
'http://127.0.0.1:9400/?page_id=11',
148+
'http://127.0.0.1:9400/?p=13',
149+
'http://127.0.0.1:9400/?p=15',
150+
'http://127.0.0.1:9400/?p=17',
151+
'http://127.0.0.1:9400/?p=19',
152+
'http://127.0.0.1:9400/?p=22',
153+
),
154+
'post_tag' => array(
155+
'tag1',
156+
'tag2',
157+
'tag3',
158+
),
159+
'unknown' => array(),
160+
);
161+
162+
$import_fn = function ( $data, $id = null ) use ( &$imported_ids, &$count ) {
163+
if ( array_key_exists( 'post_id', $data ) ) {
164+
$imported_ids['post'][] = $data['guid'];
165+
} elseif ( array_key_exists( 'taxonomy', $data ) ) {
166+
$imported_ids[ $data['taxonomy'] ][] = $data['slug'];
167+
} else {
168+
$imported_ids['unknown'][] = $data;
169+
}
170+
171+
++$count;
172+
173+
return $data;
174+
};
175+
176+
add_filter( 'wxr_importer_pre_process_post', $import_fn, 10, 2 );
177+
add_filter( 'wxr_importer_pre_process_term', $import_fn );
178+
179+
do {
180+
while ( $importer->next_step() ) {
181+
// noop
182+
}
183+
} while ( $importer->advance_to_next_stage() );
184+
185+
$this->assertEquals( $expected_ids, $imported_ids );
186+
187+
$categories = get_terms(array(
188+
'taxonomy' => 'category',
189+
'hide_empty' => false,
190+
));
191+
192+
$this->assertEquals( $expected_ids['category'], $imported_ids['category'] );
193+
// $this->assertEquals( 1, 2 );
194+
195+
remove_filter( 'wxr_importer_pre_process_post', $import_fn );
196+
remove_filter( 'wxr_importer_pre_process_term', $import_fn );
197+
198+
$this->assertEquals( 44, $count );
199+
}
200+
96201
private function small_import_counts() {
97202
$types = WP_WXR_Sorted_Reader::ENTITY_TYPES;
98203

0 commit comments

Comments
 (0)