Completed
Pull Request — master (#61)
by Bryan
05:17
created

topicsolved_test   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 181
Duplicated Lines 26.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 48
loc 181
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the phpBB Topic Solved extension package.
4
 *
5
 * @copyright (c) Bryan Petty
6
 * @license GNU General Public License, version 2 (GPL-2.0)
7
 *
8
 * @package tierra/topicsolved/tests
9
 */
10
namespace tierra\topicsolved\tests;
11
12
use tierra\topicsolved\topicsolved;
13
14
/**
15
 * Test topicsolved class.
16
 *
17
 * @package tierra/topicsolved/tests
18
 */
19
class topicsolved_test extends \phpbb_database_test_case
20
{
21
	/** @var \tierra\topicsolved\topicsolved */
22
	protected $topicsolved;
23
24
	/** @var \phpbb\user|\PHPUnit_Framework_MockObject_MockObject */
25
	protected $user;
26
27
	/** @var \phpbb\auth\auth|\PHPUnit_Framework_MockObject_MockObject */
28
	protected $auth;
29
30
	/** @var \phpbb\event\dispatcher|\PHPUnit_Framework_MockObject_MockObject */
31
	protected $dispatcher;
32
33
	/**
34
	 * Configure the test environment.
35
	 *
36
	 * @return void
37
	 */
38
	public function setUp()
39
	{
40
		global $phpbb_root_path, $phpEx;
41
42
		parent::setUp();
43
44
		$this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime'));
45
		$this->auth = $this->getMock('\phpbb\auth\auth');
46
		$this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher')
47
			->disableOriginalConstructor()->getMock();
48
49
		$this->topicsolved = new topicsolved(
50
			$this->new_dbal(), $this->user, $this->auth, $this->dispatcher, $phpbb_root_path, $phpEx
51
		);
52
	}
53
54
	/**
55
	 * Load required fixtures.
56
	 *
57
	 * @return mixed
58
	 */
59
	public function getDataSet()
60
	{
61
		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/topicsolved.xml');
62
	}
63
64
	/**
65
	 * Ensure regular user can only lock own posts.
66
	 */
67
	public function test_user_can_only_lock_own_post()
68
	{
69
		$this->auth->expects($this->exactly(2))->method('acl_get')
70
			->withConsecutive(array('m_lock', 1), array('f_user_lock', 1))
71
			->willReturn(false);
72
73
		$this->assertFalse($this->topicsolved->user_can_lock_post(1));
74
	}
75
76
	/**
77
	 * Ensure regular user can lock their own posts.
78
	 */
79
	public function test_user_can_lock_own_post()
80
	{
81
		$this->auth->expects($this->exactly(2))->method('acl_get')
82
			->willReturnMap(array(
83
				array('m_lock', 1, false),
84
				array('f_user_lock', 1, true))
85
			);
86
87
		$this->assertTrue($this->topicsolved->user_can_lock_post(1));
88
	}
89
90
	/**
91
	 * Ensure moderators can lock posts.
92
	 */
93
	public function test_moderator_can_lock_post()
94
	{
95
		$this->auth->expects($this->once())->method('acl_get')
96
			->with('m_lock', 1)->willReturn(true);
97
98
		$this->assertTrue($this->topicsolved->user_can_lock_post(1));
99
	}
100
101
	/**
102
	 * Ensure tierra.topicsolved.mark_solved_after event works.
103
	 */
104
	public function test_mark_solved_event()
105
	{
106
		$topic_data = array(
107
			'forum_lock_solved' => '1',
108
			'forum_id' => 2,
109
			'topic_id' => 1
110
		);
111
112
		$this->auth->expects($this->once())->method('acl_get')
113
			->with('m_lock', 2)->willReturn(true);
114
		$this->dispatcher->expects($this->once())->method('trigger_event')
115
			->with(
116
				'tierra.topicsolved.mark_solved_after',
117
				array(
118
					'topic_data' => $topic_data,
119
					'column_data' => array(
120
						'topic_solved' => 5,
121
						'topic_status' => ITEM_LOCKED
122
					)
123
				)
124
			)->willReturn(array());
125
126
		$this->topicsolved->mark_solved($topic_data, 5);
127
	}
128
129
	/**
130
	 * Ensure tierra.topicsolved.mark_unsolved_after event works.
131
	 */
132
	public function test_mark_unsolved_event()
133
	{
134
		$topic_data = array(
135
			'forum_lock_solved' => '1',
136
			'forum_id' => 2,
137
			'topic_id' => 1
138
		);
139
140
		$this->auth->expects($this->once())->method('acl_get')
141
			->with('m_lock', 2)->willReturn(true);
142
		$this->dispatcher->expects($this->once())->method('trigger_event')
143
			->with(
144
				'tierra.topicsolved.mark_unsolved_after',
145
				array(
146
					'topic_data' => $topic_data,
147
					'column_data' => array(
148
						'topic_solved' => 0,
149
						'topic_status' => ITEM_UNLOCKED
150
					)
151
				)
152
			)->willReturn(array());
153
154
		$this->topicsolved->mark_unsolved($topic_data);
155
	}
156
157
	/**
158
	 * Data set for test_image
159
	 *
160
	 * @return array
161
	 */
162
	public function image_test_data()
163
	{
164
		return array(
165
			array('head', 'TOPIC_SOLVED', 'test.php',
166
				'<a href="test.php" title="TOPIC_SOLVED"><img src="" /></a>'),
167
			array('list', 'T<>""S', '/anchor/test.php',
168
				'<a href="/anchor/test.php" title="T&lt;&gt;&quot;&quot;S"><img src="" /></a>'),
169
			array('post', 'TOPIC_SOLVED', '//example.com/test.php',
170
				'<a href="//example.com/test.php" title="TOPIC_SOLVED"><img src="" /></a>'),
171
			array('head', '', 'http://example.com/',
172
				'<a href="http://example.com/"><img src="" /></a>'),
173
			array('list', 'TOPIC_SOLVED', 'https://example.com',
174
				'<a href="https://example.com" title="TOPIC_SOLVED"><img src="" /></a>'),
175
			array('post', '', 'test.php?f=1&s=two%20words',
176
				'<a href="test.php?f=1&amp;s=two%20words"><img src="" /></a>'),
177
		);
178
	}
179
180
	/**
181
	 * Ensure proper image markup is being generated.
182
	 *
183
	 * @param string $type One of "head", "list", or "post".
184
	 * @param string $alt Language code for title and alternative text.
185
	 * @param string $url Optional link to solved post.
186
	 * @param string $expected Result expected from image() call.
187
	 *
188
	 * @dataProvider image_test_data
189
	 */
190
	public function test_image($type, $alt, $url, $expected)
191
	{
192
		$this->user->expects($this->any())->method('img')
193
			->willReturn('<img src="" />');
194
		$this->user->expects($this->any())->method('lang')
195
			->will($this->returnArgument(0));
196
197
		$this->assertEquals($expected, $this->topicsolved->image($type, $alt, $url));
198
	}
199
200
	/**
201
	 * Data set for test_icon
202
	 *
203
	 * @return array
204
	 */
205
	public function icon_test_data()
206
	{
207
		return array(
208
			array('', 'TOPIC_SOLVED', 'test.php',
209
				'<a href="test.php" title="TOPIC_SOLVED"><img src="" /></a>'),
210
			array('FF0000', 'T<>""S', '/anchor/test.php',
211
				'<a href="/anchor/test.php" title="T&lt;&gt;&quot;&quot;S"><img src="" /></a>'),
212
			array('00FF00', 'TOPIC_SOLVED', '//example.com/test.php',
213
				'<a href="//example.com/test.php" title="TOPIC_SOLVED"><img src="" /></a>'),
214
			array('', '', 'http://example.com/',
215
				'<a href="http://example.com/"><img src="" /></a>'),
216
			array('post', '', 'test.php?f=1&s=two%20words',
217
				'<a href="test.php?f=1&amp;s=two%20words"><img src="" /></a>'),
218
		);
219
	}
220
221
	/**
222
	 * Ensure proper icon markup is being generated.
223
	 *
224
	 * @param string $color Color to use for the icon.
225
	 * @param string $alt Language code for title and alternative text.
226
	 * @param string $url Optional link to solved post.
227
	 * @param string $expected Result expected from image() call.
228
	 *
229
	 * @dataProvider icon_test_data
230
	 */
231
	public function test_icon($color, $alt, $url, $expected)
232
	{
233
		$this->user->expects($this->any())->method('lang')
234
			->will($this->returnArgument(0));
235
236
		$this->dispatcher->expects($this->once())->method('trigger_event')
237
			->with(
238
				'tierra.topicsolved.render_icon',
239
				array(
240
					'alt' => $alt,
241
					'classes' => 'fa fa-check-circle fa-fw'
242
					'color' => empty($color) ? '00BF00' : $color,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'
Loading history...
243
					'url' => $url
244
				)
245
			)->willReturn(array());
246
247
		$this->assertEquals($expected, $this->topicsolved->icon($color, $alt, $url));
248
	}
249
}
250