Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/class-relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
}
}
Expand Down
125 changes: 125 additions & 0 deletions tests/phpunit/tests/relationships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/**
* A test case for mapping item names to related posts.
*/

namespace WP_Parser\Tests;

/**
* Test that item names are mapped to related posts correctly.
*
* @group relationships
*/
class Relationships_Test extends \WP_UnitTestCase {

/**
* The relationships instance under test.
*
* @var \WP_Parser\Relationships
*/
protected $relationships;

/**
* Set up before each test.
*/
public function set_up() {

parent::set_up();

$this->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 );
}
}