|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * REST API test |
| 4 | + * |
| 5 | + * @package tscp |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * REST API test case. |
| 10 | + */ |
| 11 | +class Tscp_Rest_Api_Test extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * REST Server |
| 15 | + * |
| 16 | + * @var WP_REST_Server |
| 17 | + */ |
| 18 | + protected $server; |
| 19 | + |
| 20 | + /** |
| 21 | + * Set up test fixtures. |
| 22 | + */ |
| 23 | + public function set_up() { |
| 24 | + parent::set_up(); |
| 25 | + global $wp_rest_server; |
| 26 | + $wp_rest_server = new WP_REST_Server(); |
| 27 | + $this->server = $wp_rest_server; |
| 28 | + do_action( 'rest_api_init' ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Tear down test fixtures. |
| 33 | + */ |
| 34 | + public function tear_down() { |
| 35 | + global $wp_rest_server; |
| 36 | + $wp_rest_server = null; |
| 37 | + parent::tear_down(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test route is registered. |
| 42 | + */ |
| 43 | + public function test_route_registered() { |
| 44 | + $routes = $this->server->get_routes(); |
| 45 | + $this->assertArrayHasKey( '/clockwork/v1/(?P<post_type>[^/]+)/(?P<post_id>\\d+)/expiration', $routes ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test GET expiration returns defaults for new post. |
| 50 | + */ |
| 51 | + public function test_get_expiration_defaults() { |
| 52 | + $user_id = $this->factory()->user->create( [ 'role' => 'editor' ] ); |
| 53 | + wp_set_current_user( $user_id ); |
| 54 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 55 | + |
| 56 | + $request = new WP_REST_Request( 'GET', '/clockwork/v1/post/' . $post_id . '/expiration' ); |
| 57 | + $response = $this->server->dispatch( $request ); |
| 58 | + |
| 59 | + $this->assertEquals( 200, $response->get_status() ); |
| 60 | + $data = $response->get_data(); |
| 61 | + $this->assertFalse( $data['should_expires'] ); |
| 62 | + $this->assertEmpty( $data['expires'] ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test POST saves expiration data with zero-padded date. |
| 67 | + */ |
| 68 | + public function test_post_expiration() { |
| 69 | + $user_id = $this->factory()->user->create( [ 'role' => 'editor' ] ); |
| 70 | + wp_set_current_user( $user_id ); |
| 71 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 72 | + |
| 73 | + $request = new WP_REST_Request( 'POST', '/clockwork/v1/post/' . $post_id . '/expiration' ); |
| 74 | + $request->set_body_params( [ |
| 75 | + 'should' => true, |
| 76 | + 'expires' => '2099-12-31 23:59:59', |
| 77 | + ] ); |
| 78 | + $response = $this->server->dispatch( $request ); |
| 79 | + |
| 80 | + $this->assertEquals( 200, $response->get_status() ); |
| 81 | + $data = $response->get_data(); |
| 82 | + $this->assertNotEmpty( $data['should_expires'] ); |
| 83 | + $this->assertEquals( '2099-12-31 23:59:59', $data['expires'] ); |
| 84 | + // Verify meta is saved. |
| 85 | + $this->assertEquals( '2099-12-31 23:59:59', get_post_meta( $post_id, '_tscp_expires', true ) ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test POST with non-zero-padded date returns error (WP 6.9 bug scenario). |
| 90 | + */ |
| 91 | + public function test_post_non_padded_date_rejected() { |
| 92 | + $user_id = $this->factory()->user->create( [ 'role' => 'editor' ] ); |
| 93 | + wp_set_current_user( $user_id ); |
| 94 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 95 | + |
| 96 | + $request = new WP_REST_Request( 'POST', '/clockwork/v1/post/' . $post_id . '/expiration' ); |
| 97 | + $request->set_body_params( [ |
| 98 | + 'should' => true, |
| 99 | + 'expires' => '2026-4-3 14:55:59', |
| 100 | + ] ); |
| 101 | + $response = $this->server->dispatch( $request ); |
| 102 | + |
| 103 | + $this->assertEquals( 400, $response->get_status() ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test POST with empty date succeeds (disabling expiration). |
| 108 | + */ |
| 109 | + public function test_post_empty_date() { |
| 110 | + $user_id = $this->factory()->user->create( [ 'role' => 'editor' ] ); |
| 111 | + wp_set_current_user( $user_id ); |
| 112 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 113 | + |
| 114 | + $request = new WP_REST_Request( 'POST', '/clockwork/v1/post/' . $post_id . '/expiration' ); |
| 115 | + $request->set_body_params( [ |
| 116 | + 'should' => false, |
| 117 | + 'expires' => '', |
| 118 | + ] ); |
| 119 | + $response = $this->server->dispatch( $request ); |
| 120 | + |
| 121 | + $this->assertEquals( 200, $response->get_status() ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Test unauthorized user cannot access. |
| 126 | + */ |
| 127 | + public function test_unauthorized_access() { |
| 128 | + wp_set_current_user( 0 ); |
| 129 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 130 | + |
| 131 | + $request = new WP_REST_Request( 'GET', '/clockwork/v1/post/' . $post_id . '/expiration' ); |
| 132 | + $response = $this->server->dispatch( $request ); |
| 133 | + |
| 134 | + $this->assertEquals( 401, $response->get_status() ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test invalid post type returns error. |
| 139 | + */ |
| 140 | + public function test_invalid_post_type() { |
| 141 | + $user_id = $this->factory()->user->create( [ 'role' => 'editor' ] ); |
| 142 | + wp_set_current_user( $user_id ); |
| 143 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 144 | + |
| 145 | + $request = new WP_REST_Request( 'GET', '/clockwork/v1/page/' . $post_id . '/expiration' ); |
| 146 | + $response = $this->server->dispatch( $request ); |
| 147 | + |
| 148 | + $this->assertEquals( 400, $response->get_status() ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test GET returns saved expiration data. |
| 153 | + */ |
| 154 | + public function test_get_saved_expiration() { |
| 155 | + $user_id = $this->factory()->user->create( [ 'role' => 'editor' ] ); |
| 156 | + wp_set_current_user( $user_id ); |
| 157 | + $post_id = $this->factory()->post->create( [ 'post_status' => 'publish' ] ); |
| 158 | + |
| 159 | + update_post_meta( $post_id, '_tscp_should_expire', 1 ); |
| 160 | + update_post_meta( $post_id, '_tscp_expires', '2099-06-15 10:30:00' ); |
| 161 | + |
| 162 | + $request = new WP_REST_Request( 'GET', '/clockwork/v1/post/' . $post_id . '/expiration' ); |
| 163 | + $response = $this->server->dispatch( $request ); |
| 164 | + |
| 165 | + $this->assertEquals( 200, $response->get_status() ); |
| 166 | + $data = $response->get_data(); |
| 167 | + $this->assertTrue( $data['should_expires'] ); |
| 168 | + $this->assertEquals( '2099-06-15 10:30:00', $data['expires'] ); |
| 169 | + } |
| 170 | +} |
0 commit comments