|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Google\Site_Kit\Tests\Core\Email_Reporting\Email_Log_Batch_QueryTest |
| 4 | + * |
| 5 | + * @package Google\Site_Kit\Tests\Core\Email_Reporting |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Google\Site_Kit\Tests\Core\Email_Reporting; |
| 9 | + |
| 10 | +use Google\Site_Kit\Context; |
| 11 | +use Google\Site_Kit\Core\Email_Reporting\Email_Log; |
| 12 | +use Google\Site_Kit\Core\Email_Reporting\Email_Log_Batch_Query; |
| 13 | +use Google\Site_Kit\Core\User\Email_Reporting_Settings; |
| 14 | +use Google\Site_Kit\Tests\TestCase; |
| 15 | + |
| 16 | +class Email_Log_Batch_QueryTest extends TestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * @var Email_Log_Batch_Query |
| 20 | + */ |
| 21 | + private $query; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + private $created_post_ids = array(); |
| 27 | + |
| 28 | + public function set_up() { |
| 29 | + parent::set_up(); |
| 30 | + |
| 31 | + $this->query = new Email_Log_Batch_Query(); |
| 32 | + $this->created_post_ids = array(); |
| 33 | + |
| 34 | + $this->register_email_log_dependencies(); |
| 35 | + } |
| 36 | + |
| 37 | + public function tear_down() { |
| 38 | + foreach ( $this->created_post_ids as $post_id ) { |
| 39 | + wp_delete_post( $post_id, true ); |
| 40 | + } |
| 41 | + |
| 42 | + if ( post_type_exists( Email_Log::POST_TYPE ) && function_exists( 'unregister_post_type' ) ) { |
| 43 | + unregister_post_type( Email_Log::POST_TYPE ); |
| 44 | + } |
| 45 | + |
| 46 | + foreach ( array( Email_Log::STATUS_SENT, Email_Log::STATUS_FAILED, Email_Log::STATUS_SCHEDULED ) as $status ) { |
| 47 | + if ( isset( $GLOBALS['wp_post_statuses'][ $status ] ) ) { |
| 48 | + unset( $GLOBALS['wp_post_statuses'][ $status ] ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + foreach ( |
| 53 | + array( |
| 54 | + Email_Log::META_REPORT_FREQUENCY, |
| 55 | + Email_Log::META_BATCH_ID, |
| 56 | + Email_Log::META_SEND_ATTEMPTS, |
| 57 | + Email_Log::META_ERROR_DETAILS, |
| 58 | + Email_Log::META_REPORT_REFERENCE_DATES, |
| 59 | + ) as $meta_key |
| 60 | + ) { |
| 61 | + if ( function_exists( 'unregister_meta_key' ) ) { |
| 62 | + unregister_meta_key( 'post', Email_Log::POST_TYPE, $meta_key ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + parent::tear_down(); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_get_pending_ids_excludes_completed_posts() { |
| 70 | + $batch_id = 'batch-pending'; |
| 71 | + |
| 72 | + $scheduled_id = $this->create_log_post( $batch_id, Email_Log::STATUS_SCHEDULED, 0 ); |
| 73 | + $retry_failed = $this->create_log_post( $batch_id, Email_Log::STATUS_FAILED, 1 ); |
| 74 | + $max_failed = $this->create_log_post( $batch_id, Email_Log::STATUS_FAILED, Email_Log_Batch_Query::MAX_ATTEMPTS ); |
| 75 | + $sent_id = $this->create_log_post( $batch_id, Email_Log::STATUS_SENT, 1 ); |
| 76 | + $other_batch = $this->create_log_post( 'other-batch', Email_Log::STATUS_SCHEDULED, 0 ); |
| 77 | + $_unused_posts = array( $max_failed, $sent_id, $other_batch ); |
| 78 | + |
| 79 | + $pending_ids = $this->query->get_pending_ids( $batch_id ); |
| 80 | + |
| 81 | + $this->assertEqualSets( array( $scheduled_id, $retry_failed ), $pending_ids, 'Only scheduled or retriable failed logs should be returned.' ); |
| 82 | + } |
| 83 | + |
| 84 | + public function test_is_complete_reflects_pending_state() { |
| 85 | + $batch_id = 'batch-complete'; |
| 86 | + $this->assertTrue( $this->query->is_complete( $batch_id ), 'Empty batches should be complete.' ); |
| 87 | + |
| 88 | + $pending_id = $this->create_log_post( $batch_id, Email_Log::STATUS_SCHEDULED, 0 ); |
| 89 | + $this->assertFalse( $this->query->is_complete( $batch_id ), 'Batches with scheduled logs should not be complete.' ); |
| 90 | + |
| 91 | + $this->query->update_status( $pending_id, Email_Log::STATUS_SENT ); |
| 92 | + $this->assertTrue( $this->query->is_complete( $batch_id ), 'After marking logs as sent, batch should be complete.' ); |
| 93 | + } |
| 94 | + |
| 95 | + public function test_increment_attempt_and_update_status() { |
| 96 | + $batch_id = 'batch-mutate'; |
| 97 | + $post_id = $this->create_log_post( $batch_id, Email_Log::STATUS_FAILED, 1 ); |
| 98 | + |
| 99 | + $this->query->increment_attempt( $post_id ); |
| 100 | + $this->assertSame( 2, (int) get_post_meta( $post_id, Email_Log::META_SEND_ATTEMPTS, true ), 'Increment attempt should increase send attempts meta.' ); |
| 101 | + |
| 102 | + $this->query->increment_attempt( $post_id ); |
| 103 | + $this->assertSame( 3, (int) get_post_meta( $post_id, Email_Log::META_SEND_ATTEMPTS, true ), 'Increment attempt can be called multiple times.' ); |
| 104 | + |
| 105 | + $this->query->update_status( $post_id, Email_Log::STATUS_SENT ); |
| 106 | + $this->assertSame( Email_Log::STATUS_SENT, get_post_status( $post_id ), 'Update status should persist new post status.' ); |
| 107 | + } |
| 108 | + |
| 109 | + private function create_log_post( $batch_id, $status, $attempts ) { |
| 110 | + $post_id = wp_insert_post( |
| 111 | + array( |
| 112 | + 'post_type' => Email_Log::POST_TYPE, |
| 113 | + 'post_status' => $status, |
| 114 | + 'post_title' => 'Log ' . uniqid(), |
| 115 | + 'meta_input' => array( |
| 116 | + Email_Log::META_BATCH_ID => $batch_id, |
| 117 | + Email_Log::META_REPORT_FREQUENCY => Email_Reporting_Settings::FREQUENCY_WEEKLY, |
| 118 | + Email_Log::META_SEND_ATTEMPTS => $attempts, |
| 119 | + ), |
| 120 | + ) |
| 121 | + ); |
| 122 | + |
| 123 | + $this->created_post_ids[] = $post_id; |
| 124 | + |
| 125 | + return $post_id; |
| 126 | + } |
| 127 | + |
| 128 | + private function register_email_log_dependencies() { |
| 129 | + $email_log = new Email_Log( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ); |
| 130 | + $register_method = new \ReflectionMethod( Email_Log::class, 'register_email_log' ); |
| 131 | + $register_method->setAccessible( true ); |
| 132 | + $register_method->invoke( $email_log ); |
| 133 | + } |
| 134 | +} |
0 commit comments