Skip to content

Commit e19f281

Browse files
committed
Start on some tests.
1 parent 7dee065 commit e19f281

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
/**
3+
* Test Plugin Pings of IndexNow
4+
*
5+
* @package NoFussIndexNow
6+
*/
7+
8+
namespace PWCC\NoFussIndexNow\Tests;
9+
10+
use WP_UnitTestCase;
11+
use WP_UnitTest_Factory;
12+
13+
/**
14+
* Test Plugin Pings of IndexNow
15+
*/
16+
class Test_IndexNow_Pings extends WP_UnitTestCase {
17+
/**
18+
* Array to store pinged URLs.
19+
*
20+
* This will be populated by the mocked_ping_response method
21+
* when a ping is made to the IndexNow API.
22+
*
23+
* @var array
24+
*/
25+
private $pings = array();
26+
27+
/**
28+
* Array to store post IDs created for testing.
29+
*
30+
* Keyed by post status, value is the post ID.
31+
*
32+
* @var array
33+
*/
34+
private static $post_ids = array();
35+
36+
/**
37+
* Set up shared fixtures.
38+
*
39+
* @global \WP_Rewrite $wp_rewrite The WordPress rewrite object.
40+
*
41+
* @param WP_UnitTest_Factory $factory The factory to create test data.
42+
*/
43+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
44+
// WP_UnitTestCase::set_permalink_structure() is not a static method, so this the hard way.
45+
global $wp_rewrite;
46+
47+
$wp_rewrite->init();
48+
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
49+
$wp_rewrite->flush_rules();
50+
51+
$statuses = array( 'publish', 'draft', 'pending', 'private' );
52+
foreach ( $statuses as $status ) {
53+
$post_id = $factory->post->create(
54+
array(
55+
'post_status' => $status,
56+
'post_title' => 'Test Post ' . ucfirst( $status ),
57+
)
58+
);
59+
self::$post_ids[ $status ] = $post_id;
60+
}
61+
}
62+
63+
/**
64+
* Set up the test environment.
65+
*/
66+
public function set_up() {
67+
parent::set_up();
68+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
69+
$this->pings = array();
70+
add_filter( 'pre_http_request', array( $this, 'mocked_ping_response' ), 10, 3 );
71+
}
72+
73+
/**
74+
* Mock the HTTP response for IndexNow pings.
75+
*
76+
* Runs on the 'pre_http_request' filter to intercept HTTP requests.
77+
*
78+
* @param mixed $pre The pre-HTTP request value.
79+
* @param array $request The HTTP request arguments.
80+
* @param string $url The URL being requested.
81+
* @return array The mocked response.
82+
*/
83+
public function mocked_ping_response( $pre, $request, $url ) {
84+
if ( ! str_starts_with( $url, 'https://api.indexnow.org/indexnow' ) ) {
85+
return $pre; // Only mock requests IndexNow URLs.
86+
}
87+
88+
// Store the pinged URLs.
89+
$body = $request['body'] ?? array();
90+
$data = json_decode( $body, true );
91+
$this->pings = $data['urlList'] ?? array();
92+
93+
// Simulate a successful response.
94+
$response = array(
95+
'headers' => array(),
96+
'body' => array(),
97+
'response' => array(
98+
'code' => 200, // HTTP status code.
99+
'message' => 'OK', // HTTP status message.
100+
),
101+
'cookies' => array(),
102+
'filename' => '',
103+
);
104+
return $response;
105+
}
106+
107+
/**
108+
* Assert that a specific URL was pinged.
109+
*
110+
* @param string $expected_url The expected URL to be pinged.
111+
* @param string $message Optional. Message to display on failure.
112+
*/
113+
public function assertPing( $expected_url, $message = '' ) {
114+
$this->assertContains( $expected_url, $this->pings, $message );
115+
}
116+
117+
/**
118+
* Assert that a specific URL was not pinged.
119+
*
120+
* @param string $expected_url The expected URL to not be pinged.
121+
* @param string $message Optional. Message to display on failure.
122+
*/
123+
public function assertNotPing( $expected_url, $message = '' ) {
124+
$this->assertNotContains( $expected_url, $this->pings, $message );
125+
}
126+
127+
/**
128+
* Ensure a new post triggers a ping to IndexNow.
129+
*/
130+
public function test_ping_on_post_publish() {
131+
$post_id = $this->factory->post->create(
132+
array(
133+
'post_status' => 'publish',
134+
'post_title' => 'Test Post',
135+
)
136+
);
137+
138+
$this->assertPing( get_permalink( $post_id ), 'Ping should include the post URL on publish.' );
139+
}
140+
141+
/**
142+
* Ensure a post update triggers a ping to IndexNow.
143+
*/
144+
public function test_ping_on_post_update() {
145+
$post_id = self::$post_ids['publish'];
146+
147+
wp_update_post(
148+
array(
149+
'ID' => $post_id,
150+
'post_content' => 'Updated Test Post',
151+
)
152+
);
153+
154+
$this->assertPing( get_permalink( $post_id ), 'Ping should include the post URL on update.' );
155+
}
156+
157+
/**
158+
* Ensure trashing a previously published post pings the old URL.
159+
*/
160+
public function test_ping_on_post_trash() {
161+
$post_id = self::$post_ids['publish'];
162+
$original_permalink = get_permalink( $post_id );
163+
164+
wp_trash_post( $post_id );
165+
$trashed_permalink = get_permalink( $post_id );
166+
167+
$this->assertPing( $original_permalink, 'Ping should include the post URL on trash.' );
168+
$this->assertNotPing( $trashed_permalink, 'Ping should not include the trashed post URL.' );
169+
}
170+
}

0 commit comments

Comments
 (0)