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\UnitTestCase
;
27 class TranslationTest
extends UnitTestCase
{
31 public function getCreateOKExamples() {
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',
45 $es['defaultStatus'] = [
47 'entity_table' => 'civicrm_event',
48 'entity_field' => 'title',
49 'entity_id' => '*EVENT*',
50 'language' => 'fr_CA',
51 'string' => 'Hello title',
58 public function getCreateBadExamples() {
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',
73 $es['malformedField'] = [
75 'entity_table' => 'civicrm_event',
76 'entity_field' => 'ti!tle',
77 'entity_id' => '*EVENT*',
78 'language' => 'fr_CA',
79 'string' => 'Hello title',
81 '/Entity reference is malformed/',
86 'entity_table' => 'typozcivicrm_event',
87 'entity_field' => 'title',
88 'entity_id' => '*EVENT*',
89 'language' => 'fr_CA',
90 'string' => 'Hello title',
92 '/(non-existent or non-translatable table|Cannot resolve permissions for dynamic foreign key)/',
95 $es['badFieldName'] = [
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',
104 '/non-existent or non-translatable field/',
107 $es['badFieldType'] = [
109 'status_id:name' => 'active',
110 'entity_table' => 'civicrm_event',
111 'entity_field' => 'event_type_id',
112 'entity_id' => '*EVENT*',
113 'language' => 'fr_CA',
116 '/non-existent or non-translatable field/',
119 $es['badEntityId'] = [
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',
128 '/Entity does not exist/',
134 public function getUpdateBadExamples() {
135 $createOk = $this->getCreateOKExamples()['asDraft'][0];
136 $bads = $this->getCreateBadExamples();
139 foreach ($bads as $id => $bad) {
140 array_unshift($bad, $createOk);
146 protected function setUp(): void
{
152 * @dataProvider getCreateOKExamples
153 * @param array $record
155 public function testCreateOK($record) {
156 $record = $this->fillRecord($record);
157 $createResults = \
civicrm_api4('Translation', 'create', [
158 'checkPermissions' => FALSE,
161 $this->assertEquals(1, $createResults->count());
162 foreach ($createResults as $createResult) {
163 $getResult = \
civicrm_api4('Translation', 'get', [
164 'where' => [['id', '=', $createResult['id']]],
166 $this->assertEquals($record['string'], $getResult->single()['string']);
171 * @dataProvider getCreateBadExamples
172 * @param array $record
173 * @param string $errorRegex
174 * Regular expression to compare against the error message.
176 public function testCreateBad($record, $errorRegex) {
177 $record = $this->fillRecord($record);
179 \
civicrm_api4('Translation', 'create', [
180 'checkPermissions' => FALSE,
183 $this->fail('Create should have failed');
185 catch (\API_Exception
$e) {
186 $this->assertRegExp($errorRegex, $e->getMessage());
191 * @dataProvider getUpdateBadExamples
192 * @param $createRecord
196 * @throws \API_Exception
197 * @throws \Civi\API\Exception\NotImplementedException
199 public function testUpdateBad($createRecord, $badUpdate, $errorRegex) {
200 $record = $this->fillRecord($createRecord);
201 $createResults = \
civicrm_api4('Translation', 'create', [
202 'checkPermissions' => FALSE,
205 $this->assertEquals(1, $createResults->count());
206 foreach ($createResults as $createResult) {
207 $badUpdate = $this->fillRecord($badUpdate);
209 \
civicrm_api4('Translation', 'update', [
210 'where' => [['id', '=', $createResult['id']]],
211 'values' => $badUpdate,
213 $this->fail('Update should fail');
215 catch (\API_Exception
$e) {
216 $this->assertRegExp($errorRegex, $e->getMessage());
222 * Fill in mocked values for the would-be record..
224 * @param array $record
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;