Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests calling getParticipantOrderParams to use the trait #26455

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions Civi/Test/ContactTestTrait.php
Original file line number Diff line number Diff line change
@@ -56,54 +56,59 @@ 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);
}

/**
* Generic function to create Individual, to be used in test cases
*
* @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];
}

/**
* Generic function to create Household, to be used in test cases
*
* @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)) {
1 change: 1 addition & 0 deletions Civi/Test/EventTestTrait.php
Original file line number Diff line number Diff line change
@@ -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],
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php
Original file line number Diff line number Diff line change
@@ -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 = [
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Contribute/Page/AjaxTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

85 changes: 43 additions & 42 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
@@ -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;
],
];
}

/**
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/EmailTest.php
Original file line number Diff line number Diff line change
@@ -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',
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/GroupOrganizationTest.php
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/MembershipPaymentTest.php
Original file line number Diff line number Diff line change
@@ -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');
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/MembershipTest.php
Original file line number Diff line number Diff line change
@@ -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 = [
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/NoteTest.php
Original file line number Diff line number Diff line change
@@ -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 = [
23 changes: 4 additions & 19 deletions tests/phpunit/api/v3/OrderTest.php
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as parent now


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();
Loading