Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / MultilingualTest.php
CommitLineData
0ac69d82
ML
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
0ac69d82 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
0ac69d82
ML
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Test class for the option.language API parameter in multilingual.
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18class api_v3_MultilingualTest extends CiviUnitTestCase {
19 protected $_apiversion = 3;
20 public $DBResetRequired = FALSE;
21
22 /**
23 * Sets up the fixture, for example, opens a network connection.
24 *
25 * This method is called before a test is executed.
26 */
27 protected function setUp() {
28 parent::setUp();
29 $this->useTransaction(TRUE);
30 }
31
32 public function tearDown() {
33 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
34 parent::tearDown();
35 }
36
3e20c1ac
CW
37 /**
38 * @dataProvider versionThreeAndFour
39 */
40 public function testOptionLanguage($version) {
3a575348 41 $this->enableMultilingual();
3e20c1ac 42 $this->_apiversion = $version;
0ac69d82
ML
43
44 CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
45
cc493f5e
CW
46 $this->callAPISuccess('Setting', 'create', [
47 'languageLimit' => [
48 'en_US' => 1,
49 'fr_CA' => 1,
50 ],
51 ]);
0ac69d82
ML
52
53 // Take a semi-random OptionGroup and test manually changing its label
54 // in one language, while making sure it stays the same in English.
9099cab3 55 $group = $this->callAPISuccess('OptionGroup', 'getsingle', [
f2cc6a82 56 'name' => 'contact_edit_options',
9099cab3 57 ]);
0ac69d82 58
9099cab3 59 $english_original = $this->callAPISuccess('OptionValue', 'getsingle', [
0ac69d82 60 'option_group_id' => $group['id'],
f2cc6a82 61 'name' => 'IM',
9099cab3 62 ]);
0ac69d82 63
9099cab3 64 $this->callAPISuccess('OptionValue', 'create', [
0ac69d82
ML
65 'id' => $english_original['id'],
66 'name' => 'IM',
67 'label' => 'Messagerie instantanée',
68 'option.language' => 'fr_CA',
9099cab3 69 ]);
0ac69d82 70
9099cab3 71 $french = $this->callAPISuccess('OptionValue', 'getsingle', [
0ac69d82
ML
72 'option_group_id' => $group['id'],
73 'name' => 'IM',
3e20c1ac 74 'options' => ['language' => 'fr_CA'],
9099cab3 75 ]);
0ac69d82 76
40557979 77 // Ensure that after language is changed in previous call it will go back to the default.
9099cab3 78 $default = $this->callAPISuccess('OptionValue', 'getsingle', [
0ac69d82 79 'option_group_id' => $group['id'],
f2cc6a82 80 'name' => 'IM',
9099cab3 81 ]);
0ac69d82
ML
82
83 $this->assertEquals($french['label'], 'Messagerie instantanée');
84 $this->assertEquals($default['label'], $english_original['label']);
85 }
86
3a575348 87 /**
88 * CRM-19677: Ensure that entity apis are not affected on Multilingual setup
89 * with check_permissions = TRUE
90 */
91 public function testAllEntities() {
92 $this->enableMultilingual();
93
94 // list of entities which has mandatory attributes
9099cab3
CW
95 $specialEntities = [
96 'Attachment' => ['id' => 13],
97 'CustomValue' => ['entity_id' => 13],
98 'MailingContact' => ['contact_id' => 13],
99 'Profile' => ['profile_id' => 13],
100 'MailingGroup' => ['mailing_id' => 13],
101 ];
3a575348 102 // deprecated or API.Get is not supported/implemented
9099cab3 103 $skippableEntities = [
3a575348 104 'Logging',
105 'MailingEventConfirm',
106 'MailingEventResubscribe',
107 'MailingEventSubscribe',
108 'MailingEventUnsubscribe',
109 'Location',
110 'Pcp',
111 'Survey',
39b959db
SL
112 // throw error for help_post column
113 'UFField',
114 //throw error for title
115 'UFGroup',
116 // need loggedIn user id
117 'User',
9099cab3 118 ];
3a575348 119 // fetch all entities
9099cab3 120 $entities = $this->callAPISuccess('Entity', 'get', []);
3a575348 121 $skippableEntities = array_merge($skippableEntities, $entities['deprecated']);
122
123 foreach ($entities['values'] as $entity) {
9099cab3 124 $params = ['check_permissions' => 1];
3a575348 125 if (in_array($entity, $skippableEntities) && $entity != 'MailingGroup') {
126 continue;
127 }
128 if (array_key_exists($entity, $specialEntities)) {
129 $params = array_merge($params, $specialEntities[$entity]);
130 }
131 $this->callAPISuccess($entity, 'get', $params);
132 }
133 }
134
0ac69d82 135}