Completed
Push — master ( faa58d...8e4bc7 )
by Bryan
03:57
created

topicsolved_test   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 231
Duplicated Lines 20.78 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataSet() 0 4 1
A test_user_can_only_lock_own_post() 0 8 1
A test_user_can_lock_own_post() 0 10 1
A test_moderator_can_lock_post() 0 7 1
A image_test_data() 0 17 1
A test_image() 0 9 1
A setUp() 0 15 1
B test_mark_solved_event() 24 24 1
B test_mark_unsolved_event() 24 24 1
A icon_test_data() 0 15 1
A test_icon() 0 18 2

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 View Code Duplication
	public function test_mark_solved_event()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
	public function test_mark_unsolved_event()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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"><i class="fa fa-check-circle fa-fw" style="color: #00BF00" aria-hidden="true"></i></a>'),
210
			array('FF0000', 'T<>""S', '/anchor/test.php',
211
				'<a href="/anchor/test.php" title="T&lt;&gt;&quot;&quot;S"><i class="fa fa-check-circle fa-fw" style="color: #FF0000" aria-hidden="true"></i></a>'),
212
			array('00FF00', 'TOPIC_SOLVED', '//example.com/test.php',
213
				'<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>'),
214
			array('', '', 'http://example.com/',
215
				'<a href="http://example.com/"><i class="fa fa-check-circle fa-fw" style="color: #00BF00" aria-hidden="true"></i></a>'),
216
			array('', '', 'test.php?f=1&s=two%20words',
217
				'<a href="test.php?f=1&amp;s=two%20words"><i class="fa fa-check-circle fa-fw" style="color: #00BF00" aria-hidden="true"></i></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,
243
					'url' => $url
244
				)
245
			)->willReturn(array());
246
247
		$this->assertEquals($expected, $this->topicsolved->icon($color, $alt, $url));
248
	}
249
}
250