From 42e4ba90565814875a598c2372d2d207e9e1d431 Mon Sep 17 00:00:00 2001 From: Enrico Battocchi Date: Wed, 27 Jan 2021 16:21:27 +0100 Subject: [PATCH] Do not fill the original post URL if the post is not a Rewrite & Republish copy --- src/ui/class-block-editor.php | 2 +- tests/ui/class-block-editor-test.php | 38 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/ui/class-block-editor.php b/src/ui/class-block-editor.php index 5d63b879f..864d8b44f 100644 --- a/src/ui/class-block-editor.php +++ b/src/ui/class-block-editor.php @@ -199,7 +199,7 @@ public function get_check_permalink() { public function get_original_post_edit_url() { $post = \get_post(); - if ( ! $post instanceof WP_Post ) { + if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) { return ''; } diff --git a/tests/ui/class-block-editor-test.php b/tests/ui/class-block-editor-test.php index e97b8a647..8b9257c92 100644 --- a/tests/ui/class-block-editor-test.php +++ b/tests/ui/class-block-editor-test.php @@ -706,6 +706,11 @@ public function test_get_original_post_edit_url_successful() { Monkey\Functions\expect( '\get_post' ) ->andReturn( $post ); + $this->permissions_helper + ->expects( 'is_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnTrue(); + $utils ->expects( 'get_original_post_id' ) ->with( $post->ID ) @@ -739,6 +744,34 @@ function ( $array, $string ) { ); } + /** + * Tests the unsuccessful get_original_post_edit_url when the post is not a Rewrite & Republish copy. + * + * @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::get_original_post_edit_url + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_get_original_post_edit_url_not_rewrite_and_republish() { + $utils = Mockery::mock( 'alias:\Yoast\WP\Duplicate_Post\Utils' ); + $post = Mockery::mock( \WP_Post::class ); + $post->ID = 128; + $original_id = 64; + $nonce = '12345678'; + + Monkey\Functions\expect( '\get_post' ) + ->andReturn( $post ); + + $this->permissions_helper + ->expects( 'is_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnFalse(); + + $this->assertSame( + '', + $this->instance->get_original_post_edit_url() + ); + } + /** * Tests the get_original_post_edit_url when there is no post. * @@ -770,6 +803,11 @@ public function test_get_original_post_edit_url_no_original() { Monkey\Functions\expect( '\get_post' ) ->andReturn( $post ); + $this->permissions_helper + ->expects( 'is_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnTrue(); + $utils ->expects( 'get_original_post_id' ) ->with( $post->ID )