@@ -198,6 +198,61 @@ public function test_clone_bulk_action_handler_processes_posts_user_can_edit() {
198198 $ this ->assertEquals ( $ redirect_to . '?bulk_cloned=1 ' , $ result );
199199 }
200200
201+ /**
202+ * Tests that clone_bulk_action_handler does not increment counter when duplication returns WP_Error.
203+ *
204+ * @covers \Yoast\WP\Duplicate_Post\Handlers\Bulk_Handler::clone_bulk_action_handler
205+ *
206+ * @return void
207+ */
208+ public function test_clone_bulk_action_handler_handles_wp_error () {
209+ $ redirect_to = 'http://example.com/wp-admin/edit.php ' ;
210+ $ post = Mockery::mock ( WP_Post::class );
211+ $ post ->ID = 1 ;
212+ $ post ->post_type = 'post ' ;
213+ $ wp_error = Mockery::mock ( 'WP_Error ' );
214+
215+ Monkey \Functions \expect ( 'current_user_can ' )
216+ ->with ( 'edit_post ' , 1 )
217+ ->andReturn ( true );
218+
219+ Monkey \Functions \expect ( 'get_post ' )
220+ ->with ( 1 )
221+ ->andReturn ( $ post );
222+
223+ $ this ->permissions_helper
224+ ->expects ( 'is_rewrite_and_republish_copy ' )
225+ ->with ( $ post )
226+ ->andReturn ( false );
227+
228+ Monkey \Functions \expect ( 'get_option ' )
229+ ->with ( 'duplicate_post_copychildren ' )
230+ ->andReturn ( 0 );
231+
232+ Monkey \Functions \expect ( 'is_post_type_hierarchical ' )
233+ ->with ( 'post ' )
234+ ->andReturn ( false );
235+
236+ Monkey \Functions \expect ( 'duplicate_post_create_duplicate ' )
237+ ->with ( $ post )
238+ ->andReturn ( $ wp_error );
239+
240+ Monkey \Functions \expect ( 'is_wp_error ' )
241+ ->with ( $ wp_error )
242+ ->andReturn ( true );
243+
244+ Monkey \Functions \expect ( 'add_query_arg ' )
245+ ->andReturnUsing (
246+ static function ( $ key , $ value , $ url ) {
247+ return $ url . ( ( \strpos ( $ url , '? ' ) === false ) ? '? ' : '& ' ) . $ key . '= ' . $ value ;
248+ }
249+ );
250+
251+ $ result = $ this ->instance ->clone_bulk_action_handler ( $ redirect_to , 'duplicate_post_bulk_clone ' , [ 1 ] );
252+
253+ $ this ->assertStringContainsString ( 'bulk_cloned=0 ' , $ result );
254+ }
255+
201256 /**
202257 * Tests that rewrite_bulk_action_handler returns early when action is not rewrite.
203258 *
0 commit comments