Update test for runner
[civicrm-core.git] / tests / phpunit / CRM / Mailing / MailStoreTest.php
1 <?php
2
3 /**
4 * @group headless
5 */
6 class CRM_Mailing_MailStoreTest extends \CiviUnitTestCase {
7
8 protected $workDir;
9
10 public function setUp(): void {
11 $this->useTransaction(TRUE);
12 parent::setUp();
13 $this->workDir = tempnam(sys_get_temp_dir(), 'mailstoretest');
14 @unlink($this->workDir);
15 }
16
17 public function tearDown(): void {
18 parent::tearDown();
19 if (is_dir($this->workDir)) {
20 CRM_Utils_File::cleanDir($this->workDir);
21 }
22 }
23
24 /**
25 * Create an example store (maildir) using default behaviors (no hooks).
26 */
27 public function testMaildirBasic() {
28 $this->createMaildirSettings([
29 'name' => __FUNCTION__,
30 ]);
31 $store = CRM_Mailing_MailStore::getStore(__FUNCTION__);
32 $this->assertTrue($store instanceof CRM_Mailing_MailStore_Maildir);
33 }
34
35 /**
36 * Create an example store (maildir) and change the driver via hook.
37 */
38 public function testMaildirHook() {
39 // This hook swaps out the implementation used for 'Maildir' stores.
40 Civi::dispatcher()
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 {
47
48 public function frobnicate() {
49 return 'totally';
50 }
51
52 };
53 };
54 }
55 });
56
57 $this->createMaildirSettings([
58 'name' => __FUNCTION__,
59 ]);
60 $store = CRM_Mailing_MailStore::getStore(__FUNCTION__);
61
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());
66 }
67
68 /**
69 * Create a "MailSettings" record for maildir store.
70 * @param array $values
71 * Some values to set
72 * @return array
73 */
74 private function createMaildirSettings($values = []):array {
75 mkdir($this->workDir);
76 $defaults = [
77 'protocol:name' => 'Maildir',
78 'name' => NULL,
79 'source' => $this->workDir,
80 'domain' => 'maildir.example.com',
81 'username' => 'pass-my-name',
82 'password' => 'pass-my-pass',
83 ];
84 $mailSettings = \Civi\Api4\MailSettings::create(0)
85 ->setValues(array_merge($defaults, $values))
86 ->execute()
87 ->single();
88 return $mailSettings;
89 }
90
91 }