From 7f00b9cb691d505d3856d2f1976946a5087ca865 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 25 Jun 2023 10:06:55 -0700 Subject: [PATCH] Move Custom Group search test to extension, remove xml brittleness --- CRM/Contact/BAO/SavedSearch.php | 7 - Civi/Test/EntityTrait.php | 92 ++++ Civi/Test/EventTestTrait.php | 53 +- .../CRM/Contact/Form/Search/Custom/Group.php | 12 +- .../legacycustomsearches.php | 7 + .../tests/phpunit/Civi/Searches/GroupTest.php | 470 ++++++++++++++++++ .../phpunit/Civi/Searches/SampleTest.php | 8 +- .../Contact/Form/Search/Custom/GroupTest.php | 214 -------- .../Search/Custom/GroupTestDataProvider.php | 378 -------------- .../Search/Custom/datasets/group-dataset.xml | 434 ---------------- tests/phpunit/CRM/Profile/Form/EditTest.php | 45 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 16 - 12 files changed, 599 insertions(+), 1137 deletions(-) create mode 100644 Civi/Test/EntityTrait.php create mode 100644 ext/legacycustomsearches/tests/phpunit/Civi/Searches/GroupTest.php delete mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php delete mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php delete mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/group-dataset.xml diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index afedb57162..a5f801dcc0 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -157,13 +157,6 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch implements } } - if ($customSearchClass = CRM_Utils_Array::value('customSearchClass', $result)) { - // check if there is a special function - formatSavedSearchFields defined in the custom search form - if (method_exists($customSearchClass, 'formatSavedSearchFields')) { - $customSearchClass::formatSavedSearchFields($result); - } - } - return $result; } diff --git a/Civi/Test/EntityTrait.php b/Civi/Test/EntityTrait.php new file mode 100644 index 0000000000..1dfe24cbfa --- /dev/null +++ b/Civi/Test/EntityTrait.php @@ -0,0 +1,92 @@ +ids = ['Event' => ['descriptive_key' => $eventID], 'Group' => [$groupID]]; + * + * @var array + */ + protected $ids = []; + + /** + * Records created which will be deleted during tearDown + * + * @var array + */ + protected $testRecords = []; + + /** + * Track tables we have modified during a test. + * + * Set up functions that add entities can register the relevant tables here for + * the cleanup process. + * + * @var array + */ + protected $tablesToCleanUp = []; + + /** + * Create an entity, recording it's details for tearDown. + * + * @param string $entity + * @param array $params + * @param string $identifier + * + * @return array + */ + protected function createTestEntity(string $entity, array $params, string $identifier = 'default'): array { + $result = NULL; + try { + $result = \civicrm_api4($entity, 'create', ['values' => $params, 'checkPermissions' => FALSE])->first(); + $this->setTestEntityID($entity, $result['id'], $identifier); + } + catch (\CRM_Core_Exception $e) { + $this->fail('Failed to create ' . $entity . ' : ' . $e->getMessage()); + } + return $result; + } + + /** + * Set the test entity on the class for access. + * + * This follows the ids patter and also the api4TestTrait pattern. + * + * @param string $entity + * @param array $values + * @param string $identifier + */ + protected function setTestEntity(string $entity, array $values, string $identifier): void { + $this->ids[$entity][$identifier] = $values['id']; + $this->testRecords[] = [$entity, [[$values['id'] => $values]]]; + $tableName = \CRM_Core_DAO_AllCoreTables::getTableForEntityName($entity); + $this->tablesToCleanUp[$tableName] = $tableName; + } + + /** + * @param string $entity + * @param int $id + * @param string $identifier + */ + protected function setTestEntityID(string $entity, int $id, string $identifier): void { + $this->setTestEntity($entity, ['id' => $id], $identifier); + } + +} diff --git a/Civi/Test/EventTestTrait.php b/Civi/Test/EventTestTrait.php index c3ed135983..4f9367800b 100644 --- a/Civi/Test/EventTestTrait.php +++ b/Civi/Test/EventTestTrait.php @@ -39,33 +39,7 @@ use Civi\Api4\UFJoin; * profiles and price set data as appropriate. */ trait EventTestTrait { - - /** - * Array of IDs created to support the test. - * - * e.g - * $this->ids = ['Event' => ['descriptive_key' => $eventID], 'Group' => [$groupID]]; - * - * @var array - */ - protected $ids = []; - - /** - * Records created which will be deleted during tearDown - * - * @var array - */ - protected $testRecords = []; - - /** - * Track tables we have modified during a test. - * - * Set up functions that add entities can register the relevant tables here for - * the cleanup process. - * - * @var array - */ - protected $tablesToCleanUp = []; + use EntityTrait; /** * Create a paid event. @@ -120,31 +94,6 @@ trait EventTestTrait { return $this->eventCreate($eventParameters, $identifier); } - /** - * Set the test entity on the class for access. - * - * This follows the ids patter and also the api4TestTrait pattern. - * - * @param string $entity - * @param array $values - * @param string $identifier - */ - protected function setTestEntity(string $entity, array $values, string $identifier): void { - $this->ids[$entity][$identifier] = $values['id']; - $this->testRecords[] = [$entity, [[$values['id'] => $values]]]; - $tableName = \CRM_Core_DAO_AllCoreTables::getTableForEntityName($entity); - $this->tablesToCleanUp[$tableName] = $tableName; - } - - /** - * @param string $entity - * @param int $id - * @param string $identifier - */ - protected function setTestEntityID(string $entity, int $id, string $identifier): void { - $this->setTestEntity($entity, ['id' => $id], $identifier); - } - /** * Get the event id of the event created in set up. * diff --git a/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/Group.php b/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/Group.php index f63bdb2f5a..4baf39450d 100644 --- a/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/Group.php +++ b/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/Group.php @@ -245,7 +245,7 @@ class CRM_Contact_Form_Search_Custom_Group extends CRM_Contact_Form_Search_Custo ) { if ($justIDs) { - $selectClause = "contact_a.id as contact_id"; + $selectClause = 'contact_a.id as contact_id'; } else { $selectClause = "contact_a.id as contact_id, @@ -275,7 +275,7 @@ class CRM_Contact_Form_Search_Custom_Group extends CRM_Contact_Form_Search_Custo $where = $this->where($includeContactIDs); if (!$justIDs && !$this->_allSearch) { - $groupBy = " GROUP BY contact_a.id"; + $groupBy = ' GROUP BY contact_a.id'; } else { // CRM-10850 @@ -292,19 +292,19 @@ class CRM_Contact_Form_Search_Custom_Group extends CRM_Contact_Form_Search_Custo if (!$justIDs) { if (!empty($sort)) { if (is_string($sort)) { - $sort = CRM_Utils_Type::escape($sort, 'String'); + $sort = \CRM_Utils_Type::escape($sort, 'String'); $sql .= " ORDER BY $sort "; } else { - $sql .= " ORDER BY " . trim($sort->orderBy()); + $sql .= ' ORDER BY ' . trim($sort->orderBy()); } } else { - $sql .= " ORDER BY contact_id ASC"; + $sql .= ' ORDER BY contact_id ASC'; } } else { - $sql .= " ORDER BY contact_a.id ASC"; + $sql .= ' ORDER BY contact_a.id ASC'; } if ($offset >= 0 && $rowcount > 0) { diff --git a/ext/legacycustomsearches/legacycustomsearches.php b/ext/legacycustomsearches/legacycustomsearches.php index 056049c041..fa19fc5823 100644 --- a/ext/legacycustomsearches/legacycustomsearches.php +++ b/ext/legacycustomsearches/legacycustomsearches.php @@ -52,6 +52,13 @@ function legacycustomsearches_civicrm_buildGroupContactCache(array $savedSearch, AND civicrm_group_contact.group_id = $groupID )"; $addSelect = "$groupID AS group_id"; $ssParams = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID); + + $customSearchClass = $ssParams['customSearchClass']; + // check if there is a special function - formatSavedSearchFields defined in the custom search form + if (method_exists($customSearchClass, 'formatSavedSearchFields')) { + $customSearchClass::formatSavedSearchFields($ssParams); + } + // CRM-7021 rectify params to what proximity search expects if there is a value for prox_distance if (!empty($ssParams)) { CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams); diff --git a/ext/legacycustomsearches/tests/phpunit/Civi/Searches/GroupTest.php b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/GroupTest.php new file mode 100644 index 0000000000..7bff678351 --- /dev/null +++ b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/GroupTest.php @@ -0,0 +1,470 @@ + (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 + * . + */ + +namespace Civi\Searches; + +use Civi\Test; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; +use CRM_Core_DAO; +use PHPUnit\Framework\TestCase; + +/** + * FIXME - Add test description. + * + * Tips: + * - With HookInterface, you may implement CiviCRM hooks directly in the test + * class. Simply create corresponding functions (e.g. "hook_civicrm_post(...)" + * or similar). + * - With TransactionalInterface, any data changes made by setUp() or + * test****() functions will rollback automatically -- as long as you don't + * manipulate schema or truncate tables. If this test needs to manipulate + * schema or truncate tables, then either: a. Do all that using setupHeadless() + * and Civi\Test. b. Disable TransactionalInterface, and handle all + * setup/teardown yourself. + * + * @group headless + */ +class GroupTest extends TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { + use Test\ContactTestTrait; + use Test\EntityTrait; + use Test\Api3TestTrait; + + /** + * 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(); + } + + /** + * Set up tags and groups for test. + */ + protected function setup(): void { + $i = 9; + while ($i < 29) { + // Weird historical data set issue. + if ($i !== 25) { + $this->individualCreate([ + 'first_name' => 'Test', + 'last_name' => 'Contact ' . $i, + ], $i); + } + $i++; + } + + foreach ([7, 9] as $tagNumber) { + $this->createTestEntity('Tag', [ + 'name' => 'Tag' . $tagNumber, + 'description' => 'Test Tag ' . $tagNumber, + ], $tagNumber); + } + $entityTags = [ + ['contact' => 10, 'tag' => 9], + ['contact' => 12, 'tag' => 9], + ['contact' => 14, 'tag' => 9], + ['contact' => 16, 'tag' => 9], + ['contact' => 18, 'tag' => 9], + ['contact' => 20, 'tag' => 9], + ['contact' => 22, 'tag' => 9], + ['contact' => 24, 'tag' => 9], + ['contact' => 11, 'tag' => 7], + ['contact' => 12, 'tag' => 7], + ['contact' => 15, 'tag' => 7], + ['contact' => 16, 'tag' => 7], + ['contact' => 19, 'tag' => 7], + ['contact' => 20, 'tag' => 7], + ['contact' => 23, 'tag' => 7], + ['contact' => 24, 'tag' => 7], + ['contact' => 26, 'tag' => 7], + ['contact' => 28, 'tag' => 7], + ]; + foreach ($entityTags as $entityTag) { + $this->createTestEntity('EntityTag', [ + 'tag_id:name' => 'Tag' . $entityTag['tag'], + 'entity_table' => 'civicrm_contact', + 'entity_id' => $this->ids['Contact'][$entityTag['contact']], + ], $entityTag['contact']); + } + + $this->createTestEntity('Group', [ + 'name' => 'Group3', + 'title' => 'Test Group 3', + ], 3); + + $this->createTestEntity('SavedSearch', [ + 'search_custom_id' => 4, + 'customSearchClass' => 'CRM_Contact_Form_Search_Custom_Group', + 'form_values' => [ + 'includeGroups' => [$this->ids['Group'][3]], + 'excludeGroups' => [], + 'customSearchID' => 4, + 'customSearchClass' => 'CRM_Contact_Form_Search_Custom_Group', + ], + ], 1); + $this->createTestEntity('SavedSearch', [ + 'search_custom_id' => 4, + 'customSearchClass' => 'CRM_Contact_Form_Search_Custom_Group', + 'form_values' => [ + 'excludeGroups' => [$this->ids['Group'][3]], + 'includeGroups' => [], + 'includeTags' => [], + 'excludeTags' => [], + 'customSearchID' => 4, + 'customSearchClass' => 'CRM_Contact_Form_Search_Custom_Group', + ], + ], 2); + + $this->createTestEntity('Group', [ + 'name' => 'Group4', + 'title' => 'Test Smart Group 4', + 'saved_search_id' => $this->ids['SavedSearch'][1], + ], 4); + + $this->createTestEntity('Group', [ + 'name' => 'Group5', + 'title' => 'Test Group 5', + ], 5); + + $this->createTestEntity('Group', [ + 'name' => 'Group6', + 'title' => 'Test Smart Group 6', + 'saved_search_id' => $this->ids['SavedSearch'][2], + ], 6); + + $groupContacts = [ + ['contact_id' => $this->ids['Contact'][13], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][14], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][15], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][16], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][21], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][22], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][23], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][24], 'group_id' => $this->ids['Group'][5], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][17], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][18], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][19], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][20], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][21], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][22], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][23], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][24], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][27], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ['contact_id' => $this->ids['Contact'][28], 'group_id' => $this->ids['Group'][3], 'status' => 'Added'], + ]; + foreach ($groupContacts as $groupContact) { + $this->createTestEntity('GroupContact', $groupContact); + } + } + + /** + * @return array + */ + public function dataProvider(): array { + return [ + 'Exclude static group 3' => [ + 'form_values' => ['excludeGroups' => [3]], + 'contact_numbers' => [1, 2, 9, 10, 11, 12, 13, 14, 15, 16, 26], + ], + 'Include static group 3' => [ + 'form_values' => ['includeGroups' => [3]], + 'contact_numbers' => [17, 18, 19, 20, 21, 22, 23, 24, 27, 28], + ], + 'Include static group 5' => [ + 'form_values' => ['includeGroups' => [5]], + 'contact_numbers' => [13, 14, 15, 16, 21, 22, 23, 24], + ], + ' Include static groups 3 and 5' => [ + 'form_values' => ['includeGroups' => [3, 5]], + 'contact_numbers' => [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28], + ], + 'Include static group 3, exclude static group 5' => [ + 'form_values' => ['includeGroups' => [3], 'excludeGroups' => [5]], + 'contact_numbers' => [17, 18, 19, 20, 27, 28], + ], + 'Exclude tag 7' => [ + 'form_values' => ['excludeTags' => [7]], + 'contact_numbers' => [1, 2, 9, 10, 13, 14, 17, 18, 21, 22, 27], + ], + 'Include tag 7' => [ + 'form_values' => ['includeTags' => [7]], + 'contact_numbers' => [11, 12, 15, 16, 19, 20, 23, 24, 26, 28], + ], + 'Include tag 9' => [ + 'form_values' => ['includeTags' => [9]], + 'contact_numbers' => [10, 12, 14, 16, 18, 20, 22, 24], + ], + 'Include tags 7 and 9' => [ + 'form_values' => ['includeTags' => [7, 9]], + 'contact_numbers' => [10, 11, 12, 14, 15, 16, 18, 19, 20, 22, 23, 24, 26, 28], + ], + 'Include tag 7, exclude tag 9' => [ + 'form_values' => ['includeTags' => [7], 'excludeTags' => [9]], + 'contact_numbers' => [11, 15, 19, 23, 26, 28], + ], + 'Include static group 3, include tag 7 (either)' => [ + 'form_values' => ['includeGroups' => [3], 'includeTags' => [7], 'andOr' => 0], + 'contact_numbers' => [11, 12, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28], + ], + 'Include static group 3, include tag 7 (both)' => [ + 'form_values' => ['includeGroups' => [3], 'includeTags' => [7], 'andOr' => 1], + 'contact_numbers' => [19, 20, 23, 24, 28], + ], + 'Include static group 3, exclude tag 7' => [ + 'form_values' => ['includeGroups' => [3], 'excludeTags' => [7]], + 'contact_numbers' => [17, 18, 21, 22, 27], + ], + 'Include tag 9, exclude static group 5' => [ + 'form_values' => ['includeTags' => [9], 'excludeGroups' => ['5']], + 'contact_numbers' => [10, 12, 18, 20], + ], + 'Exclude tag 9, exclude static group 5' => [ + 'form_values' => ['excludeTags' => [9], 'excludeGroups' => [5]], + 'contact_numbers' => [1, 2, 9, 11, 17, 19, 26, 27, 28], + ], + 'Include smart group 6' => [ + 'form_values' => ['includeGroups' => [6]], + 'contact_numbers' => [1, 2, 9, 10, 11, 12, 13, 14, 15, 16, 26], + ], + 'Include smart group 4' => [ + 'form_values' => ['includeGroups' => [4]], + 'contact_numbers' => [17, 18, 19, 20, 21, 22, 23, 24, 27, 28], + ], + 'Include smart group 4 and static group 5' => [ + 'form_values' => ['includeGroups' => [4, 5]], + 'contact_numbers' => [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28], + ], + ]; + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::all(). + * + * @dataProvider dataProvider + * + * @param array $formValues + * @param array $contactIDs + * + * @throws \CRM_Core_Exception + */ + public function testAll(array $formValues, array $contactIDs): void { + $formValues = $this->replaceFormValuesPlaceholders($formValues); + $full = []; + foreach ($contactIDs as $id) { + if ($id === 1) { + $full[] = [ + 'contact_id' => 1, + 'contact_type' => 'Organization', + 'sort_name' => 'Default Organization', + ]; + } + elseif ($id === 2) { + $full[] = [ + 'contact_id' => 2, + 'contact_type' => 'Organization', + 'sort_name' => 'Second Domain', + ]; + } + else { + $full[] = [ + 'contact_id' => $this->ids['Contact'][$id], + 'contact_type' => 'Individual', + 'sort_name' => 'Contact ' . $id . ', Test', + ]; + } + } + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $sql = $obj->all(); + $this->assertIsString($sql); + $dao = CRM_Core_DAO::executeQuery($sql); + $all = []; + while ($dao->fetch()) { + $all[] = [ + 'contact_id' => $dao->contact_id, + 'contact_type' => $dao->contact_type, + 'sort_name' => $dao->sort_name, + ]; + } + asort($all); + $this->assertEquals($full, $all); + $this->assertEquals(count($contactIDs), $obj->count()); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::contactIDs(). + * + * @dataProvider dataProvider + * + * @param $formValues + * @param $contactIDs + * + * @throws \Exception + */ + public function testContactIDs($formValues, $contactIDs): void { + $formValues = $this->replaceFormValuesPlaceholders($formValues); + $contactIDs = $this->replaceIDSPlaceholders($contactIDs); + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $sql = $obj->contactIDs(); + $this->assertIsString($sql); + $dao = CRM_Core_DAO::executeQuery($sql); + $contacts = []; + while ($dao->fetch()) { + $contacts[$dao->contact_id] = 1; + } + $contacts = array_keys($contacts); + sort($contacts, SORT_NUMERIC); + $this->assertEquals($contactIDs, $contacts); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::columns() + * It returns an array of translated name => keys + */ + public function testColumns(): void { + $formValues = []; + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $columns = $obj->columns(); + $this->assertIsArray($columns); + foreach ($columns as $key => $value) { + $this->assertIsString($key); + $this->assertIsString($value); + } + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::summary() + * It returns NULL + */ + public function testSummary(): void { + $formValues = []; + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $this->assertNull($obj->summary()); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::templateFile() + * Returns the path to the file as a string + */ + public function testTemplateFile(): void { + $formValues = []; + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $fileName = $obj->templateFile(); + $this->assertIsString($fileName); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::where( ) + * With no arguments it returns '(1)' + */ + public function testWhereNoArgs(): void { + $formValues = [ + \CRM_Core_Form::CB_PREFIX . 17 => TRUE, + \CRM_Core_Form::CB_PREFIX . 23 => TRUE, + ]; + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $this->assertEquals(' (1) ', $obj->where()); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::where( ) + * With false argument it returns '(1)' + */ + public function testWhereFalse(): void { + $formValues = [ + \CRM_Core_Form::CB_PREFIX . 17 => TRUE, + \CRM_Core_Form::CB_PREFIX . 23 => TRUE, + ]; + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $this->assertEquals(' (1) ', $obj->where(FALSE)); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::where( ) + * With true argument it returns list of contact IDs + */ + public function testWhereTrue(): void { + $formValues = [ + \CRM_Core_Form::CB_PREFIX . 17 => TRUE, + \CRM_Core_Form::CB_PREFIX . 23 => TRUE, + ]; + $obj = new \CRM_Contact_Form_Search_Custom_Group($formValues); + $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE)); + } + + /** + * Replace placeholder form values with created IDS. + * + * @param array $formValues + * + * @return array + */ + private function replaceFormValuesPlaceholders(array $formValues): array { + if (!empty($formValues['excludeGroups'])) { + foreach ($formValues['excludeGroups'] as $index => $number) { + $formValues['excludeGroups'][$index] = $this->ids['Group'][$number]; + } + } + if (!empty($formValues['includeGroups'])) { + foreach ($formValues['includeGroups'] as $index => $number) { + $formValues['includeGroups'][$index] = $this->ids['Group'][$number]; + } + } + if (!empty($formValues['excludeTags'])) { + foreach ($formValues['excludeTags'] as $index => $number) { + $formValues['excludeTags'][$index] = $this->ids['Tag'][$number]; + } + } + if (!empty($formValues['includeTags'])) { + foreach ($formValues['includeTags'] as $index => $number) { + $formValues['includeTags'][$index] = $this->ids['Tag'][$number]; + } + } + return $formValues; + } + + /** + * @param array $contactIDs + * + * @return array + */ + private function replaceIDSPlaceholders(array $contactIDs): array { + foreach ($contactIDs as $index => $id) { + if ($id > 2) { + $contactIDs[$index] = $this->ids['Contact'][$id]; + } + } + return $contactIDs; + } + +} diff --git a/ext/legacycustomsearches/tests/phpunit/Civi/Searches/SampleTest.php b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/SampleTest.php index 9c3ed347d0..737723c1cc 100644 --- a/ext/legacycustomsearches/tests/phpunit/Civi/Searches/SampleTest.php +++ b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/SampleTest.php @@ -230,15 +230,15 @@ class SampleTest extends TestCase implements HeadlessInterface, HookInterface, T public function testSavedSearch(): void { $this->setupSampleData(); $this->setupSavedSearches(); - $dataset[1] = ['id' => $this->getContactIDs(['Household - NY'])]; - $dataset[2] = [ + $dataset[0] = ['id' => $this->getContactIDs(['Household - NY'])]; + $dataset[1] = [ 'id' => $this->getContactIDs([ 'Household - CA', 'Household - CA - 2', ]), ]; $searches = SavedSearch::get()->addSelect('*')->execute(); - foreach ($searches as $search) { + foreach ($searches as $index => $search) { $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($search['id']); $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues); $sql = $obj->contactIDs(); @@ -249,7 +249,7 @@ class SampleTest extends TestCase implements HeadlessInterface, HookInterface, T $contacts[] = $dao->contact_id; } sort($contacts, SORT_NUMERIC); - $this->assertEquals($dataset[$search['id']]['id'], $contacts); + $this->assertEquals($dataset[$index]['id'], $contacts, 'Failed on search ' . $search['id']); } } diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php deleted file mode 100644 index 2f2c78976d..0000000000 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php +++ /dev/null @@ -1,214 +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 - * . - */ - -/** - * Test contact custom search functions - * - * @package CiviCRM - * @group headless - */ -class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase { - - /** - * @throws \CRM_Core_Exception - */ - public function tearDown(): void { - $this->quickCleanup([ - 'civicrm_group_contact', - 'civicrm_group', - 'civicrm_saved_search', - 'civicrm_entity_tag', - 'civicrm_tag', - ]); - parent::tearDown(); - } - - /** - * @return CRM_Contact_Form_Search_Custom_GroupTestDataProvider - */ - public function dataProvider(): CRM_Contact_Form_Search_Custom_GroupTestDataProvider { - return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider(); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::count(). - * - * @dataProvider dataProvider - * - * @param array $fv - * @param int $count - * - * @throws \CRM_Core_Exception - */ - public function testCount(array $fv, int $count): void { - $this->loadXMLDataSet(__DIR__ . '/datasets/group-dataset.xml'); - - $obj = new CRM_Contact_Form_Search_Custom_Group($fv); - - $sql = $obj->all(); - CRM_Core_DAO::executeQuery($sql); - - /** - * echo "Count: $count, OBJ: ", $obj->count( ) . "\n"; - * while ( $dao->fetch( ) ) { - * echo "{$dao->contact_id}, {$dao->contact_type}, {$dao->sort_name}, {$dao->group_names}\n"; - * } - **/ - $this->assertEquals($count, $obj->count()); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::all() - * @dataProvider dataProvider - * @param $fv - * @param $count - * @param $ids - * @param $full - * @throws \Exception - */ - public function testAll($fv, $count, $ids, $full): void { - $this->loadXMLDataSet(__DIR__ . '/datasets/group-dataset.xml'); - - $obj = new CRM_Contact_Form_Search_Custom_Group($fv); - $sql = $obj->all(); - $this->assertIsString($sql); - $dao = CRM_Core_DAO::executeQuery($sql); - $all = []; - while ($dao->fetch()) { - $all[] = [ - 'contact_id' => $dao->contact_id, - 'contact_type' => $dao->contact_type, - 'sort_name' => $dao->sort_name, - ]; - } - asort($all); - $this->assertEquals($full, $all); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::contactIDs() - * @dataProvider dataProvider - * @param $fv - * @param $count - * @param $ids - * @param $full - * @throws \Exception - */ - public function testContactIDs($fv, $count, $ids, $full): void { - $this->loadXMLDataSet(__DIR__ . '/datasets/group-dataset.xml'); - - $obj = new CRM_Contact_Form_Search_Custom_Group($fv); - $sql = $obj->contactIDs(); - $this->assertTrue(is_string($sql)); - $dao = CRM_Core_DAO::executeQuery($sql); - $contacts = []; - while ($dao->fetch()) { - $contacts[$dao->contact_id] = 1; - } - $contacts = array_keys($contacts); - sort($contacts, SORT_NUMERIC); - $this->assertEquals($ids, $contacts); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::columns() - * It returns an array of translated name => keys - */ - public function testColumns(): void { - $formValues = []; - $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); - $columns = $obj->columns(); - $this->assertIsArray($columns); - foreach ($columns as $key => $value) { - $this->assertIsString($key); - $this->assertIsString($value); - } - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::summary() - * It returns NULL - */ - public function testSummary(): void { - $formValues = []; - $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); - $this->assertNull($obj->summary()); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::templateFile() - * Returns the path to the file as a string - */ - public function testTemplateFile(): void { - $formValues = []; - $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); - $fileName = $obj->templateFile(); - $this->assertIsString($fileName); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::where( ) - * With no arguments it returns '(1)' - */ - public function testWhereNoArgs(): void { - $formValues = [ - CRM_Core_Form::CB_PREFIX . '17' => TRUE, - CRM_Core_Form::CB_PREFIX . '23' => TRUE, - ]; - $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); - $this->assertEquals(' (1) ', $obj->where()); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::where( ) - * With false argument it returns '(1)' - */ - public function testWhereFalse(): void { - $formValues = [ - CRM_Core_Form::CB_PREFIX . '17' => TRUE, - CRM_Core_Form::CB_PREFIX . '23' => TRUE, - ]; - $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); - $this->assertEquals(' (1) ', $obj->where(FALSE)); - } - - /** - * Test CRM_Contact_Form_Search_Custom_Group::where( ) - * With true argument it returns list of contact IDs - */ - public function testWhereTrue(): void { - $formValues = [ - CRM_Core_Form::CB_PREFIX . '17' => TRUE, - CRM_Core_Form::CB_PREFIX . '23' => TRUE, - ]; - $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); - $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE)); - } - -} diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php deleted file mode 100644 index 46c321f39e..0000000000 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php +++ /dev/null @@ -1,378 +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 - * @version $Id: GroupTestDataProvider.php 44314 2012-12-19 11:12:49Z kurund $ - * @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 - * . - */ - -/** - * Provide data to the CRM_Contact_Form_Search_Custom_GroupTest class - * - * @package CiviCRM - */ -class CRM_Contact_Form_Search_Custom_GroupTestDataProvider implements Iterator { - - /** - * Current count. - * - * @var int - */ - private $i = 0; - - /** - * @var mixed[] - * This dataset describes various form values and what contact - * IDs should be selected when the form values are applied to the - * database in dataset.xml - */ - private $dataset = [ - // Exclude static group 3 - [ - 'fv' => ['excludeGroups' => ['3']], - 'id' => [ - 1, - 2, - '9', - '10', - '11', - '12', - '13', - '14', - '15', - '16', - '26', - ], - ], - // Include static group 3 - [ - 'fv' => ['includeGroups' => ['3']], - 'id' => [ - '17', - '18', - '19', - '20', - '21', - '22', - '23', - '24', - '27', - '28', - ], - ], - // Include static group 5 - [ - 'fv' => ['includeGroups' => ['5']], - 'id' => [ - '13', - '14', - '15', - '16', - '21', - '22', - '23', - '24', - ], - ], - // Include static groups 3 and 5 - [ - 'fv' => ['includeGroups' => ['3', '5']], - 'id' => [ - '13', - '14', - '15', - '16', - '17', - '18', - '19', - '20', - '21', - '22', - '23', - '24', - '27', - '28', - ], - ], - // Include static group 3, exclude static group 5 - [ - 'fv' => [ - 'includeGroups' => ['3'], - 'excludeGroups' => ['5'], - ], - 'id' => ['17', '18', '19', '20', '27', '28'], - ], - // Exclude tag 7 - [ - 'fv' => ['excludeTags' => ['7']], - 'id' => [ - 1, - 2, - '9', - '10', - '13', - '14', - '17', - '18', - '21', - '22', - '27', - ], - ], - // Include tag 7 - [ - 'fv' => ['includeTags' => ['7']], - 'id' => [ - '11', - '12', - '15', - '16', - '19', - '20', - '23', - '24', - '26', - '28', - ], - ], - // Include tag 9 - [ - 'fv' => ['includeTags' => ['9']], - 'id' => [ - '10', - '12', - '14', - '16', - '18', - '20', - '22', - '24', - ], - ], - // Include tags 7 and 9 - [ - 'fv' => ['includeTags' => ['7', '9']], - 'id' => [ - '10', - '11', - '12', - '14', - '15', - '16', - '18', - '19', - '20', - '22', - '23', - '24', - '26', - '28', - ], - ], - // Include tag 7, exclude tag 9 - [ - 'fv' => [ - 'includeTags' => ['7'], - 'excludeTags' => ['9'], - ], - 'id' => ['11', '15', '19', '23', '26', '28'], - ], - // Include static group 3, include tag 7 (either) - [ - 'fv' => [ - 'includeGroups' => ['3'], - 'includeTags' => ['7'], - 'andOr' => 0, - ], - 'id' => [ - '11', - '12', - '15', - '16', - '17', - '18', - '19', - '20', - '21', - '22', - '23', - '24', - '26', - '27', - '28', - ], - ], - // Include static group 3, include tag 7 (both) - [ - 'fv' => [ - 'includeGroups' => ['3'], - 'includeTags' => ['7'], - 'andOr' => 1, - ], - 'id' => ['19', '20', '23', '24', '28'], - ], - // Include static group 3, exclude tag 7 - [ - 'fv' => [ - 'includeGroups' => ['3'], - 'excludeTags' => ['7'], - ], - 'id' => ['17', '18', '21', '22', '27'], - ], - // Include tag 9, exclude static group 5 - [ - 'fv' => [ - 'includeTags' => ['9'], - 'excludeGroups' => ['5'], - ], - 'id' => ['10', '12', '18', '20'], - ], - // Exclude tag 9, exclude static group 5 - [ - 'fv' => [ - 'excludeTags' => ['9'], - 'excludeGroups' => ['5'], - ], - 'id' => [ - 1, - 2, - '9', - '11', - '17', - '19', - '26', - '27', - '28', - ], - ], - // Include smart group 6 - [ - 'fv' => ['includeGroups' => ['6']], - 'id' => [ - 1, - 2, - '9', - '10', - '11', - '12', - '13', - '14', - '15', - '16', - '26', - ], - ], - // Include smart group 4 - [ - 'fv' => ['includeGroups' => ['4']], - 'id' => [ - '17', - '18', - '19', - '20', - '21', - '22', - '23', - '24', - '27', - '28', - ], - ], - // Include smart group 4 and static group 5 - [ - 'fv' => ['includeGroups' => ['4', '5']], - 'id' => [ - '13', - '14', - '15', - '16', - '17', - '18', - '19', - '20', - '21', - '22', - '23', - '24', - '27', - '28', - ], - ], - ]; - - #[\ReturnTypeWillChange] - public function rewind() { - $this->i = 0; - } - - /** - * @return array - */ - #[\ReturnTypeWillChange] - public function current() { - $count = count($this->dataset[$this->i]['id']); - $ids = $this->dataset[$this->i]['id']; - $full = []; - foreach ($this->dataset[$this->i]['id'] as $value) { - if ($value < 3) { - // One of the domain contacts... - $full[] = [ - 'contact_id' => $value, - 'contact_type' => 'Organization', - 'sort_name' => $value === 1 ? 'Unit Test Organization' : 'Second Domain', - ]; - } - else { - $full[] = [ - 'contact_id' => $value, - 'contact_type' => 'Individual', - 'sort_name' => "Test Contact $value", - ]; - } - } - return [$this->dataset[$this->i]['fv'], $count, $ids, $full]; - } - - /** - * @return int - */ - #[\ReturnTypeWillChange] - public function key() { - return $this->i; - } - - public function next(): void { - $this->i++; - } - - /** - * @return bool - */ - public function valid(): bool { - return isset($this->dataset[$this->i]); - } - -} diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/group-dataset.xml b/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/group-dataset.xml deleted file mode 100644 index 2b177c4d14..0000000000 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/group-dataset.xml +++ /dev/null @@ -1,434 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/phpunit/CRM/Profile/Form/EditTest.php b/tests/phpunit/CRM/Profile/Form/EditTest.php index d04b602938..9125c9cb2e 100644 --- a/tests/phpunit/CRM/Profile/Form/EditTest.php +++ b/tests/phpunit/CRM/Profile/Form/EditTest.php @@ -24,7 +24,7 @@ class CRM_Profile_Form_EditTest extends CiviUnitTestCase { */ public function testProfileUrl(): void { $profileID = $this->createTestEntity('UFGroup', [ - 'post_URL' => 'civicrm/{contact.display_name}', + 'post_url' => 'civicrm/{contact.display_name}', 'title' => 'title', ])['id']; UFJoin::create(FALSE)->setValues([ @@ -32,10 +32,10 @@ class CRM_Profile_Form_EditTest extends CiviUnitTestCase { 'uf_group_id' => $profileID, ])->execute(); $this->uFFieldCreate(['uf_group_id' => $profileID]); - $id = $this->individualCreate(); + $contactID = $this->individualCreate(); $form = $this->getFormObject('CRM_Profile_Form_Edit'); $form->set('gid', $profileID); - $form->set('id', $id); + $form->set('id', $contactID); $form->buildForm(); $form->postProcess(); $this->assertEquals('civicrm/Mr. Anthony Anderson II', CRM_Core_Session::singleton()->popUserContext()); @@ -51,33 +51,26 @@ class CRM_Profile_Form_EditTest extends CiviUnitTestCase { 'group_type' => 'Individual,Contact', 'name' => 'test_individual_contact_tag_profile', 'title' => 'Gimme a tag', - 'api.uf_field.create' => [ - [ - 'field_name' => 'first_name', - 'is_required' => 1, - 'visibility' => 'Public Pages and Listings', - 'field_type' => 'Individual', - 'label' => 'First Name', - ], - [ - 'field_name' => 'last_name', - 'is_required' => 1, - 'visibility' => 'Public Pages and Listings', - 'field_type' => 'Individual', - 'label' => 'Last Name', - ], - [ - 'field_name' => 'tag', - 'is_required' => 1, - 'visibility' => 'Public Pages and Listings', - 'field_type' => 'Contact', - 'label' => 'Tag', - ], - ], ]; $profile = $this->createTestEntity('UFGroup', $ufGroupParams); $profileID = $profile['id']; + $this->createTestEntity('UFField', [ + 'field_name' => 'first_name', + 'is_required' => 1, + 'visibility' => 'Public Pages and Listings', + 'field_type' => 'Individual', + 'label' => 'First Name', + 'uf_group_id' => $profileID, + ]); + $this->createTestEntity('UFField', [ + 'field_name' => 'tag', + 'is_required' => 1, + 'visibility' => 'Public Pages and Listings', + 'field_type' => 'Contact', + 'label' => 'Tag', + 'uf_group_id' => $profileID, + ]); // Configure the profile to be used as a standalone profile for data entry. UFJoin::create(FALSE)->setValues([ diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 0238da8f7e..48d794e157 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -559,22 +559,6 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * api Result */ - /** - * Create an entity, recording it's details for tearDown. - * - * @param string $entity - * @param array $params - * @param string $identifier - * - * @return array|int - */ - protected function createTestEntity(string $entity, array $params, string $identifier = 'default') { - $params['version'] = 4; - $result = $this->callAPISuccess($entity, 'create', $params); - $this->setTestEntityID($entity, $result['id'], $identifier); - return reset($result['values']); - } - /** * @param array $params * -- 2.25.1