From 68f97b77d4e45eecd2ffdfd5e1677da9093dbe98 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 22 Aug 2019 07:43:23 +1200 Subject: [PATCH] [Test] [NFC] upgrade remamining export tests Fully switch to checking csv output with new helper - this allows us to remove the hack from the core fn --- CRM/Export/BAO/Export.php | 13 +--- tests/phpunit/CRM/Export/BAO/ExportTest.php | 86 +++++++-------------- 2 files changed, 28 insertions(+), 71 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index b7f06d505b..22ef1aede7 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -70,9 +70,6 @@ class CRM_Export_BAO_Export { * @param array $exportParams * @param string $queryOperator * - * @return array|null - * An array can be requested from within a unit test. - * * @throws \CRM_Core_Exception */ public static function exportComponents( @@ -205,15 +202,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c $processor->mergeSameAddress(); } - // In order to be able to write a unit test against this function we need to suppress - // the csv writing. In future hopefully the csv writing & the main processing will be in separate functions. - if (empty($exportParams['suppress_csv_for_testing'])) { - $processor->writeCSVFromTable(); - } - else { - // return tableName sqlColumns headerRows in test context - return [$processor->getTemporaryTable(), $sqlColumns, $processor->getHeaderRows(), $processor]; - } + $processor->writeCSVFromTable(); // delete the export temp table and component table $sql = "DROP TABLE IF EXISTS " . $processor->getTemporaryTable(); diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 8bc6abdb88..71f8d5b068 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -893,6 +893,9 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Export City against multiple location types. + * + * @throws \CRM_Core_Exception + * @throws \League\Csv\Exception */ public function testExportAddressData() { $this->diversifyLocationTypes(); @@ -903,7 +906,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $this->contactIDs[] = $this->individualCreate(); $this->contactIDs[] = $this->householdCreate(); $this->contactIDs[] = $this->organizationCreate(); - $fields = [['Individual', 'contact_id']]; + $fields = [['name' => 'contact_id']]; foreach ($this->contactIDs as $contactID) { foreach ($locationTypes as $locationName => $locationLabel) { $this->callAPISuccess('Address', 'create', [ @@ -913,9 +916,9 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'city' => $locationLabel . $contactID . 'city', 'postal_code' => $locationLabel . $contactID . 'postal_code', ]); - $fields[] = ['Individual', 'city', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)]; - $fields[] = ['Individual', 'street_address', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)]; - $fields[] = ['Individual', 'postal_code', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)]; + $fields[] = ['contact_type' => 'Individual', 'name' => 'city', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)]; + $fields[] = ['contact_type' => 'Individual', 'name' => 'street_address', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)]; + $fields[] = ['contact_type' => 'Individual', 'name' => 'postal_code', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)]; } } @@ -940,25 +943,24 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) { foreach ($relationships as $contactID => $relationship) { $fields[] = [ - 'Individual', - $relationship['relationship_type_id'] . '_a_b', - 'city', - CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType), + 'contact_type' => 'Individual', + 'relationship_type_id' => $relationship['relationship_type_id'], + 'relationship_direction' => 'a_b', + 'name' => 'city', + 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType), ]; } } - list($tableName, $sqlColumns) = $this->doExport($fields, $this->contactIDs[0]); - - $dao = CRM_Core_DAO::executeQuery('SELECT * FROM ' . $tableName); - while ($dao->fetch()) { - $id = $dao->contact_id; - $this->assertEquals('Méin' . $id . 'city', $dao->main_city); - $this->assertEquals('Billing' . $id . 'street_address', $dao->billing_street_address); - $this->assertEquals('Whare Kai' . $id . 'postal_code', $dao->whare_kai_postal_code); + + $this->doExportTest(['fields' => $fields]); + + foreach ($this->csv as $row) { + $this->assertEquals('Méin' . $row['contact_id'] . 'city', $row['Main-City']); + $this->assertEquals('Billing' . $row['contact_id'] . 'street_address', $row['Billing-Street Address']); + $this->assertEquals('Whare Kai' . $row['contact_id'] . 'postal_code', $row['Whare Kai-Postal Code']); foreach ($relationships as $relatedContactID => $relationship) { - $relationshipString = $field = $relationship['relationship_type_id'] . '_a_b'; - $field = $relationshipString . '_main_city'; - $this->assertEquals('Méin' . $relatedContactID . 'city', $dao->$field); + $value = ((int) $row['contact_id'] === $this->contactIDs[0]) ? 'Méin' . $relatedContactID . 'city' : ''; + $this->assertEquals($value, $row[$relationship['label'] . '-Main-City'], 'checking ' . $relationship['label'] . '-Main-City'); } } @@ -997,7 +999,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { '5_a_b_other_city' => '5_a_b_other_city varchar(64)', '5_a_b_whare_kai_city' => '5_a_b_whare_kai_city varchar(64)', '5_a_b_city' => '5_a_b_city varchar(64)', - ], $sqlColumns); + ], $this->processor->getSQLColumns()); } /** @@ -1269,42 +1271,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { return [$householdID, $houseHoldTypeID]; } - /** - * Do a CiviCRM export. - * - * @param array $selectedFields - * @param int $id - * - * @param int $exportMode - * - * @return array - * @throws \CRM_Core_Exception - */ - protected function doExport($selectedFields, $id, $exportMode = CRM_Export_Form_Select::CONTACT_EXPORT) { - $ids = (array) $id; - $mappedFields = []; - foreach ((array) $selectedFields as $field) { - $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field); - } - list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents( - TRUE, - $ids, - [], - NULL, - $mappedFields, - NULL, - $exportMode, - "contact_a.id IN (" . implode(',', $ids) . ")", - NULL, - FALSE, - FALSE, - [ - 'suppress_csv_for_testing' => TRUE, - ] - ); - return [$tableName, $sqlColumns]; - } - /** * Ensure component is enabled. * @@ -1804,6 +1770,9 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Test export fields when no payment fields to be exported. + * + * @throws \CRM_Core_Exception + * @throws \League\Csv\Exception */ public function textExportParticipantSpecifyFieldsNoPayment() { $selectedFields = $this->getAllSpecifiableParticipantReturnFields(); @@ -1819,9 +1788,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { unset($expected[$index]); } } - - list($tableName, $sqlColumns) = $this->doExport($selectedFields, $this->contactIDs[1], CRM_Export_Form_Select::EVENT_EXPORT); - $this->assertEquals($expected, $sqlColumns); + $this->doExportTest(['fields' => $selectedFields, 'ids' => [$this->contactIDs[1]], 'exportMode' => CRM_Export_Form_Select::EVENT_EXPORT]); + $this->assertEquals($expected, $this->processor->getSQLColumns()); } /** -- 2.25.1