From 066c4cea31a3c762e44786a82f15d1fb20f10bf7 Mon Sep 17 00:00:00 2001 From: Enrico Battocchi Date: Thu, 19 Sep 2024 23:43:18 +0200 Subject: [PATCH] Refactor the watcher (and their tests) to make some methods private --- src/watchers/copied-post-watcher.php | 87 +++++++------ src/watchers/original-post-watcher.php | 41 ++++-- src/watchers/republished-post-watcher.php | 41 ++++-- .../Watchers/Copied_Post_Watcher_Test.php | 120 ++++++++++-------- .../Watchers/Original_Post_Watcher_Test.php | 39 +----- .../Republished_Post_Watcher_Test.php | 33 +---- 6 files changed, 183 insertions(+), 178 deletions(-) diff --git a/src/watchers/copied-post-watcher.php b/src/watchers/copied-post-watcher.php index bd79131a..2bb9cf00 100644 --- a/src/watchers/copied-post-watcher.php +++ b/src/watchers/copied-post-watcher.php @@ -38,40 +38,6 @@ public function register_hooks() { \add_action( 'enqueue_block_editor_assets', [ $this, 'add_block_editor_notice' ], 11 ); } - /** - * Generates the translated text for the notice. - * - * @param WP_Post $post The current post object. - * - * @return string The translated text for the notice. - */ - public function get_notice_text( $post ) { - if ( $this->permissions_helper->has_trashed_rewrite_and_republish_copy( $post ) ) { - return \__( - 'You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate.', - 'duplicate-post' - ); - } - - $scheduled_copy = $this->permissions_helper->has_scheduled_rewrite_and_republish_copy( $post ); - if ( ! $scheduled_copy ) { - return \__( - 'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.', - 'duplicate-post' - ); - } - - return \sprintf( - /* translators: %1$s: scheduled date of the copy, %2$s: scheduled time of the copy. */ - \__( - 'A duplicate of this post was made, which is scheduled to replace this post on %1$s at %2$s.', - 'duplicate-post' - ), - \get_the_time( \get_option( 'date_format' ), $scheduled_copy ), - \get_the_time( \get_option( 'time_format' ), $scheduled_copy ) - ); - } - /** * Shows a notice on the Classic editor. * @@ -90,7 +56,7 @@ public function add_admin_notice() { if ( $this->permissions_helper->has_rewrite_and_republish_copy( $post ) ) { print '

' - . \esc_html( $this->get_notice_text( $post ) ) + . \esc_html( $this->get_notice_copy( $post ) ) . '

'; } } @@ -110,7 +76,7 @@ public function add_block_editor_notice() { if ( $this->permissions_helper->has_rewrite_and_republish_copy( $post ) ) { $notice = [ - 'text' => $this->get_notice_text( $post ), + 'text' => $this->get_notice_copy( $post ), 'status' => 'warning', 'isDismissible' => true, ]; @@ -122,4 +88,53 @@ public function add_block_editor_notice() { ); } } + + /** + * Generates the translated text for the notice. + * + * @param WP_Post $post The current post object. + * + * @return string The translated text for the notice. + */ + private function get_notice_copy( $post ) { + if ( $this->permissions_helper->has_trashed_rewrite_and_republish_copy( $post ) ) { + return \__( + 'You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate.', + 'duplicate-post' + ); + } + + $scheduled_copy = $this->permissions_helper->has_scheduled_rewrite_and_republish_copy( $post ); + if ( ! $scheduled_copy ) { + return \__( + 'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.', + 'duplicate-post' + ); + } + + return \sprintf( + /* translators: %1$s: scheduled date of the copy, %2$s: scheduled time of the copy. */ + \__( + 'A duplicate of this post was made, which is scheduled to replace this post on %1$s at %2$s.', + 'duplicate-post' + ), + \get_the_time( \get_option( 'date_format' ), $scheduled_copy ), + \get_the_time( \get_option( 'time_format' ), $scheduled_copy ) + ); + } + + /** + * Generates the translated text for the republished notice. + * + * @deprecated 4.6 + * @codeCoverageIgnore + * + * @param WP_Post $post The current post object. + * + * @return string The translated text for the republished notice. + */ + public function get_notice_text( $post ) { + \_deprecated_function( __METHOD__, '4.6', self::class . '::get_notice_copy' ); + return $this->get_notice_copy( $post ); + } } diff --git a/src/watchers/original-post-watcher.php b/src/watchers/original-post-watcher.php index d1293923..a2ff1ecf 100644 --- a/src/watchers/original-post-watcher.php +++ b/src/watchers/original-post-watcher.php @@ -42,18 +42,6 @@ public function register_hooks() { \add_action( 'enqueue_block_editor_assets', [ $this, 'add_block_editor_notice' ], 11 ); } - /** - * Generates the translated text for the notice. - * - * @return string The translated text for the notice. - */ - public function get_notice_text() { - return \__( - 'The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.', - 'duplicate-post' - ); - } - /** * Shows a notice on the Classic editor. * @@ -72,7 +60,7 @@ public function add_admin_notice() { if ( $this->permissions_helper->has_original_changed( $post ) ) { print '

' - . \esc_html( $this->get_notice_text() ) + . \esc_html( $this->get_notice_copy() ) . '

'; } } @@ -92,7 +80,7 @@ public function add_block_editor_notice() { if ( $this->permissions_helper->has_original_changed( $post ) ) { $notice = [ - 'text' => $this->get_notice_text(), + 'text' => $this->get_notice_copy(), 'status' => 'warning', 'isDismissible' => true, ]; @@ -104,4 +92,29 @@ public function add_block_editor_notice() { ); } } + + /** + * Generates the translated text for the notice. + * + * @return string The translated text for the notice. + */ + private function get_notice_copy() { + return \__( + 'The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.', + 'duplicate-post' + ); + } + + /** + * Generates the translated text for the notice. + * + * @deprecated 4.6 + * @codeCoverageIgnore + * + * @return string The translated text for the notice. + */ + public function get_notice_text() { + \_deprecated_function( __METHOD__, '4.6', self::class . '::get_notice_copy' ); + return $this->get_notice_copy(); + } } diff --git a/src/watchers/republished-post-watcher.php b/src/watchers/republished-post-watcher.php index 41e0e323..79023e45 100644 --- a/src/watchers/republished-post-watcher.php +++ b/src/watchers/republished-post-watcher.php @@ -56,18 +56,6 @@ public function add_removable_query_args( $removable_query_args ) { return $removable_query_args; } - /** - * Generates the translated text for the republished notice. - * - * @return string The translated text for the republished notice. - */ - public function get_notice_text() { - return \__( - 'Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.', - 'duplicate-post' - ); - } - /** * Shows a notice on the Classic editor. * @@ -80,7 +68,7 @@ public function add_admin_notice() { if ( ! empty( $_REQUEST['dprepublished'] ) ) { echo '

' - . \esc_html( $this->get_notice_text() ) + . \esc_html( $this->get_notice_copy() ) . '

'; } } @@ -93,7 +81,7 @@ public function add_admin_notice() { public function add_block_editor_notice() { if ( ! empty( $_REQUEST['dprepublished'] ) ) { $notice = [ - 'text' => $this->get_notice_text(), + 'text' => $this->get_notice_copy(), 'status' => 'success', 'isDismissible' => true, ]; @@ -105,4 +93,29 @@ public function add_block_editor_notice() { ); } } + + /** + * Generates the translated text for the notice. + * + * @return string The translated text for the notice. + */ + private function get_notice_copy() { + return \__( + 'Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.', + 'duplicate-post' + ); + } + + /** + * Generates the translated text for the republished notice. + * + * @deprecated 4.6 + * @codeCoverageIgnore + * + * @return string The translated text for the republished notice. + */ + public function get_notice_text() { + \_deprecated_function( __METHOD__, '4.6', self::class . '::get_notice_copy' ); + return $this->get_notice_copy(); + } } diff --git a/tests/Unit/Watchers/Copied_Post_Watcher_Test.php b/tests/Unit/Watchers/Copied_Post_Watcher_Test.php index 273ee3ec..0676dd70 100644 --- a/tests/Unit/Watchers/Copied_Post_Watcher_Test.php +++ b/tests/Unit/Watchers/Copied_Post_Watcher_Test.php @@ -73,17 +73,31 @@ public function test_register_hooks() { } /** - * Tests the get_notice_text function when the copy is not scheduled. + * Tests the add_admin_notice function on the Classic Editor. * - * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_text + * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::add_admin_notice + * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_copy * * @return void */ - public function test_get_notice_text_not_scheduled() { + public function test_add_admin_notice_classic_not_scheduled() { + $this->stubEscapeFunctions(); $this->stubTranslationFunctions(); $post = Mockery::mock( WP_Post::class ); + $this->permissions_helper + ->expects( 'is_classic_editor' ) + ->andReturnTrue(); + + Monkey\Functions\expect( '\get_post' ) + ->andReturn( $post ); + + $this->permissions_helper + ->expects( 'has_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnTrue(); + $this->permissions_helper ->expects( 'has_scheduled_rewrite_and_republish_copy' ) ->with( $post ) @@ -94,25 +108,38 @@ public function test_get_notice_text_not_scheduled() { ->with( $post ) ->andReturnFalse(); - $this->assertSame( - 'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.', - $this->instance->get_notice_text( $post ) - ); + $this->instance->add_admin_notice(); + + $this->expectOutputString( '

A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.

' ); } /** - * Tests the get_notice_text function when the copy is scheduled. + * Tests the add_admin_notice function on the Classic Editor. * - * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_text + * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::add_admin_notice + * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_copy * * @return void */ - public function test_get_notice_text_scheduled() { + public function test_add_admin_notice_classic_scheduled() { + $this->stubEscapeFunctions(); $this->stubTranslationFunctions(); $post = Mockery::mock( WP_Post::class ); $copy = Mockery::mock( WP_Post::class ); + $this->permissions_helper + ->expects( 'is_classic_editor' ) + ->andReturnTrue(); + + Monkey\Functions\expect( '\get_post' ) + ->andReturn( $post ); + + $this->permissions_helper + ->expects( 'has_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnTrue(); + $this->permissions_helper ->expects( 'has_scheduled_rewrite_and_republish_copy' ) ->with( $post ) @@ -131,48 +158,22 @@ public function test_get_notice_text_scheduled() { ->twice() ->andReturnValues( [ '2020/12/02', '10:30 am' ] ); - $this->assertSame( - 'A duplicate of this post was made, which is scheduled to replace this post on 2020/12/02 at 10:30 am.', - $this->instance->get_notice_text( $post ) - ); - } - - /** - * Tests the get_notice_text function when the copy is in the trash. - * - * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_text - * - * @return void - */ - public function test_get_notice_text_copy_in_the_trash() { - $this->stubTranslationFunctions(); - - $post = Mockery::mock( WP_Post::class ); - - $this->permissions_helper - ->expects( 'has_scheduled_rewrite_and_republish_copy' ) - ->never(); - - $this->permissions_helper - ->expects( 'has_trashed_rewrite_and_republish_copy' ) - ->with( $post ) - ->andReturnTrue(); + $this->instance->add_admin_notice(); - $this->assertSame( - 'You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate.', - $this->instance->get_notice_text( $post ) - ); + $this->expectOutputString( '

A duplicate of this post was made, which is scheduled to replace this post on 2020/12/02 at 10:30 am.

' ); } /** * Tests the add_admin_notice function on the Classic Editor. * * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::add_admin_notice + * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_copy * * @return void */ - public function test_add_admin_notice_classic() { + public function test_add_admin_notice_classic_copy_in_the_trash() { $this->stubEscapeFunctions(); + $this->stubTranslationFunctions(); $post = Mockery::mock( WP_Post::class ); @@ -188,13 +189,18 @@ public function test_add_admin_notice_classic() { ->with( $post ) ->andReturnTrue(); - $this->instance - ->expects( 'get_notice_text' ) - ->andReturn( 'notice' ); + $this->permissions_helper + ->expects( 'has_scheduled_rewrite_and_republish_copy' ) + ->never(); + + $this->permissions_helper + ->expects( 'has_trashed_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnTrue(); $this->instance->add_admin_notice(); - $this->expectOutputString( '

notice

' ); + $this->expectOutputString( '

You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate.

' ); } /** @@ -245,10 +251,13 @@ public function test_add_admin_notice_not_rewrite_and_republish() { * Tests the add_block_editor_notice function. * * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::add_block_editor_notice + * @covers \Yoast\WP\Duplicate_Post\Watchers\Copied_Post_Watcher::get_notice_copy * * @return void */ public function test_add_block_editor_notice() { + $this->stubTranslationFunctions(); + $post = Mockery::mock( WP_Post::class ); Monkey\Functions\expect( '\get_post' ) @@ -259,25 +268,30 @@ public function test_add_block_editor_notice() { ->with( $post ) ->andReturnTrue(); - $this->instance - ->expects( 'get_notice_text' ) + $this->permissions_helper + ->expects( 'has_scheduled_rewrite_and_republish_copy' ) ->with( $post ) - ->andReturn( 'notice' ); + ->andReturnFalse(); + + $this->permissions_helper + ->expects( 'has_trashed_rewrite_and_republish_copy' ) + ->with( $post ) + ->andReturnFalse(); $notice = [ - 'text' => 'notice', + 'text' => 'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.', 'status' => 'warning', 'isDismissible' => true, ]; Monkey\Functions\expect( '\wp_json_encode' ) ->with( $notice ) - ->andReturn( '{"text":"notice","status":"warning","isDismissible":true}' ); + ->andReturn( '{"text":"A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.","status":"warning","isDismissible":true}' ); Monkey\Functions\expect( '\wp_add_inline_script' ) ->with( 'duplicate_post_edit_script', - 'duplicatePostNotices.has_rewrite_and_republish_notice = {"text":"notice","status":"warning","isDismissible":true};', + 'duplicatePostNotices.has_rewrite_and_republish_notice = {"text":"A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.","status":"warning","isDismissible":true};', 'before' ); @@ -302,10 +316,6 @@ public function test_add_block_editor_notice_not_rewrite_and_republish() { ->with( $post ) ->andReturnFalse(); - $this->instance - ->expects( 'get_notice_text' ) - ->never(); - Monkey\Functions\expect( '\wp_json_encode' ) ->never(); diff --git a/tests/Unit/Watchers/Original_Post_Watcher_Test.php b/tests/Unit/Watchers/Original_Post_Watcher_Test.php index a5a3236c..8c3f3648 100644 --- a/tests/Unit/Watchers/Original_Post_Watcher_Test.php +++ b/tests/Unit/Watchers/Original_Post_Watcher_Test.php @@ -72,22 +72,6 @@ public function test_register_hooks() { $this->assertNotFalse( \has_action( 'enqueue_block_editor_assets', [ $this->instance, 'add_block_editor_notice' ] ), 'Does not have expected enqueue_block_editor_assets action' ); } - /** - * Tests the get_notice_text function. - * - * @covers \Yoast\WP\Duplicate_Post\Watchers\Original_Post_Watcher::get_notice_text - * - * @return void - */ - public function test_get_notice_text() { - $this->stubTranslationFunctions(); - - $this->assertSame( - 'The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.', - $this->instance->get_notice_text() - ); - } - /** * Tests the add_admin_notice function on the Classic Editor. * @@ -97,6 +81,7 @@ public function test_get_notice_text() { */ public function test_add_admin_notice_classic() { $this->stubEscapeFunctions(); + $this->stubTranslationFunctions(); $post = Mockery::mock( WP_Post::class ); @@ -112,13 +97,9 @@ public function test_add_admin_notice_classic() { ->with( $post ) ->andReturnTrue(); - $this->instance - ->expects( 'get_notice_text' ) - ->andReturn( 'notice' ); - $this->instance->add_admin_notice(); - $this->expectOutputString( '

notice

' ); + $this->expectOutputString( '

The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.

' ); } /** @@ -173,6 +154,8 @@ public function test_add_admin_notice_original_not_changed() { * @return void */ public function test_add_block_editor_notice() { + $this->stubTranslationFunctions(); + $post = Mockery::mock( WP_Post::class ); Monkey\Functions\expect( '\get_post' ) @@ -183,24 +166,20 @@ public function test_add_block_editor_notice() { ->with( $post ) ->andReturnTrue(); - $this->instance - ->expects( 'get_notice_text' ) - ->andReturn( 'notice' ); - $notice = [ - 'text' => 'notice', + 'text' => 'The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.', 'status' => 'warning', 'isDismissible' => true, ]; Monkey\Functions\expect( '\wp_json_encode' ) ->with( $notice ) - ->andReturn( '{"text":"notice","status":"warning","isDismissible":true}' ); + ->andReturn( '{"text":"The original post has been edited in the meantime. If you click \"Republish\", this rewritten post will replace the original post.","status":"warning","isDismissible":true}' ); Monkey\Functions\expect( '\wp_add_inline_script' ) ->with( 'duplicate_post_edit_script', - 'duplicatePostNotices.has_original_changed_notice = {"text":"notice","status":"warning","isDismissible":true};', + 'duplicatePostNotices.has_original_changed_notice = {"text":"The original post has been edited in the meantime. If you click \"Republish\", this rewritten post will replace the original post.","status":"warning","isDismissible":true};', 'before' ); @@ -225,10 +204,6 @@ public function test_add_block_editor_notice_original_not_changed() { ->with( $post ) ->andReturnFalse(); - $this->instance - ->expects( 'get_notice_text' ) - ->never(); - Monkey\Functions\expect( '\wp_json_encode' ) ->never(); diff --git a/tests/Unit/Watchers/Republished_Post_Watcher_Test.php b/tests/Unit/Watchers/Republished_Post_Watcher_Test.php index 998de62b..7d878242 100644 --- a/tests/Unit/Watchers/Republished_Post_Watcher_Test.php +++ b/tests/Unit/Watchers/Republished_Post_Watcher_Test.php @@ -75,22 +75,6 @@ public function test_register_hooks() { $this->assertNotFalse( \has_action( 'enqueue_block_editor_assets', [ $this->instance, 'add_block_editor_notice' ] ), 'Does not have expected enqueue_block_editor_assets action' ); } - /** - * Tests the get_notice_text function. - * - * @covers \Yoast\WP\Duplicate_Post\Watchers\Republished_Post_Watcher::get_notice_text - * - * @return void - */ - public function test_get_notice_text() { - $this->stubTranslationFunctions(); - - $this->assertSame( - 'Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.', - $this->instance->get_notice_text() - ); - } - /** * Tests the add_admin_notice function on the Classic Editor. * @@ -100,20 +84,17 @@ public function test_get_notice_text() { */ public function test_add_admin_notice_classic() { $this->stubEscapeFunctions(); + $this->stubTranslationFunctions(); $this->permissions_helper ->expects( 'is_classic_editor' ) ->andReturnTrue(); - $this->instance - ->expects( 'get_notice_text' ) - ->andReturn( 'notice' ); - $_REQUEST['dprepublished'] = '1'; $this->instance->add_admin_notice(); - $this->expectOutputString( '

notice

' ); + $this->expectOutputString( '

Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.

' ); // Clean up after the test. unset( $_REQUEST['dprepublished'] ); @@ -144,24 +125,22 @@ public function test_add_admin_notice_not_classic() { * @return void */ public function test_add_block_editor_notice() { - $this->instance - ->expects( 'get_notice_text' ) - ->andReturn( 'notice' ); + $this->stubTranslationFunctions(); $notice = [ - 'text' => 'notice', + 'text' => 'Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.', 'status' => 'success', 'isDismissible' => true, ]; Monkey\Functions\expect( '\wp_json_encode' ) ->with( $notice ) - ->andReturn( '{"text":"notice","status":"success","isDismissible":true}' ); + ->andReturn( '{"text":"Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.","status":"success","isDismissible":true}' ); Monkey\Functions\expect( '\wp_add_inline_script' ) ->with( 'duplicate_post_edit_script', - 'duplicatePostNotices.republished_notice = {"text":"notice","status":"success","isDismissible":true};', + 'duplicatePostNotices.republished_notice = {"text":"Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.","status":"success","isDismissible":true};', 'before' );