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

Make MessageTemplate.master_id fully joinable #25056

Merged
merged 1 commit into from
Nov 27, 2022
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
51 changes: 51 additions & 0 deletions Civi/Api4/Event/Subscriber/MessageTemplateSchemaMapSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Civi\Api4\Event\Subscriber;

use Civi\Api4\Event\Events;
use Civi\Api4\Event\SchemaMapBuildEvent;
use Civi\Api4\Service\Schema\Joinable\Joinable;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @service civi.api4.messagetemplateSchema
*/
class MessageTemplateSchemaMapSubscriber extends \Civi\Core\Service\AutoService implements EventSubscriberInterface {

/**
* @return array
*/
public static function getSubscribedEvents() {
return [
Events::SCHEMA_MAP_BUILD => 'onSchemaBuild',
];
}

/**
* This creates a joinable which gets exposed and rendered by:
*
* @see \Civi\Api4\Service\Spec\Provider\MessageTemplateGetSpecProvider
*
* @param \Civi\Api4\Event\SchemaMapBuildEvent $event
*
* Condition based on CRM_Admin_Page_MessageTemplates::__construct()
*/
public function onSchemaBuild(SchemaMapBuildEvent $event) {
$schema = $event->getSchemaMap();
$table = $schema->getTableByName('civicrm_msg_template');

$link = new Joinable("civicrm_msg_template", 'id', "master_id");
$link->setBaseTable('civicrm_msg_template');
$link->setJoinType(Joinable::JOIN_TYPE_ONE_TO_ONE);
$link->addCondition("`{target_table}`.`id` =
(SELECT `id` FROM `civicrm_msg_template` `orig`
WHERE `{base_table}`.`workflow_name` = `orig`.`workflow_name` AND `orig`.`is_reserved` = 1 AND
( `{base_table}`.`msg_subject` != `orig`.`msg_subject` OR
`{base_table}`.`msg_text` != `orig`.`msg_text` OR
`{base_table}`.`msg_html` != `orig`.`msg_html`
))"
);
$table->addTableLink(NULL, $link);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function modifySpec(RequestSpec $spec) {
->setInputType('Select')
->setReadonly(TRUE)
->setFkEntity('MessageTemplate')
->setSqlRenderer([__CLASS__, 'revertible']);
->setSqlRenderer(['\Civi\Api4\Service\Schema\Joiner', 'getExtraJoinSql']);
Copy link
Member

Choose a reason for hiding this comment

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

Nice to see this function getting some reuse!

$spec->addFieldSpec($field);
}

Expand All @@ -37,19 +37,4 @@ public function applies($entity, $action) {
return $entity === 'MessageTemplate' && $action === 'get';
}

/**
* Callback for finding id of template to revert to
* Based on CRM_Admin_Page_MessageTemplates::__construct()
*
* @return string
*/
public static function revertible(): string {
return "(SELECT `id` FROM `civicrm_msg_template` `orig`
WHERE `a`.`workflow_name` = `orig`.`workflow_name` AND `orig`.`is_reserved` = 1 AND
( `a`.`msg_subject` != `orig`.`msg_subject` OR
`a`.`msg_text` != `orig`.`msg_text` OR
`a`.`msg_html` != `orig`.`msg_html`
))";
}

}
39 changes: 0 additions & 39 deletions tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,45 +341,6 @@ public function testCaseActivityCopyTemplate():void {
$this->assertStringContainsString('Case ID : 1234', $message);
}

/**
* Test APIv4 calculated field master_id
*/
public function testMessageTemplateMasterID() {
CRM_Core_Transaction::create(TRUE)->run(function(CRM_Core_Transaction $tx) {
$tx->rollback();

$messageTemplateID = MessageTemplate::get()
->addWhere('is_default', '=', 1)
->addWhere('workflow_name', '=', 'contribution_offline_receipt')
->addSelect('id')
->execute()->first()['id'];
$messageTemplateIDReserved = MessageTemplate::get()
->addWhere('is_reserved', '=', 1)
->addWhere('workflow_name', '=', 'contribution_offline_receipt')
->addSelect('id')
->execute()->first()['id'];
$master_id = MessageTemplate::get()
->addSelect('master_id')
->addWhere('id', '=', $messageTemplateID)
->execute()->first()['master_id'];
$this->assertNull($master_id);

MessageTemplate::update()
->addWhere('id', '=', $messageTemplateID)
->setValues([
'msg_subject' => 'Hello world',
'msg_text' => 'Hello world',
'msg_html' => '<p>Hello world</p>',
])
->execute();
$master_id = MessageTemplate::get()
->addSelect('master_id')
->addWhere('id', '=', $messageTemplateID)
->execute()->first()['master_id'];
$this->assertEquals($master_id, $messageTemplateIDReserved);
});
}

/**
* Test rendering of domain tokens.
*
Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/api/v4/Entity/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Civi\Test\DbTestTrait;
use Civi\Test\GenericAssertionsTrait;
use Civi\Test\TransactionalInterface;
use Civi\Api4\MessageTemplate;

/**
* @group headless
Expand Down Expand Up @@ -134,4 +135,54 @@ public function testSaveNullId() {
$this->assertDBQuery('<p>My body as HTML</p>', 'SELECT msg_html FROM civicrm_msg_template WHERE id = %1', [1 => [$saved['id'], 'Int']]);
}

/**
* Test APIv4 calculated field master_id
*/
public function testMessageTemplateMasterID() {
\CRM_Core_Transaction::create(TRUE)->run(function(\CRM_Core_Transaction $tx) {
$tx->rollback();

$messageTemplateID = MessageTemplate::get()
->addWhere('is_default', '=', 1)
->addWhere('workflow_name', '=', 'contribution_offline_receipt')
->addSelect('id')
->execute()->first()['id'];
$messageTemplateIDReserved = MessageTemplate::get()
->addWhere('is_reserved', '=', 1)
->addWhere('workflow_name', '=', 'contribution_offline_receipt')
->addSelect('id')
->execute()->first()['id'];
$msgTpl = MessageTemplate::get()
->addSelect('msg_subject', 'master_id', 'master_id.msg_subject')
->addWhere('id', '=', $messageTemplateID)
->execute()->first();
// confirm subject is set
$this->assertNotNull($msgTpl['msg_subject']);
// message is unchanged from original so both of these should be null
$this->assertNull($msgTpl['master_id']);
$this->assertNull($msgTpl['master_id.msg_subject']);

MessageTemplate::update()
->addWhere('id', '=', $messageTemplateID)
->setValues([
'msg_subject' => 'Hello world',
'msg_text' => 'Hello world',
'msg_html' => '<p>Hello world</p>',
])
->execute();
$msgTpl = MessageTemplate::get()
->addSelect('msg_subject', 'master_id', 'master_id.msg_subject')
->addWhere('id', '=', $messageTemplateID)
->execute()->first();
// confirm subject is set
$this->assertNotNull($msgTpl['msg_subject']);
// message is changed so both of these should be set
$this->assertEquals($msgTpl['master_id'], $messageTemplateIDReserved);
$this->assertNotNull($msgTpl['master_id.msg_subject']);
// these should be different
$this->assertNotEquals($msgTpl['msg_subject'], $msgTpl['master_id.msg_subject']);

});
}

}