4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Entity
;
22 use api\v
4\Api4TestBase
;
23 use Civi\Test\TransactionalInterface
;
28 class TranslationTest
extends Api4TestBase
implements TransactionalInterface
{
32 public function getCreateOKExamples() {
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',
46 $es['defaultStatus'] = [
48 'entity_table' => 'civicrm_event',
49 'entity_field' => 'title',
50 'entity_id' => '*EVENT*',
51 'language' => 'fr_CA',
52 'string' => 'Hello title',
59 public function getCreateBadExamples() {
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',
74 $es['malformedField'] = [
76 'entity_table' => 'civicrm_event',
77 'entity_field' => 'ti!tle',
78 'entity_id' => '*EVENT*',
79 'language' => 'fr_CA',
80 'string' => 'Hello title',
82 '/Entity reference is malformed/',
87 'entity_table' => 'typozcivicrm_event',
88 'entity_field' => 'title',
89 'entity_id' => '*EVENT*',
90 'language' => 'fr_CA',
91 'string' => 'Hello title',
93 '/(non-existent or non-translatable table|Cannot resolve permissions for dynamic foreign key)/',
96 $es['badFieldName'] = [
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',
105 '/non-existent or non-translatable field/',
108 $es['badFieldType'] = [
110 'status_id:name' => 'active',
111 'entity_table' => 'civicrm_event',
112 'entity_field' => 'event_type_id',
113 'entity_id' => '*EVENT*',
114 'language' => 'fr_CA',
117 '/non-existent or non-translatable field/',
120 $es['badEntityId'] = [
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',
129 '/Entity does not exist/',
135 public function getUpdateBadExamples() {
136 $createOk = $this->getCreateOKExamples()['asDraft'][0];
137 $bads = $this->getCreateBadExamples();
140 foreach ($bads as $id => $bad) {
141 array_unshift($bad, $createOk);
147 protected function setUp(): void
{
153 * @dataProvider getCreateOKExamples
154 * @param array $record
156 public function testCreateOK($record) {
157 $record = $this->fillRecord($record);
158 $createResults = \
civicrm_api4('Translation', 'create', [
159 'checkPermissions' => FALSE,
162 $this->assertEquals(1, $createResults->count());
163 foreach ($createResults as $createResult) {
164 $getResult = \
civicrm_api4('Translation', 'get', [
165 'where' => [['id', '=', $createResult['id']]],
167 $this->assertEquals($record['string'], $getResult->single()['string']);
172 * @dataProvider getCreateBadExamples
173 * @param array $record
174 * @param string $errorRegex
175 * Regular expression to compare against the error message.
177 public function testCreateBad($record, $errorRegex) {
178 $record = $this->fillRecord($record);
180 \
civicrm_api4('Translation', 'create', [
181 'checkPermissions' => FALSE,
184 $this->fail('Create should have failed');
186 catch (\API_Exception
$e) {
187 $this->assertRegExp($errorRegex, $e->getMessage());
192 * @dataProvider getUpdateBadExamples
193 * @param $createRecord
197 * @throws \API_Exception
198 * @throws \Civi\API\Exception\NotImplementedException
200 public function testUpdateBad($createRecord, $badUpdate, $errorRegex) {
201 $record = $this->fillRecord($createRecord);
202 $createResults = \
civicrm_api4('Translation', 'create', [
203 'checkPermissions' => FALSE,
206 $this->assertEquals(1, $createResults->count());
207 foreach ($createResults as $createResult) {
208 $badUpdate = $this->fillRecord($badUpdate);
210 \
civicrm_api4('Translation', 'update', [
211 'where' => [['id', '=', $createResult['id']]],
212 'values' => $badUpdate,
214 $this->fail('Update should fail');
216 catch (\API_Exception
$e) {
217 $this->assertRegExp($errorRegex, $e->getMessage());
223 * Fill in mocked values for the would-be record..
225 * @param array $record
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;