Skip to content

EspoCRM – Open Source CRM Application

Email template integration test

Yurii5a7de0c12520
diff --git a/application/Espo/Entities/Team.php b/application/Espo/Entities/Team.php
index db669e670a..c60cbd4c13 100644
--- a/application/Espo/Entities/Team.php
+++ b/application/Espo/Entities/Team.php
@@ -30,6 +30,7 @@
namespace Espo\Entities;
use Espo\Core\Field\Link;
+use Espo\Core\Name\Field;
use Espo\Core\ORM\Entity;
class Team extends Entity
@@ -60,4 +61,20 @@ class Team extends Entity
{
return $this->get('positionList') ?? [];
}
+
+ /**
+ * @since 10.0.3
+ */
+ public function setName(string $name): self
+ {
+ return $this->set(Field::NAME, $name);
+ }
+
+ /**
+ * @since 10.0.3
+ */
+ public function getName(): ?string
+ {
+ return $this->get(Field::NAME);
+ }
}
diff --git a/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php b/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php
new file mode 100644
index 0000000000..7b4d7d2b5f
--- /dev/null
+++ b/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php
@@ -0,0 +1,140 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace integration\Espo\Tools\EmailTemplate;
+
+use Espo\Core\Field\Link;
+use Espo\Core\Field\LinkMultiple;
+use Espo\Entities\EmailTemplate;
+use Espo\Entities\Team;
+use Espo\Entities\User;
+use Espo\Modules\Crm\Entities\Lead;
+use Espo\Tools\EmailTemplate\Data as TemplateData;
+use Espo\Tools\EmailTemplate\Params as TemplateParams;
+use Espo\Tools\EmailTemplate\Processor;
+use tests\integration\Core\BaseTestCase;
+
+class ProcessorTest extends BaseTestCase
+{
+ public function testProcess(): void
+ {
+ $em = $this->getEntityManager();
+
+ $team = $em->getRDBRepositoryByClass(Team::class)->getNew();
+ $team
+ ->setName('Team 1');
+ $em->saveEntity($team);
+
+ $user = $em->getRDBRepositoryByClass(User::class)->getNew();
+ $user
+ ->setFirstName('Test')
+ ->setLastName('Hello')
+ ->setUserName('test')
+ ->setDefaultTeam(Link::fromEntity($team))
+ ->setTeams(LinkMultiple::create()->withAddedId($team->getId()));
+ $em->saveEntity($user);
+
+ $template1 = $em->getRDBRepositoryByClass(EmailTemplate::class)->getNew();
+ $template1->setMultiple([
+ 'subject' => '{Lead.name} {Lead.assignedUser.name} {Lead.assignedUser.password}',
+ 'body' => '{Lead.name} {Lead.assignedUser.name} {Lead.assignedUser.password}',
+ ]);
+ $em->saveEntity($template1);
+
+ $template2 = $em->getRDBRepositoryByClass(EmailTemplate::class)->getNew();
+ $template2->setMultiple([
+ 'subject' => '{{name}} {{assignedUser.name}} {{assignedUser.password}}',
+ 'body' => '{{name}} {{assignedUser.name}} {{assignedUser.password}}',
+ ]);
+ $em->saveEntity($template2);
+
+ $template3 = $em->getRDBRepositoryByClass(EmailTemplate::class)->getNew();
+ $template3->setMultiple([
+ 'subject' => '{{name}} {{password}} {{defaultTeam.name}}',
+ 'body' => '{User.name} {User.password} {User.defaultTeam.name}',
+ ]);
+ $em->saveEntity($template2);
+
+ $lead = $em->getRDBRepositoryByClass(Lead::class)->getNew();
+ $lead
+ ->setFirstName('Lead')
+ ->setLastName('Abc')
+ ->setAssignedUser($user);
+ $em->saveEntity($lead);
+
+ $processor = $this->getInjectableFactory()->create(Processor::class);
+
+ $params = TemplateParams::create()
+ ->withApplyAcl(false);
+
+ $data = TemplateData::create();
+
+ //
+
+ $emailData1 = $processor->process($template1, $params, $data->withParent($lead));
+
+ $this->assertEquals(
+ 'Lead Abc Test Hello {Lead.assignedUser.password}',
+ $emailData1->getSubject(),
+ );
+
+ $this->assertEquals(
+ 'Lead Abc Test Hello {Lead.assignedUser.password}',
+ $emailData1->getBody(),
+ );
+
+ //
+
+ $emailData2 = $processor->process($template2, $params, $data->withParent($lead));
+
+ $this->assertEquals(
+ 'Lead Abc Test Hello ',
+ $emailData2->getSubject(),
+ );
+
+ $this->assertEquals(
+ 'Lead Abc Test Hello ',
+ $emailData2->getBody(),
+ );
+
+ //
+
+ $emailData3 = $processor->process($template3, $params, $data->withParent($user));
+
+ $this->assertEquals(
+ 'Test Hello ',
+ $emailData3->getSubject(),
+ );
+
+ $this->assertEquals(
+ 'Test Hello {User.password} {User.defaultTeam.name}',
+ $emailData3->getBody(),
+ );
+ }
+}