Add API for a sync of one single campaign

This commit is contained in:
Marc Michalsky forumZFD 2020-12-08 13:01:41 +01:00
parent e97ed21937
commit 1908a5b656
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
2 changed files with 98 additions and 0 deletions

View file

@ -0,0 +1,45 @@
<?php
use CRM_TwingleCampaign_ExtensionUtil as E;
/**
* TwingleSync.Singlesync API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
*
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
*/
function _civicrm_api3_twingle_sync_Singlesync_spec(&$spec) {
$spec['magicword']['api.required'] = 1;
}
/**
* TwingleSync.Singlesync API
*
* @param array $params
*
* @return array
* API result descriptor
*
* @see civicrm_api3_create_success
*
* @throws API_Exception
*/
function civicrm_api3_twingle_sync_Singlesync($params) {
if (array_key_exists('magicword', $params) && $params['magicword'] == 'sesame') {
$returnValues = array(
// OK, return several data rows
12 => ['id' => 12, 'name' => 'Twelve'],
34 => ['id' => 34, 'name' => 'Thirty four'],
56 => ['id' => 56, 'name' => 'Fifty six'],
);
// ALTERNATIVE: $returnValues = []; // OK, success
// ALTERNATIVE: $returnValues = ["Some value"]; // OK, return a single value
// Spec: civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL)
return civicrm_api3_create_success($returnValues, $params, 'TwingleSync', 'Singlesync');
}
else {
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
}
}

View file

@ -0,0 +1,53 @@
<?php
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
/**
* TwingleSync.Singlesync API Test Case
* This is a generic test class implemented with PHPUnit.
* @group headless
*/
class api_v3_TwingleSync_SinglesyncTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
use \Civi\Test\Api3TestTrait;
/**
* Set up for headless tests.
*
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
*
* See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest
*/
public function setUpHeadless() {
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}
/**
* The setup() method is executed before the test is executed (optional).
*/
public function setUp() {
parent::setUp();
}
/**
* The tearDown() method is executed after the test was executed (optional)
* This can be used for cleanup.
*/
public function tearDown() {
parent::tearDown();
}
/**
* Simple example test case.
*
* Note how the function name begins with the word "test".
*/
public function testApiExample() {
$result = civicrm_api3('TwingleSync', 'Singlesync', array('magicword' => 'sesame'));
$this->assertEquals('Twelve', $result['values'][12]['name']);
}
}