From: Eileen McNaughton Date: Sun, 17 Apr 2016 06:39:25 +0000 (+1200) Subject: Test fixes, better cache clearing (#8154) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8d35246a4e9b8b552df6d4e48e9452daa6bd4fb1;p=civicrm-core.git Test fixes, better cache clearing (#8154) --- diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php index e8ced80b71..04b7c5ecc4 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php @@ -41,7 +41,7 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { } /** - * Check method add() + * Check method add(). */ public function testAdd() { $params = array( @@ -63,7 +63,7 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { } /** - * Check method retrive() + * Check method retrieve(). */ public function testRetrieve() { $params = array( @@ -130,25 +130,25 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { } /** - * Check method testgetAvailableFinancialTypes() + * Check method testGetAvailableFinancialTypes() */ - public function testgetAvailableFinancialTypes() { + public function testGetAvailableFinancialTypes() { $this->setACL(); - $config = &CRM_Core_Config::singleton(); - $config->userPermissionClass->permissions = array( + $this->setPermissions(array( 'view contributions of type Donation', 'view contributions of type Member Dues', - ); + )); + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types); $expectedResult = array( 1 => "Donation", 2 => "Member Dues", ); $this->assertEquals($expectedResult, $types, 'Verify that only certain financial types can be retrieved'); - CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL; - $config->userPermissionClass->permissions = array( + + $this->setPermissions(array( 'view contributions of type Donation', - ); + )); unset($expectedResult[2]); CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types); $this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available financial types'); @@ -180,29 +180,29 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids); $this->setACL(); - $config = &CRM_Core_Config::singleton(); - $config->userPermissionClass->permissions = array( + + $this->setPermissions(array( 'view contributions of type Donation', 'view contributions of type Member Dues', - ); + )); CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types); $expectedResult = array( 1 => "Type One", 2 => "Type Two", ); $this->assertEquals($expectedResult, $types, 'Verify that only certain membership types can be retrieved'); - $config->userPermissionClass->permissions = array( + $this->setPermissions(array( 'view contributions of type Donation', - ); + )); unset($expectedResult[2]); CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types); $this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available membership types'); } /** - * Check method testpermissionedFinancialTypes() + * Check method testPermissionedFinancialTypes() */ - public function testpermissionedFinancialTypes() { + public function testPermissionedFinancialTypes() { // First get all core permissions $permissions = $checkPerms = CRM_Core_Permission::getCorePermissions(); $this->setACL(); @@ -286,22 +286,22 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams); CRM_Financial_BAO_FinancialType::$_statusACLFt = array(); $this->setACL(); - $config = &CRM_Core_Config::singleton(); - $config->userPermissionClass->permissions = array( + + $this->setPermissions(array( 'view contributions of type Member Dues', - ); + )); try { - $error = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view'); + CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view'); $this->fail("Missed expected exception"); } catch (Exception $e) { $this->assertEquals("A fatal error was triggered: You do not have permission to access this page.", $e->getMessage()); } - $config = &CRM_Core_Config::singleton(); - $config->userPermissionClass->permissions = array( + + $this->setPermissions(array( 'view contributions of type Donation', - ); + )); $perm = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view'); $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.'); } @@ -320,18 +320,21 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { /** * Check method testisACLFinancialTypeStatus() */ - public function testbuildPermissionedClause() { + public function testBuildPermissionedClause() { $this->setACL(); - $config = &CRM_Core_Config::singleton(); - $config->userPermissionClass->permissions = array( + $this->setPermissions(array( 'view contributions of type Donation', 'view contributions of type Member Dues', - ); + )); CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution'); $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,2)'); - $config->userPermissionClass->permissions[] = 'view contributions of type Event Fee'; + $this->setPermissions(array( + 'view contributions of type Donation', + 'view contributions of type Member Dues', + 'view contributions of type Event Fee', + )); $whereClause = NULL; - CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = array(); + CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution'); $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,4,2)'); } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 2ed8480764..83f1bf0378 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3439,4 +3439,25 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) ); } + /** + * Flush statics relating to financial type. + */ + protected function flushFinancialTypeStatics() { + if (isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'])) { + unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']); + } + CRM_Contribute_PseudoConstant::flush('financialType'); + CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL; + } + + /** + * Set the permissions to the supplied array. + * + * @param array $permissions + */ + protected function setPermissions($permissions) { + CRM_Core_Config::singleton()->userPermissionClass->permissions = $permissions; + $this->flushFinancialTypeStatics(); + } + }