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