aboutsummaryrefslogtreecommitdiffstats
path: root/tests/email/email_parsing_test.php
blob: 629df9abb6c71ae6211cfe1675ee98e891af2e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

class phpbb_email_parsing_test extends phpbb_test_case
{
	/** @var \messenger */
	protected $messenger;

	/** @var \ReflectionProperty */
	protected $reflection_template_property;

	public function setUp(): void
	{
		global $phpbb_container, $config, $phpbb_root_path, $phpEx, $request, $user;

		$phpbb_container = new phpbb_mock_container_builder;

		$config = new \phpbb\config\config(array(
			'board_email_sig'     => '-- Thanks, The Management',
			'sitename' 			=> 'yourdomain.com',
			'default_lang'		=> 'en',
		));
		$phpbb_container->set('config', $config);

		$request = new phpbb_mock_request;
		$symfony_request = new \phpbb\symfony_request(
			$request
		);
		$filesystem = new \phpbb\filesystem\filesystem();
		$phpbb_path_helper = new \phpbb\path_helper(
			$symfony_request,
			$filesystem,
			$request,
			$phpbb_root_path,
			$phpEx
		);
		$phpbb_container->set('path_helper', $phpbb_path_helper);
		$phpbb_container->set('filesystem', $filesystem);

		$cache_path = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/twig';
		$phpbb_container->setParameter('core.template.cache_path', $cache_path);

		$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
		$lang = new \phpbb\language\language($lang_loader);
		$user = new \phpbb\user($lang, '\phpbb\datetime');
		$phpbb_container->set('user', $user);
		$extension_manager = new phpbb_mock_extension_manager(
			dirname(__FILE__) . '/',
			array(
				'vendor2/foo' => array(
					'ext_name' => 'vendor2/foo',
					'ext_active' => '1',
					'ext_path' => 'ext/vendor2/foo/',
				),
			)
		);
		$phpbb_container->set('ext.manager', $extension_manager);

		$context = new \phpbb\template\context();
		$twig = new \phpbb\template\twig\environment(
			$config,
			$filesystem,
			$phpbb_path_helper,
			$cache_path,
			null,
			new \phpbb\template\twig\loader($filesystem, ''),
			new \phpbb\event\dispatcher($phpbb_container),
			array(
				'cache'			=> false,
				'debug'			=> false,
				'auto_reload'	=> true,
				'autoescape'	=> false,
			)
		);
		$twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang);
		$phpbb_container->set('template.twig.extensions.phpbb', $twig_extension);

		$twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
		$twig_extensions_collection->add('template.twig.extensions.phpbb');
		$phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection);

		$twig->addExtension($twig_extension);
		$phpbb_container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));

		if (!class_exists('messenger'))
		{
			include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
		}

		$this->messenger = new \messenger();

		$reflection = new ReflectionObject($this->messenger);
		$this->reflection_template_property = $reflection->getProperty('template');
		$this->reflection_template_property->setAccessible(true);
	}

	public function email_parsing_data()
	{
		return array(
			array('Author username', 'Any forum', 'The topic title', 'Dear user'),
			array('0', 'Any forum', 'The topic title', 'Dear user'),
		);
	}

	/**
	 * @dataProvider email_parsing_data
	 */
	public function test_email_parsing($author_name, $forum_name, $topic_title, $username)
	{
		global $config, $phpEx, $user;

		$this->messenger->set_addresses($user->data);

		$this->messenger->assign_vars(array(
			'EMAIL_SIG'	=> str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])),
			'SITENAME'	=> htmlspecialchars_decode($config['sitename']),

			'AUTHOR_NAME'				=> $author_name,
			'FORUM_NAME'				=> $forum_name,
			'TOPIC_TITLE'				=> $topic_title,
			'USERNAME'					=> $username,

			'U_FORUM'					=> generate_board_url() . "/viewforum.{$phpEx}?f=1",
			'U_STOP_WATCHING_FORUM'		=> generate_board_url() . "/viewforum.{$phpEx}?uid=2&f=1&unwatch=forum",
		));
		$this->messenger->template('newtopic_notify', $user->data['user_lang'], '', '');

		$reflection_template = $this->reflection_template_property->getValue($this->messenger);
		$msg = trim($reflection_template->assign_display('body'));

		$this->assertContains($author_name, $msg);
		$this->assertContains($forum_name, $msg);
		$this->assertContains($topic_title, $msg);
		$this->assertContains($username, $msg);
		$this->assertContains(htmlspecialchars_decode($config['sitename']), $msg);
		$this->assertContains(str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])), $msg);
		$this->assertNotContains('EMAIL_SIG', $msg);
		$this->assertNotContains('U_STOP_WATCHING_FORUM', $msg);
	}
}