-
Notifications
You must be signed in to change notification settings - Fork 19
Be more explicit with who can save and clear issues for a given post #1202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
pattonwebz
wants to merge
13
commits into
develop
from
william/no-ticket/enhance-capability-checking
Closed
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3396532
Pass flag for if user can edit the current post to the frontend highl…
pattonwebz 27a248e
Add a helper for permission checking that checks if the user can edit…
pattonwebz c735aaf
Ensure user can edit the current post when calling endpoint saving is…
pattonwebz 52487c4
Add REST API endpoints behavior tests for permissions on post scan re…
pattonwebz 2c71f90
Add test for limited user managing their own post
pattonwebz 897ebbd
Add required validation and sanitization for post ID arguments
pattonwebz fa863b0
Handle correct passed param types and return types
pattonwebz 0f4b8b0
Fix code formatting for numeric validation callbacks
pattonwebz 35227d5
Enhance API response validation in tests
pattonwebz b7ed073
updated: permission callback to require 'edit_posts' capability for a…
SteveJonesDev aed5128
updated: permission callbacks to require 'edit_posts' capability for …
SteveJonesDev 3e32884
updated: permission callbacks to require 'edit_posts' capability for …
SteveJonesDev 44983e0
updated: permission callback to require 'publish_posts' capability fo…
SteveJonesDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
tests/phpunit/includes/classes/RestApiEndpointsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| <?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 ); | ||
| $this->assertArrayHasKey( 'success', $data ); | ||
| $this->assertTrue( $data['success'] ); | ||
| $this->assertArrayHasKey( 'id', $data ); | ||
| $this->assertSame( self::$post_id, $data['id'] ); | ||
|
|
||
| // 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'] ); | ||
| $this->assertArrayHasKey( 'id', $body1 ); | ||
| $this->assertSame( self::$post_id, $body1['id'] ); | ||
| $this->assertArrayHasKey( 'flushed', $body1 ); | ||
| $this->assertTrue( $body1['flushed'] ); | ||
|
|
||
| // 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.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Verify that a limited user can manage their own post. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function test_limited_user_can_manage_own_post() { | ||
| wp_set_current_user( self::$limited_id ); | ||
| $own_post_id = self::factory()->post->create( | ||
| [ | ||
| 'post_type' => 'post', | ||
| 'post_status' => 'draft', | ||
| 'post_author' => self::$limited_id, | ||
| ] | ||
| ); | ||
|
|
||
| // Save scan results. | ||
| $req1 = new WP_REST_Request( 'POST', '/accessibility-checker/v1/post-scan-results/' . $own_post_id ); | ||
| $req1->set_param( 'id', $own_post_id ); | ||
| $req1->set_param( | ||
| 'violations', | ||
| [ | ||
| [ | ||
| 'ruleId' => 'image-alt', | ||
| 'html' => '<img>', | ||
| ], | ||
| ] | ||
| ); | ||
| $resp1 = $this->server->dispatch( $req1 ); | ||
| $this->assertSame( 200, $resp1->get_status() ); | ||
| $data1 = $resp1->get_data(); | ||
| $this->assertIsArray( $data1 ); | ||
| $this->assertArrayHasKey( 'success', $data1 ); | ||
| $this->assertTrue( $data1['success'] ); | ||
|
|
||
| // Clear issues. | ||
| $req2 = new WP_REST_Request( 'POST', '/accessibility-checker/v1/clear-issues/' . $own_post_id ); | ||
| $req2->set_param( 'id', $own_post_id ); | ||
| $req2->set_body( wp_json_encode( [ 'flush' => true ] ) ); | ||
| $req2->set_header( 'Content-Type', 'application/json' ); | ||
| $resp2 = $this->server->dispatch( $req2 ); | ||
| $this->assertSame( 200, $resp2->get_status() ); | ||
| $data2 = $resp2->get_data(); | ||
| $this->assertIsArray( $data2 ); | ||
| $this->assertArrayHasKey( 'success', $data2 ); | ||
| $this->assertTrue( $data2['success'] ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.