implement TwingleForm API

This commit is contained in:
Marc Michalsky forumZFD 2020-12-07 17:25:11 +01:00
parent 4b6b1556d5
commit fc545b7f1c
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
3 changed files with 181 additions and 0 deletions

110
api/v3/TwingleForm/Get.php Normal file
View file

@ -0,0 +1,110 @@
<?php
use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM\TwingleCampaign\Utils\ExtensionCache as Cache;
include_once E::path() . '/CRM/TwingleCampaign/Utils/ExtensionCache.php';
/**
* TwingleForm.Get 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_form_Get_spec(array &$spec) {
$spec['id'] = [
'name' => 'id',
'title' => E::ts('TwingleProject ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('ID of the TwingleProject campaign'),
];
$spec['name'] = [
'name' => 'name',
'title' => E::ts('TwingleProject name'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Name of the TwingleProject campaign'),
];
$spec['title'] = [
'name' => 'title',
'title' => E::ts('TwingleProject title'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Title of the TwingleProject campaign'),
];
$spec['twingle_project_type'] = [
'name' => 'twingle_project_type',
'title' => E::ts('TwingleProject type'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Project type can be default, event or membership'),
];
}
/**
* TwingleForm.Get API
*
* @param array $params
*
* @return array
* API result descriptor
*
* @throws API_Exception
* @see civicrm_api3_create_success
*
*/
function civicrm_api3_twingle_form_Get(array $params) {
// Get custom fields
$custom_field_mapping = Cache::getInstance()->getCustomFieldMapping();
$cf_twingle_project_id = $custom_field_mapping['twingle_project_id'];
$cf_twingle_project_type = $custom_field_mapping['twingle_project_type'];
$cf_twingle_project_widget = $custom_field_mapping['twingle_project_widget'];
$cf_twingle_project_counter = $custom_field_mapping['twingle_project_counter'];
// Replace twingle_project_type key with custom field name
if (key_exists('twingle_project_type', $params)) {
$params[$cf_twingle_project_type] = $params['twingle_project_type'];
unset($params['twingle_project_type']);
}
$request = [
'sequential' => 1,
'campaign_type_id' => "twingle_project"
];
foreach($params as $param) {
array_push($request, $param);
}
try {
$result = civicrm_api3('Campaign', 'get', $request);
if ($result['is_error'] == 0) {
$returnValues = [];
foreach($result['values'] as $value) {
array_push(
$returnValues,
[
'id' => $value['id'],
'twingle_project_id' => $value[$cf_twingle_project_id],
'title' => $value['title'],
'name' => $value['name'],
'project_type' => $value[$cf_twingle_project_type],
'embed_code' => $value[$cf_twingle_project_widget],
'counter' => $value[$cf_twingle_project_counter]
]
);
}
return civicrm_api3_create_success($returnValues, $params, 'TwingleForm', 'Get');
}
else {
// TODO: Edit error message
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
}
} catch (Exception $e) {
throw new API_Exception(/*error_message*/ 'Everyone knows that the magicword is "sesame"', /*error_code*/ 'magicword_incorrect');
}
}

18
phpunit.xml.dist Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
</whitelist>
</filter>
<listeners>
<listener class="Civi\Test\CiviTestListener">
<arguments/>
</listener>
</listeners>
</phpunit>

View file

@ -0,0 +1,53 @@
<?php
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
/**
* TwingleForm.Get API Test Case
* This is a generic test class implemented with PHPUnit.
* @group headless
*/
class api_v3_TwingleForm_GetTest 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('TwingleForm', 'Get', array('magicword' => 'sesame'));
$this->assertEquals('Twelve', $result['values'][12]['name']);
}
}