Skip to content
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

CRM-18981 Add a 'validate' action to CiviCRM API #8624

Merged
merged 4 commits into from
Aug 27, 2016

Conversation

saurabhbatra96
Copy link
Contributor

@saurabhbatra96 saurabhbatra96 commented Jun 26, 2016

@saurabhbatra96
Copy link
Contributor Author

saurabhbatra96 commented Jul 21, 2016

@eileenmcnaughton what do you imagine a test on this would look like?
PS: Also, does this approach look OK?

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Jul 26, 2016

I think what we want is to test that we get multiple failure information so something like

function testValidateContribution() {
  $validation = $this->callApiSuccess('Contribution', 'validate', array('action' => 'create', 'params' => array('financial_type_id' => 'Monopoly Money'));

$this->assertEquals(array('financial_type_id' => 'Invalid Option', 'contact_id' => 'Missing Required', $validation['values']);
}

It would be possible to do a syntax conformance test - but it may add little value over a couple of single entity tests.

*/
function _civicrm_api3_validate($entity, $action, $params) {
$errors = array();
$fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => $action));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason not to use civicrm_api3 here?

@saurabhbatra96
Copy link
Contributor Author

@eileenmcnaughton Going to add a test, but did some basic testing from the API explorer and this looks good!

@eileenmcnaughton
Copy link
Contributor

tests are showing a typo
===============================[ php -l ]===============================
PHP Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ')' in api/v3/utils.php on line 1555
Errors parsing api/v3/utils.php
===============================[ phpcs ]===============================
Found PHP errors

@@ -1538,6 +1538,87 @@ function _civicrm_api3_custom_data_get(&$returnArray, $checkPermission, $entity,
}

/**
* Used by the Validate API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super trivial - but the full whitespace standard would have a blank line here

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Aug 1, 2016

This looks good. I still have an urge to have validate throw an exception (from which the errors can be extracted) rather than return success when it finds errors - but maybe I'm overly attached to exceptions @totten should probably talk me down from that.

The second thing is that ideally one the validate api is all good we would alter the main api wrapper to call the validate api rather than having 2 paths & having the wrapper not have the benefit of a consolidated error. NB this does not have to be done as part of this PR - it's just where it would be good to take this

@saurabhbatra96 saurabhbatra96 force-pushed the validate-api branch 2 times, most recently from 3614a86 to 52265c3 Compare August 1, 2016 07:54
@saurabhbatra96
Copy link
Contributor Author

saurabhbatra96 commented Aug 1, 2016

Are the tests not written correctly? Can't seem to run them without running into "error in api call" while the API works just fine in the API explorer.

Also, reminder to self: complete the comments before merging.

@eileenmcnaughton
Copy link
Contributor

These are coming out of the syntaxconformance tests - notably the getfields-title test goes through every entity-action combo to make sure the declared fields have titles

* @param array $params
*/
function _civicrm_api3_generic_validate_spec(&$params) {
$params['action']['api.required'] = TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add $params['action']['title'] = ts('blah blah');

for a huge test improvement

'code' => "mandatory_missing",
),
);
$this->assertEquals($validation['values'][0], $expectedOut);
Copy link
Contributor Author

@saurabhbatra96 saurabhbatra96 Aug 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$validation['values'][0] - this nesting seems like a necessary evil. Cannot return the output in this dictionary format without using it:

    {
    "is_error": 0,
    "version": 3,
    "count": 1,
    "id": 0,
    "values": [
        {
            "contact_type": {
                "message": "Mandatory key(s) missing from params array: contact_type",
                "code": "mandatory_missing"
            }
        }
    ]
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm - should it be up one?

ie

$result['values']['contact_type'] => you would need to add field_name to the response array for when it's called with 'is_sequential'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up for a chat on mattermost?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just how civicrm_api3_create_success arranges the API output.
I could instead go for:

{
    "is_error": 0,
    "version": 3,
    "count": 1,
    "id": 0,
    "values": [
           {
                "name": "total_amount",
                "message": "Mandatory key(s) missing from params array: total_amount",
                "code": "mandatory_missing"
            },
            {
                "name": "contact_id",
                "message": "Mandatory key(s) missing from params array: contact_id",
                "code": "mandatory_missing"
            }
    ]
}

@saurabhbatra96 saurabhbatra96 changed the title [WIP] CRM-18981 Add a 'validate' action to CiviCRM API CRM-18981 Add a 'validate' action to CiviCRM API Aug 3, 2016
@saurabhbatra96
Copy link
Contributor Author

Looks good to merge IMO.

@eileenmcnaughton eileenmcnaughton merged commit c6bf546 into civicrm:master Aug 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants