|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test prpl_recommendations post type status transitions. |
| 4 | + * |
| 5 | + * @package Progress_Planner\Tests |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Progress_Planner\Tests; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class Prpl_Recommendations_Status_Transition_Test |
| 12 | + */ |
| 13 | +class Prpl_Recommendations_Status_Transition_Test extends \WP_UnitTestCase { |
| 14 | + |
| 15 | + /** |
| 16 | + * Test that transitioning prpl_recommendations from future to publish doesn't cause errors. |
| 17 | + * |
| 18 | + * @return void |
| 19 | + */ |
| 20 | + public function test_prpl_recommendations_future_to_publish_transition() { |
| 21 | + // Capture any errors that might occur. |
| 22 | + $errors = []; |
| 23 | + $notices = []; |
| 24 | + |
| 25 | + // Set up error handlers to capture errors and notices. |
| 26 | + set_error_handler( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler |
| 27 | + function ( $errno, $errstr, $errfile, $errline ) use ( &$errors, &$notices ) { |
| 28 | + if ( $errno === E_ERROR || $errno === E_PARSE || $errno === E_CORE_ERROR || $errno === E_COMPILE_ERROR ) { |
| 29 | + $errors[] = [ |
| 30 | + 'type' => $errno, |
| 31 | + 'message' => $errstr, |
| 32 | + 'file' => $errfile, |
| 33 | + 'line' => $errline, |
| 34 | + ]; |
| 35 | + } elseif ( $errno === E_NOTICE || $errno === E_WARNING ) { |
| 36 | + $notices[] = [ |
| 37 | + 'type' => $errno, |
| 38 | + 'message' => $errstr, |
| 39 | + 'file' => $errfile, |
| 40 | + 'line' => $errline, |
| 41 | + ]; |
| 42 | + } |
| 43 | + return false; // Let PHP handle the error as well. |
| 44 | + } |
| 45 | + ); |
| 46 | + |
| 47 | + // Remove the action that changes the post date to current time, they trigger notices since it is not checked if $post object exists or not. |
| 48 | + \remove_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 ); |
| 49 | + \remove_action( 'post_updated', 'wp_check_for_changed_dates', 12, 3 ); |
| 50 | + |
| 51 | + try { |
| 52 | + // Create a prpl_recommendations post with future status. |
| 53 | + $post_data = [ |
| 54 | + 'post_title' => 'Test Recommendation', |
| 55 | + 'post_content' => 'This is a test recommendation content.', |
| 56 | + 'post_status' => 'future', |
| 57 | + 'post_type' => 'prpl_recommendations', |
| 58 | + 'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 hour' ) ), |
| 59 | + ]; |
| 60 | + |
| 61 | + $post_id = wp_insert_post( $post_data ); |
| 62 | + |
| 63 | + // Add required taxonomy terms for the post. |
| 64 | + wp_set_object_terms( $post_id, 'test-category', 'prpl_recommendations_category' ); |
| 65 | + wp_set_object_terms( $post_id, 'test-provider', 'prpl_recommendations_provider' ); |
| 66 | + |
| 67 | + // Now publish the post (change status from future to publish). |
| 68 | + $updated_post_data = [ |
| 69 | + 'ID' => $post_id, |
| 70 | + 'post_status' => 'publish', |
| 71 | + 'post_date' => gmdate( 'Y-m-d H:i:s' ), |
| 72 | + 'post_date_gmt' => gmdate( 'Y-m-d H:i:s' ), |
| 73 | + ]; |
| 74 | + |
| 75 | + wp_update_post( $updated_post_data ); |
| 76 | + |
| 77 | + } finally { |
| 78 | + // Restore error handler. |
| 79 | + restore_error_handler(); |
| 80 | + } |
| 81 | + |
| 82 | + // Assert that no PHP errors occurred. |
| 83 | + $this->assertEmpty( $errors, 'No PHP errors should occur during the status transition. Errors found: ' . wp_json_encode( $errors ) ); |
| 84 | + |
| 85 | + // Assert that no PHP notices occurred. |
| 86 | + $this->assertEmpty( $notices, 'No PHP notices should occur during the status transition. Notices found: ' . wp_json_encode( $notices ) ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Clean up after tests. |
| 91 | + */ |
| 92 | + public function tearDown(): void { |
| 93 | + // Clean up any posts created during tests. |
| 94 | + $posts = get_posts( |
| 95 | + [ |
| 96 | + 'post_type' => 'prpl_recommendations', |
| 97 | + 'post_status' => 'any', |
| 98 | + 'numberposts' => -1, |
| 99 | + ] |
| 100 | + ); |
| 101 | + |
| 102 | + foreach ( $posts as $post ) { |
| 103 | + wp_delete_post( $post->ID, true ); |
| 104 | + } |
| 105 | + |
| 106 | + parent::tearDown(); |
| 107 | + } |
| 108 | +} |
0 commit comments