Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // vim: set si ai expandtab tabstop=4 shiftwidth=4 softtabstop=4: | |
3 | ||
4 | /** | |
5 | * File to v3 APIs for Standards compliance | |
6 | * | |
7 | * (PHP 5) | |
8 | * | |
6c6e6187 TO |
9 | * @copyright Copyright CiviCRM LLC (C) 2009 |
10 | * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html | |
6a488035 | 11 | * GNU Affero General Public License version 3 |
6c6e6187 TO |
12 | * @version $Id: |
13 | * @package CiviCRM | |
6a488035 TO |
14 | * |
15 | * This file is part of CiviCRM | |
16 | * | |
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. | |
21 | * | |
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. | |
26 | * | |
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/>. | |
30 | */ | |
31 | ||
32 | /** | |
33 | * Include class definitions | |
34 | */ | |
35 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
36 | ||
37 | /** | |
38 | * Test APIv3 civicrm_activity_* functions | |
39 | * | |
6c6e6187 TO |
40 | * @package CiviCRM_APIv3 |
41 | * @todo determine where help functions should sit (here or 'up the tree'), & best way to define API dir | |
6a488035 TO |
42 | */ |
43 | class api_v3_APIStandardsTest extends CiviUnitTestCase { | |
44 | ||
45 | protected $_apiversion; | |
46 | protected $_apiDir; | |
47 | protected $_functionFiles; | |
48 | protected $_regexForGettingAPIStdFunctions; | |
49 | /* This test case doesn't require DB reset */ | |
50 | ||
51 | ||
6a488035 TO |
52 | public $DBResetRequired = FALSE; |
53 | ||
54 | /** | |
55 | * Test setup for every test | |
56 | * | |
57 | * Connect to the database, truncate the tables that will be used | |
58 | * and redirect stdin to a temporary file | |
59 | */ | |
60 | public function setUp() { | |
61 | // Connect to the database | |
62 | parent::setUp(); | |
f44e473b | 63 | $this->useTransaction(TRUE); |
6a488035 TO |
64 | $this->_apiversion = 3; |
65 | $this->_apiDir = "../api/v3/"; | |
66 | $this->_functionFiles = array('Entity.php', 'utils.php'); | |
67 | ||
68 | //should possibly insert variable rather than '3' in below | |
69 | $this->_regexForGettingAPIStdFunctions = '/^civicrm_api3_ #starts with civicrm_api_3 (ignore internal functions) | |
b6708aeb | 70 | .*_ #any number of characters up to the last _ |
6a488035 | 71 | # negative look ahead on string getfields |
b6708aeb | 72 | (?:(?!getfields))/x'; |
6a488035 TO |
73 | // functions to skip from utils.php mainly since they get sucked in via |
74 | // a require chain in the include files | |
75 | $this->_skipFunctionList = array( | |
76 | // location api is deprecated | |
77 | 'civicrm_api3_location_create', | |
78 | 'civicrm_api3_location_get', | |
79 | 'civicrm_api3_location_delete', | |
80 | // most of these seem to be internal functions | |
81 | 'civicrm_api3_entity_create', | |
82 | 'civicrm_api3_entity_delete', | |
83 | 'civicrm_api3_survey_respondant_count', | |
84 | // functions from utils.php | |
85 | 'civicrm_api3_verify_one_mandatory', | |
86 | 'civicrm_api3_verify_mandatory', | |
87 | 'civicrm_api3_get_dao', | |
88 | 'civicrm_api3_create_success', | |
89 | 'civicrm_api3_create_error', | |
90 | 'civicrm_api3_error', | |
91 | 'civicrm_api3_check_contact_dedupe', | |
92 | 'civicrm_api3_api_check_permission', | |
93 | ); | |
94 | } | |
95 | ||
c490a46a | 96 | /** |
100fef9d | 97 | * Test checks that all v3 API return a standardised error message when |
c490a46a CW |
98 | * the $params passed in is not an array. |
99 | */ | |
00be9182 | 100 | public function testParamsNotArray() { |
e70a7fc0 TO |
101 | /* @codingStandardsIgnoreStart |
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 | |
6a488035 TO |
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__); | |
110 | $params = 'string'; | |
9b873358 | 111 | foreach ($apiStdFunctions as $key => $function) { |
6a488035 TO |
112 | if ( in_array( $function, $this->_skipFunctionList ) ) { |
113 | continue; | |
114 | } | |
115 | try { | |
116 | $result = $function($params); | |
0db6c3e1 TO |
117 | } |
118 | catch ( Exception $e ) { | |
6a488035 TO |
119 | continue; |
120 | } | |
121 | ||
e70a7fc0 TO |
122 | } |
123 | @codingStandardsIgnoreEnd */ | |
6a488035 TO |
124 | } |
125 | ||
4cbe18b8 | 126 | /** |
c490a46a | 127 | * Get all the files in the API directory for the relevant version which contain API functions |
a6c01b45 CW |
128 | * @return array |
129 | * array of php files in the directory excluding helper files | |
4cbe18b8 | 130 | */ |
00be9182 | 131 | public function getAllFilesinAPIDir() { |
6a488035 TO |
132 | $files = array(); |
133 | $handle = opendir($this->_apiDir); | |
134 | ||
135 | while (($file = readdir($handle)) !== FALSE) { | |
136 | if (strstr($file, ".php") && | |
137 | $file != 'Entity.php' && | |
138 | $file != 'Generic.php' && | |
139 | $file != 'utils.php' | |
140 | ) { | |
141 | $files[] = $file; | |
142 | } | |
143 | } | |
144 | ||
145 | closedir($handle); | |
146 | return $files; | |
147 | } | |
148 | ||
4cbe18b8 | 149 | /** |
c490a46a | 150 | * Require once Files |
5a4f6742 CW |
151 | * @param array $files |
152 | * list of files to load. | |
4cbe18b8 | 153 | */ |
00be9182 | 154 | public function requireOnceFilesArray($files) { |
6a488035 TO |
155 | foreach ($files as $key => $file) { |
156 | require_once $this->_apiDir . $file; | |
157 | } | |
158 | } | |
159 | ||
4cbe18b8 | 160 | /** |
c490a46a | 161 | * Get all api exposed functions that are expected to conform to standards |
a6c01b45 | 162 | * @return array |
4cbe18b8 | 163 | */ |
00be9182 | 164 | public function getAllAPIStdFunctions() { |
6a488035 TO |
165 | $functionlist = get_defined_functions(); |
166 | $functions = preg_grep($this->_regexForGettingAPIStdFunctions, $functionlist['user']); | |
167 | foreach ($functions as $key => $function) { | |
168 | if (stristr($function, 'getfields')) { | |
169 | unset($functions[$key]); | |
170 | } | |
171 | if (stristr($function, '_generic_')) { | |
172 | unset($functions[$key]); | |
173 | } | |
174 | } | |
175 | return $functions; | |
176 | } | |
96025800 | 177 | |
6a488035 | 178 | } |