ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilMailMimeTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 {
29  private const USER_ID = 6;
30 
31  protected function setUp(): void
32  {
34  ilMailMimeSenderUserById::addUserToCache(self::USER_ID, $this->getUserById(self::USER_ID));
35 
36  parent::setUp();
37  }
38 
43  {
44  $defaultTransport = $this->getMockBuilder(ilMailMimeTransport::class)->disableOriginalConstructor()->getMock();
45  $defaultTransport->expects($this->never())->method('send');
46 
47  $transport = $this->getMockBuilder(ilMailMimeTransport::class)->getMock();
48  $transport->expects($this->once())->method('send');
49 
50  $transportFactory = $this->getMockBuilder(ilMailMimeTransportFactory::class)->disableOriginalConstructor()->getMock();
51  $transportFactory->method('getTransport')->willReturn($defaultTransport);
52  $this->setGlobalVariable('mail.mime.transport.factory', $transportFactory);
53 
54  $refineryMock = $this->getMockBuilder(Factory::class)->disableOriginalConstructor()->getMock();
55  $this->setGlobalVariable('refinery', $refineryMock);
56 
57  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
58  'set',
59  'get',
60  ])->getMock();
61  $this->setGlobalVariable('ilSetting', $settings);
62 
63  $mail = new ilMimeMail();
64  $mail->Send($transport);
65  }
66 
71  {
72  $defaultTransport = $this->getMockBuilder(ilMailMimeTransport::class)->getMock();
73  $defaultTransport->expects($this->once())->method('send');
74 
75  $transportFactory = $this->getMockBuilder(ilMailMimeTransportFactory::class)->disableOriginalConstructor()->getMock();
76  $transportFactory->method('getTransport')->willReturn($defaultTransport);
77  $this->setGlobalVariable('mail.mime.transport.factory', $transportFactory);
78 
79  $refineryMock = $this->getMockBuilder(Factory::class)->disableOriginalConstructor()->getMock();
80  $this->setGlobalVariable('refinery', $refineryMock);
81 
82  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
83  'set',
84  'get',
85  ])->getMock();
86  $this->setGlobalVariable('ilSetting', $settings);
87 
88  $mail = new ilMimeMail();
89  $mail->Send();
90  }
91 
96  {
97  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
98  'set',
99  'get',
100  ])->getMock();
101  $settings->method('get')->willReturnCallback(static function ($key): ?string {
102  return (string) ('mail_allow_external' !== $key);
103  });
104  $this->setGlobalVariable('ilSetting', $settings);
105 
106  $eventHandler = $this->getMockBuilder(ilAppEventHandler::class)->disableOriginalConstructor()->onlyMethods([
107  'raise',
108  ])->getMock();
109 
110  $factory = new ilMailMimeTransportFactory($settings, $eventHandler);
111  $this->assertInstanceOf(ilMailMimeTransportNull::class, $factory->getTransport());
112  }
113 
118  {
119  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
120  'set',
121  'get',
122  ])->getMock();
123  $settings->method('get')->willReturnCallback(static function ($key): ?string {
124  return '1';
125  });
126  $this->setGlobalVariable('ilSetting', $settings);
127 
128  $eventHandler = $this->getMockBuilder(ilAppEventHandler::class)->disableOriginalConstructor()->onlyMethods([
129  'raise',
130  ])->getMock();
131 
132  $factory = new ilMailMimeTransportFactory($settings, $eventHandler);
133  $this->assertInstanceOf(ilMailMimeTransportSmtp::class, $factory->getTransport());
134  }
135 
140  {
141  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
142  'set',
143  'get',
144  ])->getMock();
145 
146  $settings->method('get')->willReturnCallback(static function ($key): ?string {
147  if ('mail_allow_external' === $key) {
148  return '1';
149  }
150 
151 
152  if ('mail_smtp_status' === $key) {
153  return '0';
154  }
155 
156  return '1';
157  });
158  $this->setGlobalVariable('ilSetting', $settings);
159 
160  $eventHandler = $this->getMockBuilder(ilAppEventHandler::class)->disableOriginalConstructor()->onlyMethods([
161  'raise',
162  ])->getMock();
163 
164  $factory = new ilMailMimeTransportFactory($settings, $eventHandler);
165  $this->assertInstanceOf(ilMailMimeTransportSendmail::class, $factory->getTransport());
166  }
167 
172  {
173  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
174  'set',
175  'get',
176  ])->getMock();
177  $mustache_factory = $this->getMockBuilder(ilMustacheFactory::class)->getMock();
178 
179  $factory = new ilMailMimeSenderFactory($settings, $mustache_factory);
180  $this->assertInstanceOf(ilMailMimeSenderSystem::class, $factory->getSenderByUsrId(ANONYMOUS_USER_ID));
181  }
182 
187  {
188  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
189  'set',
190  'get',
191  ])->getMock();
192  $mustache_factory = $this->getMockBuilder(ilMustacheFactory::class)->getMock();
193 
194  $factory = new ilMailMimeSenderFactory($settings, $mustache_factory);
195  $this->assertInstanceOf(ilMailMimeSenderSystem::class, $factory->system());
196  }
197 
201  protected function getUserById(int $usrId): ilObjUser
202  {
203  $user = $this->getMockBuilder(ilObjUser::class)
204  ->disableOriginalConstructor()
205  ->onlyMethods(['getId'])
206  ->getMock();
207  $user->method('getId')->willReturn($usrId);
208 
209  return $user;
210  }
211 
216  {
217  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
218  'set',
219  'get',
220  ])->getMock();
221  $mustache_factory = $this->getMockBuilder(ilMustacheFactory::class)->getMock();
222 
223  $factory = new ilMailMimeSenderFactory($settings, $mustache_factory);
224  $this->assertInstanceOf(ilMailMimeSenderUser::class, $factory->getSenderByUsrId(self::USER_ID));
225  }
226 
231  {
232  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods([
233  'set',
234  'get',
235  ])->getMock();
236  $mustache_factory = $this->getMockBuilder(ilMustacheFactory::class)->getMock();
237 
238  $factory = new ilMailMimeSenderFactory($settings, $mustache_factory);
239  $this->assertInstanceOf(ilMailMimeSenderUser::class, $factory->user(self::USER_ID));
240  }
241 }
static addUserToCache(int $usrId, ilObjUser $user)
const ANONYMOUS_USER_ID
Definition: constants.php:27
testFactoryWillReturnUserSenderForExistingUserId()
static setDefaultTransport(?ilMailMimeTransport $transport)
testTransportFactoryWillReturnSendmailTransportIfSmtpTransportIsDisabled()
testFactoryWillReturnSystemSenderForAnonymousUserId()
testFactoryWillReturnSystemSenderWhenExplicitlyRequested()
Class ilMailBaseTest.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setGlobalVariable(string $name, $value)
testMimMailDelegatesEmailDeliveryToDefaultTransport()
testMimMailDelegatesEmailDeliveryToThePassedTransporter()
testTransportFactoryWillReturnNullTransportIfExternalEmailDeliveryIsDisabled()
testFactoryWillReturnUserSenderWhenExplicitlyRequested()
testTransportFactoryWillReturnSmtpTransportIfEnabled()
getUserById(int $usrId)
Class ilMailMimeTest.