From b6b397a89c721e6fad0acd7dec84198b2e8b2ae7 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 25 Aug 2023 17:23:48 +1200 Subject: [PATCH] Add test & fix for getOptions --- CRM/Core/BAO/Discount.php | 2 +- CRM/Core/PseudoConstant.php | 2 +- Civi/Test/EventTestTrait.php | 52 +++++++++++++++------ tests/phpunit/CRM/Core/BAO/DiscountTest.php | 31 ++++++++++++ 4 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 tests/phpunit/CRM/Core/BAO/DiscountTest.php diff --git a/CRM/Core/BAO/Discount.php b/CRM/Core/BAO/Discount.php index f360709493..c0f29cfccd 100644 --- a/CRM/Core/BAO/Discount.php +++ b/CRM/Core/BAO/Discount.php @@ -78,7 +78,7 @@ class CRM_Core_BAO_Discount extends CRM_Core_DAO_Discount { $dao->entity_table = $entityTable; $dao->find(); while ($dao->fetch()) { - $optionGroupIDs[$dao->id] = $dao->price_set_id; + $optionGroupIDs[$dao->id] = (int) $dao->price_set_id; } return $optionGroupIDs; } diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index e355265f0b..a0bc56de71 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -1531,7 +1531,7 @@ WHERE id = %1 } // Filter domain specific options if (in_array('domain_id', $availableFields)) { - $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID() . ' OR domain_id is NULL'; + $wheres[] = '(domain_id = ' . CRM_Core_Config::domainID() . ' OR domain_id is NULL)'; } $queryParams = [ 1 => [$params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES], diff --git a/Civi/Test/EventTestTrait.php b/Civi/Test/EventTestTrait.php index 378a5ea15b..bf6e091e3c 100644 --- a/Civi/Test/EventTestTrait.php +++ b/Civi/Test/EventTestTrait.php @@ -13,9 +13,7 @@ namespace Civi\Test; use Civi\Api4\Event; use Civi\Api4\ExampleData; -use Civi\Api4\PriceField; use Civi\Api4\PriceFieldValue; -use Civi\Api4\PriceSet; use Civi\Api4\PriceSetEntity; use Civi\Api4\UFField; use Civi\Api4\UFGroup; @@ -77,6 +75,37 @@ trait EventTestTrait { return $event; } + /** + * Add a discount price set to the given event. + * + * @param string $eventIdentifier + * @param array $discountParameters + * @param array $priceSetParameters + * @param string $identifier + * @param float $fraction + * + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection + */ + public function addDiscountPriceSet(string $eventIdentifier = 'PaidEvent', array $discountParameters = [], array $priceSetParameters = [], string $identifier = 'discount', $fraction = .5): void { + $this->eventCreatePriceSet($priceSetParameters, $identifier); + $discountParameters = array_merge([ + 'start_date' => '1 week ago', + 'end_date' => 'tomorrow', + 'entity_id' => $this->getEventID($eventIdentifier), + 'entity_table' => 'civicrm_event', + 'price_set_id' => $this->ids['PriceSet'][$identifier], + ], $discountParameters); + $this->createTestEntity('Discount', $discountParameters, $identifier); + $priceOptions = PriceFieldValue::get(FALSE) + ->addWhere('price_field_id.price_set_id', '=', $this->ids['PriceSet'][$identifier]) + ->execute(); + foreach ($priceOptions as $price) { + PriceFieldValue::update(FALSE)->addWhere('id', '=', $price['id']) + ->setValues(['amount' => round($price['amount'] * $fraction)])->execute(); + } + } + /** * Create a paid event. * @@ -281,20 +310,18 @@ trait EventTestTrait { * * @param array $priceSetParameters * @param string $identifier - * - * @throws \CRM_Core_Exception */ private function eventCreatePriceSet(array $priceSetParameters, string $identifier): void { $priceSetParameters = array_merge($priceSetParameters, [ 'min_amount' => 0, 'title' => 'Fundraising dinner', - 'name' => 'fundraising_dinner', + 'name' => $identifier, 'extends:name' => 'CiviEvent', 'financial_type_id:name' => 'Event Fee', ]); - $this->setTestEntityID('PriceSet', PriceSet::create(FALSE)->setValues($priceSetParameters)->execute()->first()['id'], $identifier); - $this->setTestEntityID('PriceField', PriceField::create(FALSE)->setValues([ + $this->createTestEntity('PriceSet', $priceSetParameters, $identifier); + $this->createTestEntity('PriceField', [ 'label' => 'Fundraising Dinner', 'name' => 'fundraising_dinner', 'html_type' => 'Radio', @@ -303,15 +330,14 @@ trait EventTestTrait { 'price_set_id' => $this->ids['PriceSet'][$identifier], 'is_enter_qty' => 1, 'financial_type_id:name' => 'Event Fee', - ])->execute()->first()['id'], $identifier); + ], $identifier); foreach ($this->getPriceFieldOptions() as $optionIdentifier => $priceFieldOption) { - $this->setTestEntityID('PriceFieldValue', PriceFieldValue::create(FALSE)->setValues( + $this->createTestEntity('PriceFieldValue', array_merge([ 'price_field_id' => $this->ids['PriceField'][$identifier], 'financial_type_id:name' => 'Event Fee', - ], $priceFieldOption), - )->execute()->first()['id'], $identifier . '_' . $optionIdentifier); + ], $priceFieldOption), $identifier . '_' . $optionIdentifier); } } @@ -323,12 +349,10 @@ trait EventTestTrait { * functions and would allow over-riding. * * @return array[] - * - * @throws \CRM_Core_Exception */ protected function getPriceFieldOptions(string $identifier = 'PaidEvent'): array { if ($identifier !== 'PaidEvent') { - throw new \CRM_Core_Exception('Only paid event currently supported'); + $this->fail('Only paid event currently supported'); } return [ 'free' => ['name' => 'free', 'label' => 'Complementary', 'amount' => 0], diff --git a/tests/phpunit/CRM/Core/BAO/DiscountTest.php b/tests/phpunit/CRM/Core/BAO/DiscountTest.php new file mode 100644 index 0000000000..c80ed85665 --- /dev/null +++ b/tests/phpunit/CRM/Core/BAO/DiscountTest.php @@ -0,0 +1,31 @@ +eventCreatePaid(); + $this->addDiscountPriceSet(); + $options = Discount::getFields(TRUE) + ->setLoadOptions(TRUE) + ->addValue('entity_id', 1) + ->addValue('entity_table', 'civicrm_event') + ->addWhere('name', '=', 'price_set_id') + ->execute()->first()['options']; + $this->assertEquals([$this->ids['PriceSet']['discount']], array_keys($options)); + } + +} -- 2.25.1