Merge pull request #18517 from JMAConsulting/financial-issue-150
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / TranslationTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace api\v4\Entity;
21
22 use api\v4\UnitTestCase;
23
24 /**
25 * @group headless
26 */
27 class TranslationTest extends UnitTestCase {
28
29 protected $ids = [];
30
31 public function getCreateOKExamples() {
32 $es = [];
33
34 $es['asDraft'] = [
35 [
36 'status_id:name' => 'draft',
37 'entity_table' => 'civicrm_event',
38 'entity_field' => 'description',
39 'entity_id' => '*EVENT*',
40 'language' => 'fr_CA',
41 'string' => 'Hello world',
42 ],
43 ];
44
45 $es['defaultStatus'] = [
46 [
47 'entity_table' => 'civicrm_event',
48 'entity_field' => 'title',
49 'entity_id' => '*EVENT*',
50 'language' => 'fr_CA',
51 'string' => 'Hello title',
52 ],
53 ];
54
55 return $es;
56 }
57
58 public function getCreateBadExamples() {
59 $es = [];
60
61 $es['badStatus'] = [
62 [
63 'status_id:name' => 'jumping',
64 'entity_table' => 'civicrm_event',
65 'entity_field' => 'description',
66 'entity_id' => '*EVENT*',
67 'language' => 'fr_CA',
68 'string' => 'Hello world',
69 ],
70 '/Invalid status/',
71 ];
72
73 $es['malformedField'] = [
74 [
75 'entity_table' => 'civicrm_event',
76 'entity_field' => 'ti!tle',
77 'entity_id' => '*EVENT*',
78 'language' => 'fr_CA',
79 'string' => 'Hello title',
80 ],
81 '/Entity reference is malformed/',
82 ];
83
84 $es['badTable'] = [
85 [
86 'entity_table' => 'typozcivicrm_event',
87 'entity_field' => 'title',
88 'entity_id' => '*EVENT*',
89 'language' => 'fr_CA',
90 'string' => 'Hello title',
91 ],
92 '/(non-existent or non-translatable table|Cannot resolve permissions for dynamic foreign key)/',
93 ];
94
95 $es['badFieldName'] = [
96 [
97 'status_id:name' => 'active',
98 'entity_table' => 'civicrm_event',
99 'entity_field' => 'zoological_taxonomy',
100 'entity_id' => '*EVENT*',
101 'language' => 'fr_CA',
102 'string' => 'Hello world',
103 ],
104 '/non-existent or non-translatable field/',
105 ];
106
107 $es['badFieldType'] = [
108 [
109 'status_id:name' => 'active',
110 'entity_table' => 'civicrm_event',
111 'entity_field' => 'event_type_id',
112 'entity_id' => '*EVENT*',
113 'language' => 'fr_CA',
114 'string' => '9',
115 ],
116 '/non-existent or non-translatable field/',
117 ];
118
119 $es['badEntityId'] = [
120 [
121 'status_id:name' => 'active',
122 'entity_table' => 'civicrm_event',
123 'entity_field' => 'description',
124 'entity_id' => 9999999,
125 'language' => 'fr_CA',
126 'string' => 'Hello world',
127 ],
128 '/Entity does not exist/',
129 ];
130
131 return $es;
132 }
133
134 public function getUpdateBadExamples() {
135 $createOk = $this->getCreateOKExamples()['asDraft'][0];
136 $bads = $this->getCreateBadExamples();
137
138 $es = [];
139 foreach ($bads as $id => $bad) {
140 array_unshift($bad, $createOk);
141 $es[$id] = $bad;
142 }
143 return $es;
144 }
145
146 protected function setUp(): void {
147 parent::setUp();
148 $this->ids = [];
149 }
150
151 /**
152 * @dataProvider getCreateOKExamples
153 * @param array $record
154 */
155 public function testCreateOK($record) {
156 $record = $this->fillRecord($record);
157 $createResults = \civicrm_api4('Translation', 'create', [
158 'checkPermissions' => FALSE,
159 'values' => $record,
160 ]);
161 $this->assertEquals(1, $createResults->count());
162 foreach ($createResults as $createResult) {
163 $getResult = \civicrm_api4('Translation', 'get', [
164 'where' => [['id', '=', $createResult['id']]],
165 ]);
166 $this->assertEquals($record['string'], $getResult->single()['string']);
167 }
168 }
169
170 /**
171 * @dataProvider getCreateBadExamples
172 * @param array $record
173 * @param string $errorRegex
174 * Regular expression to compare against the error message.
175 */
176 public function testCreateBad($record, $errorRegex) {
177 $record = $this->fillRecord($record);
178 try {
179 \civicrm_api4('Translation', 'create', [
180 'checkPermissions' => FALSE,
181 'values' => $record,
182 ]);
183 $this->fail('Create should have failed');
184 }
185 catch (\API_Exception $e) {
186 $this->assertRegExp($errorRegex, $e->getMessage());
187 }
188 }
189
190 /**
191 * @dataProvider getUpdateBadExamples
192 * @param $createRecord
193 * @param $badUpdate
194 * @param $errorRegex
195 *
196 * @throws \API_Exception
197 * @throws \Civi\API\Exception\NotImplementedException
198 */
199 public function testUpdateBad($createRecord, $badUpdate, $errorRegex) {
200 $record = $this->fillRecord($createRecord);
201 $createResults = \civicrm_api4('Translation', 'create', [
202 'checkPermissions' => FALSE,
203 'values' => $record,
204 ]);
205 $this->assertEquals(1, $createResults->count());
206 foreach ($createResults as $createResult) {
207 $badUpdate = $this->fillRecord($badUpdate);
208 try {
209 \civicrm_api4('Translation', 'update', [
210 'where' => [['id', '=', $createResult['id']]],
211 'values' => $badUpdate,
212 ]);
213 $this->fail('Update should fail');
214 }
215 catch (\API_Exception $e) {
216 $this->assertRegExp($errorRegex, $e->getMessage());
217 }
218 }
219 }
220
221 /**
222 * Fill in mocked values for the would-be record..
223 *
224 * @param array $record
225 *
226 * @return array
227 */
228 protected function fillRecord($record) {
229 if ($record['entity_id'] === '*EVENT*') {
230 $eventId = $this->ids['*EVENT*'] ?? \CRM_Core_DAO::createTestObject('CRM_Event_BAO_Event')->id;
231 $record['entity_id'] = $this->ids['*EVENT*'] = $eventId;
232 }
233 return $record;
234 }
235
236 }