From 6bc84c7f7bf3107d3a7c46e4b5ef8a4958d5a27f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 22 May 2023 12:59:53 +1200 Subject: [PATCH] Declare paid event as example data, call from test I'm looking to create some set up helper classes in the Civi\Test space, to also be available to extensions & to standardise some of the set up routines. I figured the best place to put the ExampleData was in the structure created for that. The ExampleData structure is lightly used by the WorkflowMessage::render() function for personal entities (ie Contact,Contribution) but it is not used (& probably won't be) for configuration entities. However, the examples are intended to be multi-purpose so I think it is the right location. Note the trait that builds on it will have to have a lot more logic to build price sets etc than the exampleData space really supports --- Civi/Test/ExampleData/Contact/Alex.php | 26 +---------- Civi/Test/ExampleData/Contact/Barb.php | 26 ++--------- Civi/Test/ExampleData/Event/PaidEvent.php | 43 +++++++++++++++++++ .../phpunit/CRM/Core/Payment/BaseIPNTest.php | 2 +- .../Event/Form/Registration/ConfirmTest.php | 6 +-- .../CRM/Utils/TokenConsistencyTest.php | 2 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 41 +++++++++--------- tests/phpunit/api/v3/ContributionTest.php | 2 +- 8 files changed, 75 insertions(+), 73 deletions(-) create mode 100644 Civi/Test/ExampleData/Event/PaidEvent.php diff --git a/Civi/Test/ExampleData/Contact/Alex.php b/Civi/Test/ExampleData/Contact/Alex.php index 9b4e155db0..cea8f09cd5 100644 --- a/Civi/Test/ExampleData/Contact/Alex.php +++ b/Civi/Test/ExampleData/Contact/Alex.php @@ -47,31 +47,9 @@ class Alex extends EntityExample { 'household_name' => NULL, 'organization_name' => NULL, 'sic_code' => NULL, - 'contact_is_deleted' => '0', - 'current_employer' => NULL, - 'address_id' => NULL, - 'street_address' => NULL, - 'supplemental_address_1' => NULL, - 'supplemental_address_2' => NULL, - 'supplemental_address_3' => NULL, - 'city' => NULL, - 'postal_code_suffix' => NULL, - 'postal_code' => NULL, - 'state_province_id' => NULL, - 'country_id' => NULL, - 'phone_id' => '7', 'phone_type_id' => '1', - 'phone' => '293-6934', - 'email_id' => '7', - 'email' => 'daz.alex67@testing.net', - 'im_id' => NULL, - 'provider_id' => NULL, - 'im' => NULL, - 'worldregion_id' => NULL, - 'world_region' => NULL, - 'state_province_name' => NULL, - 'state_province' => NULL, - 'country' => NULL, + 'phone_primary.phone' => '293-6934', + 'email_primary.email' => 'daz.alex67@testing.net', ]; } diff --git a/Civi/Test/ExampleData/Contact/Barb.php b/Civi/Test/ExampleData/Contact/Barb.php index 3044a098c4..902bae4232 100644 --- a/Civi/Test/ExampleData/Contact/Barb.php +++ b/Civi/Test/ExampleData/Contact/Barb.php @@ -44,30 +44,10 @@ class Barb extends EntityExample { 'birth_date' => '1999-05-11', 'is_deceased' => '0', 'deceased_date' => NULL, - 'household_name' => NULL, 'organization_name' => NULL, - 'sic_code' => NULL, - 'contact_is_deleted' => '0', - 'current_employer' => NULL, - 'address_id' => NULL, - 'street_address' => NULL, - 'supplemental_address_1' => NULL, - 'supplemental_address_2' => NULL, - 'supplemental_address_3' => NULL, - 'city' => NULL, - 'postal_code_suffix' => NULL, - 'postal_code' => NULL, - 'geo_code_1' => NULL, - 'geo_code_2' => NULL, - 'state_province_id' => NULL, - 'country_id' => NULL, - 'phone_id' => '7', - 'phone_type_id' => '1', - 'phone' => '393-7924', - 'email_id' => '7', - 'email' => 'barb@testing.net', - 'worldregion_id' => NULL, - 'world_region' => NULL, + 'phone_primary.phone_type_id' => 1, + 'phone_primary.phone' => '393-7924', + 'email_primary.email' => 'barb@testing.net', 'email_greeting_display' => 'Dear Barb', 'postal_greeting_display' => 'Dear Barb', ]; diff --git a/Civi/Test/ExampleData/Event/PaidEvent.php b/Civi/Test/ExampleData/Event/PaidEvent.php new file mode 100644 index 0000000000..d6c2a71b07 --- /dev/null +++ b/Civi/Test/ExampleData/Event/PaidEvent.php @@ -0,0 +1,43 @@ + 'entity/' . $this->entityName . '/' . $this->getExampleName(), + ]; + } + + public function build(array &$example): void { + $example['data'] = [ + 'id' => 0, + 'title' => 'Annual CiviCRM meet', + 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now', + 'description' => 'This event is intended to give brief idea about progress of CiviCRM and giving solutions to common user issues', + 'event_type_id' => 1, + 'is_public' => TRUE, + 'start_date' => 20081021, + 'end_date' => '+ 1 month', + 'is_online_registration' => TRUE, + 'registration_start_date' => 20080601, + 'registration_end_date' => '+ 1 month', + 'max_participants' => 100, + 'event_full_text' => 'Sorry! We are already full', + 'is_monetary' => TRUE, + 'financial_type_id' => 3, + 'is_active' => 1, + 'default_role_id' => 1, + 'is_show_location' => TRUE, + 'is_email_confirm' => 1, + 'is_pay_later' => TRUE, + 'pay_later_text' => 'Transfer funds', + 'pay_later_receipt' => 'Please transfer funds to our bank account.', + 'fee_label' => 'Event fees', + ]; + } + +} diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php index 5fdc3ed032..5668141ef6 100644 --- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php @@ -370,7 +370,7 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { * */ public function _setUpParticipantObjects(string $participantStatus = 'Attended'): void { - $event = $this->eventCreate(['is_email_confirm' => 1]); + $event = $this->eventCreate(['is_email_confirm' => 1, 'email_confirm_text' => '']); $this->setupContribution(); $this->_eventId = $event['id']; diff --git a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php index a9e3d49905..2b635db9fe 100644 --- a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php +++ b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php @@ -538,7 +538,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { */ private function creatEventWithProfile($event): array { if (empty($event)) { - $event = $this->eventCreate(); + $event = $this->eventCreate(['is_monetary' => FALSE]); $this->createJoinedProfile(['entity_table' => 'civicrm_event', 'entity_id' => $event['id']]); $this->addUFField($this->ids['UFGroup']['our profile'], 'note', 'Contact', 'Comment'); } @@ -622,7 +622,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { * event#64. */ public function testSubmitNonPrimaryEmail(): void { - $event = $this->eventCreate(); + $event = $this->eventCreate(['is_monetary' => FALSE]); $mut = new CiviMailUtils($this, TRUE); $this->submitForm($event['id'], [ [ @@ -704,7 +704,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { */ protected function submitPaidEvent(array $submitValues = []): void { $this->dummyProcessorCreate(); - $event = $this->eventCreatePaid(['payment_processor' => [$this->ids['PaymentProcessor']['dummy_live']]]); + $event = $this->eventCreatePaid(['payment_processor' => [$this->ids['PaymentProcessor']['dummy_live']], 'confirm_email_text' => '', 'is_pay_later' => FALSE]); $this->submitForm($event['id'], array_merge([ 'email-Primary' => 'demo@example.com', 'credit_card_number' => '4111111111111111', diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 30aaa7d0b6..645a9d6678 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -673,7 +673,7 @@ Emerald City, Maine 90210 event.info_url :' . CRM_Utils_System::url('civicrm/event/info', NULL, TRUE) . '&reset=1&id=1 event.registration_url :' . CRM_Utils_System::url('civicrm/event/register', NULL, TRUE) . '&reset=1&id=1 -event.pay_later_receipt : +event.pay_later_receipt :Please transfer funds to our bank account. event.custom_1 :my field event.confirm_email_text : '; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 8f327ee4ea..8c013c6b13 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -31,6 +31,7 @@ use Civi\Api4\Contribution; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; use Civi\Api4\Event; +use Civi\Api4\ExampleData; use Civi\Api4\FinancialAccount; use Civi\Api4\FinancialType; use Civi\Api4\LineItem; @@ -1015,26 +1016,8 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { ]); } - // set defaults for missing params - $params = array_merge([ - 'title' => 'Annual CiviCRM meet', - 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now', - 'description' => 'This event is intended to give brief idea about progress of CiviCRM and giving solutions to common user issues', - 'event_type_id' => 1, - 'is_public' => 1, - 'start_date' => 20081021, - 'end_date' => '+ 1 month', - 'is_online_registration' => 1, - 'registration_start_date' => 20080601, - 'registration_end_date' => '+ 1 month', - 'max_participants' => 100, - 'event_full_text' => 'Sorry! We are already full', - 'is_monetary' => 0, - 'is_active' => 1, - 'default_role_id' => 1, - 'is_show_location' => 0, - 'is_email_confirm' => 1, - ], $params); + $params = array_merge($this->getExampleData('Event', 'PaidEvent'), $params); + if (!empty($params['payment_processor_id'])) { $params['payment_processor'] = is_array($params['payment_processor_id']) ? $params['payment_processor_id'] : [$params['payment_processor_id']]; } @@ -3912,4 +3895,22 @@ WHERE table_schema = DATABASE()"); } } + /** + * Get example data. + * + * @param string $entity + * @param string $name + * + * @return array + * @throws \CRM_Core_Exception + */ + protected function getExampleData(string $entity, string $name): array { + $data = ExampleData::get(FALSE) + ->addSelect('data') + ->addWhere('name', '=', 'entity/' . $entity . '/' . $name) + ->execute()->first()['data']; + unset($data['id']); + return $data; + } + } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 456c6e686d..d1dc613bcd 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -3974,7 +3974,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function createPendingParticipantContribution() { - $this->_ids['event']['test'] = $this->eventCreate(['is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'])['id']; + $this->_ids['event']['test'] = $this->eventCreate(['is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org', 'confirm_email_text' => ''])['id']; $participantID = $this->participantCreate(['event_id' => $this->_ids['event']['test'], 'status_id' => 6, 'contact_id' => $this->individualID]); $this->_ids['participant'] = $participantID; $params = array_merge($this->_params, ['contact_id' => $this->individualID, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee']); -- 2.25.1