From 5b3f83a417e1a3ed1989ebd214d93ae05aa778f2 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Aug 2026 18:25:40 +0400 Subject: [PATCH 1/2] Add failing tests for relationship name-to-post resolution --- tests/phpunit/tests/relationships.php | 125 ++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/phpunit/tests/relationships.php diff --git a/tests/phpunit/tests/relationships.php b/tests/phpunit/tests/relationships.php new file mode 100644 index 0000000..b33d28f --- /dev/null +++ b/tests/phpunit/tests/relationships.php @@ -0,0 +1,125 @@ +relationships = new \WP_Parser\Relationships; + } + + /** + * Test that a fully qualified name resolves in the global scope only. + */ + public function test_names_to_slugs_fully_qualified() { + + $this->assertSame( + array( 'foo-bar' ) + , $this->relationships->names_to_slugs( '\Foo\bar', 'Baz' ) + ); + } + + /** + * Test that an unqualified name resolves in the namespace, then globally. + */ + public function test_names_to_slugs_unqualified() { + + $this->assertSame( + array( 'baz-foo-bar', 'foo-bar' ) + , $this->relationships->names_to_slugs( 'Foo\bar', 'Baz' ) + ); + } + + /** + * Test that only the first matching scope for a name produces an ID. + */ + public function test_get_ids_for_slugs_first_matching_scope_wins() { + + $this->assertSame( + array( 'baz-foo' => 5 ) + , $this->relationships->get_ids_for_slugs( + array( array( 'baz-foo', 'foo' ) ) + , array( + 'baz-foo' => 5, + 'foo' => 7, + ) + ) + ); + } + + /** + * Test that a slug with no ID in any scope produces no ID. + */ + public function test_get_ids_for_slugs_unknown_slug_is_ignored() { + + $this->assertSame( + array() + , $this->relationships->get_ids_for_slugs( + array( array( 'baz-foo', 'foo' ) ) + , array( 'quux' => 9 ) + ) + ); + } + + /** + * Test that an empty slug map produces no connections at all. + */ + public function test_ending_import_with_empty_slug_map_makes_no_connections() { + + global $wpdb; + + $this->relationships->wp_parser_starting_import(); + + $from_id = self::factory()->post->create( + array( 'post_type' => 'wp-parser-function' ) + ); + + $this->relationships->slugs_to_ids = array(); + $this->relationships->relationships = array( + 'wp-parser-function' => array( + $from_id => array( + 'wp-parser-function' => array( array( 'baz-foo', 'foo' ) ), + ), + ), + ); + + $this->relationships->wp_parser_ending_import(); + + // The unresolvable slugs must not survive as raw candidate arrays. + $this->assertSame( + array() + , $this->relationships->relationships['wp-parser-function'][ $from_id ]['wp-parser-function'] + ); + + $connections = $wpdb->get_results( + $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}p2p WHERE p2p_from = %d" + , $from_id + ) + ); + + $this->assertSame( array(), $connections ); + } +} From 5f4e55aee203c9f16e50a758501508b406769ebb Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Aug 2026 18:25:40 +0400 Subject: [PATCH 2/2] Fix relationship resolution: qualified names, scope precedence, empty slug maps --- lib/class-relationships.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/class-relationships.php b/lib/class-relationships.php index ad29e18..4ff46e7 100644 --- a/lib/class-relationships.php +++ b/lib/class-relationships.php @@ -274,6 +274,10 @@ public function wp_parser_ending_import() { // Convert slugs to IDs. if ( empty( $this->slugs_to_ids[ $to_type ] ) ) { // TODO why might this be empty? test class-IXR.php + // No posts of this type exist; without a slug map the + // raw slug candidates must not reach the connection + // loop, where they would be coerced to post ID 1. + $this->relationships[ $from_type ][ $from_id ][ $to_type ] = array(); continue; } @@ -385,7 +389,7 @@ public function wp_parser_ending_import() { * namespace, and falling back to the global namespace. */ public function names_to_slugs( $name, $namespace = null ) { - $fully_qualified = ( 0 === strpos( '\\', $name ) ); + $fully_qualified = ( 0 === strpos( $name, '\\' ) ); $name = ltrim( $name, '\\' ); $names = array(); @@ -426,7 +430,7 @@ public function get_ids_for_slugs( array $slugs, array $slugs_to_ids ) { if ( array_key_exists( $slug, $slugs_to_ids ) ) { $slugs_with_ids[ $slug ] = $slugs_to_ids[ $slug ]; // if we found it in this scope, stop searching the chain. - continue; + break; } } }