Skip to content

Commit 63203f4

Browse files
committed
[ticket/17490] Add test coverage for remainder of base test
PHPBB-17490
1 parent aa2da5b commit 63203f4

File tree

2 files changed

+289
-2
lines changed

2 files changed

+289
-2
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
* For full copyright and license information, please see
10+
* the docs/CREDITS.txt file.
11+
*
12+
*/
13+
14+
use phpbb\config\config;
15+
use phpbb\language\language;
16+
use phpbb\language\language_file_loader;
17+
use phpbb\messenger\method\email;
18+
use phpbb\messenger\queue;
19+
use phpbb\path_helper;
20+
use phpbb\symfony_request;
21+
use phpbb\template\assets_bag;
22+
23+
class phpbb_messenger_method_base_test extends \phpbb_test_case
24+
{
25+
protected $assets_bag;
26+
protected $cache_path;
27+
protected config $config;
28+
protected $dispatcher;
29+
protected $extension_manager;
30+
protected email $method_email;
31+
protected $method_base;
32+
protected $language;
33+
protected $log;
34+
protected $path_helper;
35+
protected queue $queue;
36+
protected $request;
37+
protected $twig_extensions_collection;
38+
protected $twig_lexer;
39+
protected $user;
40+
41+
public function setUp(): void
42+
{
43+
global $config, $request, $symfony_request, $user, $phpbb_root_path, $phpEx;
44+
45+
$this->assets_bag = new assets_bag();
46+
$this->cache_path = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/twig';
47+
$this->config = new config([
48+
'force_server_vars' => false,
49+
]);
50+
$config = $this->config;
51+
$this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher')
52+
->disableOriginalConstructor()
53+
->getMock();
54+
$this->filesystem = new \phpbb\filesystem\filesystem();
55+
$this->language = new language(new language_file_loader($phpbb_root_path, $phpEx));
56+
$this->queue = $this->createMock(queue::class);
57+
$this->request = new phpbb_mock_request();
58+
$request = $this->request;
59+
$this->symfony_request = new symfony_request(new phpbb_mock_request());
60+
$symfony_request = $this->symfony_request;
61+
$this->user = $this->getMockBuilder('\phpbb\user')
62+
->setConstructorArgs([$this->language, '\phpbb\datetime'])
63+
->getMock();
64+
$user = $this->user;
65+
$user->page['root_script_path'] = 'phpbb/';
66+
$this->user->host = 'yourdomain.com';
67+
$this->path_helper = new path_helper(
68+
$this->symfony_request,
69+
$this->request,
70+
$phpbb_root_path,
71+
$phpEx
72+
);
73+
$phpbb_container = new phpbb_mock_container_builder;
74+
$this->twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
75+
$twig = new \phpbb\template\twig\environment(
76+
$this->assets_bag,
77+
$this->config,
78+
$this->filesystem,
79+
$this->path_helper,
80+
$this->cache_path,
81+
null,
82+
new \phpbb\template\twig\loader(''),
83+
$this->dispatcher,
84+
array(
85+
'cache' => false,
86+
'debug' => false,
87+
'auto_reload' => true,
88+
'autoescape' => false,
89+
)
90+
);
91+
$this->twig_lexer = new \phpbb\template\twig\lexer($twig);
92+
$this->extension_manager = new phpbb_mock_extension_manager(
93+
__DIR__ . '/',
94+
array(
95+
'vendor2/foo' => array(
96+
'ext_name' => 'vendor2/foo',
97+
'ext_active' => '1',
98+
'ext_path' => 'ext/vendor2/foo/',
99+
),
100+
)
101+
);
102+
$this->log = $this->createMock(\phpbb\log\log_interface::class);
103+
104+
$this->method_email = new email(
105+
$this->assets_bag,
106+
$this->config,
107+
$this->dispatcher,
108+
$this->language,
109+
$this->queue,
110+
$this->path_helper,
111+
$this->request,
112+
$this->twig_extensions_collection,
113+
$this->twig_lexer,
114+
$this->user,
115+
$phpbb_root_path,
116+
$this->cache_path,
117+
$this->extension_manager,
118+
$this->log
119+
);
120+
121+
$this->method_base = $this->getMockBuilder(\phpbb\messenger\method\base::class)
122+
->setConstructorArgs([
123+
$this->assets_bag,
124+
$this->config,
125+
$this->dispatcher,
126+
$this->language,
127+
$this->queue,
128+
$this->path_helper,
129+
$this->request,
130+
$this->twig_extensions_collection,
131+
$this->twig_lexer,
132+
$this->user,
133+
$phpbb_root_path,
134+
$this->cache_path,
135+
$this->extension_manager,
136+
$this->log
137+
])
138+
->getMockForAbstractClass();
139+
}
140+
141+
public function test_header()
142+
{
143+
$this->method_base->header('X-AntiAbuse', 'Board servername - ' . $this->user->host);
144+
$this->assertTrue(true); // No exception should be thrown
145+
}
146+
147+
public function test_set_use_queue()
148+
{
149+
$use_queue_property = new \ReflectionProperty($this->method_base, 'use_queue');
150+
$this->method_base->set_use_queue();
151+
$this->assertTrue($use_queue_property->getValue($this->method_base));
152+
$this->method_base->set_use_queue(false);
153+
$this->assertFalse($use_queue_property->getValue($this->method_base));
154+
}
155+
156+
public function test_error_wout_session()
157+
{
158+
$errors = [];
159+
$this->log->method('add')
160+
->willReturnCallback(function($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = []) use (&$errors) {
161+
$errors[] = $additional_data[0];
162+
});
163+
164+
$this->user->data['user_id'] = 2;
165+
$this->user->session_id = '';
166+
$this->user
167+
->expects($this->once())
168+
->method('session_begin')
169+
->willReturnCallback(function() {
170+
$this->assertTrue(true);
171+
});
172+
173+
$this->method_base->error('Test error message');
174+
175+
$this->assertCount(1, $errors);
176+
$this->assertEquals('<strong></strong><br><em></em><br><br>Test error message<br>', $errors[0]);
177+
}
178+
179+
public function test_save_queue()
180+
{
181+
$this->queue->expects($this->once())
182+
->method('save');
183+
$this->method_base->set_use_queue(false);
184+
$this->method_base->save_queue();
185+
$this->method_base->set_use_queue(true);
186+
$this->method_base->save_queue();
187+
}
188+
189+
public function test_template_no_lang()
190+
{
191+
$template_mock = $this->getMockBuilder(\phpbb\template\template::class)
192+
->disableOriginalConstructor()
193+
->getMock();
194+
$filenames = [];
195+
$template_mock->method('set_filenames')
196+
->willReturnCallback(function($filename_array) use (&$filenames, $template_mock) {
197+
$filenames = array_merge($filenames, $filename_array);
198+
199+
return $template_mock;
200+
});
201+
202+
$base_reflection = new \ReflectionClass($this->method_base);
203+
$template_reflection = $base_reflection->getProperty('template');
204+
$template_reflection->setValue($this->method_base, $template_mock);
205+
206+
$this->config->set('default_lang', 'en');
207+
$this->method_base->template('test');
208+
$this->assertEquals(['body' => 'test.txt'], $filenames);
209+
}
210+
211+
public function test_template_template_path()
212+
{
213+
global $phpbb_root_path;
214+
215+
$template_mock = $this->getMockBuilder(\phpbb\template\template::class)
216+
->disableOriginalConstructor()
217+
->getMock();
218+
$filenames = [];
219+
$template_mock->method('set_filenames')
220+
->willReturnCallback(function($filename_array) use (&$filenames, $template_mock) {
221+
$filenames = array_merge($filenames, $filename_array);
222+
223+
return $template_mock;
224+
});
225+
$template_mock->method('set_custom_style')
226+
->willReturnCallback(function($path_name, $paths) use($phpbb_root_path) {
227+
$this->assertEquals([['name' => 'en_email', 'ext_path' => 'language/en/email']], $path_name);
228+
$this->assertEquals([$phpbb_root_path . 'language/en/email'], $paths);
229+
});
230+
231+
$base_reflection = new \ReflectionClass($this->method_base);
232+
$template_reflection = $base_reflection->getProperty('template');
233+
$template_reflection->setValue($this->method_base, $template_mock);
234+
235+
$this->config->set('default_lang', 'en');
236+
$this->method_base->template('test', '', $phpbb_root_path . 'language/en/email');
237+
$this->assertEquals(['body' => 'test.txt'], $filenames);
238+
}
239+
240+
public function test_template_path_fallback()
241+
{
242+
global $phpbb_root_path;
243+
244+
$template_mock = $this->getMockBuilder(\phpbb\template\template::class)
245+
->disableOriginalConstructor()
246+
->getMock();
247+
$filenames = [];
248+
$template_mock->method('set_filenames')
249+
->willReturnCallback(function($filename_array) use (&$filenames, $template_mock) {
250+
$filenames = array_merge($filenames, $filename_array);
251+
252+
return $template_mock;
253+
});
254+
$template_mock->method('set_custom_style')
255+
->willReturnCallback(function($path_name, $paths) use($phpbb_root_path) {
256+
$this->assertEquals([
257+
['name' => 'de_email', 'ext_path' => 'language/de/email'],
258+
['name' => 'en_email', 'ext_path' => 'language/en/email'],
259+
], $path_name);
260+
$this->assertEquals([
261+
$phpbb_root_path . 'language/de/email',
262+
$phpbb_root_path . 'language/en/email'
263+
], $paths);
264+
});
265+
266+
$base_reflection = new \ReflectionClass($this->method_base);
267+
$template_reflection = $base_reflection->getProperty('template');
268+
$template_reflection->setValue($this->method_base, $template_mock);
269+
270+
$this->config->set('default_lang', 'de');
271+
$this->method_base->template('test', 'de');
272+
$this->assertEquals(['body' => 'test.txt'], $filenames);
273+
}
274+
}

tests/messenger/method_email_test.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,24 @@ public function test_send_break()
576576
$this->method_email->to('[email protected]');
577577
$this->method_email->subject('Test email');
578578
$this->method_email->template('test', 'en');
579+
$this->method_email->assign_block_vars('foo', ['bar' => 'baz']);
579580

580581
$this->method_email->send();
581582
}
582583

583-
public function test_send_no_queue()
584+
public function email_template_data(): array
585+
{
586+
return [
587+
['test'],
588+
['admin_send_email'],
589+
['topic_notify'],
590+
];
591+
}
592+
593+
/**
594+
* @dataProvider email_template_data
595+
*/
596+
public function test_send_no_queue($email_template)
584597
{
585598
global $phpbb_root_path;
586599

@@ -636,7 +649,7 @@ public function test_send_no_queue()
636649

637650
$this->method_email->to('[email protected]');
638651
$this->method_email->subject('Test email');
639-
$this->method_email->template('test', 'en');
652+
$this->method_email->template($email_template, 'en');
640653

641654
$this->assertTrue($this->method_email->send());
642655
$this->assertEquals(1, $sent_emails);

0 commit comments

Comments
 (0)