-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remote spec rule validator UI (#45099)
* Add Remote Spec Rule validator * Add changelog * Remove unused var * Add remote spec validator * Load composer autoload * Remove remote spec validation
- Loading branch information
Showing
15 changed files
with
243 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
34 changes: 34 additions & 0 deletions
34
...rce-beta-tester/api/remote-spec-validator/class-wca-test-helper-remote-spec-validator.php
This file contains 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,34 @@ | ||
<?php | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator; | ||
|
||
register_woocommerce_admin_test_helper_rest_route( | ||
'/remote-spec-validator/validate', | ||
'wca_test_helper_validate_remote_spec', | ||
array( | ||
'methods' => 'POST', | ||
'args' => array( | ||
'spec' => array( | ||
'description' => 'The remote spec to validate.', | ||
'type' => 'string', | ||
'required' => true, | ||
'sanitize_callback' => 'sanitize_text_field', | ||
), | ||
), | ||
) | ||
); | ||
|
||
/** | ||
* @param WP_REST_Request $request The full request data. | ||
*/ | ||
function wca_test_helper_validate_remote_spec( $request ) { | ||
$spec = json_decode( $request->get_param( 'spec' ) ); | ||
$rule_evaluator = new RuleEvaluator(); | ||
$result = [ | ||
'valid' => $rule_evaluator->evaluate( $spec ), | ||
]; | ||
|
||
return new WP_REST_RESPONSE( $result, 200 ); | ||
} |
4 changes: 4 additions & 0 deletions
4
plugins/woocommerce-beta-tester/changelog/add-remote-spec-rule-validator
This file contains 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,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add Remote Spec Rule validator |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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
6 changes: 6 additions & 0 deletions
6
plugins/woocommerce-beta-tester/src/remote-spec-validator/data/action-types.js
This file contains 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,6 @@ | ||
const TYPES = { | ||
VALIDATE: 'VALIDATE', | ||
SET_MESSAGE: 'SET_MESSAGE', | ||
}; | ||
|
||
export default TYPES; |
52 changes: 52 additions & 0 deletions
52
plugins/woocommerce-beta-tester/src/remote-spec-validator/data/actions.js
This file contains 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,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { apiFetch } from '@wordpress/data-controls'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import TYPES from './action-types'; | ||
import { API_NAMESPACE } from './constants'; | ||
|
||
export function* validate( jsonString ) { | ||
try { | ||
const response = yield apiFetch( { | ||
method: 'POST', | ||
path: `${ API_NAMESPACE }/remote-spec-validator/validate`, | ||
headers: { 'content-type': 'application/json' }, | ||
data: { | ||
spec: JSON.stringify( JSON.parse( jsonString ) ), | ||
}, | ||
} ); | ||
if ( response.valid ) { | ||
yield { | ||
type: TYPES.SET_MESSAGE, | ||
message: { | ||
type: 'notice notice-success', | ||
text: 'Validation passed', | ||
}, | ||
}; | ||
} else { | ||
yield { | ||
type: TYPES.SET_MESSAGE, | ||
message: { | ||
type: 'error', | ||
text: 'Validation failed', | ||
}, | ||
}; | ||
} | ||
} catch { | ||
throw new Error(); | ||
} | ||
} | ||
|
||
export function setMessage( type, text ) { | ||
return { | ||
type: TYPES.SET_MESSAGE, | ||
message: { | ||
type, | ||
text, | ||
}, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
plugins/woocommerce-beta-tester/src/remote-spec-validator/data/constants.js
This file contains 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,2 @@ | ||
export const STORE_KEY = 'wc-admin-helper/remote-spec-validator'; | ||
export const API_NAMESPACE = '/wc-admin-test-helper'; |
22 changes: 22 additions & 0 deletions
22
plugins/woocommerce-beta-tester/src/remote-spec-validator/data/index.js
This file contains 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,22 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { registerStore } from '@wordpress/data'; | ||
import { controls } from '@wordpress/data-controls'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as actions from './actions'; | ||
import * as resolvers from './resolvers'; | ||
import * as selectors from './selectors'; | ||
import reducer from './reducer'; | ||
import { STORE_KEY } from './constants'; | ||
|
||
export default registerStore( STORE_KEY, { | ||
actions, | ||
selectors, | ||
resolvers, | ||
controls, | ||
reducer, | ||
} ); |
25 changes: 25 additions & 0 deletions
25
plugins/woocommerce-beta-tester/src/remote-spec-validator/data/reducer.js
This file contains 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,25 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import TYPES from './action-types'; | ||
|
||
const DEFAULT_STATE = { | ||
message: { | ||
type: null, | ||
text: null, | ||
}, | ||
}; | ||
|
||
const reducer = ( state = DEFAULT_STATE, action ) => { | ||
switch ( action.type ) { | ||
case TYPES.SET_MESSAGE: | ||
return { | ||
...state, | ||
message: action.message, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default reducer; |
Empty file.
3 changes: 3 additions & 0 deletions
3
plugins/woocommerce-beta-tester/src/remote-spec-validator/data/selectors.js
This file contains 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,3 @@ | ||
export function getMessage( state ) { | ||
return state.message; | ||
} |
74 changes: 74 additions & 0 deletions
74
plugins/woocommerce-beta-tester/src/remote-spec-validator/index.js
This file contains 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,74 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { compose } from '@wordpress/compose'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { STORE_KEY } from './data/constants'; | ||
import './data'; | ||
|
||
function RemoteSpecValidator( { validate, message, setMessage } ) { | ||
const exampleText = JSON.stringify( | ||
[ | ||
{ | ||
type: 'plugin_version', | ||
plugin: 'woocommerce', | ||
version: '6.5.0-dev', | ||
operator: '>=', | ||
}, | ||
], | ||
null, | ||
4 | ||
); | ||
const [ spec, setSpec ] = useState( exampleText ); | ||
return ( | ||
<> | ||
<p>Paste your Remote Spec rule and click Validate button.</p> | ||
<div id="wc-admin-test-helper-remote-spec-validator"> | ||
<textarea | ||
value={ spec } | ||
onChange={ ( e ) => setSpec( e.target.value ) } | ||
/> | ||
{ message && message.text && ( | ||
<div className={ message.type }>{ message.text }</div> | ||
) } | ||
<input | ||
type="button" | ||
className="button btn-primary btn-validate" | ||
value="Validate" | ||
onClick={ () => { | ||
try { | ||
if ( JSON.parse( spec ) ) { | ||
setMessage( null, null ); | ||
} | ||
validate( spec ); | ||
} catch ( e ) { | ||
setMessage( 'error', 'Invalid JSON' ); | ||
} | ||
} } | ||
/> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default compose( | ||
withSelect( ( select ) => { | ||
const { getMessage } = select( STORE_KEY ); | ||
return { | ||
message: getMessage(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { validate, setMessage } = dispatch( STORE_KEY ); | ||
|
||
return { | ||
validate, | ||
setMessage, | ||
}; | ||
} ) | ||
)( RemoteSpecValidator ); |