forked from tierra/topicsolved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopicsolved_test.php
249 lines (220 loc) · 7.07 KB
/
topicsolved_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* This file is part of the phpBB Topic Solved extension package.
*
* @copyright (c) Bryan Petty
* @license GNU General Public License, version 2 (GPL-2.0)
*
* @package tierra/topicsolved/tests
*/
namespace tierra\topicsolved\tests;
use tierra\topicsolved\topicsolved;
/**
* Test topicsolved class.
*
* @package tierra/topicsolved/tests
*/
class topicsolved_test extends \phpbb_database_test_case
{
/** @var \tierra\topicsolved\topicsolved */
protected $topicsolved;
/** @var \phpbb\user|\PHPUnit_Framework_MockObject_MockObject */
protected $user;
/** @var \phpbb\auth\auth|\PHPUnit_Framework_MockObject_MockObject */
protected $auth;
/** @var \phpbb\event\dispatcher|\PHPUnit_Framework_MockObject_MockObject */
protected $dispatcher;
/**
* Configure the test environment.
*
* @return void
*/
public function setUp()
{
global $phpbb_root_path, $phpEx;
parent::setUp();
$this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime'));
$this->auth = $this->getMock('\phpbb\auth\auth');
$this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher')
->disableOriginalConstructor()->getMock();
$this->topicsolved = new topicsolved(
$this->new_dbal(), $this->user, $this->auth, $this->dispatcher, $phpbb_root_path, $phpEx
);
}
/**
* Load required fixtures.
*
* @return mixed
*/
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/topicsolved.xml');
}
/**
* Ensure regular user can only lock own posts.
*/
public function test_user_can_only_lock_own_post()
{
$this->auth->expects($this->exactly(2))->method('acl_get')
->withConsecutive(array('m_lock', 1), array('f_user_lock', 1))
->willReturn(false);
$this->assertFalse($this->topicsolved->user_can_lock_post(1));
}
/**
* Ensure regular user can lock their own posts.
*/
public function test_user_can_lock_own_post()
{
$this->auth->expects($this->exactly(2))->method('acl_get')
->willReturnMap(array(
array('m_lock', 1, false),
array('f_user_lock', 1, true))
);
$this->assertTrue($this->topicsolved->user_can_lock_post(1));
}
/**
* Ensure moderators can lock posts.
*/
public function test_moderator_can_lock_post()
{
$this->auth->expects($this->once())->method('acl_get')
->with('m_lock', 1)->willReturn(true);
$this->assertTrue($this->topicsolved->user_can_lock_post(1));
}
/**
* Ensure tierra.topicsolved.mark_solved_after event works.
*/
public function test_mark_solved_event()
{
$topic_data = array(
'forum_lock_solved' => '1',
'forum_id' => 2,
'topic_id' => 1
);
$this->auth->expects($this->once())->method('acl_get')
->with('m_lock', 2)->willReturn(true);
$this->dispatcher->expects($this->once())->method('trigger_event')
->with(
'tierra.topicsolved.mark_solved_after',
array(
'topic_data' => $topic_data,
'column_data' => array(
'topic_solved' => 5,
'topic_status' => ITEM_LOCKED
)
)
)->willReturn(array());
$this->topicsolved->mark_solved($topic_data, 5);
}
/**
* Ensure tierra.topicsolved.mark_unsolved_after event works.
*/
public function test_mark_unsolved_event()
{
$topic_data = array(
'forum_lock_solved' => '1',
'forum_id' => 2,
'topic_id' => 1
);
$this->auth->expects($this->once())->method('acl_get')
->with('m_lock', 2)->willReturn(true);
$this->dispatcher->expects($this->once())->method('trigger_event')
->with(
'tierra.topicsolved.mark_unsolved_after',
array(
'topic_data' => $topic_data,
'column_data' => array(
'topic_solved' => 0,
'topic_status' => ITEM_UNLOCKED
)
)
)->willReturn(array());
$this->topicsolved->mark_unsolved($topic_data);
}
/**
* Data set for test_image
*
* @return array
*/
public function image_test_data()
{
return array(
array('head', 'TOPIC_SOLVED', 'test.php',
'<a href="test.php" title="TOPIC_SOLVED"><img src="" /></a>'),
array('list', 'T<>""S', '/anchor/test.php',
'<a href="/anchor/test.php" title="T<>""S"><img src="" /></a>'),
array('post', 'TOPIC_SOLVED', '//example.com/test.php',
'<a href="//example.com/test.php" title="TOPIC_SOLVED"><img src="" /></a>'),
array('head', '', 'http://example.com/',
'<a href="http://example.com/"><img src="" /></a>'),
array('list', 'TOPIC_SOLVED', 'https://example.com',
'<a href="https://example.com" title="TOPIC_SOLVED"><img src="" /></a>'),
array('post', '', 'test.php?f=1&s=two%20words',
'<a href="test.php?f=1&s=two%20words"><img src="" /></a>'),
);
}
/**
* Ensure proper image markup is being generated.
*
* @param string $type One of "head", "list", or "post".
* @param string $alt Language code for title and alternative text.
* @param string $url Optional link to solved post.
* @param string $expected Result expected from image() call.
*
* @dataProvider image_test_data
*/
public function test_image($type, $alt, $url, $expected)
{
$this->user->expects($this->any())->method('img')
->willReturn('<img src="" />');
$this->user->expects($this->any())->method('lang')
->will($this->returnArgument(0));
$this->assertEquals($expected, $this->topicsolved->image($type, $alt, $url));
}
/**
* Data set for test_icon
*
* @return array
*/
public function icon_test_data()
{
return array(
array('', 'TOPIC_SOLVED', 'test.php',
'<a href="test.php" title="TOPIC_SOLVED"><i class="fa fa-check-circle fa-fw" style="color: #00BF00" aria-hidden="true"></i></a>'),
array('FF0000', 'T<>""S', '/anchor/test.php',
'<a href="/anchor/test.php" title="T<>""S"><i class="fa fa-check-circle fa-fw" style="color: #FF0000" aria-hidden="true"></i></a>'),
array('00FF00', 'TOPIC_SOLVED', '//example.com/test.php',
'<a href="//example.com/test.php" title="TOPIC_SOLVED"><i class="fa fa-check-circle fa-fw" style="color: #00FF00" aria-hidden="true"></i></a>'),
array('', '', 'http://example.com/',
'<a href="http://example.com/"><i class="fa fa-check-circle fa-fw" style="color: #00BF00" aria-hidden="true"></i></a>'),
array('', '', 'test.php?f=1&s=two%20words',
'<a href="test.php?f=1&s=two%20words"><i class="fa fa-check-circle fa-fw" style="color: #00BF00" aria-hidden="true"></i></a>'),
);
}
/**
* Ensure proper icon markup is being generated.
*
* @param string $color Color to use for the icon.
* @param string $alt Language code for title and alternative text.
* @param string $url Optional link to solved post.
* @param string $expected Result expected from image() call.
*
* @dataProvider icon_test_data
*/
public function test_icon($color, $alt, $url, $expected)
{
$this->user->expects($this->any())->method('lang')
->will($this->returnArgument(0));
$this->dispatcher->expects($this->once())->method('trigger_event')
->with(
'tierra.topicsolved.render_icon',
array(
'alt' => $alt,
'classes' => 'fa fa-check-circle fa-fw',
'color' => empty($color) ? '00BF00' : $color,
'url' => $url
)
)->willReturn(array());
$this->assertEquals($expected, $this->topicsolved->icon($color, $alt, $url));
}
}