From fded4d36476baf2925f99fa4f9e88aa39ba8a754 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 6 Jun 2023 15:20:21 +1200 Subject: [PATCH] Fix events calling getParticipantOrderParams to use the trait --- Civi/Test/ContactTestTrait.php | 55 +++++++----- Civi/Test/EventTestTrait.php | 1 + .../Contribute/Form/Contribution/MainTest.php | 4 +- .../phpunit/CRM/Contribute/Page/AjaxTest.php | 2 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 85 +++++++++--------- tests/phpunit/api/v3/EmailTest.php | 2 +- .../phpunit/api/v3/GroupOrganizationTest.php | 2 +- .../phpunit/api/v3/MembershipPaymentTest.php | 2 +- tests/phpunit/api/v3/MembershipTest.php | 2 +- tests/phpunit/api/v3/NoteTest.php | 2 +- tests/phpunit/api/v3/OrderTest.php | 23 +---- tests/phpunit/api/v3/PaymentTest.php | 88 ++++++------------- tests/phpunit/api/v3/PriceFieldValueTest.php | 78 ++++++---------- tests/phpunit/api/v3/ReportTemplateTest.php | 17 ++-- .../api/v3/TaxContributionPageTest.php | 6 +- 15 files changed, 153 insertions(+), 216 deletions(-) diff --git a/Civi/Test/ContactTestTrait.php b/Civi/Test/ContactTestTrait.php index 0242c8380122..39ae9acb8ac9 100644 --- a/Civi/Test/ContactTestTrait.php +++ b/Civi/Test/ContactTestTrait.php @@ -56,18 +56,18 @@ public function createLoggedInUser(): int { * * @param array $params * parameters for civicrm_contact_add api function call - * @param int $seq - * sequence number if creating multiple organizations + * @param int|string $identifier + * If the identifier is numeric (discouraged) it will affect which contact is loaded. + * Numeric identifiers and values for random other than FALSE are generally + * discouraged in favour if specifying data in params where variety is needed. * * @return int * id of Organisation created */ - public function organizationCreate($params = [], $seq = 0): int { - if (!$params) { - $params = []; - } + public function organizationCreate(array $params = [], $identifier = 'organization_0'): int { + $seq = is_numeric($identifier) ? $identifier : 0; $params = array_merge($this->sampleContact('Organization', $seq), $params); - return $this->_contactCreate($params); + return $this->_contactCreate($params, $identifier); } /** @@ -75,17 +75,21 @@ public function organizationCreate($params = [], $seq = 0): int { * * @param array $params * parameters for civicrm_contact_add api function call - * @param int $seq - * sequence number if creating multiple individuals + * @param int|string $identifier + * If the identifier is numeric (discouraged) it will affect which contact is loaded. + * Numeric identifiers and values for random other than FALSE are generally + * discouraged in favour if specifying data in params where variety is needed. * @param bool $random + * Random is deprecated. * * @return int * id of Individual created */ - public function individualCreate(array $params = [], $seq = 0, $random = FALSE): int { + public function individualCreate(array $params = [], $identifier = 'individual_0', bool $random = FALSE): int { + $seq = is_numeric($identifier) ? $identifier : 0; $params = array_merge($this->sampleContact('Individual', $seq, $random), $params); - $this->ids['Contact']['individual_' . $seq] = $this->_contactCreate($params); - return $this->ids['Contact']['individual_' . $seq]; + $this->_contactCreate($params, $identifier); + return $this->ids['Contact'][$identifier]; } /** @@ -93,17 +97,18 @@ public function individualCreate(array $params = [], $seq = 0, $random = FALSE): * * @param array $params * parameters for civicrm_contact_add api function call - * @param int $seq - * sequence number if creating multiple households + * @param int|string $identifier + * If the identifier is numeric (discouraged) it will affect which contact is loaded. + * Numeric identifiers and values for random other than FALSE are generally + * discouraged in favour if specifying data in params where variety is needed. * * @return int * id of Household created - * - * @throws \CRM_Core_Exception */ - public function householdCreate($params = [], $seq = 0) { + public function householdCreate(array $params = [], $identifier = 'household_0'): int { + $seq = is_numeric($identifier) ? $identifier : 0; $params = array_merge($this->sampleContact('Household', $seq), $params); - return $this->_contactCreate($params); + return $this->_contactCreate($params, $identifier); } /** @@ -118,7 +123,7 @@ public function householdCreate($params = [], $seq = 0) { * @return array * properties of sample contact (ie. $params for API call) */ - public function sampleContact($contact_type, $seq = 0, $random = FALSE) { + public function sampleContact(string $contact_type, int $seq = 0, bool $random = FALSE): array { $samples = [ 'Individual' => [ // The number of values in each list need to be coprime numbers to not have duplicates @@ -143,10 +148,10 @@ public function sampleContact($contact_type, $seq = 0, $random = FALSE) { foreach ($samples[$contact_type] as $key => $values) { $params[$key] = $values[$seq % count($values)]; if ($random) { - $params[$key] .= substr(sha1(rand()), 0, 5); + $params[$key] .= substr(sha1(mt_rand()), 0, 5); } } - if ($contact_type == 'Individual') { + if ($contact_type === 'Individual') { $params['email'] = strtolower( $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org' ); @@ -161,15 +166,17 @@ public function sampleContact($contact_type, $seq = 0, $random = FALSE) { * * @param array $params * For civicrm_contact_add api function call. + * @param string $identifier * * @return int * id of contact created */ - private function _contactCreate(array $params): int { + private function _contactCreate(array $params, string $identifier = 'Contact'): int { $version = $this->_apiversion; $this->_apiversion = 3; - $result = $this->callAPISuccess('contact', 'create', $params); + $result = $this->callAPISuccess('Contact', 'create', $params); $this->_apiversion = $version; + $this->ids['Contact'][$identifier] = (int) $result['id']; return (int) $result['id']; } @@ -179,7 +186,7 @@ private function _contactCreate(array $params): int { * @param int $contactID * Contact ID to delete */ - public function contactDelete($contactID) { + public function contactDelete($contactID): void { $domain = new \CRM_Core_BAO_Domain(); $domain->contact_id = $contactID; if (!$domain->find(TRUE)) { diff --git a/Civi/Test/EventTestTrait.php b/Civi/Test/EventTestTrait.php index 35cf652dd904..6e7da0df61ae 100644 --- a/Civi/Test/EventTestTrait.php +++ b/Civi/Test/EventTestTrait.php @@ -380,6 +380,7 @@ protected function getPriceFieldOptions(string $identifier = 'PaidEvent'): array return [ 'free' => ['name' => 'free', 'label' => 'Complementary', 'amount' => 0], 'student' => ['name' => 'student', 'label' => 'Student Rate', 'amount' => 100], + 'student_plus' => ['name' => 'student_plus', 'label' => 'Student Deluxe', 'amount' => 200], 'standard' => ['name' => 'standard', 'label' => 'Standard Rate', 'amount' => 300], 'family_package' => ['name' => 'family_package', 'label' => 'Family Deal', 'amount' => 1550.55], 'corporate_table' => ['name' => 'corporate_table', 'label' => 'Corporate Table', 'amount' => 8000.67], diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php index 628b7c926c88..671a4b31a72a 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php @@ -168,7 +168,7 @@ protected function getContributionForm($params = []) { /** * Test expired priceset are not returned from buildPriceSet() Function */ - public function testExpiredPriceSet() { + public function testExpiredPriceSet(): void { $priceSetParams1 = [ 'name' => 'priceset', 'title' => 'Priceset with Multiple Terms', @@ -204,7 +204,7 @@ public function testExpiredPriceSet() { $priceField2 = $this->callAPISuccess('PriceField', 'create', $params); //Create price options. - $membershipOrgId = $this->organizationCreate(NULL); + $membershipOrgId = $this->organizationCreate(); $memtype = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]); foreach ([$priceField1, $priceField2] as $priceField) { $priceFieldValueParams = [ diff --git a/tests/phpunit/CRM/Contribute/Page/AjaxTest.php b/tests/phpunit/CRM/Contribute/Page/AjaxTest.php index d307fe71b44a..454255a05876 100644 --- a/tests/phpunit/CRM/Contribute/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Contribute/Page/AjaxTest.php @@ -35,7 +35,7 @@ public function setUp(): void { // Create three sample contacts. foreach ([0, 1, 2] as $seq) { - $this->individualCreate([], $seq); + $this->individualCreate([], 'individual_' . $seq); } } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 98e59f94628b..82a4451df827 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3398,57 +3398,58 @@ protected function resetLabels() { /** * Get parameters to set up a multi-line participant order. * - * @param int|null $eventID - * Optional event ID. A new event will be created if no event ID is given. - * * @return array */ - protected function getParticipantOrderParams(?int $eventID = NULL): array { - if (!$eventID) { - $event = $this->eventCreate(); - $eventID = $event['id']; - } - - $eventParams = [ - 'id' => $eventID, - 'financial_type_id' => 4, - 'is_monetary' => 1, - ]; - $this->callAPISuccess('Event', 'create', $eventParams); - $priceFields = $this->createPriceSet('event', $eventID); - $orderParams = [ + protected function getParticipantOrderParams(): array { + $this->eventCreatePaid(); + return [ 'total_amount' => 300, 'currency' => 'USD', 'contact_id' => $this->individualCreate(), 'financial_type_id' => 4, - 'contribution_status_id' => 'Pending', - ]; - foreach ($priceFields['values'] as $priceField) { - $orderParams['line_items'][] = [ - 'line_item' => [ - [ - 'price_field_id' => $priceField['price_field_id'], - 'price_field_value_id' => $priceField['id'], - 'label' => $priceField['label'], - 'field_title' => $priceField['label'], - 'qty' => 1, - 'unit_price' => $priceField['amount'], - 'line_total' => $priceField['amount'], - 'financial_type_id' => $priceField['financial_type_id'], - 'entity_table' => 'civicrm_participant', + 'line_items' => [ + [ + 'line_item' => [ + [ + 'price_field_id' => $this->ids['PriceField']['PaidEvent'], + 'price_field_value_id' => $this->ids['PriceFieldValue']['PaidEvent_student'], + 'qty' => 1, + 'unit_price' => 100, + 'line_total' => 100, + 'entity_table' => 'civicrm_participant', + ], + ], + 'params' => [ + 'financial_type_id' => 4, + 'event_id' => $this->getEventID('PaidEvent'), + 'role_id' => 1, + 'status_id' => 14, + 'fee_currency' => 'USD', + 'contact_id' => $this->individualCreate(), ], ], - 'params' => [ - 'financial_type_id' => 4, - 'event_id' => $eventID, - 'role_id' => 1, - 'status_id' => 14, - 'fee_currency' => 'USD', - 'contact_id' => $this->individualCreate(), + [ + 'line_item' => [ + [ + 'price_field_id' => $this->ids['PriceField']['PaidEvent'], + 'price_field_value_id' => $this->ids['PriceFieldValue']['PaidEvent_student_plus'], + 'qty' => 1, + 'unit_price' => 200, + 'line_total' => 200, + 'entity_table' => 'civicrm_participant', + ], + ], + 'params' => [ + 'financial_type_id' => 4, + 'event_id' => $this->getEventID('PaidEvent'), + 'role_id' => 1, + 'status_id' => 14, + 'fee_currency' => 'USD', + 'contact_id' => $this->individualCreate(), + ], ], - ]; - } - return $orderParams; + ], + ]; } /** diff --git a/tests/phpunit/api/v3/EmailTest.php b/tests/phpunit/api/v3/EmailTest.php index 65bbb3377a1f..efec14e4cfd9 100644 --- a/tests/phpunit/api/v3/EmailTest.php +++ b/tests/phpunit/api/v3/EmailTest.php @@ -26,7 +26,7 @@ public function setUp(): void { parent::setUp(); $this->useTransaction(TRUE); - $this->_contactID = $this->organizationCreate(NULL); + $this->_contactID = $this->organizationCreate(); $this->_locationTypeID = $this->locationTypeCreate(); $this->locationType2ID = $this->locationTypeCreate([ 'name' => 'New Location Type 2', diff --git a/tests/phpunit/api/v3/GroupOrganizationTest.php b/tests/phpunit/api/v3/GroupOrganizationTest.php index dc75418b05f6..86f7a07b63fc 100644 --- a/tests/phpunit/api/v3/GroupOrganizationTest.php +++ b/tests/phpunit/api/v3/GroupOrganizationTest.php @@ -38,7 +38,7 @@ protected function setUp(): void { $this->useTransaction(TRUE); $this->_groupID = $this->groupCreate(); - $this->_orgID = $this->organizationCreate(NULL); + $this->_orgID = $this->organizationCreate(); } ///////////////// civicrm_group_organization_get methods diff --git a/tests/phpunit/api/v3/MembershipPaymentTest.php b/tests/phpunit/api/v3/MembershipPaymentTest.php index e84085d55d3f..93a68cf5e23c 100644 --- a/tests/phpunit/api/v3/MembershipPaymentTest.php +++ b/tests/phpunit/api/v3/MembershipPaymentTest.php @@ -38,7 +38,7 @@ public function setUp(): void { parent::setUp(); $this->useTransaction(TRUE); - $this->_contactID = $this->organizationCreate(NULL); + $this->_contactID = $this->organizationCreate(); $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]); $this->_membershipStatusID = $this->membershipStatusCreate('test status'); $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name'); diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index d0b2685c0bd0..5ddd4acf5248 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -479,7 +479,7 @@ public function testGetNoContactExists() { * @throws \CRM_Core_Exception */ public function testGetWithRelationship() { - $membershipOrgId = $this->organizationCreate(NULL); + $membershipOrgId = $this->organizationCreate(); $memberContactId = $this->individualCreate(); $relTypeParams = [ diff --git a/tests/phpunit/api/v3/NoteTest.php b/tests/phpunit/api/v3/NoteTest.php index 8dca6afeeef1..3c510d6e031d 100644 --- a/tests/phpunit/api/v3/NoteTest.php +++ b/tests/phpunit/api/v3/NoteTest.php @@ -27,7 +27,7 @@ public function setUp(): void { parent::setUp(); $this->useTransaction(TRUE); - $this->_contactID = $this->organizationCreate(NULL); + $this->_contactID = $this->organizationCreate(); $this->_createdDate = date('Y-m-d H:i:s', strtotime('-10 years')); $this->_params = [ diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index e6751dc82494..9dca59820ba3 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -23,16 +23,6 @@ class api_v3_OrderTest extends CiviUnitTestCase { use CRMTraits_Financial_TaxTrait; - /** - * Should financials be checked after the test but before tear down. - * - * Ideally all tests (or at least all that call any financial api calls ) should do this but there - * are some test data issues and some real bugs currently blocking. - * - * @var bool - */ - protected $isValidateFinancialsOnPostAssert = TRUE; - protected $_individualId; protected $_financialTypeId = 1; @@ -44,8 +34,6 @@ class api_v3_OrderTest extends CiviUnitTestCase { /** * Setup function. - * - * @throws \CRM_Core_Exception */ public function setUp(): void { parent::setUp(); @@ -56,12 +44,11 @@ public function setUp(): void { /** * Clean up after each test. - * - * @throws \CRM_Core_Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); $this->quickCleanup(['civicrm_uf_match']); + parent::tearDown(); } /** @@ -145,7 +132,6 @@ public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) * @param array $extraParams * * @return array - * @throws \CRM_Core_Exception */ public function addOrder(bool $isPriceSet, float $amount = 300.00, array $extraParams = []): array { $p = [ @@ -323,7 +309,6 @@ public function testAddOrderForMembership(): void { * Test create order api for membership. * * @dataProvider dataForTestAddOrderForMembershipWithDates - * @throws \CRM_Core_Exception * * @param array $membershipExtraParams Optional additional params for the membership, * e.g. skipStatusCal or start_date. This can also have a 'renewalOf' key, in which @@ -333,13 +318,13 @@ public function testAddOrderForMembership(): void { */ public function testAddOrderForMembershipWithDates(array $membershipExtraParams, ?string $paymentDate, array $expectations): void { if (date('Y-m-d') > static::$phpunitStartedDate) { - $this->markTestSkipped("Test run spanned 2 days so skipping test as results would be affected"); + $this->markTestSkipped('Test run spanned 2 days so skipping test as results would be affected'); } if (date('Hi') > '2357') { - $this->markTestSkipped("It‘s less than 2 mins to midnight, test skipped as 'today' may change during test."); + $this->markTestSkipped("It‘s less than 2 minutes to midnight, test skipped as 'today' may change during test."); } if (isset($membershipExtraParams['skipStatusCal']) && !$this->skipStatusCalStillExists()) { - $this->markTestSkipped("The test was skipped as skipStatusCal seems to have been removed, so this test is useless and should be removed."); + $this->markTestSkipped('The test was skipped as skipStatusCal seems to have been removed, so this test is useless and should be removed.'); } $membershipType = $this->membershipTypeCreate(); diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 5e407b29934a..09b5a146e138 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -18,30 +18,12 @@ */ class api_v3_PaymentTest extends CiviUnitTestCase { - protected $_individualId; - - protected $_financialTypeId = 1; - - /** - * Setup function. - */ - public function setUp(): void { - parent::setUp(); - - $this->_apiversion = 3; - $this->_individualId = $this->individualCreate(); - CRM_Core_Config::singleton()->userPermissionClass->permissions = []; - } - /** * Clean up after each test. - * - * @throws \Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); $this->quickCleanup(['civicrm_uf_match']); - unset(CRM_Core_Config::singleton()->userPermissionClass->permissions); parent::tearDown(); } @@ -51,15 +33,14 @@ public function tearDown(): void { * @throws \CRM_Core_Exception */ public function testGetPayment(): void { - $p = [ - 'contact_id' => $this->_individualId, + $contribution = $this->callAPISuccess('Contribution', 'create', [ + 'contact_id' => $this->individualCreate(), 'receive_date' => '2010-01-20', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => 'Donation', 'trxn_id' => 23456, 'contribution_status_id' => 1, - ]; - $contribution = $this->callAPISuccess('contribution', 'create', $p); + ]); $params = [ 'contribution_id' => $contribution['id'], @@ -96,7 +77,7 @@ public function testGetPayment(): void { */ public function testMultiplePaymentsForContribution(): void { $params = [ - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'total_amount' => 100, 'contribution_status_id' => 'Pending', ]; @@ -141,7 +122,7 @@ public function testMultiplePaymentsForContribution(): void { public function testGetPaymentWithTrxnID(): void { $individual2 = $this->individualCreate(); $params1 = [ - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'trxn_id' => 111111, 'total_amount' => 10, ]; @@ -204,7 +185,7 @@ public function testPaymentSendContributionReceipt(): void { $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]); $this->assertNotEmpty($contribution['receipt_date']); $mut->checkMailLog([ - 'Price Field - Price Field 1 1 $100.00 $100.00', + 'Fundraising Dinner -... 1 $100.00 $100.00', 'event place', 'streety street', ]); @@ -216,12 +197,11 @@ public function testPaymentSendContributionReceipt(): void { * @throws \CRM_Core_Exception */ public function testFullRefundWithPaymentAlreadyRefunded(): void { - $params1 = [ - 'contact_id' => $this->_individualId, + $contributionID1 = $this->contributionCreate([ + 'contact_id' => $this->individualCreate(), 'trxn_id' => 111111, 'total_amount' => 10, - ]; - $contributionID1 = $this->contributionCreate($params1); + ]); $paymentParams = ['contribution_id' => $contributionID1]; $this->callAPISuccess('Payment', 'create', ['total_amount' => '-10', 'contribution_id' => $contributionID1]); $this->callAPISuccess('payment', 'get', $paymentParams); @@ -235,7 +215,7 @@ public function testFullRefundWithPaymentAlreadyRefunded(): void { */ public function testNegativePaymentWithNegativeContribution(): void { $params1 = [ - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'trxn_id' => 111111, 'total_amount' => -10, ]; @@ -306,7 +286,7 @@ public function testPaymentEmailReceiptFullyPaid(): void { 'contribution_id' => $contribution['id'], 'total_amount' => 150, ]; - $payment = $this->callAPISuccess('payment', 'create', $params); + $payment = $this->callAPISuccess('Payment', 'create', $params); // Here we set the email to an invalid email & use check_permissions, domain email should be used. $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']); @@ -378,8 +358,6 @@ public function testRefundEmailReceipt(string $thousandSeparator): void { /** * Test adding a payment to a pending multi-line order. - * - * @throws \CRM_Core_Exception */ public function testCreatePaymentPendingOrderNoLineItems(): void { $order = $this->createPendingParticipantOrder(); @@ -392,11 +370,13 @@ public function testCreatePaymentPendingOrderNoLineItems(): void { /** * Test that Payment.create does not fail if the line items are missing. * - * In the original spec it was anticipated that financial items would not be created - * for pending contributions in some circumstances. We've backed away from this and - * I mostly could not find a way to do it through the UI. But I did seem to once & - * I want to be sure that if they ARE missing no fatal occurs so this tests - * that in an artificial way. + * In the original spec it was anticipated that financial items would not be + * created for pending contributions in some circumstances. We've backed away + * from this and I mostly could not find a way to do it through the UI. But I + * did seem to once & I want to be sure that if they ARE missing no fatal + * occurs so this tests that in an artificial way. + * + * @throws \Civi\Core\Exception\DBQueryException */ public function testAddPaymentMissingFinancialItems(): void { $contribution = $this->callAPISuccess('Contribution', 'create', [ @@ -413,8 +393,6 @@ public function testAddPaymentMissingFinancialItems(): void { * Add participant with contribution * * @return array - * - * @throws \CRM_Core_Exception */ protected function createPendingParticipantOrder(): array { return $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams()); @@ -578,7 +556,7 @@ public function testRefundPayment(): void { $result = $this->callAPISuccess('Contribution', 'create', [ 'financial_type_id' => 'Donation', 'total_amount' => 100, - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), ]); $contributionID = $result['id']; @@ -610,14 +588,12 @@ public function testRefundPayment(): void { /** * Test negative payment using create API when the "cancelled_payment_id" param is set. - * - * @throws \CRM_Core_Exception */ public function testRefundPaymentWithCancelledPaymentId(): void { $result = $this->callAPISuccess('Contribution', 'create', [ 'financial_type_id' => 'Donation', 'total_amount' => 100, - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), ]); $contributionID = $result['id']; @@ -850,7 +826,7 @@ public function testCreatePaymentPayLater(): void { $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'financial_type_id' => 1, 'contribution_status_id' => 2, 'is_pay_later' => 1, @@ -908,8 +884,6 @@ public function testCreatePaymentPayLater(): void { /** * Test net amount is set when fee amount is passed in. - * - * @throws \CRM_Core_Exception */ public function testNetAmount(): void { $order = $this->createPendingParticipantOrder(); @@ -927,7 +901,7 @@ public function testCreatePaymentIncompletePaymentPartialPayment(): void { $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'financial_type_id' => 1, 'contribution_status_id' => 2, ]; @@ -967,7 +941,7 @@ public function testCreatePaymentOnFailedContribution(): void { $contributionParams = [ 'total_amount' => 50, 'currency' => 'USD', - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'financial_type_id' => 1, 'contribution_status_id' => 'Failed', ]; @@ -994,7 +968,7 @@ public function testCreatePaymentOnFailedContribution(): void { $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'financial_type_id' => 1, 'contribution_status_id' => 'Pending', 'is_pay_later' => 1, @@ -1081,15 +1055,13 @@ public function createPartialPaymentOnContribution($contributionID, $partialAmou /** * Test create payment api for pay later contribution with partial payment. - * - * @throws \CRM_Core_Exception */ public function testCreatePaymentPayLaterPartialPayment(): void { $this->createLoggedInUser(); $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'financial_type_id' => 1, 'contribution_status_id' => 2, 'is_pay_later' => 1, @@ -1137,13 +1109,11 @@ public function testCreatePaymentPayLaterPartialPayment(): void { 'id' => $contribution['id'], ]); $this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]); - $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId, 'activity_type_id' => 'Payment'], 2); + $this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->ids['Contact']['individual_0'], 'activity_type_id' => 'Payment'], 2); } /** * Test that Payment.create uses the to_account of the payment processor. - * - * @throws \CRM_Core_Exception */ public function testPaymentWithProcessorWithOddFinancialAccount(): void { $processor = $this->dummyProcessorCreate(['financial_account_id' => 'Deposit Bank Account', 'payment_instrument_id' => 'Cash']); @@ -1151,7 +1121,7 @@ public function testPaymentWithProcessorWithOddFinancialAccount(): void { $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', - 'contact_id' => $this->_individualId, + 'contact_id' => $this->individualCreate(), 'financial_type_id' => 1, 'contribution_status_id' => 'Pending', ]; @@ -1242,8 +1212,6 @@ protected function checkPaymentIsValid(int $paymentID, int $contributionID, int * * @see https://github.com/civicrm/civicrm-core/pull/17688 * @see https://lab.civicrm.org/dev/financial/-/issues/139 - * - * @throws \CRM_Core_Exception */ public function testPaymentCreateTrxnIdAndDates(): void { diff --git a/tests/phpunit/api/v3/PriceFieldValueTest.php b/tests/phpunit/api/v3/PriceFieldValueTest.php index 899a32acefc7..87cb7732d0f7 100644 --- a/tests/phpunit/api/v3/PriceFieldValueTest.php +++ b/tests/phpunit/api/v3/PriceFieldValueTest.php @@ -10,13 +10,11 @@ */ /** - * Class api_v3_PriceFieldValueTest + * Class api_v3_PriceFieldValueTest. + * * @group headless */ class api_v3_PriceFieldValueTest extends CiviUnitTestCase { - protected $_apiversion = 3; - - const ENTITY = 'price_field_value'; /** * @var array @@ -87,7 +85,7 @@ public function setUp(): void { 'financial_type_id' => 1, ]; - $membershipOrgId = $this->organizationCreate(NULL); + $membershipOrgId = $this->organizationCreate(); $this->membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]); $priceSetParams2 = [ @@ -115,63 +113,43 @@ public function setUp(): void { /** * Tear down function. - * - * @throws \Exception */ public function tearDown(): void { - $tablesToTruncate = [ - 'civicrm_contact', - 'civicrm_contribution', - ]; - $this->quickCleanup($tablesToTruncate); - $this->membershipTypeDelete(['id' => $this->membershipTypeID]); - $this->callAPISuccess('PriceField', 'delete', [ - 'id' => $this->priceFieldID1, - ]); - $delete = $this->callAPISuccess('PriceSet', 'delete', [ - 'id' => $this->priceSetID1, - ]); - $this->callAPISuccess('PriceField', 'delete', [ - 'id' => $this->priceFieldID2, - ]); - $this->callAPISuccess('PriceSet', 'delete', [ - 'id' => $this->priceSetID2, - ]); - - $this->assertAPISuccess($delete); + $this->quickCleanUpFinancialEntities(); + parent::tearDown(); } public function testCreatePriceFieldValue() { - $result = $this->callAPIAndDocument(self::ENTITY, 'create', $this->params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument('PriceFieldValue', 'create', $this->params, __FUNCTION__, __FILE__); $this->assertAPISuccess($result); $this->assertEquals(1, $result['count']); $this->assertNotNull($result['values'][$result['id']]['id']); - $this->getAndCheck($this->params, $result['id'], self::ENTITY); + $this->getAndCheck($this->params, $result['id'], 'PriceFieldValue'); } - public function testGetBasicPriceFieldValue() { - $createResult = $this->callAPISuccess(self::ENTITY, 'create', $this->params); + public function testGetBasicPriceFieldValue(): void { + $createResult = $this->callAPISuccess('PriceFieldValue', 'create', $this->params); $this->assertAPISuccess($createResult); $getParams = [ 'name' => 'contribution_amount', ]; - $getResult = $this->callAPIAndDocument(self::ENTITY, 'get', $getParams, __FUNCTION__, __FILE__); + $getResult = $this->callAPIAndDocument('PriceFieldValue', 'get', $getParams, __FUNCTION__, __FILE__); $this->assertEquals(1, $getResult['count']); $this->callAPISuccess('price_field_value', 'delete', ['id' => $createResult['id']]); } public function testDeletePriceFieldValue() { - $startCount = $this->callAPISuccess(self::ENTITY, 'getcount', []); - $createResult = $this->callAPISuccess(self::ENTITY, 'create', $this->params); + $startCount = $this->callAPISuccess('PriceFieldValue', 'getcount', []); + $createResult = $this->callAPISuccess('PriceFieldValue', 'create', $this->params); $deleteParams = ['id' => $createResult['id']]; - $deleteResult = $this->callAPIAndDocument(self::ENTITY, 'delete', $deleteParams, __FUNCTION__, __FILE__); + $deleteResult = $this->callAPIAndDocument('PriceFieldValue', 'delete', $deleteParams, __FUNCTION__, __FILE__); - $endCount = $this->callAPISuccess(self::ENTITY, 'getcount', []); + $endCount = $this->callAPISuccess('PriceFieldValue', 'getcount', []); $this->assertEquals($startCount, $endCount); } - public function testGetFieldsPriceFieldValue() { - $result = $this->callAPISuccess(self::ENTITY, 'getfields', ['action' => 'create']); + public function testGetFieldsPriceFieldValue(): void { + $result = $this->callAPISuccess('PriceFieldValue', 'getfields', ['action' => 'create']); $this->assertEquals(1, $result['values']['max_value']['type']); } @@ -186,10 +164,10 @@ public function testCreatePriceFieldValuewithMultipleTerms() { 'is_active' => 1, 'financial_type_id' => 2, ]; - $result = $this->callAPIAndDocument(self::ENTITY, 'create', $params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument('PriceFieldValue', 'create', $params, __FUNCTION__, __FILE__); $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2); $this->assertEquals(1, $result['count']); - $this->callAPISuccess(self::ENTITY, 'delete', ['id' => $result['id']]); + $this->callAPISuccess('PriceFieldValue', 'delete', ['id' => $result['id']]); } public function testGetPriceFieldValuewithMultipleTerms() { @@ -213,15 +191,15 @@ public function testGetPriceFieldValuewithMultipleTerms() { 'is_active' => 1, 'financial_type_id' => 2, ]; - $result1 = $this->callAPISuccess(self::ENTITY, 'create', $params2); - $result2 = $this->callAPISuccess(self::ENTITY, 'create', $params2); - $result = $this->callAPISuccess(self::ENTITY, 'get', ['price_field_id' => $this->priceFieldID2]); + $result1 = $this->callAPISuccess('PriceFieldValue', 'create', $params2); + $result2 = $this->callAPISuccess('PriceFieldValue', 'create', $params2); + $result = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $this->priceFieldID2]); $this->assertEquals(2, $result['count']); - $this->callAPISuccess(self::ENTITY, 'delete', ['id' => $result1['id']]); - $this->callAPISuccess(self::ENTITY, 'delete', ['id' => $result2['id']]); + $this->callAPISuccess('PriceFieldValue', 'delete', ['id' => $result1['id']]); + $this->callAPISuccess('PriceFieldValue', 'delete', ['id' => $result2['id']]); } - public function testCreatePriceFieldValueWithDisabledFinancialType() { + public function testCreatePriceFieldValueWithDisabledFinancialType(): void { $financialTypeParams = [ 'is_active' => 0, 'name' => 'Disabled Donations', @@ -235,20 +213,20 @@ public function testCreatePriceFieldValueWithDisabledFinancialType() { 'is_active' => 1, 'financial_type_id' => $financialType['id'], ]; - $this->callAPIFailure(self::ENTITY, 'create', $params); + $this->callAPIFailure('PriceFieldValue', 'create', $params); } /** * This is the same as testCreatePriceFieldValue but where is_default = 1. */ - public function testCreatePriceFieldValueAsDefault() { + public function testCreatePriceFieldValueAsDefault(): void { $params = $this->params; $params['is_default'] = 1; - $result = $this->callAPISuccess(self::ENTITY, 'create', $params); + $result = $this->callAPISuccess('PriceFieldValue', 'create', $params); $this->assertAPISuccess($result); $this->assertEquals(1, $result['count']); $this->assertNotNull($result['values'][$result['id']]['id']); - $this->getAndCheck($params, $result['id'], self::ENTITY); + $this->getAndCheck($params, $result['id'], 'PriceFieldValue'); } } diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index 0cf7e8dc9555..723dd7f7c313 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -9,6 +9,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Contact; use Civi\Test\ACLPermissionTrait; /** @@ -35,16 +36,12 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { /** * Our group reports use an alter so transaction cleanup won't work. - * - * @throws \Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); $this->quickCleanup(['civicrm_group', 'civicrm_saved_search', 'civicrm_group_contact', 'civicrm_group_contact_cache', 'civicrm_group'], TRUE); (new CRM_Logging_Schema())->dropAllLogTables(); CRM_Utils_Hook::singleton()->reset(); - $config = CRM_Core_Config::singleton(); - unset($config->userPermissionClass->permissions); parent::tearDown(); } @@ -1496,10 +1493,10 @@ public function testContactSubtypeNotIn() { * * @throws \CRM_Core_Exception */ - public function testPcpReportTotals() { + public function testPcpReportTotals(): void { $donor1ContactId = $this->individualCreate(); - $donor2ContactId = $this->individualCreate(); - $donor3ContactId = $this->individualCreate(); + $donor2ContactId = $this->individualCreate(['last_name' => 'Black']); + $donor3ContactId = $this->individualCreate(['last_name' => 'Cherry']); // We are going to create two PCP pages. We will create two contributions // on the first PCP page and one contribution on the second PCP page. @@ -1535,6 +1532,7 @@ public function testPcpReportTotals() { $pcpParams = $this->pcpParams(); // Keep track of the owner of the page. $pcpOwnerContact2Id = $pcpParams['contact_id']; + Contact::update()->addWhere('id', '=', $pcpOwnerContact2Id)->setValues(['last_name' => 'Green'])->execute(); // We're using the same pcpBlock id and contribution page that we created above. $pcpParams['pcp_block_id'] = $pcpBlock->id; $pcpParams['page_id'] = $contribution_page_id; @@ -1590,15 +1588,14 @@ public function testPcpReportTotals() { ]; $c3 = $this->contributionCreate($contribution3params); // Now the soft contribution. - $p = [ + $this->callAPISuccess('ContributionSoft', 'create', [ 'contribution_id' => $c3, 'pcp_id' => $pcp2->id, 'contact_id' => $pcpOwnerContact2Id, 'amount' => 200.00, 'currency' => 'USD', 'soft_credit_type_id' => $pcp_soft_credit_type_id, - ]; - $this->callAPISuccess('contribution_soft', 'create', $p); + ]); $template = 'contribute/pcp'; $rows = $this->callAPISuccess('report_template', 'getrows', [ diff --git a/tests/phpunit/api/v3/TaxContributionPageTest.php b/tests/phpunit/api/v3/TaxContributionPageTest.php index d350adf4c661..0365e2d95336 100644 --- a/tests/phpunit/api/v3/TaxContributionPageTest.php +++ b/tests/phpunit/api/v3/TaxContributionPageTest.php @@ -42,7 +42,7 @@ class api_v3_TaxContributionPageTest extends CiviUnitTestCase { public function setUp(): void { parent::setUp(); $this->ids['Contact']['individual'] = $this->individualCreate(); - $this->ids['Contact']['organization'] = $this->organizationCreate(NULL); + $this->organizationCreate(); $this->ids['PaymentProcessor'] = $this->paymentProcessorCreate(); $this->params = [ @@ -73,7 +73,7 @@ public function setUp(): void { // Financial Account with 20% tax rate $financialAccount = $this->callAPISuccess('financial_account', 'create', [ 'name' => 'vat full tax rate account', - 'contact_id' => $this->ids['Contact']['organization'], + 'contact_id' => $this->ids['Contact']['organization_0'], 'financial_account_type_id' => 2, 'is_tax' => 1, 'tax_rate' => 20.00, @@ -100,7 +100,7 @@ public function setUp(): void { // Financial type with 5% tax rate $financialAccountHalfTax = [ 'name' => 'vat half tax_rate account', - 'contact_id' => $this->ids['Contact']['organization'], + 'contact_id' => $this->ids['Contact']['organization_0'], 'financial_account_type_id' => 2, 'is_tax' => 1, 'tax_rate' => 5.00,