|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test Scheduler class. |
| 4 | + * |
| 5 | + * @package ActivityPub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub; |
| 9 | + |
| 10 | +/** |
| 11 | + * Test cases for the Scheduler class. |
| 12 | + * |
| 13 | + * @coversDefaultClass \Activitypub\Scheduler |
| 14 | + */ |
| 15 | +class Test_Scheduler extends \WP_UnitTestCase { |
| 16 | + /** |
| 17 | + * Test post. |
| 18 | + * |
| 19 | + * @var \WP_Post |
| 20 | + */ |
| 21 | + protected $post; |
| 22 | + |
| 23 | + /** |
| 24 | + * Set up test resources before each test. |
| 25 | + * |
| 26 | + * Creates a test post in draft status. |
| 27 | + */ |
| 28 | + public function set_up() { |
| 29 | + parent::set_up(); |
| 30 | + |
| 31 | + $this->post = self::factory()->post->create_and_get( |
| 32 | + array( |
| 33 | + 'post_title' => 'Test Post', |
| 34 | + 'post_content' => 'Test Content', |
| 35 | + 'post_status' => 'draft', |
| 36 | + 'post_author' => 1, |
| 37 | + ) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Clean up test resources after each test. |
| 43 | + * |
| 44 | + * Deletes the test post. |
| 45 | + */ |
| 46 | + public function tear_down() { |
| 47 | + wp_delete_post( $this->post->ID, true ); |
| 48 | + parent::tear_down(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test that moving a draft post to trash does not schedule federation. |
| 53 | + * |
| 54 | + * @covers ::schedule_post_activity |
| 55 | + */ |
| 56 | + public function test_draft_to_trash_should_not_schedule_federation() { |
| 57 | + Scheduler::schedule_post_activity( 'trash', 'draft', $this->post ); |
| 58 | + |
| 59 | + $this->assertFalse( |
| 60 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Delete' ) ), |
| 61 | + 'Draft to trash transition should not schedule federation' |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test that moving a published post to trash schedules a delete activity only if federated. |
| 67 | + * |
| 68 | + * @covers ::schedule_post_activity |
| 69 | + */ |
| 70 | + public function test_publish_to_trash_should_schedule_delete_only_if_federated() { |
| 71 | + wp_publish_post( $this->post->ID ); |
| 72 | + $this->post = get_post( $this->post->ID ); |
| 73 | + |
| 74 | + // Test without federation state. |
| 75 | + Scheduler::schedule_post_activity( 'trash', 'publish', $this->post ); |
| 76 | + $this->assertFalse( |
| 77 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Delete' ) ), |
| 78 | + 'Published to trash transition should not schedule delete activity when not federated' |
| 79 | + ); |
| 80 | + |
| 81 | + // Test with federation state. |
| 82 | + set_wp_object_state( $this->post, 'federated' ); |
| 83 | + Scheduler::schedule_post_activity( 'trash', 'publish', $this->post ); |
| 84 | + |
| 85 | + $this->assertNotFalse( |
| 86 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Delete' ) ), |
| 87 | + 'Published to trash transition should schedule delete activity when federated' |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test that updating a draft post does not schedule federation. |
| 93 | + * |
| 94 | + * @covers ::schedule_post_activity |
| 95 | + */ |
| 96 | + public function test_draft_to_draft_should_not_schedule_federation() { |
| 97 | + Scheduler::schedule_post_activity( 'draft', 'draft', $this->post ); |
| 98 | + |
| 99 | + $this->assertFalse( |
| 100 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Update' ) ), |
| 101 | + 'Draft to draft transition should not schedule federation' |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test that moving a published post to draft schedules an update activity. |
| 107 | + * |
| 108 | + * @covers ::schedule_post_activity |
| 109 | + */ |
| 110 | + public function test_publish_to_draft_should_schedule_update() { |
| 111 | + wp_publish_post( $this->post->ID ); |
| 112 | + $this->post = get_post( $this->post->ID ); |
| 113 | + Scheduler::schedule_post_activity( 'draft', 'publish', $this->post ); |
| 114 | + |
| 115 | + $this->assertNotFalse( |
| 116 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Update' ) ), |
| 117 | + 'Published to draft transition should schedule update activity' |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test that publishing a draft post schedules a create activity. |
| 123 | + * |
| 124 | + * @covers ::schedule_post_activity |
| 125 | + */ |
| 126 | + public function test_draft_to_publish_should_schedule_create() { |
| 127 | + Scheduler::schedule_post_activity( 'publish', 'draft', $this->post ); |
| 128 | + |
| 129 | + $this->assertNotFalse( |
| 130 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Create' ) ), |
| 131 | + 'Draft to publish transition should schedule create activity' |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Test that updating a published post schedules an update activity. |
| 137 | + * |
| 138 | + * @covers ::schedule_post_activity |
| 139 | + */ |
| 140 | + public function test_publish_to_publish_should_schedule_update() { |
| 141 | + wp_publish_post( $this->post->ID ); |
| 142 | + $this->post = get_post( $this->post->ID ); |
| 143 | + Scheduler::schedule_post_activity( 'publish', 'publish', $this->post ); |
| 144 | + |
| 145 | + $this->assertNotFalse( |
| 146 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Update' ) ), |
| 147 | + 'Published to published transition should schedule update activity' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test that various non-standard status transitions do not schedule federation. |
| 153 | + * |
| 154 | + * Tests transitions from pending, private, and future statuses. |
| 155 | + * |
| 156 | + * @covers ::schedule_post_activity |
| 157 | + */ |
| 158 | + public function test_other_status_transitions_should_not_schedule_federation() { |
| 159 | + // Test pending to draft. |
| 160 | + Scheduler::schedule_post_activity( 'draft', 'pending', $this->post ); |
| 161 | + |
| 162 | + $this->assertFalse( |
| 163 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Update' ) ), |
| 164 | + 'Pending to draft transition should not schedule federation' |
| 165 | + ); |
| 166 | + |
| 167 | + // Test private to draft. |
| 168 | + Scheduler::schedule_post_activity( 'draft', 'private', $this->post ); |
| 169 | + |
| 170 | + $this->assertFalse( |
| 171 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Update' ) ), |
| 172 | + 'Private to draft transition should not schedule federation' |
| 173 | + ); |
| 174 | + |
| 175 | + // Test future to draft. |
| 176 | + Scheduler::schedule_post_activity( 'draft', 'future', $this->post ); |
| 177 | + |
| 178 | + $this->assertFalse( |
| 179 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Update' ) ), |
| 180 | + 'Future to draft transition should not schedule federation' |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Test that disabled posts do not schedule federation activities. |
| 186 | + * |
| 187 | + * @covers ::schedule_post_activity |
| 188 | + */ |
| 189 | + public function test_disabled_post_should_not_schedule_federation() { |
| 190 | + update_post_meta( $this->post->ID, 'activitypub_content_visibility', ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL ); |
| 191 | + Scheduler::schedule_post_activity( 'publish', 'draft', $this->post ); |
| 192 | + |
| 193 | + $this->assertFalse( |
| 194 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Create' ) ), |
| 195 | + 'Disabled posts should not schedule federation activities' |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Test that password protected posts do not schedule federation activities. |
| 201 | + * |
| 202 | + * @covers ::schedule_post_activity |
| 203 | + */ |
| 204 | + public function test_password_protected_post_should_not_schedule_federation() { |
| 205 | + wp_update_post( |
| 206 | + array( |
| 207 | + 'ID' => $this->post->ID, |
| 208 | + 'post_password' => 'test-password', |
| 209 | + ) |
| 210 | + ); |
| 211 | + $this->post = get_post( $this->post->ID ); |
| 212 | + Scheduler::schedule_post_activity( 'publish', 'draft', $this->post ); |
| 213 | + |
| 214 | + $this->assertFalse( |
| 215 | + wp_next_scheduled( 'activitypub_send_post', array( $this->post->ID, 'Create' ) ), |
| 216 | + 'Password protected posts should not schedule federation activities' |
| 217 | + ); |
| 218 | + } |
| 219 | +} |
0 commit comments