Skip to content

Commit

Permalink
Merge pull request #19317 from ahed-compucorp/dev/core#2285
Browse files Browse the repository at this point in the history
dev/core#2285 Fix civicrm_alterReportVar hook for contribute detail r…
  • Loading branch information
eileenmcnaughton authored Apr 5, 2021
2 parents 214a6b8 + 7164d1a commit 7901585
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions CRM/Report/Form/Contribute/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ public function beginPostProcessCommon() {

// we inner join with temp1 to restrict soft contributions to those in temp1 table.
// no group by here as we want to display as many soft credit rows as actually exist.
CRM_Utils_Hook::alterReportVar('sql', $this, $this);
$sql = "{$select} {$this->_from} {$this->_where} $this->_groupBy";
$this->createTemporaryTable('civireport_contribution_detail_temp2', $sql);

Expand Down
79 changes: 79 additions & 0 deletions tests/phpunit/CRM/Report/Form/Contribute/DetailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,83 @@ public function testMultipleSoftCredits() {

}

/**
* Make sure the civicrm_alterReportVar hook for contribute detail report work well.
*/
public function testContributeDetailReportWithNewColumnFromCustomTable() {
$this->quickCleanup($this->_tablesToTruncate);

$solParams = [
'first_name' => 'Solicitor 1',
'last_name' => 'User ' . rand(),
'contact_type' => 'Individual',
];
$solicitor1Id = $this->individualCreate($solParams);
$solParams['first_name'] = 'Solicitor 2';
$solicitor2Id = $this->individualCreate($solParams);
$solParams['first_name'] = 'Donor';
$donorId = $this->individualCreate($solParams);

$contribParams = [
'total_amount' => 150,
'contact_id' => $donorId,
// TODO: We're getting a "DB Error: already exists" when inserting a line
// item, but that's beside the point for this test, so skipping.
'skipLineItem' => 1,
];
$contribId = $this->contributionCreate($contribParams);

$config = CRM_Core_Config::singleton();
CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/fixtures/value_extension_tng55_table.sql");
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_value_extension_tng55 (`entity_id`, `title`) VALUES (%1, 'some_title')", [1 => [$contribId, 'Positive']]);

CRM_Utils_Hook::singleton()->setHook('civicrm_alterReportVar', function ($varType, &$var, &$reportForm) {
if ($varType === 'columns') {
$var['civicrm_value_extension_tng55'] = [
'fields' => [
'extension_tng55_title' => [
'title' => ts('Extension Title'),
'dbAlias' => "civicrm_value_extension_tng55.title",
],
],
'filters' => [
'extension_tng55_title' => [
'title' => ts('Extension Title'),
'dbAlias' => "civicrm_value_extension_tng55.title",
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => [],
],
],
];
}
if ($varType === 'sql') {
$from = $var->getVar('_from');
$from .= "
LEFT JOIN civicrm_value_extension_tng55
ON (civicrm_value_extension_tng55.entity_id = contribution_civireport.id)
";
$var->setVar('_from', $from);
}
});

$input = [
'extension_tng55_title_op' => 'nnll',
'fields' => [
'sort_name' => 1,
'contribution_id' => 1,
'total_amount' => 1,
'extension_tng55_title' => 1,
],
];
$params = array_merge([
'report_id' => 'contribute/detail',
], $input);
$rows = $this->callAPISuccess('ReportTemplate', 'getrows', $params)['values'];

$this->assertCount(1, $rows);
$this->assertEquals('some_title', $rows[0]['civicrm_value_extension_tng55_extension_tng55_title']);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- phpMyAdmin SQL Dump
-- version 4.9.7
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 04, 2021 at 06:30 AM
-- Server version: 5.7.32
-- PHP Version: 7.4.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET FOREIGN_KEY_CHECKS=0;

--
-- Database: `civicrm_tests_dev`
--

--
-- Dumping data for table `civicrm_value_extension_tng55`
--

CREATE TABLE `civicrm_value_extension_tng55` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Default MySQL primary key',
`entity_id` int(10) UNSIGNED NOT NULL,
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

0 comments on commit 7901585

Please sign in to comment.