DAO function to list all foreign key references to a table
[civicrm-core.git] / tests / phpunit / CRM / Core / DAOTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4 class CRM_Core_DAOTest extends CiviUnitTestCase {
5 function get_info() {
6 return array(
7 'name' => 'DAO',
8 'description' => 'Test core DAO functions',
9 'group' => 'Core',
10 );
11 }
12
13 function testGetReferenceColumns() {
14 // choose CRM_Core_DAO_Email as an arbitrary example
15 $emailRefs = CRM_Core_DAO_Email::getReferenceColumns();
16 $refsByTarget = array();
17 foreach ($emailRefs as $refSpec) {
18 $refsByTarget[$refSpec->getTargetTable()] = $refSpec;
19 }
20 $this->assertTrue(array_key_exists('civicrm_contact', $refsByTarget));
21 $contactRef = $refsByTarget['civicrm_contact'];
22 $this->assertEquals('contact_id', $contactRef->getReferenceKey());
23 $this->assertEquals('id', $contactRef->getTargetKey());
24 $this->assertEquals(FALSE, $contactRef->isGeneric());
25 }
26
27 function testGetReferencesToTable() {
28 $refs = CRM_Core_DAO::getReferencesToTable(CRM_Financial_DAO_FinancialType::getTableName());
29 $refsBySource = array();
30 foreach ($refs as $refSpec) {
31 $refsBySource[$refSpec->getReferenceTable()] = $refSpec;
32 }
33 $this->assertTrue(array_key_exists('civicrm_entity_financial_account', $refsBySource));
34 $genericRef = $refsBySource['civicrm_entity_financial_account'];
35 $this->assertEquals('entity_id', $genericRef->getReferenceKey());
36 $this->assertEquals('entity_table', $genericRef->getTypeColumn());
37 $this->assertEquals('id', $genericRef->getTargetKey());
38 $this->assertEquals(TRUE, $genericRef->isGeneric());
39 }
40
41 function testFindReferences() {
42 $params = array(
43 'first_name' => 'Testy',
44 'last_name' => 'McScallion',
45 'contact_type' => 'Individual',
46 );
47
48 $contact = CRM_Contact_BAO_Contact::add($params);
49 $this->assertNotNull($contact->id);
50
51 $params = array(
52 'email' => 'spam@dev.null',
53 'contact_id' => $contact->id,
54 'is_primary' => 0,
55 'location_type_id' => 1,
56 );
57
58 $email = CRM_Core_BAO_Email::add($params);
59
60 $refs = $contact->findReferences();
61 $refsByTable = array();
62 foreach ($refs as $refObj) {
63 $refsByTable[$refObj->__table] = $refObj;
64 }
65
66 $this->assertTrue(array_key_exists('civicrm_email', $refsByTable));
67 $refDao = $refsByTable['civicrm_email'];
68 $refDao->find(TRUE);
69 $this->assertEquals($contact->id, $refDao->contact_id);
70 }
71 }