Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

38197 added unit tests for pingback #6436

Closed
15 changes: 13 additions & 2 deletions src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,7 @@ function discover_pingback_server_uri( $url, $deprecated = '' ) {

$pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote );
$pingback_link_offset_squote = strpos( $contents, $pingback_str_squote );

if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
$quote = ( $pingback_link_offset_dquote ) ? '"' : '\'';
$pingback_link_offset = ( '"' === $quote ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
Expand Down Expand Up @@ -3079,9 +3080,11 @@ function generic_ping( $post_id = 0 ) {
*
* @since 0.71
* @since 4.7.0 `$post` can be a WP_Post object.
* @since 6.8.0 Returns an array of pingback statuses indexed by link.
*
* @param string $content Post content to check for links. If empty will retrieve from post.
* @param int|WP_Post $post Post ID or object.
* @return array<string, bool> An array of pingback statuses indexed by link.
*/
function pingback( $content, $post ) {
require_once ABSPATH . WPINC . '/class-IXR.php';
Expand All @@ -3093,7 +3096,7 @@ function pingback( $content, $post ) {
$post = get_post( $post );

if ( ! $post ) {
return;
return array();
}

$pung = get_pung( $post );
Expand All @@ -3108,6 +3111,7 @@ function pingback( $content, $post ) {
*/
$post_links_temp = wp_extract_urls( $content );

$ping_status = array();
/*
* Step 2.
* Walking through the links array.
Expand Down Expand Up @@ -3179,11 +3183,18 @@ function pingback( $content, $post ) {
// When set to true, this outputs debug messages by itself.
$client->debug = false;

if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered.
$status = $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto );

if ( $status // Ping registered.
|| ( isset( $client->error->code ) && 48 === $client->error->code ) // Already registered.
) {
add_ping( $post, $pagelinkedto );
}
$ping_status[ $pagelinkedto ] = $status;
}
}

return $ping_status;
}

/**
Expand Down
92 changes: 92 additions & 0 deletions tests/phpunit/tests/comment/pingback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

// Test the output of Comment Querying functions.

/**
* @group comment
*/
class Tests_Comment_Pingback extends WP_UnitTestCase {

protected static $post_id;
protected $response = array();

public function set_up() {
parent::set_up();

add_filter( 'pre_http_request', array( $this, 'request_response' ) );
}

public function tear_down() {
remove_filter( 'pre_http_request', array( $this, 'request_response' ) );
parent::tear_down();
}

public function test_pingback() {
$content = <<<HTML
<a href="http://example.org">test</a>
<a href="http://example1.org/test">test</a>
<a href="http://example3.org/">test</a>
HTML;

$body = <<<BODY
<a rel="pingback" href="https://example1.org/test/pingback">test</a>
BODY;

$this->response = array(
'body' => $body,
'response' => array( 'code' => 200 ),
);

self::$post_id = self::factory()->post->create(
array( 'post_content' => $content )
);

$post = get_post( self::$post_id );
$this->assertEquals( array( 'http://example1.org/test' => false ), pingback( $post->post_content, self::$post_id ) );
}

public function test_pingback_no_ping_back() {
$content = <<<HTML
<a href="http://example.org">test</a>
<a href="http://example1.org/test">test</a>
<a href="http://example3.org/">test</a>
HTML;

$body = <<<BODY
<a href="https://example1.org/test">test</a>
BODY;

$this->response = array(
'body' => $body,
'response' => array( 'code' => 200 ),
);

self::$post_id = self::factory()->post->create(
array( 'post_content' => $content )
);

$post = get_post( self::$post_id );
$this->assertEquals( array(), pingback( $post->post_content, self::$post_id ) );
}

public function test_pingback_error_response() {
$content = <<<HTML
<a href="http://example.org">test</a>
<a href="http://example1.org/test">test</a>
<a href="http://example3.org/">test</a>
HTML;

$this->response = new WP_Error();

self::$post_id = self::factory()->post->create(
array( 'post_content' => $content )
);

$post = get_post( self::$post_id );
$this->assertEquals( array(), pingback( $post->post_content, self::$post_id ) );
}

public function request_response() {
return $this->response;
}
}
Loading