|
| 1 | +<?php |
| 2 | + |
| 3 | +class CCFTestNotifier extends WP_UnitTestCase { |
| 4 | + |
| 5 | + /** |
| 6 | + * Setup each test making sure we are logged in as an admin |
| 7 | + * |
| 8 | + * @since 1.0 |
| 9 | + */ |
| 10 | + public function setUp() { |
| 11 | + parent::setUp(); |
| 12 | + |
| 13 | + $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
| 14 | + wp_set_current_user( $admin_id ); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Create a subscription for testing |
| 19 | + * |
| 20 | + * @param string $target |
| 21 | + * @param array $events |
| 22 | + * @param string $post_type |
| 23 | + * @param integer $content_id |
| 24 | + * @since 1.0 |
| 25 | + * @return int|bool |
| 26 | + */ |
| 27 | + private function _create_subscription( $target, $signature, $events = array( 'delete', 'create', 'update' ), $post_type = 'post', $content_id = 0 ) { |
| 28 | + $subscription_id = wp_insert_post( array( |
| 29 | + 'post_title' => 'subscription ' . esc_url_raw( $target ), |
| 30 | + 'post_type' => 'jras_subscription', |
| 31 | + 'post_status' => 'publish', |
| 32 | + 'post_parent' => $content_id, |
| 33 | + ) ); |
| 34 | + |
| 35 | + if ( ! is_wp_error( $subscription_id ) ) { |
| 36 | + |
| 37 | + update_post_meta( $subscription_id, 'jras_events', $events ); |
| 38 | + update_post_meta( $subscription_id, 'jras_target', $target ); |
| 39 | + update_post_meta( $subscription_id, 'jras_content_type', $post_type ); |
| 40 | + update_post_meta( $subscription_id, 'jras_signature', md5( $signature ) ); |
| 41 | + |
| 42 | + return $subscription_id; |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test one posts notification with one subscriber |
| 50 | + * |
| 51 | + * @since 1.0 |
| 52 | + */ |
| 53 | + public function testNotificationPostsOneSubscriber() { |
| 54 | + $successful_notification = false; |
| 55 | + $signature = 'test'; |
| 56 | + |
| 57 | + $this->_create_subscription( 'http://test.com', $signature ); |
| 58 | + |
| 59 | + // Stub the wp_remote_request function |
| 60 | + \Patchwork\replace( 'wp_remote_request', function( $url, $args = array() ) use ( $signature ) { |
| 61 | + return array( |
| 62 | + 'response' => array( |
| 63 | + 'code' => 200, |
| 64 | + ), |
| 65 | + 'headers' => array( |
| 66 | + 'x-wp-subscription-signature' => $signature, |
| 67 | + ), |
| 68 | + ); |
| 69 | + } ); |
| 70 | + |
| 71 | + $post_id = wp_insert_post( array( |
| 72 | + 'post_title' => 'Test post', |
| 73 | + 'post_content' => 'test', |
| 74 | + 'post_status' => 'publish', |
| 75 | + 'post_type' => 'post', |
| 76 | + ) ); |
| 77 | + |
| 78 | + add_action( 'jras_successful_notification', function() use ( &$successful_notification ) { |
| 79 | + $successful_notification = true; |
| 80 | + } ); |
| 81 | + |
| 82 | + JRAS_Notifier::factory()->notify(); |
| 83 | + |
| 84 | + $this->assertTrue( $successful_notification ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test failed one posts notification with one subscriber with a bad signature |
| 89 | + * |
| 90 | + * @since 1.0 |
| 91 | + */ |
| 92 | + public function testFailedNotificationPostsOneSubscriberBadSignature() { |
| 93 | + $unsuccessful_notification = false; |
| 94 | + $signature = 'test'; |
| 95 | + |
| 96 | + $this->_create_subscription( 'http://test.com', $signature ); |
| 97 | + |
| 98 | + // Stub the wp_remote_request function |
| 99 | + \Patchwork\replace( 'wp_remote_request', function( $url, $args = array() ) use ( $signature ) { |
| 100 | + return array( |
| 101 | + 'response' => array( |
| 102 | + 'code' => 200, |
| 103 | + ), |
| 104 | + 'headers' => array( |
| 105 | + 'x-wp-subscription-signature' => $signature . 'BAD', |
| 106 | + ), |
| 107 | + ); |
| 108 | + } ); |
| 109 | + |
| 110 | + $post_id = wp_insert_post( array( |
| 111 | + 'post_title' => 'Test post', |
| 112 | + 'post_content' => 'test', |
| 113 | + 'post_status' => 'publish', |
| 114 | + 'post_type' => 'post', |
| 115 | + ) ); |
| 116 | + |
| 117 | + add_action( 'jras_unsuccessful_notification', function() use ( &$unsuccessful_notification ) { |
| 118 | + $unsuccessful_notification = true; |
| 119 | + } ); |
| 120 | + |
| 121 | + JRAS_Notifier::factory()->notify(); |
| 122 | + |
| 123 | + $this->assertTrue( $unsuccessful_notification ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Test failed one posts notification with one subscriber with a bad response code |
| 128 | + * |
| 129 | + * @since 1.0 |
| 130 | + */ |
| 131 | + public function testFailedNotificationPostsOneSubscriberBadResponse() { |
| 132 | + $unsuccessful_notification = false; |
| 133 | + $signature = 'test'; |
| 134 | + |
| 135 | + $this->_create_subscription( 'http://test.com', $signature ); |
| 136 | + |
| 137 | + // Stub the wp_remote_request function |
| 138 | + \Patchwork\replace( 'wp_remote_request', function( $url, $args = array() ) use ( $signature ) { |
| 139 | + return array( |
| 140 | + 'response' => array( |
| 141 | + 'code' => 500, |
| 142 | + ), |
| 143 | + 'headers' => array( |
| 144 | + 'x-wp-subscription-signature' => $signature, |
| 145 | + ), |
| 146 | + ); |
| 147 | + } ); |
| 148 | + |
| 149 | + $post_id = wp_insert_post( array( |
| 150 | + 'post_title' => 'Test post', |
| 151 | + 'post_content' => 'test', |
| 152 | + 'post_status' => 'publish', |
| 153 | + 'post_type' => 'post', |
| 154 | + ) ); |
| 155 | + |
| 156 | + add_action( 'jras_unsuccessful_notification', function() use ( &$unsuccessful_notification ) { |
| 157 | + $unsuccessful_notification = true; |
| 158 | + } ); |
| 159 | + |
| 160 | + JRAS_Notifier::factory()->notify(); |
| 161 | + |
| 162 | + $this->assertTrue( $unsuccessful_notification ); |
| 163 | + } |
| 164 | +} |
0 commit comments