From 27b29f0c779b40ea634a62625c42e8a7a7c1cc93 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:05:43 +1100 Subject: [PATCH] Tests for the internal hooks functions. --- tests/php/HooksTest.php | 235 +++++++++++++++++++++++++++++++++++ tests/php/bootstrap.php.dist | 1 + 2 files changed, 236 insertions(+) create mode 100644 tests/php/HooksTest.php diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php new file mode 100644 index 000000000..c62c1517a --- /dev/null +++ b/tests/php/HooksTest.php @@ -0,0 +1,235 @@ + 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 ); + } +} diff --git a/tests/php/bootstrap.php.dist b/tests/php/bootstrap.php.dist index 36c49c7e1..371de0f5f 100644 --- a/tests/php/bootstrap.php.dist +++ b/tests/php/bootstrap.php.dist @@ -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' );