AllCoreTables - Allow virtual entities to share a DAO class
[civicrm-core.git] / tests / phpunit / CRM / Core / DAO / AllCoreTablesTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_DAO_AllCoreTablesTest
5 * @group headless
6 */
7 class CRM_Core_DAO_AllCoreTablesTest extends CiviUnitTestCase {
8
9 public function testGetTableForClass() {
10 $this->assertEquals('civicrm_email', CRM_Core_DAO_AllCoreTables::getTableForClass('CRM_Core_DAO_Email'));
11 $this->assertEquals('civicrm_email', CRM_Core_DAO_AllCoreTables::getTableForClass('CRM_Core_BAO_Email'));
12 }
13
14 /**
15 * Ensure that hook_civicrm_entityTypes runs and correctly handles the
16 * 'fields_callback' option.
17 */
18 public function testHook() {
19 // 1. First, check the baseline fields()...
20 $fields = CRM_Core_DAO_Email::fields();
21 $this->assertFalse(isset($fields['location_type_id']['foo']));
22
23 $exports = CRM_Core_DAO_Email::export();
24 $this->assertFalse(isset($exports['contact_id']));
25
26 // 2. Now, let's hook into it...
27 $this->hookClass->setHook('civicrm_entityTypes', [$this, '_hook_civicrm_entityTypes']);
28 unset(Civi::$statics['CRM_Core_DAO_Email']);
29 CRM_Core_DAO_AllCoreTables::init(1);
30
31 // 3. And see if the data has changed...
32 $fields = CRM_Core_DAO_Email::fields();
33 $this->assertEquals('bar', $fields['location_type_id']['foo']);
34
35 $exports = CRM_Core_DAO_Email::export();
36 $this->assertTrue(is_array($exports['contact_id']));
37 }
38
39 /**
40 * Implements hook_civicrm_entityTypes().
41 *
42 * @see CRM_Utils_Hook::entityTypes()
43 */
44 public function _hook_civicrm_entityTypes(&$entityTypes) {
45 $entityTypes['CRM_Core_DAO_Email']['fields_callback'][] = function ($class, &$fields) {
46 $fields['location_type_id']['foo'] = 'bar';
47 $fields['contact_id']['export'] = TRUE;
48 };
49 }
50
51 protected function tearDown(): void {
52 CRM_Utils_Hook::singleton()->reset();
53 CRM_Core_DAO_AllCoreTables::init(1);
54 parent::tearDown();
55 }
56
57 /**
58 * Test CRM_Core_DAO_AllCoreTables::indices() function.
59 *
60 * Ensure indices are listed correctly with and without localization
61 */
62 public function testIndices() {
63 // civicrm_group UI_title is localizable
64 // Check indices without localization
65 $indices = CRM_Core_DAO_AllCoreTables::indices(FALSE);
66 $this->assertEquals($indices['civicrm_group']['UI_title']['name'], 'UI_title');
67 $this->assertEquals($indices['civicrm_group']['UI_title']['sig'], 'civicrm_group::1::title');
68
69 // Not sure how we should be setting the locales, but this works for testing purposes
70 $domain = new CRM_Core_DAO_Domain();
71 $domain->find(TRUE);
72 $domain->locales = implode(CRM_Core_DAO::VALUE_SEPARATOR, ['en_UK', 'fr_FR']);
73 $domain->save();
74
75 // Check indices with localization
76 $indices = CRM_Core_DAO_AllCoreTables::indices(TRUE);
77 $this->assertEquals($indices['civicrm_group']['UI_title_en_UK']['name'], 'UI_title_en_UK');
78 $this->assertEquals($indices['civicrm_group']['UI_title_en_UK']['sig'], 'civicrm_group::1::title_en_UK');
79
80 $this->assertEquals($indices['civicrm_group']['UI_title_fr_FR']['name'], 'UI_title_fr_FR');
81 $this->assertEquals($indices['civicrm_group']['UI_title_fr_FR']['sig'], 'civicrm_group::1::title_fr_FR');
82 }
83
84 /**
85 * Check CRM_Core_DAO_AllCoreTables::multilingualize()
86 */
87 public function testMultilingualize() {
88 // in civicrm_group, title is localizable, name is not
89 $originalIndices = [
90 'test_index1' => [
91 'name' => 'test_index1',
92 'field' => [
93 'name',
94 ],
95 'localizable' => 0,
96 ],
97 'test_index2' => [
98 'name' => 'test_index2',
99 'field' => [
100 'title',
101 ],
102 'localizable' => 1,
103 ],
104 'test_index3' => [
105 'name' => 'test_index3',
106 'field' => [
107 'name(3)',
108 ],
109 'localizable' => 0,
110 ],
111 'test_index4' => [
112 'name' => 'test_index4',
113 'field' => [
114 'title(4)',
115 ],
116 'localizable' => 1,
117 ],
118 'test_index5' => [
119 'name' => 'test_index5',
120 'field' => [
121 'title(4)',
122 'name(3)',
123 ],
124 'localizable' => 1,
125 ],
126 ];
127
128 $expectedIndices = [
129 'test_index1' => [
130 'name' => 'test_index1',
131 'field' => [
132 'name',
133 ],
134 'localizable' => 0,
135 'sig' => 'civicrm_group::0::name',
136 ],
137 'test_index2_en_UK' => [
138 'name' => 'test_index2_en_UK',
139 'field' => [
140 'title_en_UK',
141 ],
142 'localizable' => 1,
143 'sig' => 'civicrm_group::0::title_en_UK',
144 ],
145 'test_index2_fr_FR' => [
146 'name' => 'test_index2_fr_FR',
147 'field' => [
148 'title_fr_FR',
149 ],
150 'localizable' => 1,
151 'sig' => 'civicrm_group::0::title_fr_FR',
152 ],
153 'test_index3' => [
154 'name' => 'test_index3',
155 'field' => [
156 'name(3)',
157 ],
158 'localizable' => 0,
159 'sig' => 'civicrm_group::0::name(3)',
160 ],
161 'test_index4_en_UK' => [
162 'name' => 'test_index4_en_UK',
163 'field' => [
164 'title_en_UK(4)',
165 ],
166 'localizable' => 1,
167 'sig' => 'civicrm_group::0::title_en_UK(4)',
168 ],
169 'test_index4_fr_FR' => [
170 'name' => 'test_index4_fr_FR',
171 'field' => [
172 'title_fr_FR(4)',
173 ],
174 'localizable' => 1,
175 'sig' => 'civicrm_group::0::title_fr_FR(4)',
176 ],
177 'test_index5_en_UK' => [
178 'name' => 'test_index5_en_UK',
179 'field' => [
180 'title_en_UK(4)',
181 'name(3)',
182 ],
183 'localizable' => 1,
184 'sig' => 'civicrm_group::0::title_en_UK(4)::name(3)',
185 ],
186 'test_index5_fr_FR' => [
187 'name' => 'test_index5_fr_FR',
188 'field' => [
189 'title_fr_FR(4)',
190 'name(3)',
191 ],
192 'localizable' => 1,
193 'sig' => 'civicrm_group::0::title_fr_FR(4)::name(3)',
194 ],
195 ];
196
197 // Not sure how we should be setting the locales, but this works for testing purposes
198 $domain = new CRM_Core_DAO_Domain();
199 $domain->find(TRUE);
200 $domain->locales = implode(CRM_Core_DAO::VALUE_SEPARATOR, ['en_UK', 'fr_FR']);
201 $domain->save();
202
203 // needs a real DAO so use Group
204 $newIndices = CRM_Core_DAO_AllCoreTables::multilingualize('CRM_Contact_DAO_Group', $originalIndices);
205 $this->assertEquals($newIndices, $expectedIndices);
206 }
207
208 /**
209 * Test CRM_Core_DAO_AllCoreTables::isCoreTable
210 */
211 public function testIsCoreTable() {
212 $this->assertTrue(CRM_Core_DAO_AllCoreTables::isCoreTable('civicrm_contact'), 'civicrm_contact should be a core table');
213 $this->assertFalse(CRM_Core_DAO_AllCoreTables::isCoreTable('civicrm_invalid_table'), 'civicrm_invalid_table should NOT be a core table');
214 }
215
216 public function testGetBriefName() {
217 $this->assertEquals('Contact', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Contact_BAO_Contact'));
218 $this->assertEquals('Contact', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Contact_DAO_Contact'));
219 $this->assertNull(CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Core_DAO_XqZy'));
220 }
221
222 public function testGetFullName() {
223 $this->assertEquals('CRM_Contact_DAO_Contact', CRM_Core_DAO_AllCoreTables::getFullName('Contact'));
224 $this->assertNull(CRM_Core_DAO_AllCoreTables::getFullName('XqZy'));
225 }
226
227 public function testGetEntityNameForTable() {
228 $this->assertEquals('Contact', CRM_Core_DAO_AllCoreTables::getEntityNameForTable('civicrm_contact'));
229 $this->assertEquals('RelationshipCache', CRM_Core_DAO_AllCoreTables::getEntityNameForTable('civicrm_relationship_cache'));
230 $this->assertNull(CRM_Core_DAO_AllCoreTables::getEntityNameForTable('civicrm_invalid_table'));
231 }
232
233 }