Skip to content

Commit

Permalink
Tests for the internal hooks functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Dec 20, 2022
1 parent fc19f05 commit 27b29f0
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
235 changes: 235 additions & 0 deletions tests/php/HooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<?php

namespace Distributor;

use Distributor\Hooks;
use WP_Mock\Tools\TestCase;

class HooksTest extends TestCase {

/**
* Set up with WP_Mock
*
* Set up common mocks required for multiple tests.
*
* @since x.x.x
*/
public function setUp(): void {
parent::setUp();


// Return voids.
\WP_Mock::userFunction( '_prime_post_caches' );
\WP_Mock::userFunction( 'update_object_term_cache' );
\WP_Mock::userFunction( 'update_postmeta_cache' );
}

/**
* Helper function to mock get_post.
*/
public function setup_post_mock( $post_overrides = array() ) {
$defaults = array(
'ID' => 1,
'post_title' => 'Test Post',
'post_content' => 'Test Content',
'post_excerpt' => 'Test Excerpt',
'post_status' => 'publish',
'post_type' => 'post',
'post_author' => 1,
'post_date' => '2020-01-01 00:00:00',
'post_date_gmt' => '2020-01-01 00:00:00',
'post_modified' => '2020-01-01 00:00:00',
'post_modified_gmt' => '2020-01-01 00:00:00',
'post_parent' => 0,
'post_mime_type' => '',
'comment_count' => 0,
'comment_status' => 'open',
'ping_status' => 'open',
'guid' => 'http://example.org/?p=1',
'menu_order' => 0,
'pinged' => '',
'to_ping' => '',
'post_password' => '',
'post_name' => 'test-post',
'post_content_filtered' => '',
);

$post = array_merge( $defaults, $post_overrides );


\WP_Mock::userFunction(
'get_post',
array(
'return' => (object) $post,
)
);
}

/**
* Helper function to mock get_post_meta.
*/
public function setup_post_meta_mock( $post_meta ) {
$get_post_meta = function( $post_id, $key = '', $single = false ) use ( $post_meta ) {
if ( empty( $key ) ) {
return $post_meta;
}

if ( isset( $post_meta[ $key ] ) ) {
if ( $single ) {
return $post_meta[ $key ][0];
}
return $post_meta[ $key ];
}

return '';
};

\WP_Mock::userFunction(
'get_post_meta',
array(
'return' => $get_post_meta,
)
);
}

/**
* Test get_canonical_url
*
* @since x.x.x
* @runInSeparateProcess
*/
public function test_get_canonical_url() {
$this->setup_post_mock();
$this->setup_post_meta_mock(array());

\WP_Mock::userFunction(
'get_permalink',
array(
'return' => 'https://example.com/?p=1',
)
);

\WP_Mock::userFunction(
'is_singular',
array(
'return' => true,
)
);

$actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) );
$this->assertSame( 'https://example.com/?p=1', $actual );
}

/**
* Test get_canonical_url
*
* @since x.x.x
* @runInSeparateProcess
*/
public function test_get_canonical_url_external_pushed() {
$this->setup_post_mock();
$this->setup_post_meta_mock(
array (
'dt_original_post_id' => array( '10' ),
'dt_original_site_name' => array( 'Test External, Pushed Origin' ),
'dt_original_site_url' => array( 'http://origin.example.org/' ),
'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ),
'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ),
'dt_syndicate_time' => array( '1670384223' ),
'dt_full_connection' => array( '1' ),
'dt_original_source_id' => array( '2' ),
)
);

\WP_Mock::userFunction(
'get_permalink',
array(
'return' => 'https://example.com/?p=1',
)
);

\WP_Mock::userFunction(
'is_singular',
array(
'return' => true,
)
);

$actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) );
$this->assertSame( 'http://origin.example.org/?p=10', $actual );
}

/**
* Test get_canonical_url
*
* @since x.x.x
* @runInSeparateProcess
*/
public function test_get_canonical_url_external_pulled() {
$this->setup_post_mock();
$this->setup_post_meta_mock(
array (
'dt_original_post_id' => array( '10' ),
'dt_original_site_name' => array( 'Test External, Pulled Origin' ),
'dt_original_site_url' => array( 'http://origin.example.org/' ),
'dt_original_post_url' => array( 'http://origin.example.org/?p=11' ),
'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ),
'dt_syndicate_time' => array( '1670384223' ),
'dt_full_connection' => array( '' ),
'dt_original_source_id' => array( '3' ),
)
);

\WP_Mock::userFunction(
'get_permalink',
array(
'return' => 'https://example.com/?p=1',
)
);

\WP_Mock::userFunction(
'is_singular',
array(
'return' => true,
)
);

$actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) );
$this->assertSame( 'http://origin.example.org/?p=11', $actual );
}

/**
* Test get_canonical_url
*
* @since x.x.x
* @runInSeparateProcess
*/
public function test_get_canonical_url_internal() {
$this->setup_post_mock();
$this->setup_post_meta_mock(
array (
'dt_original_post_id' => array( '10' ),
'dt_original_blog_id' => array( '2' ),
'dt_syndicate_time' => array ( '1670383190' ),
'dt_original_post_url' => array ( 'http://origin.example.org/?p=12' ),
)
);

\WP_Mock::userFunction(
'get_permalink',
array(
'return' => 'https://example.com/?p=1',
)
);

\WP_Mock::userFunction(
'is_singular',
array(
'return' => true,
)
);

$actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) );
$this->assertSame( 'http://origin.example.org/?p=12', $actual );
}
}
1 change: 1 addition & 0 deletions tests/php/bootstrap.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ WP_Mock::bootstrap();

require_once __DIR__ . '/includes/common.php';

require_once( __DIR__ . '/../../includes/hooks.php' );
require_once( __DIR__ . '/../../includes/utils.php' );
require_once( __DIR__ . '/../../includes/debug-info.php' );
require_once( __DIR__ . '/../../includes/subscriptions.php' );
Expand Down

0 comments on commit 27b29f0

Please sign in to comment.