4 * Class CRM_Core_DAO_AllCoreTablesTest
7 class CRM_Core_DAO_AllCoreTablesTest
extends CiviUnitTestCase
{
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'));
15 * Ensure that hook_civicrm_entityTypes runs and correctly handles the
16 * 'fields_callback' option.
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']));
23 $exports = CRM_Core_DAO_Email
::export();
24 $this->assertFalse(isset($exports['contact_id']));
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);
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']);
35 $exports = CRM_Core_DAO_Email
::export();
36 $this->assertTrue(is_array($exports['contact_id']));
40 * Implements hook_civicrm_entityTypes().
42 * @see CRM_Utils_Hook::entityTypes()
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;
51 protected function tearDown(): void
{
52 CRM_Utils_Hook
::singleton()->reset();
53 CRM_Core_DAO_AllCoreTables
::init(1);
58 * Test CRM_Core_DAO_AllCoreTables::indices() function.
60 * Ensure indices are listed correctly with and without localization
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');
69 // Not sure how we should be setting the locales, but this works for testing purposes
70 $domain = new CRM_Core_DAO_Domain();
72 $domain->locales
= implode(CRM_Core_DAO
::VALUE_SEPARATOR
, ['en_UK', 'fr_FR']);
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');
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');
85 * Check CRM_Core_DAO_AllCoreTables::multilingualize()
87 public function testMultilingualize() {
88 // in civicrm_group, title is localizable, name is not
91 'name' => 'test_index1',
98 'name' => 'test_index2',
105 'name' => 'test_index3',
112 'name' => 'test_index4',
119 'name' => 'test_index5',
130 'name' => 'test_index1',
135 'sig' => 'civicrm_group::0::name',
137 'test_index2_en_UK' => [
138 'name' => 'test_index2_en_UK',
143 'sig' => 'civicrm_group::0::title_en_UK',
145 'test_index2_fr_FR' => [
146 'name' => 'test_index2_fr_FR',
151 'sig' => 'civicrm_group::0::title_fr_FR',
154 'name' => 'test_index3',
159 'sig' => 'civicrm_group::0::name(3)',
161 'test_index4_en_UK' => [
162 'name' => 'test_index4_en_UK',
167 'sig' => 'civicrm_group::0::title_en_UK(4)',
169 'test_index4_fr_FR' => [
170 'name' => 'test_index4_fr_FR',
175 'sig' => 'civicrm_group::0::title_fr_FR(4)',
177 'test_index5_en_UK' => [
178 'name' => 'test_index5_en_UK',
184 'sig' => 'civicrm_group::0::title_en_UK(4)::name(3)',
186 'test_index5_fr_FR' => [
187 'name' => 'test_index5_fr_FR',
193 'sig' => 'civicrm_group::0::title_fr_FR(4)::name(3)',
197 // Not sure how we should be setting the locales, but this works for testing purposes
198 $domain = new CRM_Core_DAO_Domain();
200 $domain->locales
= implode(CRM_Core_DAO
::VALUE_SEPARATOR
, ['en_UK', 'fr_FR']);
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);
209 * Test CRM_Core_DAO_AllCoreTables::isCoreTable
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');
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'));
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'));
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'));