From 2d88391dd4b8d103e4982da0aa2214a65dce612a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 5 Jun 2023 11:27:20 +1200 Subject: [PATCH] Move legacy prices set search test to extension --- Civi/Test/EventTestTrait.php | 71 +++++++++++- .../Contact/Form/Search/Custom/PriceSet.php | 4 +- .../phpunit/Civi/Searches/PriceSetTest.php | 104 ++++++++++++++++++ .../Form/Search/Custom/PriceSetTest.php | 77 ------------- 4 files changed, 173 insertions(+), 83 deletions(-) create mode 100644 ext/legacycustomsearches/tests/phpunit/Civi/Searches/PriceSetTest.php delete mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/PriceSetTest.php diff --git a/Civi/Test/EventTestTrait.php b/Civi/Test/EventTestTrait.php index a50b818b20..35cf652dd9 100644 --- a/Civi/Test/EventTestTrait.php +++ b/Civi/Test/EventTestTrait.php @@ -13,6 +13,9 @@ 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; @@ -78,10 +81,7 @@ trait EventTestTrait { $event = $this->eventCreate($eventParameters, $identifier); if (array_keys($priceSetParameters) !== ['id']) { try { - - // @todo - eventPriceSetCreate is only available from core tests so this can't yet - // be used in extensions. - $this->setTestEntityID('PriceSet', $this->eventPriceSetCreate(8000.67, 0, 'Radio', [['name' => 'family_package', 'amount' => 1550.55], ['name' => 'corporate_table', 'amount' => 8000.67]]), $identifier); + $this->eventCreatePriceSet([], $identifier); $this->setTestEntityID('PriceSetEntity', PriceSetEntity::create(FALSE) ->setValues([ 'entity_table' => 'civicrm_event', @@ -323,4 +323,67 @@ trait EventTestTrait { } } + /** + * Create a price set for an event. + * + * @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', + '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([ + 'label' => 'Fundraising Dinner', + 'name' => 'fundraising_dinner', + 'html_type' => 'Radio', + 'is_display_amounts' => 1, + 'options_per_line' => 1, + 'price_set_id' => $this->ids['PriceSet'][$identifier], + 'is_enter_qty' => 1, + 'financial_type_id:name' => 'Event Fee', + ])->execute()->first()['id'], $identifier); + + foreach ($this->getPriceFieldOptions() as $optionIdentifier => $priceFieldOption) { + $this->setTestEntityID('PriceFieldValue', PriceFieldValue::create(FALSE)->setValues( + array_merge([ + 'price_field_id' => $this->ids['PriceField'][$identifier], + 'financial_type_id:name' => 'Event Fee', + ], $priceFieldOption), + )->execute()->first()['id'], $identifier . '_' . $optionIdentifier); + } + } + + /** + * Get the options for the price set. + * + * @param string $identifier Optional string if we want to specify different + * options. This is not currently used but is consistent with our other + * 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'); + } + return [ + 'free' => ['name' => 'free', 'label' => 'Complementary', 'amount' => 0], + 'student' => ['name' => 'student', 'label' => 'Student Rate', 'amount' => 100], + '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/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/PriceSet.php b/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/PriceSet.php index 007845840d..444f84ce2d 100644 --- a/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/PriceSet.php +++ b/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/PriceSet.php @@ -55,10 +55,10 @@ class CRM_Contact_Form_Search_Custom_PriceSet extends CRM_Contact_Form_Search_Cu } public function buildTempTable() { - $sql = "id int unsigned NOT NULL AUTO_INCREMENT, + $sql = 'id int unsigned NOT NULL AUTO_INCREMENT, contact_id int unsigned NOT NULL, participant_id int unsigned NOT NULL, -"; +'; foreach ($this->_columns as $dontCare => $fieldName) { if (in_array($fieldName, [ diff --git a/ext/legacycustomsearches/tests/phpunit/Civi/Searches/PriceSetTest.php b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/PriceSetTest.php new file mode 100644 index 0000000000..7be1112653 --- /dev/null +++ b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/PriceSetTest.php @@ -0,0 +1,104 @@ + (801) 534-1262 + * @copyright Copyright CiviCRM LLC (C) 2009 + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html + * GNU Affero General Public License version 3 + * @package CiviCRM + * + * This file is part of CiviCRM + * + * CiviCRM is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. + * + * CiviCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this program. If not, see + * . + */ + +use Civi\Test; +use Civi\Test\Api3TestTrait; +use Civi\Test\ContactTestTrait; +use Civi\Test\EventTestTrait; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; +use PHPUnit\Framework\TestCase; + +/** + * Test contact custom search functions + * + * @group headless + */ +class CRM_Contact_Form_Search_Custom_PriceSetTest extends TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { + + use EventTestTrait; + use Api3TestTrait; + use ContactTestTrait; + + /** + * Civi\Test has many helpers, like install(), uninstall(), sql(), and + * sqlFile(). See: + * https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md + */ + public function setUpHeadless(): Test\CiviEnvBuilder { + return Test::headless() + ->install(['legacycustomsearches']) + ->apply(); + } + + /** + * @throws \Civi\Core\Exception\DBQueryException + */ + public function testRunSearch(): void { + $this->eventCreatePaid(); + $contactID = $this->individualCreate(); + $this->callAPISuccess('Order', 'create', [ + 'total_amount' => 100, + 'currency' => 'USD', + 'contact_id' => $contactID, + 'financial_type_id' => 4, + 'line_items' => [ + [ + 'line_item' => [ + [ + 'price_field_value_id' => $this->ids['PriceFieldValue']['PaidEvent_student'], + 'price_field_id' => $this->ids['PriceField']['PaidEvent'], + 'qty' => 1, + 'line_total' => 100, + 'entity_table' => 'civicrm_participant', + ], + ], + 'params' => [ + 'financial_type_id' => 4, + 'event_id' => $this->getEventID(), + 'role_id' => 1, + 'status_id' => 14, + 'fee_currency' => 'USD', + 'contact_id' => $contactID , + ], + ], + ], + ]); + $formValues = ['event_id' => $this->getEventID()]; + $form = new CRM_Contact_Form_Search_Custom_PriceSet($formValues); + $sql = $form->all(); + // Assert that we have created a standard temp table + $this->assertStringContainsString('civicrm_tmp_e_priceset', $sql); + // Check that the temp table has been populated. + $result = CRM_Core_DAO::executeQuery($sql)->fetchAll(); + $this->assertNotEmpty($result); + } + +} diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/PriceSetTest.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/PriceSetTest.php deleted file mode 100644 index d777f14515..0000000000 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/PriceSetTest.php +++ /dev/null @@ -1,77 +0,0 @@ - (801) 534-1262 - * @copyright Copyright CiviCRM LLC (C) 2009 - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html - * GNU Affero General Public License version 3 - * @package CiviCRM - * - * This file is part of CiviCRM - * - * CiviCRM is free software; you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. - * - * CiviCRM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with this program. If not, see - * . - */ - -/** - * Include parent class definition - */ - - -/** - * Include class under test - */ - -/** - * Include form definitions - */ - -/** - * Include DAO to do queries - */ - -/** - * Include dataProvider for tests - */ - -/** - * Test contact custom search functions - * - * @package CiviCRM - * @group headless - */ -class CRM_Contact_Form_Search_Custom_PriceSetTest extends CiviUnitTestCase { - - public function testRunSearch() { - $event = $this->eventCreate(); - $order = $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams($event['id'])); - $this->callAPISuccess('Payment', 'create', [ - 'order_id' => $order['id'], - 'total_amount' => 50, - ]); - $this->validateAllPayments(); - $formValues = ['event_id' => $event['id']]; - $form = new CRM_Contact_Form_Search_Custom_PriceSet($formValues); - $sql = $form->all(); - // Assert that we have created a standard temp table - $this->assertStringContainsString('civicrm_tmp_e_priceset', $sql); - // Check that the temp table has been populated. - $result = CRM_Core_DAO::executeQuery($sql)->fetchAll(); - $this->assertTrue(!empty($result)); - } - -} -- 2.25.1