Skip to content

Commit 861d638

Browse files
committed
Add tests; fix post formating; fix CPT registration
1 parent da7112a commit 861d638

File tree

7 files changed

+241
-6
lines changed

7 files changed

+241
-6
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
"email": "[email protected]"
1010
}
1111
],
12-
"require": {}
12+
"require": {
13+
"antecedent/patchwork": "^1.4"
14+
}
1315
}

composer.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/class-jras-notifier.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ public function prepare_date_response( $date_gmt, $date = null ) {
7474
* @since 1.0
7575
* @return array
7676
*/
77-
public function format_post_for_request( $post ) {
77+
public function format_post_for_request( $request_post ) {
78+
global $post;
79+
80+
$post = $request_post;
81+
82+
setup_postdata( $post );
83+
7884
return array(
7985
'id' => $post->ID,
8086
'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
@@ -93,7 +99,7 @@ public function format_post_for_request( $post ) {
9399
),
94100
'excerpt' => array(
95101
'raw' => $post->post_excerpt,
96-
'rendered' => apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt ) ),
102+
'rendered' => apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) ),
97103
),
98104
'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
99105
'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
@@ -103,6 +109,8 @@ public function format_post_for_request( $post ) {
103109
'link' => $post->permalink,
104110
'author' => $post->author,
105111
);
112+
113+
wp_reset_postdata();
106114
}
107115

108116
/**

lib/class-jras-subscriptions-cpt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function setup_cpt() {
3535
'has_archive' => false,
3636
);
3737

38-
register_post_type( 'jra_subscription', $args );
38+
register_post_type( 'jras_subscription', $args );
3939
}
4040

4141
/**

tests/bootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
$_tests_dir = '/tmp/wordpress-tests-lib';
77
}
88

9+
require( __DIR__ . '/../vendor/antecedent/patchwork/Patchwork.php' );
10+
911
require_once( $_tests_dir . '/includes/functions.php' );
1012

1113
tests_add_filter( 'muplugins_loaded', function() {
12-
require( dirname( __FILE__ ) . '/../plugin.php' );
14+
require( __DIR__ . '/../plugin.php' );
1315
} );
1416

1517
require_once( $_tests_dir . '/includes/bootstrap.php' );

tests/test-listener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class CCFTestPostCreation extends WP_UnitTestCase {
3+
class CCFTestListener extends WP_UnitTestCase {
44

55
/**
66
* Setup each test making sure we are logged in as an admin

tests/test-notifier.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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

Comments
 (0)