Merge remote-tracking branch 'origin/5.15'
[civicrm-core.git] / tests / phpunit / api / v3 / MultilingualTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for the option.language API parameter in multilingual.
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class api_v3_MultilingualTest extends CiviUnitTestCase {
35 protected $_apiversion = 3;
36 public $DBResetRequired = FALSE;
37
38 /**
39 * Sets up the fixture, for example, opens a network connection.
40 *
41 * This method is called before a test is executed.
42 */
43 protected function setUp() {
44 parent::setUp();
45 $this->useTransaction(TRUE);
46 }
47
48 public function tearDown() {
49 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
50 parent::tearDown();
51 }
52
53 /**
54 * @dataProvider versionThreeAndFour
55 */
56 public function testOptionLanguage($version) {
57 $this->enableMultilingual();
58 $this->_apiversion = $version;
59
60 CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
61
62 $this->callAPISuccess('Setting', 'create', array(
63 'languageLimit' => array(
64 'en_US',
65 'fr_CA',
66 ),
67 ));
68
69 // Take a semi-random OptionGroup and test manually changing its label
70 // in one language, while making sure it stays the same in English.
71 $group = $this->callAPISuccess('OptionGroup', 'getsingle', array(
72 'name' => 'contact_edit_options',
73 ));
74
75 $english_original = $this->callAPISuccess('OptionValue', 'getsingle', array(
76 'option_group_id' => $group['id'],
77 'name' => 'IM',
78 ));
79
80 $this->callAPISuccess('OptionValue', 'create', array(
81 'id' => $english_original['id'],
82 'name' => 'IM',
83 'label' => 'Messagerie instantanée',
84 'option.language' => 'fr_CA',
85 ));
86
87 $french = $this->callAPISuccess('OptionValue', 'getsingle', array(
88 'option_group_id' => $group['id'],
89 'name' => 'IM',
90 'options' => ['language' => 'fr_CA'],
91 ));
92
93 // Ensure that after language is changed in previous call it will go back to the default.
94 $default = $this->callAPISuccess('OptionValue', 'getsingle', array(
95 'option_group_id' => $group['id'],
96 'name' => 'IM',
97 ));
98
99 $this->assertEquals($french['label'], 'Messagerie instantanée');
100 $this->assertEquals($default['label'], $english_original['label']);
101 }
102
103 /**
104 * CRM-19677: Ensure that entity apis are not affected on Multilingual setup
105 * with check_permissions = TRUE
106 */
107 public function testAllEntities() {
108 $this->enableMultilingual();
109
110 // list of entities which has mandatory attributes
111 $specialEntities = array(
112 'Attachment' => array('id' => 13),
113 'CustomValue' => array('entity_id' => 13),
114 'MailingContact' => array('contact_id' => 13),
115 'Profile' => array('profile_id' => 13),
116 'MailingGroup' => array('mailing_id' => 13),
117 );
118 // deprecated or API.Get is not supported/implemented
119 $skippableEntities = array(
120 'Logging',
121 'MailingEventConfirm',
122 'MailingEventResubscribe',
123 'MailingEventSubscribe',
124 'MailingEventUnsubscribe',
125 'Location',
126 'Pcp',
127 'Survey',
128 // throw error for help_post column
129 'UFField',
130 //throw error for title
131 'UFGroup',
132 // need loggedIn user id
133 'User',
134 );
135 // fetch all entities
136 $entities = $this->callAPISuccess('Entity', 'get', array());
137 $skippableEntities = array_merge($skippableEntities, $entities['deprecated']);
138
139 foreach ($entities['values'] as $entity) {
140 $params = array('check_permissions' => 1);
141 if (in_array($entity, $skippableEntities) && $entity != 'MailingGroup') {
142 continue;
143 }
144 if (array_key_exists($entity, $specialEntities)) {
145 $params = array_merge($params, $specialEntities[$entity]);
146 }
147 $this->callAPISuccess($entity, 'get', $params);
148 }
149 }
150
151 }