-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRestApiEndpointsTest.php
More file actions
163 lines (146 loc) · 4.77 KB
/
Copy pathRestApiEndpointsTest.php
File metadata and controls
163 lines (146 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* REST API endpoints behavior tests.
*
* @package Accessibility_Checker
*/
/**
* Test class for REST API endpoints.
*
* @group rest
*/
class RestApiEndpointsTest extends WP_UnitTestCase {
/**
* Admin user ID.
*
* @var int
*/
protected static $admin_id;
/**
* Limited user ID.
*
* @var int
*/
protected static $limited_id;
/**
* Post ID used for tests.
*
* @var int
*/
protected static $post_id;
/**
* REST server instance for dispatching requests.
*
* @var WP_REST_Server|null
*/
private $server;
/**
* Set up before each test.
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
// Initialize REST routes for each test.
do_action( 'init' );
do_action( 'rest_api_init' );
$this->server = rest_get_server();
}
/**
* Clean up after each test.
*
* @return void
*/
protected function tearDown(): void {
// Reset current user between tests.
wp_set_current_user( 0 );
parent::tearDown();
}
/**
* Create shared fixtures for this test class.
*
* @param WP_UnitTest_Factory $factory Factory instance.
* @return void
*/
public static function wpSetUpBeforeClass( $factory ) {
// Ensure posts are scannable by plugin.
update_option( 'edac_post_types', [ 'post' ] );
// Ensure plugin DB table exists for tests (normally created via admin_init).
( new \EDAC\Admin\Update_Database() )->edac_update_database();
self::$admin_id = $factory->user->create( [ 'role' => 'administrator' ] );
self::$limited_id = $factory->user->create( [ 'role' => 'subscriber' ] );
// Give limited user edit_posts but not edit_others_posts so they cannot edit this post.
$user = new WP_User( self::$limited_id );
$user->add_cap( 'edit_posts' );
self::$post_id = $factory->post->create(
[
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => self::$admin_id,
'post_title' => 'EDAC PHPUnit Post',
'post_content' => '<main><h1>Title</h1><p>Img without alt <img src="/wp-includes/images/media/default.png"></p></main>',
]
);
}
/**
* Verify permissions for saving post scan results.
*
* @return void
*/
public function test_rest_post_scan_results_permissions() {
$this->assertNotNull( $this->server );
// Minimal violations payload similar to scanner output.
$violations = [
[
'ruleId' => 'image-alt',
'html' => '<img src="/wp-includes/images/media/default.png">',
'impact' => 'error',
'landmark' => null,
],
];
// Admin can POST results for the post.
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/accessibility-checker/v1/post-scan-results/' . self::$post_id );
$request->set_param( 'id', self::$post_id );
$request->set_param( 'violations', $violations );
$response = $this->server->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Admin should be allowed to save scan results.' );
$data = $response->get_data();
$this->assertIsArray( $data );
// Limited user cannot POST results for the admin-owned post.
wp_set_current_user( self::$limited_id );
$request2 = new WP_REST_Request( 'POST', '/accessibility-checker/v1/post-scan-results/' . self::$post_id );
$request2->set_param( 'id', self::$post_id );
$request2->set_param( 'violations', $violations );
$response2 = $this->server->dispatch( $request2 );
$this->assertSame( 403, $response2->get_status(), 'Limited user must not be allowed to save scan results for another user\'s post.' );
}
/**
* Verify permissions for clearing issues for a post.
*
* @return void
*/
public function test_rest_clear_issues_permissions() {
$this->assertNotNull( $this->server );
// Admin can clear issues.
wp_set_current_user( self::$admin_id );
$r1 = new WP_REST_Request( 'POST', '/accessibility-checker/v1/clear-issues/' . self::$post_id );
$r1->set_param( 'id', self::$post_id );
$r1->set_body( wp_json_encode( [ 'flush' => true ] ) );
$r1->set_header( 'Content-Type', 'application/json' );
$resp1 = $this->server->dispatch( $r1 );
$this->assertSame( 200, $resp1->get_status(), 'Admin should be allowed to clear issues.' );
$body1 = $resp1->get_data();
$this->assertIsArray( $body1 );
$this->assertArrayHasKey( 'success', $body1 );
$this->assertTrue( $body1['success'] );
// Limited user cannot clear issues for a post they cannot edit.
wp_set_current_user( self::$limited_id );
$r2 = new WP_REST_Request( 'POST', '/accessibility-checker/v1/clear-issues/' . self::$post_id );
$r2->set_param( 'id', self::$post_id );
$r2->set_body( wp_json_encode( [ 'flush' => true ] ) );
$r2->set_header( 'Content-Type', 'application/json' );
$resp2 = $this->server->dispatch( $r2 );
$this->assertSame( 403, $resp2->get_status(), 'Limited user must not be allowed to clear issues.' );
}
}