From 9dec4e6156170b871bde27afb99f21730a28f62e Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 20 Aug 2013 16:54:31 +1200 Subject: [PATCH] CRM-13234 updated tests & examples for profile api improvements Note, always commit these separately so that the PR won't be left to stagnate due to an over-sized commit & the patches can be easily backported --- api/v3/examples/ProfileGetFields.php | 172 ++++++++++++++++++ .../{ProfileSet.php => ProfileSubmit.php} | 10 +- tests/phpunit/api/v3/ProfileTest.php | 113 +++++++----- 3 files changed, 245 insertions(+), 50 deletions(-) create mode 100644 api/v3/examples/ProfileGetFields.php rename api/v3/examples/{ProfileSet.php => ProfileSubmit.php} (93%) diff --git a/api/v3/examples/ProfileGetFields.php b/api/v3/examples/ProfileGetFields.php new file mode 100644 index 0000000000..c4ab716249 --- /dev/null +++ b/api/v3/examples/ProfileGetFields.php @@ -0,0 +1,172 @@ + 'submit', + 'profile_id' => 25, +); + +try{ + $result = civicrm_api3('profile', 'getfields', $params); +} +catch (CiviCRM_API3_Exception $e) { + // handle error here + $errorMessage = $e->getMessage(); + $errorCode = $e->getErrorCode(); + $errorData = $e->getExtraParams(); + return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData); +} + +return $result; +} + +/** + * Function returns array of result expected from previous function + */ +function profile_getfields_expectedresult(){ + + $expectedResult = array( + 'is_error' => 0, + 'version' => 3, + 'count' => 9, + 'values' => array( + 'first_name' => array( + 'name' => 'first_name', + 'type' => 2, + 'title' => 'First Name', + 'maxlength' => 64, + 'size' => 30, + 'import' => true, + 'where' => 'civicrm_contact.first_name', + 'headerPattern' => '/^first|(f(irst\s)?name)$/i', + 'dataPattern' => '/^\w+$/', + 'export' => true, + ), + 'last_name' => array( + 'name' => 'last_name', + 'type' => 2, + 'title' => 'Last Name', + 'maxlength' => 64, + 'size' => 30, + 'import' => true, + 'where' => 'civicrm_contact.last_name', + 'headerPattern' => '/^last|(l(ast\s)?name)$/i', + 'dataPattern' => '/^\w+(\s\w+)?+$/', + 'export' => true, + ), + 'email' => array( + 'name' => 'email', + 'type' => 2, + 'title' => 'Email', + 'maxlength' => 64, + 'size' => 20, + 'import' => true, + 'where' => 'civicrm_email.email', + 'headerPattern' => '/e.?mail/i', + 'dataPattern' => '/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/', + 'export' => true, + 'rule' => 'email', + 'api.required' => 1, + ), + 'phone-1-1' => array( + 'name' => 'phone', + 'type' => 2, + 'title' => 'Phone', + 'maxlength' => 32, + 'size' => 20, + 'import' => true, + 'where' => 'civicrm_phone.phone', + 'headerPattern' => '/phone/i', + 'dataPattern' => '/^[\d\(\)\-\.\s]+$/', + 'export' => true, + 'api.required' => 1, + ), + 'country-1' => array( + 'name' => 'country_id', + 'type' => 1, + 'title' => 'Country', + 'FKClassName' => 'CRM_Core_DAO_Country', + 'pseudoconstant' => array( + 'table' => 'civicrm_country', + 'keyColumn' => 'id', + 'labelColumn' => 'name', + 'nameColumn' => 'iso_code', + ), + ), + 'state_province-1' => array( + 'name' => 'state_province_id', + 'type' => 1, + 'title' => 'State', + 'FKClassName' => 'CRM_Core_DAO_StateProvince', + 'pseudoconstant' => array( + 'table' => 'civicrm_state_province', + 'keyColumn' => 'id', + 'labelColumn' => 'name', + ), + ), + 'postal_code-1' => array( + 'name' => 'postal_code', + 'type' => 2, + 'title' => 'Postal Code', + 'maxlength' => 12, + 'size' => 12, + 'import' => true, + 'where' => 'civicrm_address.postal_code', + 'headerPattern' => '/postal|zip/i', + 'dataPattern' => '/\d?\d{4}(-\d{4})?/', + 'export' => true, + ), + 'custom_1' => array( + 'label' => '_addCustomFieldToProfile', + 'groupTitle' => '_addCustomFie', + 'data_type' => 'String', + 'html_type' => 'Text', + 'default_value' => 'defaultValue', + 'text_length' => '', + 'options_per_line' => '', + 'custom_group_id' => '1', + 'extends' => 'Contact', + 'is_search_range' => 0, + 'extends_entity_column_value' => '', + 'extends_entity_column_id' => '', + 'is_view' => 0, + 'is_multiple' => 0, + 'option_group_id' => '', + 'date_format' => '', + 'time_format' => '', + 'name' => 'custom_1', + ), + 'profile_id' => array( + 'api.required' => true, + ), + ), +); + + return $expectedResult; +} + + +/* +* This example has been generated from the API test suite. The test that created it is called +* +* testGetFields and can be found in +* http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/ProfileTest.php +* +* You can see the outcome of the API tests at +* http://tests.dev.civicrm.org/trunk/results-api_v3 +* +* To Learn about the API read +* http://book.civicrm.org/developer/current/techniques/api/ +* +* and review the wiki at +* http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs +* +* Read more about testing here +* http://wiki.civicrm.org/confluence/display/CRM/Testing +* +* API Standards documentation: +* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards +*/ \ No newline at end of file diff --git a/api/v3/examples/ProfileSet.php b/api/v3/examples/ProfileSubmit.php similarity index 93% rename from api/v3/examples/ProfileSet.php rename to api/v3/examples/ProfileSubmit.php index 7d4e51b119..910c21ec7d 100644 --- a/api/v3/examples/ProfileSet.php +++ b/api/v3/examples/ProfileSubmit.php @@ -1,9 +1,9 @@ 25, 'contact_id' => 1, @@ -16,7 +16,7 @@ $params = array( ); try{ - $result = civicrm_api3('profile', 'set', $params); + $result = civicrm_api3('profile', 'submit', $params); } catch (CiviCRM_API3_Exception $e) { // handle error here @@ -32,7 +32,7 @@ return $result; /** * Function returns array of result expected from previous function */ -function profile_set_expectedresult(){ +function profile_submit_expectedresult(){ $expectedResult = array( 'is_error' => 0, @@ -99,7 +99,7 @@ function profile_set_expectedresult(){ /* * This example has been generated from the API test suite. The test that created it is called * -* testProfileSet and can be found in +* testProfileSubmit and can be found in * http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/ProfileTest.php * * You can see the outcome of the API tests at diff --git a/tests/phpunit/api/v3/ProfileTest.php b/tests/phpunit/api/v3/ProfileTest.php index a3169a202a..c604189c49 100644 --- a/tests/phpunit/api/v3/ProfileTest.php +++ b/tests/phpunit/api/v3/ProfileTest.php @@ -38,6 +38,7 @@ require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php'; */ class api_v3_ProfileTest extends CiviUnitTestCase { protected $_apiversion; + protected $_profileID; function get_info() { return array( 'name' => 'Profile Test', @@ -270,25 +271,43 @@ class api_v3_ProfileTest extends CiviUnitTestCase { } } - /////////////// test $this->callAPISuccess3_profile_set ////////////////// - /** - * check with no array + * Check getfields works & gives us our fields + */ + function testGetFields() { + $this->_createIndividualProfile(); + $this->_addCustomFieldToProfile($this->_profileID); + $result = $this->callAPIAndDocument('profile', 'getfields', array('action' => 'submit', 'profile_id' => 25), __FUNCTION__, __FILE__, + 'demonstrates retrieving profile fields passing in an id'); + $this->assertArrayKeyExists('first_name', $result['values']); + $this->assertEquals('2', $result['values']['first_name']['type']); + $this->assertEquals('Email', $result['values']['email']['title']); + $this->assertEquals('civicrm_state_province', $result['values']['state_province-1']['pseudoconstant']['table']); + $this->assertEquals('defaultValue', $result['values']['custom_1']['default_value']); + $this->assertFalse(array_key_exists('participant_status', $result['values'])); + } + /** + * Check getfields works & gives us our fields */ - function testProfileSetNoArray() { - $params = NULL; - $result = $this->callAPIFailure('profile', 'set', $params); - $this->assertEquals($result['error_message'], 'Input variable `params` is not an array'); + function testGetFieldsParticipantProfile() { + $result = $this->callAPISuccess('profile', 'getfields', array( + 'action' => 'submit', + 'profile_id' => 'participant_status', + 'get_options' => 'all') + ); + $this->assertTrue(array_key_exists('participant_status', $result['values'])); + $this->assertEquals('Attended', $result['values']['participant_status']['options'][2]); } + /////////////// test $this->callAPISuccess3_profile_set ////////////////// /** * check Without ProfileId */ - function testProfileSetWithoutProfileId() { + function testProfileSubmitWithoutProfileId() { $params = array( 'contact_id' => 1, ); - $result = $this->callAPIFailure('profile', 'set', $params, + $result = $this->callAPIFailure('profile', 'submit', $params, 'Mandatory key(s) missing from params array: profile_id' ); } @@ -296,18 +315,18 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * check with no invalid profile Id */ - function testProfileSetInvalidProfileId() { + function testProfileSubmitInvalidProfileId() { $params = array( 'contact_id' => 1, 'profile_id' => 1000, ); - $result = $this->callAPIFailure('profile', 'set', $params); + $result = $this->callAPIFailure('profile', 'submit', $params); } /** * check with missing required field in profile */ - function testProfileSetCheckProfileRequired() { + function testProfileSubmitCheckProfileRequired() { $pofileFieldValues = $this->_createIndividualContact(); current($pofileFieldValues); $contactId = key($pofileFieldValues); @@ -323,7 +342,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $updateParams ); - $result = $this->callAPIFailure('profile', 'set', $params, + $result = $this->callAPIFailure('profile', 'submit', $params, 'Missing required parameters for profile id 25: email-Primary' ); } @@ -331,7 +350,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * check with success */ - function testProfileSet() { + function testProfileSubmit() { $pofileFieldValues = $this->_createIndividualContact(); current($pofileFieldValues); $contactId = key($pofileFieldValues); @@ -350,7 +369,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { 'contact_id' => $contactId, ), $updateParams); - $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__); $getParams = array( 'profile_id' => 25, @@ -367,35 +386,30 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /* * check contact activity profile without activity id */ - function testContactActivitySetWithoutActivityId() { + function testContactActivitySubmitWithoutActivityId() { list($params, $expected) = $this->_createContactWithActivity(); $params = array_merge($params, $expected); unset($params['activity_id']); - $result = $this->callAPIFailure('profile', 'set', $params); + $result = $this->callAPIFailure('profile', 'submit', $params); $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id'); - - $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact')); } /* * check contact activity profile wrong activity id */ - function testContactActivitySetWrongActivityId() { + function testContactActivitySubmitWrongActivityId() { list($params, $expected) = $this->_createContactWithActivity(); - $params = array_merge($params, $expected); $params['activity_id'] = 100001; - $result = $this->callAPIFailure('profile', 'set', $params); + $result = $this->callAPIFailure('profile', 'submit', $params); $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).'); - - $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact')); } /* * check contact activity profile with wrong activity type */ - function testContactActivitySetWrongActivityType() { + function testContactActivitySubmitWrongActivityType() { //flush cache by calling with reset $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); @@ -421,14 +435,14 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $params = array_merge($params, $expected); $params['activity_id'] = $activityValues['id']; - $result = $this->callAPIFailure('profile', 'set', $params, + $result = $this->callAPIFailure('profile', 'submit', $params, 'This activity cannot be edited or viewed via this profile.'); } /* * check contact activity profile with success */ - function testContactActivitySetSuccess() { + function testContactActivitySubmitSuccess() { list($params, $expected) = $this->_createContactWithActivity(); $updateParams = array( @@ -442,7 +456,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { 'activity_status_id' => '2', ); $profileParams = array_merge($params, $updateParams); - $profile = $this->callAPISuccess('profile', 'set', $profileParams); + $profile = $this->callAPISuccess('profile', 'submit', $profileParams); $result = $this->callAPISuccess('profile', 'get', $params); foreach ($updateParams as $profileField => $value) { @@ -566,23 +580,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { ); $contactID = $this->individualCreate($contactParams); - - // Create new profile having group_type: Contact,Individual - $op = new PHPUnit_Extensions_Database_Operation_Insert(); - $op->execute($this->_dbconn, - new PHPUnit_Extensions_Database_DataSet_XMLDataSet( - dirname(__FILE__) . "/dataset/uf_group_25.xml" - ) - ); - // Create Contact + Idividual fields for profile - $op = new PHPUnit_Extensions_Database_Operation_Insert(); - $op->execute($this->_dbconn, - new PHPUnit_Extensions_Database_DataSet_XMLDataSet( - dirname(__FILE__) . "/dataset/uf_field_uf_group_25.xml" - ) - ); - - + $this->_createIndividualProfile(); // expected result of above created profile with contact Id $contactId $profileData[$contactID] = array( 'first_name' => 'abc1', @@ -671,5 +669,30 @@ class api_v3_ProfileTest extends CiviUnitTestCase { return array($profileParams, $expected); } + /** + * Create a profile + */ + function _createIndividualProfile() { + // Create new profile having group_type: Contact,Individual + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + new PHPUnit_Extensions_Database_DataSet_XMLDataSet( + dirname(__FILE__) . "/dataset/uf_group_25.xml" + ) + ); + // Create Contact + Idividual fields for profile + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + new PHPUnit_Extensions_Database_DataSet_XMLDataSet( + dirname(__FILE__) . "/dataset/uf_field_uf_group_25.xml" + ) + ); + $this->_profileID = 25; + } + + function _addCustomFieldToProfile($profileID) { + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, ''); + $this->uFFieldCreate(array('uf_group_id' => $profileID, 'field_name' => 'custom_' . $ids['custom_field_id'], 'contact_type' => 'Contact')); + } } -- 2.25.1