1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\Tests\Notifications;
9
10use Tiki\Notifications\Email;
11
12class EmailTest extends \PHPUnit_Framework_TestCase
13{
14	protected static $objects = [];
15
16	protected function setUp()
17	{
18		$_SERVER["SERVER_NAME"] = 'test.example.org';
19	}
20
21	public static function tearDownAfterClass()
22	{
23		$commentslib = \TikiLib::lib('comments');
24		foreach (self::$objects['comments'] as $commentId) {
25			$commentslib->remove_comment($commentId);
26		}
27
28		$commentslib = \TikiLib::lib('comments');
29		foreach (self::$objects['forums'] as $forumId) {
30			$commentslib->remove_forum($forumId);
31		}
32
33		// Remove forum, removes all forum posts/comments
34		$bloglib = \TikiLib::lib('blog');
35		foreach (self::$objects['blogs'] as $blogId) {
36			$bloglib->remove_blog($blogId);
37		}
38	}
39
40	/**
41	 * @covers Tiki\Notifications\Email::getEmailThreadHeaders()
42	 */
43	public function testGetEmailThreadHeadersForForums()
44	{
45		global $user;
46
47		/** @var \Comments $commentsLib */
48		$commentsLib = \TikiLib::lib('comments');
49		$forumId = $commentsLib->replace_forum(0, 'Test Forum');
50
51		self::$objects['forums'][] = $forumId;
52
53		$messageId = '';
54		$rand = rand(0, 9);
55
56		//In a forumthread there is a first post (no parent available):
57		$commentId = $commentsLib->post_new_comment(
58			'forum:' . $forumId,
59			0,
60			$user,
61			'Test forum thread comment - ' . $rand,
62			'This is a test - ' . $rand,
63			$messageId
64		);
65
66		self::$objects['comments'][] = $commentId;
67
68		$md5Header = md5('forum.' . $forumId) . '@' . $_SERVER["SERVER_NAME"];
69		$headers = Email::getEmailThreadHeaders('forum', $commentId);
70
71		$this->assertEquals($messageId, $headers['Message-Id']);
72		$this->assertArrayNotHasKey('In-Reply-To', $headers);
73		$this->assertEquals($md5Header, $headers['References']);
74
75		//2nd post in same thread:
76
77		$messageId2 = '';
78		$rand2 = rand(10, 19);
79
80		$commentId2 = $commentsLib->post_new_comment(
81			'forum:' . $forumId,
82			$commentId,
83			$user,
84			'Test forum thread comment - ' . $rand2,
85			'This is a test - ' . $rand2,
86			$messageId2,
87			$messageId
88		);
89
90		self::$objects['comments'][] = $commentId2;
91
92		$headers2 = Email::getEmailThreadHeaders('forum', $commentId2);
93
94		$this->assertEquals($messageId2, $headers2['Message-Id']);
95		$this->assertEquals($messageId, $headers2['In-Reply-To']);
96		$this->assertEquals($md5Header . ' ' . $messageId, $headers2['References']);
97
98		//3rd post in same thread (no reply):
99		$messageId3 = '';
100		$rand3 = rand(20, 29);
101
102		$commentId3 = $commentsLib->post_new_comment(
103			'forum:' . $forumId,
104			0,
105			$user,
106			'Test forum thread comment - ' . $rand3,
107			'This is a test - ' . $rand3,
108			$messageId3
109		);
110
111		self::$objects['comments'][] = $commentId3;
112
113		$headers3 = Email::getEmailThreadHeaders('forum', $commentId3);
114
115		$this->assertEquals($messageId3, $headers3['Message-Id']);
116		$this->assertArrayNotHasKey('In-Reply-To', $headers3);
117		$this->assertEquals($md5Header, $headers3['References']);
118
119		//4th post in same thread, reply to comment 2:
120		$messageId4 = '';
121		$rand4 = rand(30, 39);
122
123		$commentId4 = $commentsLib->post_new_comment(
124			'forum:' . $forumId,
125			$commentId2,
126			$user,
127			'Test forum thread comment - ' . $rand4,
128			'This is a test - ' . $rand4,
129			$messageId4,
130			$messageId2
131		);
132
133		self::$objects['comments'][] = $commentId4;
134
135		$headers4 = Email::getEmailThreadHeaders('forum', $commentId4);
136
137		$this->assertEquals($messageId4, $headers4['Message-Id']);
138		$this->assertEquals($messageId2, $headers4['In-Reply-To']);
139		$this->assertEquals($md5Header . ' ' . $messageId . ' ' . $messageId2, $headers4['References']);
140	}
141
142	/**
143	 * @covers Tiki\Notifications\Email::getEmailThreadHeaders()
144	 */
145	public function testGetEmailThreadHeadersForBlogPosts()
146	{
147		global $user;
148
149		$commentsLib = \TikiLib::lib('comments');
150		$blogLib = \TikiLib::lib('blog');
151		$blogId = $blogLib->replace_blog('Test Blog', '', 'admin', 'y', 25, 0, '', 'y', 'y', 'y', 'y', 'y', 'y', 'n', 'y', 'n', 'n', '', 'n', 5, 'n');
152
153		self::$objects['blogs'][] = $blogId;
154
155		$blogPostId = $blogLib->blog_post($blogId, 'Test blog post', 'Test blog post', $user, 'Test blog post');
156		self::$objects['blog_posts'][] = $blogPostId;
157
158
159		$messageId = '';
160		$rand = rand(0, 9);
161
162		//1st comment:
163		$commentId = $commentsLib->post_new_comment(
164			'blog post:' . $blogPostId,
165			0,
166			$user,
167			'Test blog post comment - ' . $rand,
168			'This is a test - ' . $rand,
169			$messageId
170		);
171
172		self::$objects['comments'][] = $commentId;
173
174		$md5Header = md5('blog post.' . $blogPostId) . '@' . $_SERVER["SERVER_NAME"];
175		$headers = Email::getEmailThreadHeaders('blog post', $commentId);
176
177		$this->assertEquals($messageId, $headers['Message-Id']);
178		$this->assertArrayNotHasKey('In-Reply-To', $headers);
179		$this->assertEquals($md5Header, $headers['References']);
180
181		//2nd comment (reply to first - will have a parentId but no in_reply_to in database)
182		$messageId2 = '';
183		$rand2 = rand(10, 19);
184
185		$commentId2 = $commentsLib->post_new_comment(
186			'blog post:' . $blogPostId,
187			$commentId,
188			$user,
189			'Test blog post comment - ' . $rand2,
190			'This is a test - ' . $rand2,
191			$messageId2
192		);
193
194		self::$objects['comments'][] = $commentId2;
195
196		$headers2 = Email::getEmailThreadHeaders('blog post', $commentId2);
197
198		$this->assertEquals($messageId2, $headers2['Message-Id']);
199		$this->assertEquals($messageId, $headers2['In-Reply-To']);
200		$this->assertEquals($md5Header . ' ' . $messageId, $headers2['References']);
201
202		//3rd comment (no reply to other comment):
203		$messageId3 = '';
204		$rand3 = rand(20, 29);
205
206		$commentId3 = $commentsLib->post_new_comment(
207			'blog post:' . $blogPostId,
208			0,
209			$user,
210			'Test blog post comment - ' . $rand3,
211			'This is a test - ' . $rand3,
212			$messageId3
213		);
214
215		self::$objects['comments'][] = $commentId3;
216
217		$headers3 = Email::getEmailThreadHeaders('blog post', $commentId3);
218
219		$this->assertEquals($messageId3, $headers3['Message-Id']);
220		$this->assertArrayNotHasKey('In-Reply-To', $headers3);
221		$this->assertEquals($md5Header, $headers3['References']);
222
223		//4th comment, reply to comment 2:
224		$messageId4 = '';
225		$rand4 = rand(30, 39);
226
227		$commentId4 = $commentsLib->post_new_comment(
228			'blog post:' . $blogPostId,
229			$commentId2,
230			$user,
231			'Test blog post comment - ' . $rand4,
232			'This is a test - ' . $rand4,
233			$messageId4
234		);
235
236		self::$objects['comments'][] = $commentId4;
237
238		$headers4 = Email::getEmailThreadHeaders('blog post', $commentId4);
239
240		$this->assertEquals($messageId4, $headers4['Message-Id']);
241		$this->assertEquals($messageId2, $headers4['In-Reply-To']);
242		$this->assertEquals($md5Header . ' ' . $messageId  . ' ' . $messageId2, $headers4['References']);
243	}
244}
245