From 06eef904b2030bd3de72a1d7845384a0578bb6ce Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 17 Nov 2021 19:35:55 -0500 Subject: [PATCH] CiviGrant - Move unit tests --- ext/civigrant/phpunit.xml.dist | 18 +++++ .../tests}/phpunit/api/v3/GrantTest.php | 70 +++++++++++++------ ext/civigrant/tests/phpunit/bootstrap.php | 64 +++++++++++++++++ tests/phpunit/CRM/Core/BAO/UFFieldTest.php | 13 +++- tests/phpunit/CRM/Core/PseudoConstantTest.php | 19 ----- tests/phpunit/CRM/Dedupe/MergerTest.php | 3 - .../phpunit/CRM/UF/Page/ProfileEditorTest.php | 9 ++- tests/phpunit/api/v3/ReportTemplateTest.php | 21 +----- .../phpunit/api/v3/SyntaxConformanceTest.php | 9 --- 9 files changed, 149 insertions(+), 77 deletions(-) create mode 100644 ext/civigrant/phpunit.xml.dist rename {tests => ext/civigrant/tests}/phpunit/api/v3/GrantTest.php (70%) create mode 100644 ext/civigrant/tests/phpunit/bootstrap.php diff --git a/ext/civigrant/phpunit.xml.dist b/ext/civigrant/phpunit.xml.dist new file mode 100644 index 0000000000..fc8f870b72 --- /dev/null +++ b/ext/civigrant/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + ./tests/phpunit + + + + + ./ + + + + + + + + diff --git a/tests/phpunit/api/v3/GrantTest.php b/ext/civigrant/tests/phpunit/api/v3/GrantTest.php similarity index 70% rename from tests/phpunit/api/v3/GrantTest.php rename to ext/civigrant/tests/phpunit/api/v3/GrantTest.php index 70d9a2fede..644b4c603a 100644 --- a/tests/phpunit/api/v3/GrantTest.php +++ b/ext/civigrant/tests/phpunit/api/v3/GrantTest.php @@ -16,7 +16,11 @@ * @subpackage API_Grant * @group headless */ -class api_v3_GrantTest extends CiviUnitTestCase { +class api_v3_GrantTest extends \PHPUnit\Framework\TestCase implements \Civi\Test\HeadlessInterface { + use \Civi\Test\Api3TestTrait; + use \Civi\Test\ContactTestTrait; + + protected $_apiversion = 3; protected $params; protected $ids = []; @@ -24,6 +28,12 @@ class api_v3_GrantTest extends CiviUnitTestCase { public $DBResetRequired = FALSE; + public function setUpHeadless() { + return \Civi\Test::headless() + ->installMe(__DIR__) + ->apply(); + } + public function setUp(): void { parent::setUp(); $this->ids['contact'][0] = $this->individualCreate(); @@ -50,15 +60,14 @@ class api_v3_GrantTest extends CiviUnitTestCase { $this->callAPISuccess($entity, 'delete', ['id' => $id]); } } - $this->quickCleanup(['civicrm_grant']); parent::tearDown(); } public function testCreateGrant() { - $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__); + $result = $this->callAPISuccess($this->_entity, 'create', $this->params); + $this->ids['grant'][0] = $result['id']; $this->assertEquals(1, $result['count']); $this->assertNotNull($result['values'][$result['id']]['id']); - $this->getAndCheck($this->params, $result['id'], $this->_entity); } /** @@ -67,13 +76,16 @@ class api_v3_GrantTest extends CiviUnitTestCase { * We want to ensure they are saved with separators as appropriate */ public function testCreateCustomCheckboxGrant() { - $ids = []; - $result = $this->customGroupCreate(['extends' => 'Grant']); - $ids['custom_group_id'] = $result['id']; - $customTable = $result['values'][$result['id']]['table_name']; - $result = $this->customFieldCreate([ + $cg = $this->callAPISuccess('customGroup', 'create', [ + 'title' => 'Grant custom group', + 'extends' => 'Grant', + ]); + $customTable = $cg['values'][$cg['id']]['table_name']; + $cf = $this->callAPISuccess('CustomField', 'create', [ + 'label' => 'Custom Field', + 'data_type' => 'String', 'html_type' => 'CheckBox', - 'custom_group_id' => $ids['custom_group_id'], + 'custom_group_id' => $cg['id'], 'option_values' => [ ['label' => 'my valley', 'value' => 'valley', 'is_active' => TRUE, 'weight' => 1], ['label' => 'my goat', 'value' => 'goat', 'is_active' => TRUE, 'weight' => 2], @@ -81,9 +93,10 @@ class api_v3_GrantTest extends CiviUnitTestCase { ['label' => 'hungry', 'value' => '', 'is_active' => TRUE, 'weight' => 3], ], ]); - $columnName = $result['values'][$result['id']]['column_name']; - $ids['custom_field_id'] = $result['id']; - $customFieldLabel = 'custom_' . $ids['custom_field_id']; + $columnName = $cf['values'][$cf['id']]['column_name']; + $this->ids['custom_field'][] = $cf['id']; + $this->ids['custom_group'][] = $cg['id']; + $customFieldLabel = 'custom_' . $cf['id']; $expectedValue = CRM_Core_DAO::VALUE_SEPARATOR . 'valley' . CRM_Core_DAO::VALUE_SEPARATOR; //first we pass in the core separators ourselves $this->params[$customFieldLabel] = $expectedValue; @@ -112,24 +125,19 @@ class api_v3_GrantTest extends CiviUnitTestCase { $result = $this->callAPISuccess($this->_entity, 'create', $this->params); $savedValue = CRM_Core_DAO::singleValueQuery("SELECT {$columnName} FROM $customTable WHERE entity_id = {$result['id']}"); $this->assertEquals($expectedValue, $savedValue); - - $this->customFieldDelete($ids['custom_field_id']); - $this->customGroupDelete($ids['custom_group_id']); } public function testGetGrant() { $result = $this->callAPISuccess($this->_entity, 'create', $this->params); $this->ids['grant'][0] = $result['id']; - $result = $this->callAPIAndDocument($this->_entity, 'get', ['rationale' => 'Just Because'], __FUNCTION__, __FILE__); - $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); - $this->assertEquals(1, $result['count']); + $result = $this->callAPISuccess($this->_entity, 'get', ['rationale' => 'Just Because']); + $this->assertGreaterThanOrEqual(1, $result['count']); } public function testDeleteGrant() { $result = $this->callAPISuccess($this->_entity, 'create', $this->params); - $result = $this->callAPIAndDocument($this->_entity, 'delete', ['id' => $result['id']], __FUNCTION__, __FILE__); - $this->assertAPISuccess($result); - $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []); + $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]); + $checkDeleted = $this->callAPISuccess($this->_entity, 'get', ['id' => $result['id']]); $this->assertEquals(0, $checkDeleted['count']); } @@ -156,4 +164,22 @@ class api_v3_GrantTest extends CiviUnitTestCase { $this->assertEquals($validation['values'][0], $expectedOut); } + /** + * Test contact subtype filter on grant report. + * + * @throws \CRM_Core_Exception + */ + public function testGrantReportSeparatedFilter() { + $this->ids['contact'][] = $contactID = $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]); + $this->ids['contact'][] = $contactID2 = $this->individualCreate(); + $this->ids['grant'][] = $this->callAPISuccess('Grant', 'create', ['contact_id' => $contactID, 'status_id' => 1, 'grant_type_id' => 1, 'amount_total' => 1])['id']; + $this->ids['grant'][] = $this->callAPISuccess('Grant', 'create', ['contact_id' => $contactID2, 'status_id' => 1, 'grant_type_id' => 1, 'amount_total' => 1])['id']; + $rows = $this->callAPISuccess('report_template', 'getrows', [ + 'report_id' => 'grant/detail', + 'contact_sub_type_op' => 'in', + 'contact_sub_type_value' => ['Student'], + ]); + $this->assertEquals(1, $rows['count']); + } + } diff --git a/ext/civigrant/tests/phpunit/bootstrap.php b/ext/civigrant/tests/phpunit/bootstrap.php new file mode 100644 index 0000000000..4da10a185a --- /dev/null +++ b/ext/civigrant/tests/phpunit/bootstrap.php @@ -0,0 +1,64 @@ +add('CRM_', __DIR__); +$loader->add('Civi\\', __DIR__); +$loader->add('api_', __DIR__); +$loader->add('api\\', __DIR__); +$loader->register(); + +/** + * Call the "cv" command. + * + * @param string $cmd + * The rest of the command to send. + * @param string $decode + * Ex: 'json' or 'phpcode'. + * @return mixed + * Response output (if the command executed normally). + * For 'raw' or 'phpcode', this will be a string. For 'json', it could be any JSON value. + * @throws \RuntimeException + * If the command terminates abnormally. + */ +function cv(string $cmd, string $decode = 'json') { + $cmd = 'cv ' . $cmd; + $descriptorSpec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => STDERR]; + $oldOutput = getenv('CV_OUTPUT'); + putenv('CV_OUTPUT=json'); + + // Execute `cv` in the original folder. This is a work-around for + // phpunit/codeception, which seem to manipulate PWD. + $cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd); + + $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); + putenv("CV_OUTPUT=$oldOutput"); + fclose($pipes[0]); + $result = stream_get_contents($pipes[1]); + fclose($pipes[1]); + if (proc_close($process) !== 0) { + throw new RuntimeException("Command failed ($cmd):\n$result"); + } + switch ($decode) { + case 'raw': + return $result; + + case 'phpcode': + // If the last output is /*PHPCODE*/, then we managed to complete execution. + if (substr(trim($result), 0, 12) !== '/*BEGINPHP*/' || substr(trim($result), -10) !== '/*ENDPHP*/') { + throw new \RuntimeException("Command failed ($cmd):\n$result"); + } + return $result; + + case 'json': + return json_decode($result, 1); + + default: + throw new RuntimeException("Bad decoder format ($decode)"); + } +} diff --git a/tests/phpunit/CRM/Core/BAO/UFFieldTest.php b/tests/phpunit/CRM/Core/BAO/UFFieldTest.php index 61814f004e..1b6fd1d122 100644 --- a/tests/phpunit/CRM/Core/BAO/UFFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/UFFieldTest.php @@ -226,12 +226,19 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { $this->hookClass->setHook('civicrm_alterUFFields', [$this, 'modifyUFFields']); $fields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); - $this->assertEquals('Grant', $fields['grant_id']['field_type']); - $this->assertEquals('contact_id', $fields['grant_contact_id']['name']); + $this->assertEquals('Foo', $fields['foo_id']['field_type']); + $this->assertEquals('contact_id', $fields['foo_contact_id']['name']); } public function modifyUFFields(&$fields) { - $fields['Grant'] = CRM_Grant_DAO_Grant::export(); + $fields['Foo'] = [ + 'foo_id' => [ + 'name' => 'id', + ], + 'foo_contact_id' => [ + 'name' => 'contact_id', + ], + ]; } } diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index edc5f66f50..29f75600b1 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -881,25 +881,6 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { 'sample' => $group_name, ], ], - 'CRM_Grant_DAO_Grant' => [ - [ - 'fieldName' => 'status_id', - 'sample' => 'Approved for Payment', - ], - [ - 'fieldName' => 'grant_type_id', - 'sample' => 'Emergency', - ], - [ - 'fieldName' => 'currency', - 'sample' => ['USD' => 'US Dollar'], - 'max' => 200, - ], - [ - 'fieldName' => 'financial_type_id', - 'sample' => 'Donation', - ], - ], 'CRM_Contribute_DAO_Contribution' => [ [ 'fieldName' => 'payment_instrument_id', diff --git a/tests/phpunit/CRM/Dedupe/MergerTest.php b/tests/phpunit/CRM/Dedupe/MergerTest.php index e511befeb1..e4865b29b3 100644 --- a/tests/phpunit/CRM/Dedupe/MergerTest.php +++ b/tests/phpunit/CRM/Dedupe/MergerTest.php @@ -1302,9 +1302,6 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase { 'civicrm_financial_item' => [ 0 => 'contact_id', ], - 'civicrm_grant' => [ - 0 => 'contact_id', - ], 'civicrm_group' => [ 0 => 'created_id', 1 => 'modified_id', diff --git a/tests/phpunit/CRM/UF/Page/ProfileEditorTest.php b/tests/phpunit/CRM/UF/Page/ProfileEditorTest.php index a4442a239b..95814bce19 100644 --- a/tests/phpunit/CRM/UF/Page/ProfileEditorTest.php +++ b/tests/phpunit/CRM/UF/Page/ProfileEditorTest.php @@ -48,7 +48,7 @@ class CRM_UF_Page_ProfileEditorTest extends CiviUnitTestCase { public function testGetSchemaWithHooks() { CRM_Utils_Hook::singleton()->setHook('civicrm_alterUFFields', [$this, 'hook_civicrm_alterUFFIelds']); $schema = CRM_UF_Page_ProfileEditor::getSchema(['IndividualModel', 'GrantModel']); - $this->assertEquals('Grant', $schema['GrantModel']['schema']['grant_decision_date']['civiFieldType']); + $this->assertEquals('Grant', $schema['GrantModel']['schema']['grant_id']['civiFieldType']); } /** @@ -61,7 +61,12 @@ class CRM_UF_Page_ProfileEditorTest extends CiviUnitTestCase { } public function hook_civicrm_alterUFFIelds(&$fields) { - $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); + $fields['Grant'] = [ + 'grant_id' => [ + 'name' => 'id', + 'title' => 'Grant ID', + ], + ]; } } diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index 6722019768..09a92e80fd 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -54,7 +54,8 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { $this->assertEquals(1, $result['count']); $entityId = $result['id']; $this->assertIsNumeric($entityId); - $this->assertEquals(7, $result['values'][$entityId]['component_id']); + $caseComponentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', 'CiviCase', 'id', 'name'); + $this->assertEquals($caseComponentId, $result['values'][$entityId]['component_id']); $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Report_Form_Examplez" AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") '); @@ -1390,24 +1391,6 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { ]); } - /** - * Test contact subtype filter on grant report. - * - * @throws \CRM_Core_Exception - */ - public function testGrantReportSeparatedFilter() { - $contactID = $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]); - $contactID2 = $this->individualCreate(); - $this->callAPISuccess('Grant', 'create', ['contact_id' => $contactID, 'status_id' => 1, 'grant_type_id' => 1, 'amount_total' => 1]); - $this->callAPISuccess('Grant', 'create', ['contact_id' => $contactID2, 'status_id' => 1, 'grant_type_id' => 1, 'amount_total' => 1]); - $rows = $this->callAPISuccess('report_template', 'getrows', [ - 'report_id' => 'grant/detail', - 'contact_sub_type_op' => 'in', - 'contact_sub_type_value' => ['Student'], - ]); - $this->assertEquals(1, $rows['count']); - } - /** * Test contact subtype filter on summary report. * diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index 9acb59823c..1940039d40 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -69,7 +69,6 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { 'Event' => 'CiviEvent', 'Case' => 'CiviCase', 'Pledge' => 'CiviPledge', - 'Grant' => 'CiviGrant', 'Campaign' => 'CiviCampaign', 'Survey' => 'CiviCampaign', ]; @@ -1506,10 +1505,6 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { 'id' => $entity['id'], $field => $entity[$field] ?? NULL, ]; - if (isset($updateParams['financial_type_id']) && in_array($entityName, ['Grant'])) { - //api has special handling on these 2 fields for backward compatibility reasons - $entity['contribution_type_id'] = $updateParams['financial_type_id']; - } if (isset($updateParams['next_sched_contribution_date']) && in_array($entityName, ['ContributionRecur'])) { //api has special handling on these 2 fields for backward compatibility reasons $entity['next_sched_contribution'] = $updateParams['next_sched_contribution_date']; @@ -1558,10 +1553,6 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { $entity = array_merge($entity, $resetFKTo); $updateParams = array_merge($updateParams, $resetFKTo); $this->callAPISuccess($entityName, 'create', $updateParams); - if (isset($updateParams['financial_type_id']) && in_array($entityName, ['Grant'])) { - //api has special handling on these 2 fields for backward compatibility reasons - $entity['contribution_type_id'] = $updateParams['financial_type_id']; - } if (isset($updateParams['next_sched_contribution_date']) && in_array($entityName, ['ContributionRecur'])) { //api has special handling on these 2 fields for backward compatibility reasons $entity['next_sched_contribution'] = $updateParams['next_sched_contribution_date']; -- 2.25.1