2 // vim: set si ai expandtab tabstop=4 shiftwidth=4 softtabstop=4:
5 * File to v3 APIs for Standards compliance
9 * @copyright Copyright CiviCRM LLC (C) 2009
10 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
11 * GNU Affero General Public License version 3
15 * This file is part of CiviCRM
17 * CiviCRM is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Affero General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
22 * CiviCRM is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
27 * You should have received a copy of the GNU Affero General Public
28 * License along with this program. If not, see
29 * <http://www.gnu.org/licenses/>.
33 * Include class definitions
35 require_once 'CiviTest/CiviUnitTestCase.php';
38 * Test APIv3 civicrm_activity_* functions
40 * @package CiviCRM_APIv3
41 * @todo determine where help functions should sit (here or 'up the tree'), & best way to define API dir
43 class api_v3_APIStandardsTest
extends CiviUnitTestCase
{
45 protected $_apiversion;
47 protected $_functionFiles;
48 protected $_regexForGettingAPIStdFunctions;
49 /* This test case doesn't require DB reset */
53 public $DBResetRequired = FALSE;
56 * Test setup for every test
58 * Connect to the database, truncate the tables that will be used
59 * and redirect stdin to a temporary file
61 public function setUp() {
62 // Connect to the database
64 $this->useTransaction(TRUE);
65 $this->_apiversion
= 3;
66 $this->_apiDir
= "../api/v3/";
67 $this->_functionFiles
= array('Entity.php', 'utils.php');
69 //should possibly insert variable rather than '3' in below
70 $this->_regexForGettingAPIStdFunctions
= '/^civicrm_api3_ #starts with civicrm_api_3 (ignore internal functions)
71 .*_ #any number of characters up to the last _
72 # negative look ahead on string getfields
74 // functions to skip from utils.php mainly since they get sucked in via
75 // a require chain in the include files
76 $this->_skipFunctionList
= array(
77 // location api is deprecated
78 'civicrm_api3_location_create',
79 'civicrm_api3_location_get',
80 'civicrm_api3_location_delete',
81 // most of these seem to be internal functions
82 'civicrm_api3_entity_create',
83 'civicrm_api3_entity_delete',
84 'civicrm_api3_survey_respondant_count',
85 // functions from utils.php
86 'civicrm_api3_verify_one_mandatory',
87 'civicrm_api3_verify_mandatory',
88 'civicrm_api3_get_dao',
89 'civicrm_api3_create_success',
90 'civicrm_api3_create_error',
92 'civicrm_api3_check_contact_dedupe',
93 'civicrm_api3_api_check_permission',
98 * Test checks that all v3 API return a standardised error message when
99 * the $params passed in is not an array.
101 public function testParamsNotArray() {
102 /*I have commented this out as the check for is_array has been moved to civicrm_api. But keeping in place as
103 * this test, in contrast to the standards test, tests all existing API rather than just CRUD ones
104 * so want to keep code for re-use
105 $files = $this->getAllFilesinAPIDir();
106 $this->assertGreaterThan(1, count($files),"something has gone wrong listing the files in line " . __LINE__);
107 $this->requireOnceFilesArray($files);
108 $apiStdFunctions = $this->getAllAPIStdFunctions();
109 $this->assertGreaterThan(1, count($apiStdFunctions),"something has gone wrong getting the std functions in line " . __LINE__);
111 foreach ($apiStdFunctions as $key => $function) {
112 if ( in_array( $function, $this->_skipFunctionList ) ) {
116 $result = $function($params);
118 catch ( Exception $e ) {
126 * Get all the files in the API directory for the relevant version which contain API functions
128 * array of php files in the directory excluding helper files
130 public function getAllFilesinAPIDir() {
132 $handle = opendir($this->_apiDir
);
134 while (($file = readdir($handle)) !== FALSE) {
135 if (strstr($file, ".php") &&
136 $file != 'Entity.php' &&
137 $file != 'Generic.php' &&
150 * @param array $files
151 * list of files to load.
153 public function requireOnceFilesArray($files) {
154 foreach ($files as $key => $file) {
155 require_once $this->_apiDir
. $file;
160 * Get all api exposed functions that are expected to conform to standards
163 public function getAllAPIStdFunctions() {
164 $functionlist = get_defined_functions();
165 $functions = preg_grep($this->_regexForGettingAPIStdFunctions
, $functionlist['user']);
166 foreach ($functions as $key => $function) {
167 if (stristr($function, 'getfields')) {
168 unset($functions[$key]);
170 if (stristr($function, '_generic_')) {
171 unset($functions[$key]);