Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Helper/Helper_Options_Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,17 @@ public function get_registered_fields() {
'additionalProperties' => false,
'properties' => [
'fieldId' => [
'type' => 'string',
'type' => 'string',
'required' => true,
],
'operator' => [
'type' => 'string',
'enum' => [ 'is', 'isnot', '<>', 'not in', 'in', '>', '<', 'contains', 'starts_with', 'ends_with', 'like', '>=', '<=' ],
'type' => 'string',
'enum' => [ 'is', 'isnot', '<>', 'not in', 'in', '>', '<', 'contains', 'starts_with', 'ends_with', 'like', '>=', '<=' ],
'required' => true,
],
'value' => [
'type' => 'string',
'type' => 'string',
'required' => true,
],
],
],
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/integration/Rest/Test_Rest_Form_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'logicType', $args['conditionalLogic']['properties'] );
$this->assertArrayHasKey( 'rules', $args['conditionalLogic']['properties'] );

/* Each conditional logic rule property should be flagged as required in the schema */
$rule_properties = $args['conditionalLogic']['properties']['rules']['items']['properties'];
$this->assertTrue( $rule_properties['fieldId']['required'] );
$this->assertTrue( $rule_properties['operator']['required'] );
$this->assertTrue( $rule_properties['value']['required'] );

$this->assertContains( 'A4', $args['pdf_size']['enum'] );
$this->assertContains( 'CUSTOM', $args['pdf_size']['enum'] );

Expand Down Expand Up @@ -945,6 +951,35 @@ public function test_input_validation_update() {
$this->assertSame( 'rest_invalid_hex_color', $data['data']['details']['font_colour']['code'] );
}

/**
* Check the REST API rejects conditional logic rules that are missing a required property
*/
public function test_input_validation_conditional_logic_rule_required() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'POST', '/gravity-pdf/v1/form/' . $this->form_id );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );

/* The single rule omits the required "value" property */
$request->set_body_params( [
'name' => 'Label',
'template' => 'rubix',
'conditionalLogic' => [
'actionType' => 'show',
'logicType' => 'any',
'rules' => [
[ 'fieldId' => '7', 'operator' => 'is' ],
],
],
] );

$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 400, $response->get_status() );
$this->assertSame( 'rest_property_required', $data['data']['details']['conditionalLogic']['code'] );
}

/**
* Check the REST API auto-validates inputs on the DELETE endpoint
*/
Expand Down
Loading