Completed
Push — master ( 3f5086...28f5b7 )
by mw
02:06
created

testIsApprovedUpdate_TrueNoApprovedRev()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SMW\ApprovedRevs\Tests;
4
5
use SMW\ApprovedRevs\ApprovedRevsHandler;
6
7
/**
8
 * @covers \SMW\ApprovedRevs\ApprovedRevsHandler
9
 * @group semantic-approved-revs
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class ApprovedRevsHandlerTest extends \PHPUnit_Framework_TestCase {
17
18
	private $approvedRevsFacade;
19
20
	protected function setUp() {
21
22
		$this->approvedRevsFacade = $this->getMockBuilder( '\SMW\ApprovedRevs\ApprovedRevsFacade' )
23
			->disableOriginalConstructor()
24
			->getMock();
25
	}
26
27
	public function testCanConstruct() {
28
29
		$this->assertInstanceOf(
30
			ApprovedRevsHandler::class,
31
			new ApprovedRevsHandler( $this->approvedRevsFacade )
32
		);
33
	}
34
35
	public function testIsApprovedUpdate_True() {
36
37
		$this->approvedRevsFacade->expects( $this->once() )
38
			->method( 'hasApprovedRevision' )
39
			->will( $this->returnValue( true ) );
40
41
		$this->approvedRevsFacade->expects( $this->once() )
42
			->method( 'getApprovedRevID' )
43
			->will( $this->returnValue( 42 ) );
44
45
		$title = $this->getMockBuilder( '\Title' )
46
			->disableOriginalConstructor()
47
			->getMock();
48
49
		$instance = new ApprovedRevsHandler(
50
			$this->approvedRevsFacade
51
		);
52
53
		$this->assertTrue(
54
			$instance->isApprovedUpdate( $title, 42 )
55
		);
56
	}
57
58
	public function testIsApprovedUpdate_True_WhenNoApprovedRevIsAvailable() {
59
60
		$this->approvedRevsFacade->expects( $this->once() )
61
			->method( 'hasApprovedRevision' )
62
			->will( $this->returnValue( false ) );
63
64
		$title = $this->getMockBuilder( '\Title' )
65
			->disableOriginalConstructor()
66
			->getMock();
67
68
		$instance = new ApprovedRevsHandler(
69
			$this->approvedRevsFacade
70
		);
71
72
		$this->assertTrue(
73
			$instance->isApprovedUpdate( $title, 42 )
74
		);
75
	}
76
77
	public function testIsApprovedUpdate_False() {
78
79
		$this->approvedRevsFacade->expects( $this->once() )
80
			->method( 'hasApprovedRevision' )
81
			->will( $this->returnValue( true ) );
82
83
		$this->approvedRevsFacade->expects( $this->once() )
84
			->method( 'getApprovedRevID' )
85
			->will( $this->returnValue( 42 ) );
86
87
		$title = $this->getMockBuilder( '\Title' )
88
			->disableOriginalConstructor()
89
			->getMock();
90
91
		$instance = new ApprovedRevsHandler(
92
			$this->approvedRevsFacade
93
		);
94
95
		$this->assertFalse(
96
			$instance->isApprovedUpdate( $title, 1001 )
97
		);
98
	}
99
100
	public function testIsApprovedUpdate_TrueNoApprovedRev() {
101
102
		$this->approvedRevsFacade->expects( $this->once() )
103
			->method( 'hasApprovedRevision' )
104
			->will( $this->returnValue( true ) );
105
106
		$this->approvedRevsFacade->expects( $this->once() )
107
			->method( 'getApprovedRevID' )
108
			->will( $this->returnValue( null ) );
109
110
		$title = $this->getMockBuilder( '\Title' )
111
			->disableOriginalConstructor()
112
			->getMock();
113
114
		$instance = new ApprovedRevsHandler(
115
			$this->approvedRevsFacade
116
		);
117
118
		$this->assertTrue(
119
			$instance->isApprovedUpdate( $title, 1001 )
120
		);
121
	}
122
123
	public function testDoChangeRevision() {
124
125
		$this->approvedRevsFacade->expects( $this->once() )
126
			->method( 'getApprovedRevID' )
127
			->will( $this->returnValue( 42 ) );
128
129
		$title = $this->getMockBuilder( '\Title' )
130
			->disableOriginalConstructor()
131
			->getMock();
132
133
		$instance = new ApprovedRevsHandler(
134
			$this->approvedRevsFacade
135
		);
136
137
		$rev = null;
138
139
		$instance->doChangeRevision( $title, $rev );
140
	}
141
142
	public function testDoChangeRevisionID() {
143
144
		$this->approvedRevsFacade->expects( $this->once() )
145
			->method( 'getApprovedRevID' )
146
			->will( $this->returnValue( 42 ) );
147
148
		$title = $this->getMockBuilder( '\Title' )
149
			->disableOriginalConstructor()
150
			->getMock();
151
152
		$instance = new ApprovedRevsHandler(
153
			$this->approvedRevsFacade
154
		);
155
156
		$rev = null;
157
158
		$instance->doChangeRevisionID( $title, $rev );
159
	}
160
161
	public function testDoChangeFile_NoSha1() {
162
163
		$this->approvedRevsFacade->expects( $this->once() )
164
			->method( 'getApprovedFileInfo' )
165
			->will( $this->returnValue( [ '', false ] ) );
166
167
		$title = $this->getMockBuilder( '\Title' )
168
			->disableOriginalConstructor()
169
			->getMock();
170
171
		$instance = new ApprovedRevsHandler(
172
			$this->approvedRevsFacade
173
		);
174
175
		$file = null;
176
177
		$this->assertTrue(
178
			$instance->doChangeFile( $title, $file )
179
		);
180
	}
181
182
	public function testDoChangeFile_FromLocalRepo() {
183
184
		$f = null;
185
186
		$this->approvedRevsFacade->expects( $this->once() )
187
			->method( 'getApprovedFileInfo' )
188
			->will( $this->returnValue( [ '1552165749', '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12' ] ) );
189
190
		$file = $this->getMockBuilder( '\File' )
191
			->disableOriginalConstructor()
192
			->getMock();
193
194
		$title = $this->getMockBuilder( '\Title' )
195
			->disableOriginalConstructor()
196
			->getMock();
197
198
		$db = $this->getMockBuilder( '\DatabaseBase' )
199
			->disableOriginalConstructor()
200
			->getMock();
201
202
		$localRepo = $this->getMockBuilder( '\LocalRepo' )
203
			->disableOriginalConstructor()
204
			->getMock();
205
206
		$localRepo->expects( $this->once() )
207
			->method( 'getReplicaDB' )
208
			->will( $this->returnValue( $db ) );
209
210
		$localRepo->expects( $this->once() )
211
			->method( 'findBySha1' )
212
			->with( $this->equalTo( '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12' ) )
213
			->will( $this->returnValue( [ $file ] ) );
214
215
		$repoGroup = $this->getMockBuilder( '\RepoGroup' )
216
			->disableOriginalConstructor()
217
			->getMock();
218
219
		$repoGroup->expects( $this->once() )
220
			->method( 'getLocalRepo' )
221
			->will( $this->returnValue( $localRepo ) );
222
223
		$instance = new ApprovedRevsHandler(
224
			$this->approvedRevsFacade,
225
			$repoGroup
226
		);
227
228
		$instance->doChangeFile( $title, $f );
229
230
		$this->assertSame(
231
			'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12',
232
			$f->file_sha1
233
		);
234
	}
235
236
}
237