-
Notifications
You must be signed in to change notification settings - Fork 14
/
controller_test.php
185 lines (159 loc) · 5.65 KB
/
controller_test.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
*
* Topic Preview
*
* @copyright (c) 2016 Matt Friedman
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace vse\topicpreview\tests\acp;
require_once __DIR__ . '/../../../../../includes/functions_acp.php';
class controller_test extends \phpbb_database_test_case
{
protected static function setup_extensions()
{
return array('vse/topicpreview');
}
public function getDataSet()
{
return $this->createXMLDataSet(__DIR__ . '/fixtures/topic_preview.xml');
}
/** @var \phpbb\config\config */
protected $config;
/** @var \vse\topicpreview\controller\acp_controller */
protected $controller;
/** @var \phpbb\language\language */
protected $language;
/** @var \PHPUnit_Framework_MockObject_MockObject|\phpbb\request\request */
protected $request;
/** @var \vse\topicpreview\core\settings */
protected $settings;
/** @var \PHPUnit_Framework_MockObject_MockObject|\phpbb\template\template */
protected $template;
/** @var \phpbb\user */
protected $user;
public function setUp()
{
parent::setUp();
global $config, $phpbb_extension_manager, $phpbb_dispatcher, $request, $template, $phpbb_root_path, $phpEx;
$cache = new \phpbb_mock_cache;
$config = $this->config = new \phpbb\config\config(array());
$db = $this->new_dbal();
$phpbb_extension_manager = new \phpbb_mock_extension_manager($phpbb_root_path);
$phpbb_dispatcher = new \phpbb_mock_event_dispatcher();
$request = $this->request = $this->getMockBuilder('\phpbb\request\request')
->disableOriginalConstructor()
->getMock();
$template = $this->template = $this->getMockBuilder('\phpbb\template\template')
->getMock();
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
$this->language = new \phpbb\language\language($lang_loader);
$this->user = new \phpbb\user($this->language, '\phpbb\datetime');
$this->settings = new \vse\topicpreview\core\settings(
$cache,
$this->config,
$db,
$phpbb_extension_manager,
$this->request,
$phpbb_root_path
);
$this->controller = new \vse\topicpreview\controller\acp_controller(
$this->language,
$this->request,
$this->settings,
$this->template
);
}
/**
* Test the main module displays expected data
*/
public function test_main_display()
{
$this->template->expects($this->at(1))
->method('assign_vars')
->with(array(
'TOPIC_PREVIEW_LIMIT' => $this->config['topic_preview_limit'],
'TOPIC_PREVIEW_WIDTH' => $this->config['topic_preview_width'],
'TOPIC_PREVIEW_DELAY' => $this->config['topic_preview_delay'],
'TOPIC_PREVIEW_DRIFT' => $this->config['topic_preview_drift'],
'S_TOPIC_PREVIEW_AVATARS' => $this->config['topic_preview_avatars'],
'S_TOPIC_PREVIEW_LAST_POST' => $this->config['topic_preview_last_post'],
'TOPIC_PREVIEW_STRIP' => $this->config['topic_preview_strip_bbcodes'],
'TOPIC_PREVIEW_STYLES' => $this->invokeMethod($this->settings, 'get_styles'),
'TOPIC_PREVIEW_THEMES' => $this->invokeMethod($this->settings, 'get_themes'),
'TOPIC_PREVIEW_DEFAULT' => \vse\topicpreview\core\settings::DEFAULT_THEME,
'TOPIC_PREVIEW_NO_THEME' => \vse\topicpreview\core\settings::NO_THEME,
'U_ACTION' => 'u_action',
));
$this->controller->set_u_action('u_action')->handle();
}
/**
* Test the main module accepts submitted data
*
* @param $data_map
* @param $error
* @param $expected
* @dataProvider main_submit_test_data
*/
public function test_main_submit($data_map, $error, $expected)
{
// Set up some test configurations
$this->user->data['user_id'] = 2;
$this->config['form_token_lifetime'] = -1;
$this->request->expects($this->any())
->method('is_set_post')
->will($this->returnValueMap(array(
array('submit', true),
array('form_token', true),
array('creation_time', true),
)));
$this->request->expects($this->any())
->method('variable')
->will($this->returnValueMap($data_map));
$this->setExpectedTriggerError($error, $this->language->lang($expected));
$this->controller->handle();
}
public function main_submit_test_data()
{
return array(
array(
array(
array('topic_preview_limit', 0, false, \phpbb\request\request_interface::REQUEST, 500),
array('topic_preview_width', 0, false, \phpbb\request\request_interface::REQUEST, 400),
array('topic_preview_delay', 0, false, \phpbb\request\request_interface::REQUEST, 300),
array('topic_preview_drift', 0, false, \phpbb\request\request_interface::REQUEST, 200),
array('topic_preview_avatars', 0, false, \phpbb\request\request_interface::REQUEST, 1),
array('topic_preview_last_post', 0, false, \phpbb\request\request_interface::REQUEST, 1),
array('topic_preview_strip_bbcodes', '', false, \phpbb\request\request_interface::REQUEST, 'foo'),
// hidden fields used for check_form_key
array('form_token', '', false, \phpbb\request\request_interface::REQUEST, sha1(0 . 'acp_topic_preview')),
array('creation_time', 0, false, \phpbb\request\request_interface::REQUEST, 0),
),
E_USER_NOTICE,
'CONFIG_UPDATED',
),
array(
array(),
E_USER_WARNING,
'FORM_INVALID',
),
);
}
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}