diff --git a/CRM/Dataprocessor/DAO/DataProcessor.php b/CRM/Dataprocessor/DAO/DataProcessor.php index 0a2da39ab03f5014244a85f4e7776679aa025b21..e7cf965703e9775da714366bec40e6c7818e7d83 100644 --- a/CRM/Dataprocessor/DAO/DataProcessor.php +++ b/CRM/Dataprocessor/DAO/DataProcessor.php @@ -4,9 +4,9 @@ * @package CRM * @copyright CiviCRM LLC (c) 2004-2019 * - * Generated from /buildkit/build/search/web/sites/all/modules/civicrm/tools/extensions/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessor.xml + * Generated from /buildkit/build/dmaster/sites/default/files/civicrm/ext/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessor.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:d6907bd0c85e77ce033cc7aef3d0aa3a) + * (GenCodeChecksum:0963e22b20916c48e9e2f9ab6a2b3845) */ /** diff --git a/CRM/Dataprocessor/DAO/DataProcessorField.php b/CRM/Dataprocessor/DAO/DataProcessorField.php index d4c0393992c351249eff6b964f88892b8c5813de..0bf75bcfcff13f87ec8e88580590ccf35a90ecae 100644 --- a/CRM/Dataprocessor/DAO/DataProcessorField.php +++ b/CRM/Dataprocessor/DAO/DataProcessorField.php @@ -4,9 +4,9 @@ * @package CRM * @copyright CiviCRM LLC (c) 2004-2019 * - * Generated from /buildkit/build/search/web/sites/all/modules/civicrm/tools/extensions/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorField.xml + * Generated from /buildkit/build/dmaster/sites/default/files/civicrm/ext/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorField.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:d274dcffe6113de94c614b35f32dc06b) + * (GenCodeChecksum:d5389a48546e169033f2adf9c1997062) */ /** @@ -120,6 +120,7 @@ class CRM_Dataprocessor_DAO_DataProcessorField extends CRM_Core_DAO { 'entity' => 'DataProcessorField', 'bao' => 'CRM_Dataprocessor_DAO_DataProcessorField', 'localizable' => 0, + 'FKClassName' => 'CRM_Dataprocessor_DAO_DataProcessor', ], 'weight' => [ 'name' => 'weight', diff --git a/CRM/Dataprocessor/DAO/DataProcessorFilter.php b/CRM/Dataprocessor/DAO/DataProcessorFilter.php index f8db5256df224d14b5a7cb9837efc7a6227b836c..60fdb1dd6d18d3e83b411c8c2e7ca2807e97eef9 100644 --- a/CRM/Dataprocessor/DAO/DataProcessorFilter.php +++ b/CRM/Dataprocessor/DAO/DataProcessorFilter.php @@ -4,9 +4,9 @@ * @package CRM * @copyright CiviCRM LLC (c) 2004-2019 * - * Generated from /buildkit/build/search/web/sites/all/modules/civicrm/tools/extensions/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorFilter.xml + * Generated from /buildkit/build/dmaster/sites/default/files/civicrm/ext/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorFilter.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:24f21f0d15aa13b3b28792ad454e68e8) + * (GenCodeChecksum:5811dcf05d6329b04f426920538c3661) */ /** @@ -135,6 +135,7 @@ class CRM_Dataprocessor_DAO_DataProcessorFilter extends CRM_Core_DAO { 'entity' => 'DataProcessorFilter', 'bao' => 'CRM_Dataprocessor_DAO_DataProcessorFilter', 'localizable' => 0, + 'FKClassName' => 'CRM_Dataprocessor_DAO_DataProcessor', ], 'weight' => [ 'name' => 'weight', diff --git a/CRM/Dataprocessor/DAO/DataProcessorOutput.php b/CRM/Dataprocessor/DAO/DataProcessorOutput.php index 9651e89afd387e505ebb3c3968d7424291821226..aceb15057310b026a6a2dcb1e61d7d25b6f7b262 100644 --- a/CRM/Dataprocessor/DAO/DataProcessorOutput.php +++ b/CRM/Dataprocessor/DAO/DataProcessorOutput.php @@ -4,9 +4,9 @@ * @package CRM * @copyright CiviCRM LLC (c) 2004-2019 * - * Generated from /buildkit/build/search/web/sites/all/modules/civicrm/tools/extensions/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorOutput.xml + * Generated from /buildkit/build/dmaster/sites/default/files/civicrm/ext/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorOutput.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:b084899f5823b297a409b16cc41bdf7a) + * (GenCodeChecksum:811c729e9dd8df469a391ef9392ee624) */ /** @@ -125,6 +125,7 @@ class CRM_Dataprocessor_DAO_DataProcessorOutput extends CRM_Core_DAO { 'entity' => 'DataProcessorOutput', 'bao' => 'CRM_Dataprocessor_DAO_DataProcessorOutput', 'localizable' => 0, + 'FKClassName' => 'CRM_Dataprocessor_DAO_DataProcessor', ], 'type' => [ 'name' => 'type', diff --git a/CRM/Dataprocessor/DAO/DataProcessorSource.php b/CRM/Dataprocessor/DAO/DataProcessorSource.php index 5aab7945248b68b6b99b85867a8bc8e2bf7d0039..c9aa0ccfa171ec9b46b7ac2988404e3dae84ea5a 100644 --- a/CRM/Dataprocessor/DAO/DataProcessorSource.php +++ b/CRM/Dataprocessor/DAO/DataProcessorSource.php @@ -4,9 +4,9 @@ * @package CRM * @copyright CiviCRM LLC (c) 2004-2019 * - * Generated from /buildkit/build/search/web/sites/all/modules/civicrm/tools/extensions/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorSource.xml + * Generated from /buildkit/build/dmaster/sites/default/files/civicrm/ext/dataprocessor/xml/schema/CRM/Dataprocessor/DataProcessorSource.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:8b610b28b2023badbccde15e8fa1010d) + * (GenCodeChecksum:c3767f626a9a361ecebe5385f6653d6f) */ /** @@ -130,6 +130,7 @@ class CRM_Dataprocessor_DAO_DataProcessorSource extends CRM_Core_DAO { 'entity' => 'DataProcessorSource', 'bao' => 'CRM_Dataprocessor_DAO_DataProcessorSource', 'localizable' => 0, + 'FKClassName' => 'CRM_Dataprocessor_DAO_DataProcessor', ], 'weight' => [ 'name' => 'weight', diff --git a/CRM/Dataprocessor/Upgrader/Base.php b/CRM/Dataprocessor/Upgrader/Base.php index 63c0118c705d9c286b953c15b5b0cbf4adc99a02..6200bf838007fe4cab136452ab90c2d2ba56e56b 100644 --- a/CRM/Dataprocessor/Upgrader/Base.php +++ b/CRM/Dataprocessor/Upgrader/Base.php @@ -281,7 +281,7 @@ class CRM_Dataprocessor_Upgrader_Base { // ******** Hook delegates ******** /** - * @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install */ public function onInstall() { $files = glob($this->extensionDir . '/sql/*_install.sql'); @@ -308,7 +308,7 @@ class CRM_Dataprocessor_Upgrader_Base { } /** - * @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall */ public function onPostInstall() { $revisions = $this->getRevisions(); @@ -321,7 +321,7 @@ class CRM_Dataprocessor_Upgrader_Base { } /** - * @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall */ public function onUninstall() { $files = glob($this->extensionDir . '/sql/*_uninstall.mysql.tpl'); @@ -342,7 +342,7 @@ class CRM_Dataprocessor_Upgrader_Base { } /** - * @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable */ public function onEnable() { // stub for possible future use @@ -352,7 +352,7 @@ class CRM_Dataprocessor_Upgrader_Base { } /** - * @see https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable */ public function onDisable() { // stub for possible future use diff --git a/Civi/DataProcessor/Output/UIOutputHelper.php b/Civi/DataProcessor/Output/UIOutputHelper.php index f42d17a2b5acd5aa4c9ebdd57bd5a9803b328829..9b98dcd62f2abfb8f2ac7c293f1965baf913a81c 100644 --- a/Civi/DataProcessor/Output/UIOutputHelper.php +++ b/Civi/DataProcessor/Output/UIOutputHelper.php @@ -66,6 +66,10 @@ class UIOutputHelper { */ public static function preHook($op, $objectName, $id, &$params) { if ($objectName == 'DataProcessorOutput') { + // Disable this hook in unit tests because the menu rebuild it causes breaks transactions. + if (CIVICRM_UF === 'UnitTests') { + return; + } if ($op == 'delete') { $output = civicrm_api3('DataProcessorOutput', 'getsingle', ['id' => $id]); self::removeOutputFromNavigation($output['configuration']); @@ -141,6 +145,10 @@ class UIOutputHelper { if ($objectName != 'DataProcessorOutput') { return; } + // Disable this hook in unit tests because the menu rebuild it causes breaks transactions. + if (CIVICRM_UF === 'UnitTests') { + return; + } if (self::$rebuildMenu) { // Rebuild the CiviCRM Menu (which holds all the pages) diff --git a/dataprocessor.civix.php b/dataprocessor.civix.php index ca12f343bffe55b61eb011f906d002b0700e6282..950e7f5415c80ef17e43947ab242d6692791dccc 100644 --- a/dataprocessor.civix.php +++ b/dataprocessor.civix.php @@ -24,9 +24,9 @@ class CRM_Dataprocessor_ExtensionUtil { * Translated text. * @see ts */ - public static function ts($text, $params = array()) { + public static function ts($text, $params = []) { if (!array_key_exists('domain', $params)) { - $params['domain'] = array(self::LONG_NAME, NULL); + $params['domain'] = [self::LONG_NAME, NULL]; } return ts($text, $params); } @@ -100,7 +100,7 @@ function _dataprocessor_civix_civicrm_config(&$config = NULL) { array_unshift($template->template_dir, $extDir); } else { - $template->template_dir = array($extDir, $template->template_dir); + $template->template_dir = [$extDir, $template->template_dir]; } $include_path = $extRoot . PATH_SEPARATOR . get_include_path(); @@ -140,7 +140,7 @@ function _dataprocessor_civix_civicrm_install() { function _dataprocessor_civix_civicrm_postInstall() { _dataprocessor_civix_civicrm_config(); if ($upgrader = _dataprocessor_civix_upgrader()) { - if (is_callable(array($upgrader, 'onPostInstall'))) { + if (is_callable([$upgrader, 'onPostInstall'])) { $upgrader->onPostInstall(); } } @@ -166,7 +166,7 @@ function _dataprocessor_civix_civicrm_uninstall() { function _dataprocessor_civix_civicrm_enable() { _dataprocessor_civix_civicrm_config(); if ($upgrader = _dataprocessor_civix_upgrader()) { - if (is_callable(array($upgrader, 'onEnable'))) { + if (is_callable([$upgrader, 'onEnable'])) { $upgrader->onEnable(); } } @@ -181,7 +181,7 @@ function _dataprocessor_civix_civicrm_enable() { function _dataprocessor_civix_civicrm_disable() { _dataprocessor_civix_civicrm_config(); if ($upgrader = _dataprocessor_civix_upgrader()) { - if (is_callable(array($upgrader, 'onDisable'))) { + if (is_callable([$upgrader, 'onDisable'])) { $upgrader->onDisable(); } } @@ -217,22 +217,23 @@ function _dataprocessor_civix_upgrader() { } /** - * Search directory tree for files which match a glob pattern + * Search directory tree for files which match a glob pattern. * * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored. * Note: In Civi 4.3+, delegate to CRM_Utils_File::findFiles() * - * @param $dir string, base dir - * @param $pattern string, glob pattern, eg "*.txt" + * @param string $dir base dir + * @param string $pattern , glob pattern, eg "*.txt" + * * @return array(string) */ function _dataprocessor_civix_find_files($dir, $pattern) { - if (is_callable(array('CRM_Utils_File', 'findFiles'))) { + if (is_callable(['CRM_Utils_File', 'findFiles'])) { return CRM_Utils_File::findFiles($dir, $pattern); } - $todos = array($dir); - $result = array(); + $todos = [$dir]; + $result = []; while (!empty($todos)) { $subdir = array_shift($todos); foreach (_dataprocessor_civix_glob("$subdir/$pattern") as $match) { @@ -296,14 +297,13 @@ function _dataprocessor_civix_civicrm_caseTypes(&$caseTypes) { $name = preg_replace('/\.xml$/', '', basename($file)); if ($name != CRM_Case_XMLProcessor::mungeCaseType($name)) { $errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor::mungeCaseType($name)); - CRM_Core_Error::fatal($errorMessage); - // throw new CRM_Core_Exception($errorMessage); + throw new CRM_Core_Exception($errorMessage); } - $caseTypes[$name] = array( + $caseTypes[$name] = [ 'module' => E::LONG_NAME, 'name' => $name, 'file' => $file, - ); + ]; } } @@ -361,11 +361,12 @@ function _dataprocessor_civix_civicrm_themes(&$themes) { * * @link http://php.net/glob * @param string $pattern + * * @return array, possibly empty */ function _dataprocessor_civix_glob($pattern) { $result = glob($pattern); - return is_array($result) ? $result : array(); + return is_array($result) ? $result : []; } /** @@ -376,16 +377,18 @@ function _dataprocessor_civix_glob($pattern) { * 'Mailing', or 'Administer/System Settings' * @param array $item - the item to insert (parent/child attributes will be * filled for you) + * + * @return bool */ function _dataprocessor_civix_insert_navigation_menu(&$menu, $path, $item) { // If we are done going down the path, insert menu if (empty($path)) { - $menu[] = array( - 'attributes' => array_merge(array( + $menu[] = [ + 'attributes' => array_merge([ 'label' => CRM_Utils_Array::value('name', $item), 'active' => 1, - ), $item), - ); + ], $item), + ]; return TRUE; } else { @@ -396,9 +399,9 @@ function _dataprocessor_civix_insert_navigation_menu(&$menu, $path, $item) { foreach ($menu as $key => &$entry) { if ($entry['attributes']['name'] == $first) { if (!isset($entry['child'])) { - $entry['child'] = array(); + $entry['child'] = []; } - $found = _dataprocessor_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key); + $found = _dataprocessor_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item); } } return $found; @@ -409,7 +412,7 @@ function _dataprocessor_civix_insert_navigation_menu(&$menu, $path, $item) { * (Delegated) Implements hook_civicrm_navigationMenu(). */ function _dataprocessor_civix_navigationMenu(&$nodes) { - if (!is_callable(array('CRM_Core_BAO_Navigation', 'fixNavigationMenu'))) { + if (!is_callable(['CRM_Core_BAO_Navigation', 'fixNavigationMenu'])) { _dataprocessor_civix_fixNavigationMenu($nodes); } } diff --git a/info.xml b/info.xml index 42c04fccf04ebda21512ad622631be241aa320bd..6366d4077aa5752b005bd6860a90e299479acf4a 100644 --- a/info.xml +++ b/info.xml @@ -25,7 +25,7 @@ <ver>5.15</ver> </compatibility> <classloader> - <psr4 prefix="Civi\" path="Civi" /> + <psr4 prefix="Civi\" path="Civi"/> </classloader> <civix> <namespace>CRM/Dataprocessor</namespace> diff --git a/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorDataSourceTest.php b/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorDataSourceTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c28b2f43308053e7723335df1afeddebbc3698f2 --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorDataSourceTest.php @@ -0,0 +1,52 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * 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 CRM_Dataprocessor_CreateDataProcessorDataSourceTest extends CRM_Dataprocessor_TestBase { + public function testCreateDataProcessorDataSource() { + + $data_processor_id = $this->createTestDataProcessorFixture(); + + $factory = dataprocessor_get_factory(); + if ($factory === NULL) { + $this->fail("Test cannot complete, unable to obtain data processor factory."); + } + $data_sources = $factory->getDataSources(); + + foreach ($data_sources as $key => $value) { + $params = []; + + // Params for setting data source parameters + $params['data_processor_id'] = $data_processor_id; + $params['title'] = 'testDataSource'; + $params['type'] = $key; + + $source_id = civicrm_api3('DataProcessorSource', 'create', $params)['id']; + + // Retrieving the data processor source + $result_datasource = civicrm_api3('DataProcessorSource', 'get', ['id' => $source_id]); + $this->assertEquals(1, $result_datasource['count']); + $this->assertArrayHasKey($source_id, $result_datasource['values'], "Failed to add DataProcessorSource"); + $this->assertEquals('testDataSource', $result_datasource['values'][$source_id]['title']); + $this->assertEquals($key, $result_datasource['values'][$source_id]['type']); + } + } + +} diff --git a/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorFieldTest.php b/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorFieldTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c07040c8fd70d349eefa35837ec00627f70b7d9c --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorFieldTest.php @@ -0,0 +1,31 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * Tests we can create a 'field' on a data processor. + * + * @group headless + */ +class CRM_Dataprocessor_CreateDataProcessorFieldTest extends CRM_Dataprocessor_TestBase { + + public function testCreateDataProcessorField() { + + $data_processor_id = $this->createTestDataProcessorFixture(); + $id_datafield = $this->createTestDataProcessorField('id'); + $result = $this->dataprocessor_fields['id']; + + $expectations = [ + 'title' => 'id test field', + 'type' => 'raw', + 'data_processor_id' => $data_processor_id, + 'configuration.field' => 'id', + 'configuration.datasource' => 'testdatasource', + ]; + $this->checkNestedArray($expectations, $result, __FUNCTION__); + } +} + diff --git a/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorTest.php b/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..7e1989c94e08c02b3e49fdfa23711fd798b7c3f9 --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/CreateDataProcessorTest.php @@ -0,0 +1,33 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * Simple test that we can create a data processor. + * + * 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 CRM_Dataprocessor_CreateDataProcessorTest extends CRM_Dataprocessor_TestBase { + + public function testCreateDataProcessor() { + $id_dataprocessor = $this->createTestDataProcessorFixture(); + $processor = $this->data_processors[$id_dataprocessor]; + $this->assertEquals('title', $processor['title']); + $this->assertEquals('Creating a Test Description', $processor['description']); + $this->assertEquals(1, $processor['is_active']); + $this->assertEquals('test', $processor['name']); + } + +} diff --git a/tests/phpunit/CRM/Dataprocessor/DummyTest.php b/tests/phpunit/CRM/Dataprocessor/DummyTest.php new file mode 100644 index 0000000000000000000000000000000000000000..cc6f4a3d46cda618294740830b589c6d3f538065 --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/DummyTest.php @@ -0,0 +1,26 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; + +/** + * Dummy TEMPORARY test which requires the reinstallation of the test db + * for debugging problems with installation. + * + * @group headless + */ +class CRM_Dataprocessor_Dummy extends CRM_Dataprocessor_TestBase { + + public function setUpHeadless() { + // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). + // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest + return \Civi\Test::headless() + ->installMe(__DIR__) + ->apply(TRUE); + } + + public function testDummy() { + $this->assertTrue(TRUE); + } + +} + diff --git a/tests/phpunit/CRM/Dataprocessor/ExportDataProcessorTest.php b/tests/phpunit/CRM/Dataprocessor/ExportDataProcessorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..bf17ea30f4830e7b184af3bb391517243e74c223 --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/ExportDataProcessorTest.php @@ -0,0 +1,103 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * Test exporting a data processor to json. + * + * @group headless + */ +class CRM_Dataprocessor_ExportDataProcessorTest extends CRM_Dataprocessor_TestBase { + + public function checkContent($json){ + + $returnvalue = True; + + if($json->name != 'test') + $returnvalue = False; + + if($json->title != 'title') + $returnvalue = False; + + if($json->description != 'Creating a Test Description') + $returnvalue = False; + + if(sizeof($json->data_sources)==1){ + $datasource = reset($json->data_sources); + if($datasource->name != 'testdatasource') + $returnvalue = False; + if($datasource->title != 'testDataSource') + $returnvalue = False; + if($datasource->type != 'contact') + $returnvalue = False; + } + else{ + $returnvalue = False; + } + + // print_r($json->fields); + + if(sizeof($json->fields)==2){ + $id_field = $json->fields[0]; + if($id_field->name != 'id_test') + $returnvalue = False; + if($id_field->configuration->field != 'id') + $returnvalue = False; + if($id_field->configuration->datasource != 'testdatasource') + $returnvalue = False; + + $name_field = $json->fields[1]; + if($name_field->name != 'first_name_test') + $returnvalue = False; + if($name_field->configuration->field != 'display_name') + $returnvalue = False; + if($name_field->configuration->datasource != 'testdatasource') + $returnvalue = False; + } + else{ + $returnvalue = False; + } + + if(sizeof($json->outputs)==1){ + $output = reset($json->outputs); + if($output->type != 'contact_search') + $returnvalue = False; + + $output_configuration = $output->configuration; + + if($output_configuration->title != 'contact') + $returnvalue = False; + if($output_configuration->contact_id_field != 'id_test') + $returnvalue = False; + } + else{ + $returnvalue = False; + } + + return $returnvalue; + + } + + public function testExportDataProcessor() { + + $id_dataprocessor = $this->createTestDataProcessorFixture(); + $this->createTestDataProcessorSourceFixture(); + $this->createTestDataProcessorField('id'); + $this->createTestDataProcessorField('first_name'); + + $params = []; // Params for setting data source parameters + $params['data_processor_id'] = $id_dataprocessor; + $params['type'] = 'contact_search'; + $params['configuration'] = array("title" => "contact", "contact_id_field" => "id_test"); + $id_dataprocessor_output = civicrm_api3('DataProcessorOutput', 'create', $params)['id'] ?? 0; + $this->assertGreaterThan(0, $id_dataprocessor_output, "Failed calling DataProcessorOutput.create With " . json_encode($params)); + + $buffer = json_encode(CRM_Dataprocessor_Utils_Importer::export($id_dataprocessor), JSON_PRETTY_PRINT); + $this->checkContent(json_decode($buffer)); + } + + +} diff --git a/tests/phpunit/CRM/Dataprocessor/ImportDataProcessorTest.php b/tests/phpunit/CRM/Dataprocessor/ImportDataProcessorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0e511fb509d6cad95455420078d128e182245f85 --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/ImportDataProcessorTest.php @@ -0,0 +1,103 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * Test that we can import a data processor. + * + * @group headless + */ +class CRM_Dataprocessor_ImportDataProcessorTest extends CRM_Dataprocessor_TestBase { + + public function checkDataProcessorFields($fields){ + + $keys = array_keys($fields['values']); + + $field = $fields['values'][$keys[0]]; + $expectations = [ + 'name' => 'contact_id', + 'configuration.field' => 'id', + 'configuration.datasource' => 'contact', + ]; + $this->checkNestedArray($expectations, $field, "While checking the id field."); + + $field = $fields['values'][$keys[1]]; + $expectations = [ + 'name' => 'first_name', + 'configuration.field' => 'first_name', + 'configuration.datasource' => 'contact', + ]; + $this->checkNestedArray($expectations, $field, "While checking the first name field."); + + $field = $fields['values'][$keys[2]]; + $expectations = [ + 'name' => 'gender', + 'configuration.field' => 'gender_id', + 'configuration.datasource' => 'contact', + ]; + $this->checkNestedArray($expectations, $field, "While checking the first name field."); + + } + + public function checkDataProcessorSource($datasource){ + $keys = array_keys($datasource['values']); + $datasource_check = $datasource['values'][$keys[0]]; + + $expectations = [ + 'name' => 'contact', + 'title' => 'Contact', + 'type' => 'contact', + 'configuration.filter.is_deleted.op' => '=', + 'configuration.filter.is_deleted.value' => 0, + ]; + $this->checkNestedArray($expectations, $datasource_check, "While checking the source."); + } + + public function checkDataProcessorOutput($output){ + $keys = array_keys($output['values']); + $returnvalue = True; + $output_check = $output['values'][$keys[0]]; + if($output_check['type']!='contact_search') + $returnvalue = False; + if($output_check['configuration']['title']!='Test') + $returnvalue = False; + if($output_check['configuration']['contact_id_field']!='contact_id') + $returnvalue = False; + + return $returnvalue; + + } + + public function testImportDataProcessor() { + $file = __DIR__ . '/test-dataprocessor-import-fixture.json'; + $configuration = file_get_contents($file); + + $importCode = json_decode($configuration, TRUE); + $importResult = CRM_Dataprocessor_Utils_Importer::import($importCode, '', true); + + $id = $importResult['new_id']; + $result = civicrm_api3('DataProcessor', 'get', array("id"=> $id)); + + $fields = civicrm_api3('DataProcessorField', 'get', array("data_processor_id"=> $id)); + $datasource = civicrm_api3('DataProcessorSource', 'get', array("data_processor_id"=> $id)); + $outputs = civicrm_api3('DataProcessorOutput', 'get', array("data_processor_id"=> $id)); + + // Check the data processor + $expectations = [ + 'name' => 'test', + 'title' => 'Test', + 'description' => 'This is a test Data Processor', + 'is_active' => 1, + ]; + $this->checkNestedArray($expectations, $result['values'][$id], "While checking the imported data processor:"); + + // Check the fields. + $this->checkDataProcessorFields($fields); + $this->checkDataProcessorSource($datasource); + $this->checkDataProcessorOutput($outputs); + } + +} diff --git a/tests/phpunit/CRM/Dataprocessor/OutputResultTest.php b/tests/phpunit/CRM/Dataprocessor/OutputResultTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3272b88c6ac6bc023062ef3e0e606b336720ce31 --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/OutputResultTest.php @@ -0,0 +1,103 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * Test we can create an output. + * + * 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 CRM_Dataprocessor_OutputResultTest extends CRM_Dataprocessor_TestBase { + + var $check_results = array( + 1 => array( + 'id_test' => 1, + 'first_name_test' => 'Default Organization', + ), + + 2 => array( + 'id_test' => 2, + 'first_name_test' => 'Second Domain', + ), + + ); + + + public function checkRecord($record, $id) { + + $check = array(); + + foreach (array_keys($record) as $key){ + $check[$key] = $record[$key]->formattedValue; + } + + $this->assertEquals($check, $this->check_results[$id]); + } + + + public function testCreateDataProcessorField() { + + $id_dataprocessor = $this->createTestDataProcessorFixture(); + $id_datasource = $this->createTestDataProcessorSourceFixture(); + + // Create the ID field + $params = []; + $params['data_processor_id'] = $id_dataprocessor; + $params['title'] = 'id_test'; + $params['type'] = 'raw'; + $params['configuration'] = array("field" => "id", "datasource" => "testdatasource"); + $id_datafield_id = civicrm_api3('DataProcessorField', 'create', $params)['id'] ?? 0; + $this->assertGreaterThan(0, $id_datafield_id, "Failed to call DataProcessorField.create with " . json_encode($params)); + + + // Create the first name field. + $params['title'] = 'first_name_test'; + $params['configuration'] = array("field" => "display_name", "datasource" => "testdatasource"); + $id_datafield_first_name = civicrm_api3('DataProcessorField', 'create', $params)['id'] ?? 0; + $this->assertGreaterThan(0, $id_datafield_first_name, "Failed to call DataProcessorField.create with " . json_encode($params)); + + $result_field = civicrm_api3('DataProcessorField', 'get'); + + $field_check = TRUE; //True if no error in adding Data Processor Field + foreach ($result_field['values'] as $value) { + $this->assertArrayHasKey('id', $value, "Failed setting up the DataProcessorField"); + } + + $params = []; // Params for setting data source parameters + $params['data_processor_id'] = $id_dataprocessor; + $params['type'] = 'contact_search'; + $params['configuration'] = array("title" => "contact","contact_id_field" =>"id_test"); + + $id_dataprocessor_output = civicrm_api3('DataProcessorOutput', 'create', $params)['id'] ?? 0; + $this->assertGreaterThan(0, $id_dataprocessor_output, "Failed calling DataProcessorOutput.create with " . json_encode($params)); + + $result_output = civicrm_api3('DataProcessorOutput', 'get', ['id' => $id_dataprocessor_output]); + $this->assertEquals(1, $result_output['count']); + + $dataProcessor = $this->data_processors[$id_dataprocessor]; + $dataProcessorClass = CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);; + + try { + while ($record = $dataProcessorClass->getDataFlow()->nextRecord()) { + $check_results_id = $record['id_test']->formattedValue; + $this->checkRecord($record, $check_results_id, "Failed checking output"); + } + } + catch (\Civi\DataProcessor\DataFlow\EndOfFlowException $e) { + // Do nothing + } + } + +} diff --git a/tests/phpunit/CRM/Dataprocessor/TestBase.php b/tests/phpunit/CRM/Dataprocessor/TestBase.php new file mode 100644 index 0000000000000000000000000000000000000000..85ced743b1597168702ebfaae073b28b2b72f67d --- /dev/null +++ b/tests/phpunit/CRM/Dataprocessor/TestBase.php @@ -0,0 +1,199 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * This is a base class for code shared between the data processor tests. + * + * 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 CRM_Dataprocessor_TestBase extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { + + /** + * @var array keyed by ID + */ + protected $data_processors = []; + /** + * @var array keyed by ID + */ + protected $sources = []; + /** + * @var array + * keyed by field name, not it's ID (for ease of access) + */ + protected $dataprocessor_fields = []; + + /** + * @var array + */ + protected $field_fixtures = [ + 'id' => [ + 'title' => 'id test field', + 'type' => 'raw', + 'configuration' => [ + 'field' => 'id', + 'datasource' => 'testdatasource', + ] + ], + 'first_name' => [ + 'title' => 'first name test', + 'type' => 'raw', + 'configuration' => [ + 'field' => 'first_name', + 'datasource' => 'testdatasource', + ] + ], + ]; + + public function setUpHeadless() { + // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). + // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest + return \Civi\Test::headless() + ->installMe(__DIR__) + ->apply(FALSE); + } + + public function setUp() { + // This assertion is here to catch the issue previously caused in UIOutputHelper::postHook + // whereby running a test would break the transaction of the test and therefore leave + // a processor defined in the test databsae, corrupting the fixture for the next tests. + $c = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_data_processor"); + $this->assertEquals(0, $c, "setup: already $c data processors defined."); + parent::setUp(); + } + + public function tearDown() { + + foreach (array_keys($this->data_processors) as $id) { + civicrm_api3('DataProcessor', 'delete', ['id' => $id]); + } + parent::tearDown(); + } + + /** + * Create a data processor for our tests. + * + * @return int ID of new data processor. + */ + public function createTestDataProcessorFixture() { + + $params['name'] = "test"; + $params['title'] = "title"; + $params['description'] = 'Creating a Test Description'; + $params['is_active'] = 1; + + // Creating a DataProcessor + $data_processor_id = civicrm_api3('DataProcessor', 'create', $params)['id']; + $this->assertGreaterThan(0, $data_processor_id, "Failed calling DataProcessor.create with " . json_encode($params)); + + // Reload the data processor + $this->data_processors[$data_processor_id] = civicrm_api3('DataProcessor', 'getsingle', ['id' => $data_processor_id]); + + return $data_processor_id; + } + /** + * Create a data processor source for our tests. + * + * @return int ID of new data processor source. + */ + public function createTestDataProcessorSourceFixture() { + + $data_processor_id = array_keys($this->data_processors)[0] ?? 0; + $this->assertGreaterThan(0, $data_processor_id, "No data processor; call createTestDataProcessorFixture before " . __FUNCTION__); + $params = [ + 'data_processor_id' => $data_processor_id, + 'title' => 'testDataSource', + 'type' => 'contact', + ]; + + // Creating a DataProcessor Source + $id_datasource = civicrm_api3('DataProcessorSource', 'create', $params)['id'] ?? 0; + $this->assertGreaterThan(0, $id_datasource, "Failed creating DataProcessorSource"); + + // I'm not sure we need this much checking... + $result_datasource = civicrm_api3('DataProcessorSource', 'get', ['id' => $id_datasource]); + $this->assertEquals(1, $result_datasource['count']); + $this->assertArrayHasKey($id_datasource, $result_datasource['values'], "Failed to add DataProcessorSource"); + + $result_datasource_value = $result_datasource['values'][$id_datasource]; + foreach ($params as $k => $v) { + $this->assertArrayHasKey($k, $result_datasource_value); + $this->assertEquals($v, $result_datasource_value[$k]); + } + + // All good, store and return the ID. + $this->sources[$id_datasource] = $result_datasource_value; + return $id_datasource; + } + /** + * + * Create an field from our fixtures. + * + * @param string $name The code name of the fixture - see $this->field_fixtures + * @return int field ID. + */ + public function createTestDataProcessorField($name) { + + $data_processor_id = array_keys($this->data_processors)[0] ?? 0; + $this->assertGreaterThan(0, $data_processor_id, "No data processor; call createTestDataProcessorFixture before " . __FUNCTION__); + + $this->assertArrayHasKey($name, $this->field_fixtures, "Test code error: there is no '$name' field fixture defined"); + + // Params for setting field parameters + $params = $this->field_fixtures[$name]; + // Add in the procesor ID + $params['data_processor_id'] = $data_processor_id; + + // Create + $id_datafield = civicrm_api3('DataProcessorField', 'create', $params)['id'] ?? 0; + $this->assertGreaterThan(0, $id_datafield, "Failed to create DataProcessorField"); + + // Reload + $result_field = civicrm_api3('DataProcessorField', 'get', ['id' => $id_datafield]); + $this->assertEquals(1, $result_field['count'], "Failed to reload DataProcessorField"); + + // Store it for access if needed. + $this->dataprocessor_fields[$name] = $result_field['values'][$id_datafield]; + + return $id_datafield; + } + /** + * Helper function. + * + * @param array $expectations keys are dot-nested keys, values are expected values. + * @param array $source + * @param string $text To give context to any assertion errors. + */ + public function checkNestedArray($expectations, $source, $text) { + foreach ($expectations as $key => $expected) { + + // Dig down keys.like.this + $parts = explode('.', $key); + $_ = $source; + while ($parts) { + $subkey = array_shift($parts); + $this->assertArrayHasKey($subkey, $_, $text . " (key: $key)"); + $_ = $_[$subkey]; + } + + // Finally, check the value + $this->assertEquals($expected, $_, $text . " (key: $key)"); + + } + } + +} + + diff --git a/tests/phpunit/CRM/Myextension/test.json b/tests/phpunit/CRM/Dataprocessor/test-dataprocessor-import-fixture.json similarity index 100% rename from tests/phpunit/CRM/Myextension/test.json rename to tests/phpunit/CRM/Dataprocessor/test-dataprocessor-import-fixture.json diff --git a/tests/phpunit/CRM/Myextension/CreateDataProcessorDataSourceTest.php b/tests/phpunit/CRM/Myextension/CreateDataProcessorDataSourceTest.php deleted file mode 100644 index 709c3d3c1787ff08f4586a42e9ef16f3ceb649d3..0000000000000000000000000000000000000000 --- a/tests/phpunit/CRM/Myextension/CreateDataProcessorDataSourceTest.php +++ /dev/null @@ -1,103 +0,0 @@ -<?php - -use CRM_Dataprocessor_ExtensionUtil as E; -use Civi\Test\HeadlessInterface; -use Civi\Test\HookInterface; -use Civi\Test\TransactionalInterface; - -/** - * 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 CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - - public function setUpHeadless() { - // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). - // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest - return \Civi\Test::headless() - ->installMe(__DIR__) - ->apply(); - } - - public function setUp() { - parent::setUp(); - } - - public function tearDown() { - parent::tearDown(); - } - - public function testCreateDataProcessorDataSource() { - - $params['name'] = "test"; - $params['title'] = "title"; - $params['description'] = 'Creating a Test Description'; - $params['is_active'] = 1; - - // Creating a DataProcessor - civicrm_api3('DataProcessor', 'create', $params); - - // Retrieving the data processor - $result = civicrm_api3('DataProcessor', 'get'); - - if(isset($result['id'])){ - // Retrieving the id of data processor - $id = $result['id']; - $factory = dataprocessor_get_factory(); - $data_sources = $factory->getDataSources(); - - foreach ($data_sources as $key => $value) { - - $params = []; - - - // Params for setting data source parameters - $params['data_processor_id'] = $id; - $params['title'] = 'testDataSource'; - $params['type'] = $key; - - civicrm_api3('DataProcessorSource', 'create', $params); - - // Retrieving the data processor source - $result_datasource = civicrm_api3('DataProcessorSource', 'get'); - - if(isset($result_datasource['id'])){ - - $id_datasource = $result_datasource['id']; - - $this->assertEquals('testDataSource', $result_datasource['values'][$id_datasource]['title']); - $this->assertEquals($key, $result_datasource['values'][$id_datasource]['type']); - - civicrm_api3('DataProcessorSource', 'delete', ['id'=>$id_datasource]); - - } - else{ - - echo "Failed to add DataProcessorSource"; - $this->assertFalse(true); - } - } - } - else{ - echo "DataProcessor Failed to Setup"; - $this->assertFalse(true); - } - - // $factory = dataprocessor_get_factory(); - // $data_sources = $factory->getDataSources(); - - - } - - -} diff --git a/tests/phpunit/CRM/Myextension/CreateDataProcessorFieldTest.php b/tests/phpunit/CRM/Myextension/CreateDataProcessorFieldTest.php deleted file mode 100644 index 0a426a990a8730806d169947a071b0163b81db86..0000000000000000000000000000000000000000 --- a/tests/phpunit/CRM/Myextension/CreateDataProcessorFieldTest.php +++ /dev/null @@ -1,123 +0,0 @@ -<?php - -use CRM_Dataprocessor_ExtensionUtil as E; -use Civi\Test\HeadlessInterface; -use Civi\Test\HookInterface; -use Civi\Test\TransactionalInterface; - -/** - * 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 CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - - public function setUpHeadless() { - // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). - // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest - return \Civi\Test::headless() - ->installMe(__DIR__) - ->apply(); - } - - public function setUp() { - parent::setUp(); - } - - public function tearDown() { - parent::tearDown(); - } - - public function testCreateDataProcessorField() { - - $params['name'] = "test"; - $params['title'] = "title"; - $params['description'] = 'Creating a Test Description'; - $params['is_active'] = 1; - - // Creating a DataProcessor - civicrm_api3('DataProcessor', 'create', $params); - - // Retrieving the data processor - $result = civicrm_api3('DataProcessor', 'get'); - - if(isset($result['id'])){ - // Retrieving the id of data processor - $data_processor_id = $result['id']; - $factory = dataprocessor_get_factory(); - $data_sources = $factory->getDataSources(); - - $params = []; - // Params for setting data source parameters - - $params['data_processor_id'] = $data_processor_id; - $params['title'] = 'testDataSource'; - $params['type'] = 'contact'; - - // Creating a DataProcessor Source - civicrm_api3('DataProcessorSource', 'create', $params); - - $result_datasource = civicrm_api3('DataProcessorSource', 'get'); - - if(isset($result_datasource['id'])){ - - $id_datasource = $result_datasource['id']; - - $params = []; - // Params for setting data source parameters - - $params['data_processor_id'] = $data_processor_id; - $params['title'] = 'testDataField'; - $params['type'] = 'raw'; - $params['configuration'] = array("field" => "id","datasource" =>"contact"); - - civicrm_api3('DataProcessorField', 'create', $params); - - $result_field = civicrm_api3('DataProcessorField', 'get'); - - if(isset($result_field['id'])){ - - $id_datafield = $result_field['id']; - - $this->assertEquals('testDataField', $result_field['values'][$id_datafield]['title']); - $this->assertEquals('raw', $result_field['values'][$id_datafield]['type']); - $this->assertEquals($data_processor_id, $result_field['values'][$id_datafield]['data_processor_id']); - $this->assertEquals('id', $result_field['values'][$id_datafield]['configuration']['field']); - $this->assertEquals('contact', $result_field['values'][$id_datafield]['configuration']['datasource']); - - } - else{ - echo "Failed to add DataProcessor Field"; - $this->assertFalse(true); - } - - - } - else{ - - echo "Failed to add DataProcessorSource"; - $this->assertFalse(true); - } - } - else{ - echo "DataProcessor Failed to Setup"; - $this->assertFalse(true); - } - - // $factory = dataprocessor_get_factory(); - // $data_sources = $factory->getDataSources(); - - - } - - -} diff --git a/tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php b/tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php deleted file mode 100644 index 2648a4ff83da705b34d3051e5f38cbb7f5cdea15..0000000000000000000000000000000000000000 --- a/tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php +++ /dev/null @@ -1,69 +0,0 @@ -<?php - -use CRM_Dataprocessor_ExtensionUtil as E; -use Civi\Test\HeadlessInterface; -use Civi\Test\HookInterface; -use Civi\Test\TransactionalInterface; - -/** - * 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 CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - - public function setUpHeadless() { - // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). - // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest - return \Civi\Test::headless() - ->installMe(__DIR__) - ->apply(); - } - - public function setUp() { - parent::setUp(); - } - - public function tearDown() { - parent::tearDown(); - } - - public function testCreateDataProcessor() { - - $params['name'] = "test"; - $params['title'] = "title"; - $params['description'] = 'Creating a Test Description'; - $params['is_active'] = 1; - - // Creating a DataProcessor - civicrm_api3('DataProcessor', 'create', $params); - - // Retrieving the data processor - $result = civicrm_api3('DataProcessor', 'get'); - if(isset($result['id'])){ - // Retrieving the id of data processor - $id = $result['id']; - - $this->assertEquals('test', $result['values'][$id]['name']); - $this->assertEquals('title', $result['values'][$id]['title']); - $this->assertEquals('Creating a Test Description', $result['values'][$id]['description']); - $this->assertEquals(1, $result['values'][$id]['is_active']); - } - else{ - echo "DataProcessor Failed to setup"; - $this->assertFalse(true); - } - - } - - -} diff --git a/tests/phpunit/CRM/Myextension/ExportDataProcessorTest.php b/tests/phpunit/CRM/Myextension/ExportDataProcessorTest.php deleted file mode 100644 index fe952825e62c63863d9be53c1f8959ae4fd772f7..0000000000000000000000000000000000000000 --- a/tests/phpunit/CRM/Myextension/ExportDataProcessorTest.php +++ /dev/null @@ -1,215 +0,0 @@ -<?php - -use CRM_Dataprocessor_ExtensionUtil as E; -use Civi\Test\HeadlessInterface; -use Civi\Test\HookInterface; -use Civi\Test\TransactionalInterface; - -/** - * 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 CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - - public function setUpHeadless() { - // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). - // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest - return \Civi\Test::headless() - ->installMe(__DIR__) - ->apply(); - } - - public function setUp() { - parent::setUp(); - } - - public function tearDown() { - parent::tearDown(); - } - - public function checkContent($json){ - - $returnvalue = True; - - if($json->name != 'test') - $returnvalue = False; - - if($json->title != 'title') - $returnvalue = False; - - if($json->description != 'Creating a Test Description') - $returnvalue = False; - - if(sizeof($json->data_sources)==1){ - $datasource = reset($json->data_sources); - if($datasource->name != 'testdatasource') - $returnvalue = False; - if($datasource->title != 'testDataSource') - $returnvalue = False; - if($datasource->type != 'contact') - $returnvalue = False; - } - else{ - $returnvalue = False; - } - - // print_r($json->fields); - - if(sizeof($json->fields)==2){ - $id_field = $json->fields[0]; - if($id_field->name != 'id_test') - $returnvalue = False; - if($id_field->configuration->field != 'id') - $returnvalue = False; - if($id_field->configuration->datasource != 'testdatasource') - $returnvalue = False; - - $name_field = $json->fields[1]; - if($name_field->name != 'first_name_test') - $returnvalue = False; - if($name_field->configuration->field != 'display_name') - $returnvalue = False; - if($name_field->configuration->datasource != 'testdatasource') - $returnvalue = False; - } - else{ - $returnvalue = False; - } - - if(sizeof($json->outputs)==1){ - $output = reset($json->outputs); - if($output->type != 'contact_search') - $returnvalue = False; - - $output_configuration = $output->configuration; - - if($output_configuration->title != 'contact') - $returnvalue = False; - if($output_configuration->contact_id_field != 'id_test') - $returnvalue = False; - } - else{ - $returnvalue = False; - } - - return $returnvalue; - - } - - public function testExportDataProcessor() { - - $params['name'] = "test"; - $params['title'] = "title"; - $params['description'] = 'Creating a Test Description'; - $params['is_active'] = 1; - - // Creating a DataProcessor - civicrm_api3('DataProcessor', 'create', $params); - - // Retrieving the data processor - $result = civicrm_api3('DataProcessor', 'get'); - - if(isset($result['id'])){ - // Retrieving the id of data processor - $data_processor_id = $result['id']; - - $params = []; - // Params for setting data source parameters - - $params['data_processor_id'] = $data_processor_id; - $params['title'] = 'testDataSource'; - $params['type'] = 'contact'; - - // Creating a DataProcessor Source - civicrm_api3('DataProcessorSource', 'create', $params); - - $result_datasource = civicrm_api3('DataProcessorSource', 'get'); - - if(isset($result_datasource['id'])){ - - $id_datasource = $result_datasource['id']; - - $params = []; - // Params for setting data field parameters - - $params['data_processor_id'] = $data_processor_id; - $params['title'] = 'id_test'; - $params['type'] = 'raw'; - $params['configuration'] = array("field" => "id","datasource" =>"testdatasource"); - - civicrm_api3('DataProcessorField', 'create', $params); - - $params['title'] = 'first_name_test'; - $params['configuration'] = array("field" => "display_name","datasource" =>"testdatasource"); - - civicrm_api3('DataProcessorField', 'create', $params); - - - $result_field = civicrm_api3('DataProcessorField', 'get'); - - $field_check = True; //True if no error in adding Data Processor Field - - foreach ($result_field['values'] as $key => $value) { - if(!isset($value['id'])) - { - $field_check = False; - break; - } - } - - if($field_check){ //If field_check is false then fields of data processor has not been setup - - $params = []; // Params for setting data source parameters - - $params['data_processor_id'] = $data_processor_id; - $params['type'] = 'contact_search'; - $params['configuration'] = array("title" => "contact","contact_id_field" =>"id_test"); - - civicrm_api3('DataProcessorOutput', 'create', $params); - - $result_output = civicrm_api3('DataProcessorOutput', 'get'); - $outputId = $result_output['id']; - - $file_download_name = 'test.json'; - $mime_type = 'application/json'; - $buffer = json_encode(CRM_Dataprocessor_Utils_Importer::export($data_processor_id), JSON_PRETTY_PRINT); - if($this->checkContent(json_decode($buffer))) - $this->assertTrue(true); - else - $this->assertFalse(true); - - - - } - else{ - echo "Failed to add DataProcessor Field"; - $this->assertFalse(true); - } - - - } - else{ - - echo "Failed to add DataProcessorSource"; - $this->assertFalse(true); - } - } - else{ - echo "DataProcessor Failed to Setup"; - $this->assertFalse(true); - } - - } - - -} diff --git a/tests/phpunit/CRM/Myextension/ImportDataProcessorTest.php b/tests/phpunit/CRM/Myextension/ImportDataProcessorTest.php deleted file mode 100644 index 6241c69236e364d04050a0a03c8144c5871f7b8a..0000000000000000000000000000000000000000 --- a/tests/phpunit/CRM/Myextension/ImportDataProcessorTest.php +++ /dev/null @@ -1,155 +0,0 @@ -<?php - -use CRM_Dataprocessor_ExtensionUtil as E; -use Civi\Test\HeadlessInterface; -use Civi\Test\HookInterface; -use Civi\Test\TransactionalInterface; - -/** - * 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 CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - - public function setUpHeadless() { - // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). - // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest - return \Civi\Test::headless() - ->installMe(__DIR__) - ->apply(); - } - - public function setUp() { - parent::setUp(); - } - - public function tearDown() { - parent::tearDown(); - } - - public function checkDataProcessor($result,$id){ - $returnvalue = True; - $values = $result['values'][$id]; - if($values['name']!='test') - $returnvalue = False; - if($values['title']!='Test') - $returnvalue = False; - if($values['description']!='This is a test Data Processor') - $returnvalue = False; - if($values['is_active']!=1) - $returnvalue = False; - - return $returnvalue; - } - - public function checkDataProcessorFields($fields){ - $keys = array_keys($fields['values']); - $returnvalue = True; - $field_0 = $fields['values'][$keys[0]]; - if($field_0['name']!='contact_id') - $returnvalue = False; - if($field_0['configuration']['field']!='id') - $returnvalue = False; - if($field_0['configuration']['datasource']!='contact') - $returnvalue = False; - - $field_1 = $fields['values'][$keys[1]]; - if($field_1['name']!='first_name') - $returnvalue = False; - if($field_1['configuration']['field']!='first_name') - $returnvalue = False; - if($field_1['configuration']['datasource']!='contact') - $returnvalue = False; - - $field_2 = $fields['values'][$keys[2]]; - if($field_2['name']!='gender') - $returnvalue = False; - if($field_2['configuration']['field']!='gender_id') - $returnvalue = False; - if($field_2['configuration']['datasource']!='contact') - $returnvalue = False; - - return $returnvalue; - - } - - public function checkDataProcessorSource($datasource){ - $keys = array_keys($datasource['values']); - $returnvalue = True; - $datasource_check = $datasource['values'][$keys[0]]; - if($datasource_check['name']!='contact') - $returnvalue = False; - if($datasource_check['title']!='Contact') - $returnvalue = False; - if($datasource_check['type']!='contact') - $returnvalue = False; - if($datasource_check['configuration']['filter']['is_deleted']['op']!='=') - $returnvalue = False; - if($datasource_check['configuration']['filter']['is_deleted']['value']!=0) - $returnvalue = False; - - return $returnvalue; - - } - - public function checkDataProcessorOutput($output){ - $keys = array_keys($output['values']); - $returnvalue = True; - $output_check = $output['values'][$keys[0]]; - if($output_check['type']!='contact_search') - $returnvalue = False; - if($output_check['configuration']['title']!='Test') - $returnvalue = False; - if($output_check['configuration']['contact_id_field']!='contact_id') - $returnvalue = False; - - return $returnvalue; - - } - - public function testImportDataProcessor() { - $file = __DIR__.'/test.json'; - $configuration = file_get_contents($file); - - $importCode = json_decode($configuration, true); - $importResult = CRM_Dataprocessor_Utils_Importer::import($importCode, '', true); - $id = $importResult['original_id']; - $result = civicrm_api3('DataProcessor', 'get', array("id"=> $id)); - $fields = civicrm_api3('DataProcessorField', 'get', array("data_processor_id"=> $id)); - $datasource = civicrm_api3('DataProcessorSource', 'get', array("data_processor_id"=> $id)); - $outputs = civicrm_api3('DataProcessorOutput', 'get', array("data_processor_id"=> $id)); - - - if($this->checkDataProcessor($result,$id)) - $this->assertTrue(true); - else - $this->assertFalse(true); - - if($this->checkDataProcessorFields($fields)) - $this->assertTrue(true); - else - $this->assertFalse(true); - - if($this->checkDataProcessorSource($datasource)) - $this->assertTrue(true); - else - $this->assertFalse(true); - - if($this->checkDataProcessorOutput($outputs)) - $this->assertTrue(true); - else - $this->assertFalse(true); - - } - -} diff --git a/tests/phpunit/CRM/Myextension/OutputResultTest.php b/tests/phpunit/CRM/Myextension/OutputResultTest.php deleted file mode 100644 index 7bf1bb4ba27912ab4fb376922209cb77a7aa6feb..0000000000000000000000000000000000000000 --- a/tests/phpunit/CRM/Myextension/OutputResultTest.php +++ /dev/null @@ -1,189 +0,0 @@ -<?php - -use CRM_Dataprocessor_ExtensionUtil as E; -use Civi\Test\HeadlessInterface; -use Civi\Test\HookInterface; -use Civi\Test\TransactionalInterface; - -/** - * 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 CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - - var $check_results = array( - 1 => array( - 'id_test' => 1, - 'first_name_test' => 'Default Organization', - ), - - 2 => array( - 'id_test' => 2, - 'first_name_test' => 'Second Domain', - ), - - ); - - - public function checkRecord($record,$id) { - - $check = array(); - - foreach(array_keys($record) as $key){ - $check[$key] = $record[$key]->formattedValue; - } - - if(empty(array_diff($check,$this->check_results[$id]))) // Check for difference in array - return True; - else - return False; - } - - - public function setUpHeadless() { - // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). - // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest - return \Civi\Test::headless() - ->installMe(__DIR__) - ->apply(); - } - - public function setUp() { - parent::setUp(); - } - - public function tearDown() { - parent::tearDown(); - } - - public function testCreateDataProcessorField() { - - $params['name'] = "test"; - $params['title'] = "title"; - $params['description'] = 'Creating a Test Description'; - $params['is_active'] = 1; - - // Creating a DataProcessor - civicrm_api3('DataProcessor', 'create', $params); - - // Retrieving the data processor - $result = civicrm_api3('DataProcessor', 'get'); - - if(isset($result['id'])){ - // Retrieving the id of data processor - $data_processor_id = $result['id']; - - $params = []; - // Params for setting data source parameters - - $params['data_processor_id'] = $data_processor_id; - $params['title'] = 'testDataSource'; - $params['type'] = 'contact'; - - // Creating a DataProcessor Source - civicrm_api3('DataProcessorSource', 'create', $params); - - $result_datasource = civicrm_api3('DataProcessorSource', 'get'); - - if(isset($result_datasource['id'])){ - - $id_datasource = $result_datasource['id']; - - $params = []; - // Params for setting data field parameters - - $params['data_processor_id'] = $data_processor_id; - $params['title'] = 'id_test'; - $params['type'] = 'raw'; - $params['configuration'] = array("field" => "id","datasource" =>"testdatasource"); - - civicrm_api3('DataProcessorField', 'create', $params); - - $params['title'] = 'first_name_test'; - $params['configuration'] = array("field" => "display_name","datasource" =>"testdatasource"); - - civicrm_api3('DataProcessorField', 'create', $params); - - - $result_field = civicrm_api3('DataProcessorField', 'get'); - - $field_check = True; //True if no error in adding Data Processor Field - - foreach ($result_field['values'] as $key => $value) { - if(!isset($value['id'])) - { - $field_check = False; - break; - } - } - - if($field_check){ //If field_check is false then fields of data processor has not been setup - - $params = []; // Params for setting data source parameters - - $params['data_processor_id'] = $data_processor_id; - $params['type'] = 'contact_search'; - $params['configuration'] = array("title" => "contact","contact_id_field" =>"id_test"); - - civicrm_api3('DataProcessorOutput', 'create', $params); - - $result_output = civicrm_api3('DataProcessorOutput', 'get'); - $outputId = $result_output['id']; - - - $dataProcessor = civicrm_api3('DataProcessor', 'getsingle', array('id' => $data_processor_id)); - $output = civicrm_api3('DataProcessorOutput', 'getsingle', array('id' => $outputId)); - $dataProcessorClass = CRM_Dataprocessor_BAO_DataProcessor::dataProcessorToClass($dataProcessor);; - - try - { - while($record = $dataProcessorClass->getDataFlow()->nextRecord()){ - $check_results_id = $record['id_test']->formattedValue; - if($this->checkRecord($record,$check_results_id)){ - $this->assertTrue(true); - } - else{ - echo "Output results don't match."; - $this->assertFalse(true); - } - } - } - catch (\Civi\DataProcessor\DataFlow\EndOfFlowException $e) { - // Do nothing - } - } - else{ - echo "Failed to add DataProcessor Field"; - $this->assertFalse(true); - } - - - } - else{ - - echo "Failed to add DataProcessorSource"; - $this->assertFalse(true); - } - } - else{ - echo "DataProcessor Failed to Setup"; - $this->assertFalse(true); - } - - } - - - - - -} diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index afa827e95ae842a2cc0acdcdeee69b8fb018f0f5..352e007050f2a100faafc6c95b15e38820286699 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -2,6 +2,7 @@ ini_set('memory_limit', '2G'); ini_set('safe_mode', 0); +// phpcs:ignore eval(cv('php:boot --level=classloader', 'phpcode')); // Allow autoloading of PHPUnit helper classes in this extension.