From 999307e16b839c104019456419bd112237fa6c9b Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 4 Jan 2024 16:29:26 +1300 Subject: [PATCH] Move report test to civi_report --- ext/civi_report/phpunit.xml.dist | 18 ++++ ext/civi_report/tests/phpunit/ReportTest.php | 91 +++++++++++++++++++ ext/civi_report/tests/phpunit/bootstrap.php | 65 +++++++++++++ tests/phpunit/CRM/Report/Utils/ReportTest.php | 52 ----------- 4 files changed, 174 insertions(+), 52 deletions(-) create mode 100644 ext/civi_report/phpunit.xml.dist create mode 100644 ext/civi_report/tests/phpunit/ReportTest.php create mode 100644 ext/civi_report/tests/phpunit/bootstrap.php diff --git a/ext/civi_report/phpunit.xml.dist b/ext/civi_report/phpunit.xml.dist new file mode 100644 index 0000000000..ea391745fa --- /dev/null +++ b/ext/civi_report/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + ./tests/phpunit + + + + + ./ + + + + + + + + diff --git a/ext/civi_report/tests/phpunit/ReportTest.php b/ext/civi_report/tests/phpunit/ReportTest.php new file mode 100644 index 0000000000..b2d377ca32 --- /dev/null +++ b/ext/civi_report/tests/phpunit/ReportTest.php @@ -0,0 +1,91 @@ +installMe(__DIR__) + ->apply(); + } + + /** + * Test makeCsv functionality. + * + * Include some special characters to check they are handled. + */ + public function testMakeCsv(): void { + $form = new CRM_Report_Form(); + $form->_columnHeaders = [ + 'civicrm_activity_activity_type_id' => [ + 'title' => 'Activity Type', + 'type' => 2, + ], + 'civicrm_activity_activity_subject' => [ + 'title' => 'Subject', + 'type' => 2, + ], + 'civicrm_activity_details' => [ + 'title' => 'Activity Details', + 'type' => NULL, + ], + ]; + + $details = <<Here's some typical data from an activity details field.

+

дè some non-ascii and html styling and these ̋“weird” quotes - ’.

+

Also some named entities "hello". And & é. Also, some math like 2 < 4.

+ENDDETAILS; + + $expectedOutput = << 'Meeting', + 'civicrm_activity_activity_subject' => 'Meeting with the apostrophe\'s and that person who does "air quotes". Some non-ascii characters: дè', + 'civicrm_activity_details' => $details, + ], + ]; + + $csvString = CRM_Report_Utils_Report::makeCsv($form, $rows); + $this->assertEquals($expectedOutput, $csvString); + } + +} diff --git a/ext/civi_report/tests/phpunit/bootstrap.php b/ext/civi_report/tests/phpunit/bootstrap.php new file mode 100644 index 0000000000..eaa8379442 --- /dev/null +++ b/ext/civi_report/tests/phpunit/bootstrap.php @@ -0,0 +1,65 @@ +add('CRM_', [__DIR__ . '/../..', __DIR__]); +$loader->addPsr4('Civi\\', [__DIR__ . '/../../Civi', __DIR__ . '/Civi']); +$loader->add('api_', [__DIR__ . '/../..', __DIR__]); +$loader->addPsr4('api\\', [__DIR__ . '/../../api', __DIR__ . '/api']); + +$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/Report/Utils/ReportTest.php b/tests/phpunit/CRM/Report/Utils/ReportTest.php index fa397d6165..e667de3f95 100644 --- a/tests/phpunit/CRM/Report/Utils/ReportTest.php +++ b/tests/phpunit/CRM/Report/Utils/ReportTest.php @@ -17,58 +17,6 @@ */ class CRM_Report_Utils_ReportTest extends CiviUnitTestCase { - /** - * Test makeCsv - */ - public function testMakeCsv(): void { - $form = new CRM_Report_Form(); - $form->_columnHeaders = [ - 'civicrm_activity_activity_type_id' => [ - 'title' => 'Activity Type', - 'type' => 2, - ], - 'civicrm_activity_activity_subject' => [ - 'title' => 'Subject', - 'type' => 2, - ], - 'civicrm_activity_details' => [ - 'title' => 'Activity Details', - 'type' => NULL, - ], - ]; - - $details = <<Here's some typical data from an activity details field. -

-

дè some non-ascii and html styling and these ̋“weird” quotes’s. -

-

Also some named entities "hello". And & é. Also some math like 2 < 4. -

-ENDDETAILS; - - $expectedOutput = << 'Meeting', - 'civicrm_activity_activity_subject' => 'Meeting with the apostrophe\'s and that person who does "air quotes". Some non-ascii characters: дè', - 'civicrm_activity_details' => $details, - ], - ]; - - $csvString = CRM_Report_Utils_Report::makeCsv($form, $rows); - $this->assertEquals($expectedOutput, $csvString); - } - /** * Test when you choose Print from the actions dropdown. * -- 2.25.1