Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#2422 Add created_id, modified_id, expires_date to saved search table #19709

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions CRM/Contact/BAO/SavedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,13 @@ public static function create(&$params) {
}
$params['name'] = $name . $suffix;
}

$loggedInContactID = CRM_Core_Session::getLoggedInContactID();
if ($loggedInContactID) {
if (empty($params['id'])) {
$params['created_id'] = $loggedInContactID;
}
$params['modified_id'] = $loggedInContactID;
}
return self::writeRecord($params);
}

Expand Down Expand Up @@ -419,7 +425,7 @@ public static function decodeRelativeFields(&$formValues, $fieldName, $op, $valu

switch ($op) {
case 'BETWEEN':
list($formValues[$fieldName . '_from'], $formValues[$fieldName . '_to']) = $value;
[$formValues[$fieldName . '_from'], $formValues[$fieldName . '_to']] = $value;
break;

case '>=':
Expand Down
133 changes: 132 additions & 1 deletion CRM/Contact/DAO/SavedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/SavedSearch.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:165072ec4f0aa9a3002222c544b3047c)
* (GenCodeChecksum:1ca4440b5a09843b5d87d595a6d826ef)
*/

/**
Expand Down Expand Up @@ -86,6 +86,46 @@ class CRM_Contact_DAO_SavedSearch extends CRM_Core_DAO {
*/
public $api_params;

/**
* FK to contact table.
*
* @var int
*/
public $created_id;

/**
* FK to contact table.
*
* @var int
*/
public $modified_id;

/**
* Optional date after which the search is not needed
*
* @var timestamp
*/
public $expires_date;

/**
* When the search was created.
*
* @var timestamp
*/
public $created_date;

/**
* When the search was last modified.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "lase"

* @var timestamp
*/
public $modified_date;

/**
* @var text
*/
public $description;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -114,6 +154,8 @@ public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'mapping_id', 'civicrm_mapping', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'created_id', 'civicrm_contact', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'modified_id', 'civicrm_contact', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
Expand Down Expand Up @@ -250,6 +292,95 @@ public static function &fields() {
'serialize' => self::SERIALIZE_JSON,
'add' => '5.24',
],
'created_id' => [
'name' => 'created_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Created By Contact ID'),
'description' => ts('FK to contact table.'),
'where' => 'civicrm_saved_search.created_id',
'table_name' => 'civicrm_saved_search',
'entity' => 'SavedSearch',
'bao' => 'CRM_Contact_BAO_SavedSearch',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
'html' => [
'label' => ts("Created By"),
],
'add' => '5.36',
],
'modified_id' => [
'name' => 'modified_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Modified By Contact ID'),
'description' => ts('FK to contact table.'),
'where' => 'civicrm_saved_search.modified_id',
'table_name' => 'civicrm_saved_search',
'entity' => 'SavedSearch',
'bao' => 'CRM_Contact_BAO_SavedSearch',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
'html' => [
'label' => ts("Modified By"),
],
'add' => '5.36',
],
'expires_date' => [
'name' => 'expires_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => ts('Search Expiry Date'),
'description' => ts('Optional date after which the search is not needed'),
'required' => FALSE,
'where' => 'civicrm_saved_search.expires_date',
'table_name' => 'civicrm_saved_search',
'entity' => 'SavedSearch',
'bao' => 'CRM_Contact_BAO_SavedSearch',
'localizable' => 0,
'add' => '5.36',
],
'created_date' => [
'name' => 'created_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => ts('Created Date'),
'description' => ts('When the search was created.'),
'required' => TRUE,
'where' => 'civicrm_saved_search.created_date',
'default' => 'CURRENT_TIMESTAMP',
'table_name' => 'civicrm_saved_search',
'entity' => 'SavedSearch',
'bao' => 'CRM_Contact_BAO_SavedSearch',
'localizable' => 0,
'add' => '5.36',
],
'modified_date' => [
'name' => 'modified_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => ts('Modified Date'),
'description' => ts('When the search was last modified.'),
'required' => TRUE,
'where' => 'civicrm_saved_search.modified_date',
'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
'table_name' => 'civicrm_saved_search',
'entity' => 'SavedSearch',
'bao' => 'CRM_Contact_BAO_SavedSearch',
'localizable' => 0,
'add' => '5.36',
],
'description' => [
'name' => 'description',
'type' => CRM_Utils_Type::T_TEXT,
'title' => ts('Saved Search Description'),
'rows' => 2,
'cols' => 60,
'where' => 'civicrm_saved_search.description',
'table_name' => 'civicrm_saved_search',
'entity' => 'SavedSearch',
'bao' => 'CRM_Contact_BAO_SavedSearch',
'localizable' => 0,
'html' => [
'type' => 'TextArea',
],
'add' => '5.36',
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
56 changes: 41 additions & 15 deletions CRM/Upgrade/Incremental/php/FiveThirtySix.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,47 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
* (change the x in the function name):
*/

// /**
// * Upgrade function.
// *
// * @param string $rev
// */
// public function upgrade_5_0_x($rev) {
// $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
// $this->addTask('Do the foo change', 'taskFoo', ...);
// // Additional tasks here...
// // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
// // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
// }
/**
* Upgrade function.
*
* @param string $rev
*/
public function upgrade_5_36_alpha1(string $rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);

$this->addTask('core-issue#2422 - Add created_id to civicrm_saved_search', 'addColumn',
'civicrm_saved_search', 'created_id', "int(10) unsigned DEFAULT NULL COMMENT 'FK to contact table.'");
$this->addTask('core-issue#2422 - Add modified_id to civicrm_saved_search', 'addColumn',
'civicrm_saved_search', 'modified_id', "int(10) unsigned DEFAULT NULL COMMENT 'FK to contact table.'");
$this->addTask('core-issue#2422 - Add expires_date to civicrm_saved_search', 'addColumn',
'civicrm_saved_search', 'expires_date', "timestamp NULL DEFAULT NULL COMMENT 'Optional date after which the search is not needed'");
$this->addTask('core-issue#2422 - Add created_date to civicrm_saved_search', 'addColumn',
'civicrm_saved_search', 'created_date', "timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'When the search was created.'");
$this->addTask('core-issue#2422 - Add modified_date to civicrm_saved_search', 'addColumn',
'civicrm_saved_search', 'modified_date', "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'When the search was last modified.'");
$this->addTask('core-issue#2422 - Add description to civicrm_saved_search', 'addColumn',
'civicrm_saved_search', 'description', "text DEFAULT NULL");

$this->addTask('core-issue#2422 - Add constraints to civicrm_saved_search', 'taskAddConstraints');

// public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) {
// return TRUE;
// }
}

/**
* @param \CRM_Queue_TaskContext $ctx
*
* @return bool
*/
public static function taskAddConstraints(CRM_Queue_TaskContext $ctx): bool {
CRM_Core_DAO::executeQuery("
ALTER TABLE `civicrm_saved_search`
ADD CONSTRAINT `FK_civicrm_saved_search_created_id`
FOREIGN KEY (`created_id`) REFERENCES `civicrm_contact` (`id`)
ON DELETE SET NULL,
ADD CONSTRAINT `FK_civicrm_saved_search_modified_id`
FOREIGN KEY (`modified_id`) REFERENCES `civicrm_contact` (`id`)
ON DELETE SET NULL;
");
return TRUE;
}

}
3 changes: 2 additions & 1 deletion tests/phpunit/CRM/Dedupe/MergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function testBatchMergeAllDuplicates() {
/**
* The goal of this function is to test that all required tables are returned.
*/
public function testGetCidRefs() {
public function testGetCidRefs(): void {
$sortRefs = function($a) {
ksort($a);
foreach ($a as &$fields) {
Expand Down Expand Up @@ -1368,6 +1368,7 @@ public function getStaticCIDRefs() {
'civicrm_print_label' => [
0 => 'created_id',
],
'civicrm_saved_search' => ['created_id', 'modified_id'],
'civicrm_relationship' => [
0 => 'contact_id_a',
1 => 'contact_id_b',
Expand Down
24 changes: 11 additions & 13 deletions tests/phpunit/api/v3/SavedSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ public function setUp() {
// I created a smart group using the CiviCRM gui. The smart group contains
// all contacts tagged with 'company'.
// I got the params below from the database.

$url = CIVICRM_UF_BASEURL . "/civicrm/contact/search/advanced?reset=1";
$serialized_url = serialize($url);

// params for saved search that returns all volunteers for the
// default organization.
$this->params = [
'expires_date' => '2021-08-08',
'form_values' => [
// Is volunteer for
'relation_type_id' => '6_a_b',
Expand All @@ -72,21 +69,22 @@ public function setUp() {
* Create a saved search, and see whether the returned values make sense.
*/
public function testCreateSavedSearch() {
// Act:
$contactID = $this->createLoggedInUser();
$result = $this->callAPIAndDocument(
$this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
$this->assertEquals(1, $result['count']);
$this->_entity, 'create', $this->params, __FUNCTION__, __FILE__)['values'];
$this->assertEquals(1, count($result));
$savedSearch = reset($result);

// Assert:
// getAndCheck fails, I think because form_values is an array.
//$this->getAndCheck($this->params, $result['id'], $this->_entity);
// Check whether the new ID is correctly returned by the API.
$this->assertNotNull($result['values'][$result['id']]['id']);
$this->assertEquals($contactID, $savedSearch['created_id']);
$this->assertEquals($contactID, $savedSearch['modified_id']);
$this->assertEquals('20210808000000', $savedSearch['expires_date']);

$this->assertNotNull($savedSearch['id']);

// Check whether the relation type ID is correctly returned.
$this->assertEquals(
$this->params['form_values']['relation_type_id'],
$result['values'][$result['id']]['form_values']['relation_type_id']);
$savedSearch['form_values']['relation_type_id']);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions xml/schema/Contact/SavedSearch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,73 @@
<serialize>JSON</serialize>
<add>5.24</add>
</field>
<field>
<name>created_id</name>
<type>int unsigned</type>
<title>Created By Contact ID</title>
<comment>FK to contact table.</comment>
<html>
<label>Created By</label>
</html>
<add>5.36</add>
</field>
<foreignKey>
<name>created_id</name>
<table>civicrm_contact</table>
<key>id</key>
<add>5.36</add>
<onDelete>SET NULL</onDelete>
</foreignKey>
<field>
<name>modified_id</name>
<type>int unsigned</type>
<title>Modified By Contact ID</title>
<comment>FK to contact table.</comment>
<html>
<label>Modified By</label>
</html>
<add>5.36</add>
</field>
<foreignKey>
<name>modified_id</name>
<table>civicrm_contact</table>
<key>id</key>
<add>5.36</add>
<onDelete>SET NULL</onDelete>
</foreignKey>
<field>
<name>expires_date</name>
<type>timestamp</type>
<title>Search Expiry Date</title>
<required>false</required>
<comment>Optional date after which the search is not needed</comment>
<add>5.36</add>
</field>
<field>
<name>created_date</name>
<type>timestamp</type>
<comment>When the search was created.</comment>
<required>true</required>
<default>CURRENT_TIMESTAMP</default>
<add>5.36</add>
</field>
<field>
<name>modified_date</name>
<type>timestamp</type>
<comment>When the search was last modified.</comment>
<required>true</required>
<default>CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</default>
<add>5.36</add>
</field>
<field>
<name>description</name>
<type>text</type>
<title>Saved Search Description</title>
<html>
<type>TextArea</type>
<rows>2</rows>
<cols>60</cols>
</html>
<add>5.36</add>
</field>
</table>