From 5767aa07ef2b358c2570c6cdf1cdea2f34191308 Mon Sep 17 00:00:00 2001 From: Anthony Nemirovsky Date: Thu, 4 Dec 2014 13:36:25 -0800 Subject: [PATCH] Fix foreign key error reported by the findById DAO test. The root cause of the error was a problem with the CiviUnitTestCase setUpBeforeClass and tearDown methods. The setUpBeforeClass method imports an sql file that creates two domains and two contacts to associate with those domains. The tearDown was set to truncate the civicrm_contact table and this would happen after every test function run. So, if a test function that was not the first test function in the class created a contact that it then wanted to delete, the foreign key error would be thrown. Rather than doing a workaround, I decided to fix the underlying issue and add back the two domain contacts after the db tables are truncated. This broke some tests that were depending on using contact ID 1 for the contact record that was created, so I went ahead and fixed those, too. --- tests/phpunit/CRM/Member/Form/MembershipTest.php | 1 - tests/phpunit/CiviTest/CiviUnitTestCase.php | 10 ++++++++++ tests/phpunit/api/v3/MailingContactTest.php | 4 ++-- tests/phpunit/api/v3/dataset/mailing_bounced.xml | 2 +- tests/phpunit/api/v3/dataset/mailing_delivered.xml | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index d6db032709..4567ce9941 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -81,7 +81,6 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'civicrm_acl_contact_cache', 'civicrm_relationship_type', 'civicrm_saved_search', - 'civicrm_contact_type', 'civicrm_mailing_component', 'civicrm_mailing_bounce_type', 'civicrm_mailing_bounce_pattern', diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index c043e2dfb6..ca8620d142 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -444,6 +444,15 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $this->tempDirs = array(); } + /** + * Create default domain contacts for the two domains added during test class + * database population. + */ + public function createDomainContacts() { + $default_domain_contact = $this->organizationCreate(); + $second_domain_contact = $this->organizationCreate(); + } + /** * Common teardown functions for all unit tests */ @@ -453,6 +462,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $session->set('userID', NULL); $tablesToTruncate = array('civicrm_contact'); $this->quickCleanup($tablesToTruncate); + $this->createDomainContacts(); $this->cleanTempDirs(); $this->unsetExtensionSystem(); CRM_Core_Transaction::forceRollbackIfEnabled(); diff --git a/tests/phpunit/api/v3/MailingContactTest.php b/tests/phpunit/api/v3/MailingContactTest.php index 87a622a11d..b1863f8601 100644 --- a/tests/phpunit/api/v3/MailingContactTest.php +++ b/tests/phpunit/api/v3/MailingContactTest.php @@ -155,7 +155,7 @@ class api_v3_MailingContactTest extends CiviUnitTestCase { $this->assertFalse(empty($result['values']), "In line " . __LINE__); $this->assertEquals($result['values'][1]['mailing_id'], 1, "In line " . __LINE__); $this->assertEquals($result['values'][1]['subject'], "Some Subject", "In line " . __LINE__); - $this->assertEquals($result['values'][1]['creator_id'], 1, "In line " . __LINE__); + $this->assertEquals($result['values'][1]['creator_id'], 3, "In line " . __LINE__); $this->assertEquals($result['values'][1]['creator_name'], "xyz1, abc1", "In line " . __LINE__); } @@ -188,7 +188,7 @@ class api_v3_MailingContactTest extends CiviUnitTestCase { $this->assertFalse(empty($result['values']), "In line " . __LINE__); $this->assertEquals($result['values'][2]['mailing_id'], 2, "In line " . __LINE__); $this->assertEquals($result['values'][2]['subject'], "Some Subject", "In line " . __LINE__); - $this->assertEquals($result['values'][2]['creator_id'], 1, "In line " . __LINE__); + $this->assertEquals($result['values'][2]['creator_id'], 3, "In line " . __LINE__); $this->assertEquals($result['values'][2]['creator_name'], "xyz1, abc1", "In line " . __LINE__); } } diff --git a/tests/phpunit/api/v3/dataset/mailing_bounced.xml b/tests/phpunit/api/v3/dataset/mailing_bounced.xml index 5224911cbe..228aea0c61 100644 --- a/tests/phpunit/api/v3/dataset/mailing_bounced.xml +++ b/tests/phpunit/api/v3/dataset/mailing_bounced.xml @@ -60,7 +60,7 @@ 1 - 1 + 3 diff --git a/tests/phpunit/api/v3/dataset/mailing_delivered.xml b/tests/phpunit/api/v3/dataset/mailing_delivered.xml index cc530236c8..ea2430e552 100644 --- a/tests/phpunit/api/v3/dataset/mailing_delivered.xml +++ b/tests/phpunit/api/v3/dataset/mailing_delivered.xml @@ -60,7 +60,7 @@ 1 - 1 + 3 -- 2.25.1