6 class CRM_Mailing_MailStoreTest
extends \CiviUnitTestCase
{
10 public function setUp(): void
{
11 $this->useTransaction(TRUE);
13 $this->workDir
= tempnam(sys_get_temp_dir(), 'mailstoretest');
14 @unlink
($this->workDir
);
17 public function tearDown(): void
{
19 if (is_dir($this->workDir
)) {
20 CRM_Utils_File
::cleanDir($this->workDir
);
25 * Create an example store (maildir) using default behaviors (no hooks).
27 public function testMaildirBasic() {
28 $this->createMaildirSettings([
29 'name' => __FUNCTION__
,
31 $store = CRM_Mailing_MailStore
::getStore(__FUNCTION__
);
32 $this->assertTrue($store instanceof CRM_Mailing_MailStore_Maildir
);
36 * Create an example store (maildir) and change the driver via hook.
38 public function testMaildirHook() {
39 // This hook swaps out the implementation used for 'Maildir' stores.
41 ->addListener('hook_civicrm_alterMailStore', function ($e) {
42 if ($e->params
['protocol'] === 'Maildir') {
43 $e->params
['factory'] = function ($mailSettings) {
44 $this->assertEquals('testMaildirHook', $mailSettings['name']);
45 // Make a fake object that technically meets the contract of 'MailStore'
46 return new class extends CRM_Mailing_MailStore
{
48 public function frobnicate() {
57 $this->createMaildirSettings([
58 'name' => __FUNCTION__
,
60 $store = CRM_Mailing_MailStore
::getStore(__FUNCTION__
);
62 // The hook gave us an unusual instance of MailStore.
63 $this->assertTrue($store instanceof CRM_Mailing_MailStore
);
64 $this->assertFalse($store instanceof CRM_Mailing_MailStore_Maildir
);
65 $this->assertEquals('totally', $store->frobnicate());
69 * Create a "MailSettings" record for maildir store.
70 * @param array $values
74 private function createMaildirSettings($values = []):array {
75 mkdir($this->workDir
);
77 'protocol:name' => 'Maildir',
79 'source' => $this->workDir
,
80 'domain' => 'maildir.example.com',
81 'username' => 'pass-my-name',
82 'password' => 'pass-my-pass',
84 $mailSettings = \Civi\Api4\MailSettings
::create(0)
85 ->setValues(array_merge($defaults, $values))