From c1c6d215c74a8246183510b7a4e94aa07e5b82d4 Mon Sep 17 00:00:00 2001 From: Bennet Becker Date: Fri, 23 Dec 2022 12:46:15 +0100 Subject: fix getAutoSubmitted return type and adding unit tests Signed-off-by: Bennet Becker --- tests/lib/Mail/MessageTest.php | 142 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) (limited to 'tests') diff --git a/tests/lib/Mail/MessageTest.php b/tests/lib/Mail/MessageTest.php index bec70d1c78a..aae25497876 100644 --- a/tests/lib/Mail/MessageTest.php +++ b/tests/lib/Mail/MessageTest.php @@ -8,7 +8,9 @@ namespace Test\Mail; +use Egulias\EmailValidator\EmailValidator; use OC\Mail\Message; +use OCP\Mail\AutoSubmittedValue; use OCP\Mail\IEMailTemplate; use Swift_Message; use Test\TestCase; @@ -263,4 +265,144 @@ class MessageTest extends TestCase { $message->useTemplate($template); } + + public function testSetAutoSubmitted1() { + + $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') + ->disableOriginalConstructor() + ->getMock(); + + $swiftMessage = $this->getMockBuilder('\Swift_Message') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->disallowMockingUnknownTypes() + ->getMock(); + + + $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('has') + ->with('Auto-Submitted'); + + $swiftMimeSimpleHeaderSet->expects($this->never()) + ->method('get'); + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('addTextHeader') + ->with('Auto-Submitted', AutoSubmittedValue::AUTO_GENERATED); + + $message = new Message($swiftMessage, false); + + $message->setAutoSubmitted(AutoSubmittedValue::AUTO_GENERATED); + + } + + public function testSetAutoSubmitted2() { + + $swiftMimeHeader = $this->getMockBuilder('\Swift_Mime_Header') + ->disableOriginalConstructor() + ->addMethods(['setValue']) + ->getMockForAbstractClass(); + + $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') + ->disableOriginalConstructor() + ->getMock(); + + $swiftMessage = $this->getMockBuilder('\Swift_Message') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->disallowMockingUnknownTypes() + ->getMock(); + + + $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); + $swiftMimeSimpleHeaderSet->method('get')->with("Auto-Submitted")->willReturn($swiftMimeHeader); + + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('has') + ->with('Auto-Submitted') + ->willReturn(true); + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('get'); + + $swiftMimeSimpleHeaderSet->expects($this->never()) + ->method('addTextHeader') + ->with('Auto-Submitted', AutoSubmittedValue::AUTO_GENERATED); + + $swiftMimeHeader->expects($this->once()) + ->method('setValue') + ->with(AutoSubmittedValue::AUTO_GENERATED); + + $message = new Message($swiftMessage, false); + + $message->setAutoSubmitted(AutoSubmittedValue::AUTO_GENERATED); + } + + public function testGetAutoSubmitted1() { + + $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') + ->disableOriginalConstructor() + ->getMock(); + + $swiftMessage = $this->getMockBuilder('\Swift_Message') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->disallowMockingUnknownTypes() + ->getMock(); + + + $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('has') + ->with('Auto-Submitted'); + + $swiftMimeSimpleHeaderSet->expects($this->never()) + ->method('get'); + + $message = new Message($swiftMessage, false); + + $this->assertSame("no", $message->getAutoSubmitted()); + } + public function testGetAutoSubmitted2() { + + $swiftMimeHeader = $this->getMockBuilder('\Swift_Mime_Header') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') + ->disableOriginalConstructor() + ->getMock(); + + $swiftMessage = $this->getMockBuilder('\Swift_Message') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->disallowMockingUnknownTypes() + ->getMock(); + + + $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); + $swiftMimeHeader->method('toString')->willReturn(AutoSubmittedValue::AUTO_GENERATED); + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('has') + ->with('Auto-Submitted') + ->willReturn(true); + + $swiftMimeSimpleHeaderSet->expects($this->once()) + ->method('get') + ->willReturn($swiftMimeHeader); + + $message = new Message($swiftMessage, false); + + $this->assertSame(AutoSubmittedValue::AUTO_GENERATED, $message->getAutoSubmitted()); + + } } -- cgit v1.2.3