1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Xtools; |
4
|
|
|
|
5
|
|
|
class EditCounterTest extends \PHPUnit_Framework_TestCase |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Get counts of revisions: deleted, not-deleted, and total. |
10
|
|
|
*/ |
11
|
|
View Code Duplication |
public function testLiveAndDeletedEdits() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$editCounterRepo = $this->getMock(EditCounterRepository::class); |
14
|
|
|
$editCounterRepo->expects($this->once()) |
15
|
|
|
->method('getRevisionCounts') |
16
|
|
|
->willReturn([ |
17
|
|
|
'deleted' => 10, |
18
|
|
|
'live' => 100, |
19
|
|
|
]); |
20
|
|
|
|
21
|
|
|
$project = new Project('TestProject'); |
22
|
|
|
$user = new User('Testuser'); |
23
|
|
|
$editCounter = new EditCounter($project, $user); |
24
|
|
|
$editCounter->setRepository($editCounterRepo); |
25
|
|
|
|
26
|
|
|
$this->assertEquals(100, $editCounter->countLiveRevisions()); |
27
|
|
|
$this->assertEquals(10, $editCounter->countDeletedRevisions()); |
28
|
|
|
$this->assertEquals(110, $editCounter->countAllRevisions()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* A first and last date, and number of days between. |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function testDates() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$editCounterRepo = $this->getMock(EditCounterRepository::class); |
37
|
|
|
$editCounterRepo->expects($this->once())->method('getRevisionDates')->willReturn([ |
38
|
|
|
'first' => '20170510100000', |
39
|
|
|
'last' => '20170515150000', |
40
|
|
|
]); |
41
|
|
|
$project = new Project('TestProject'); |
42
|
|
|
$user = new User('Testuser1'); |
43
|
|
|
$editCounter = new EditCounter($project, $user); |
44
|
|
|
$editCounter->setRepository($editCounterRepo); |
45
|
|
|
$this->assertEquals( |
46
|
|
|
new \DateTime('2017-05-10 10:00'), |
47
|
|
|
$editCounter->datetimeFirstRevision() |
48
|
|
|
); |
49
|
|
|
$this->assertEquals( |
50
|
|
|
new \DateTime('2017-05-15 15:00'), |
51
|
|
|
$editCounter->datetimeLastRevision() |
52
|
|
|
); |
53
|
|
|
$this->assertEquals(5, $editCounter->getDays()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Only one edit means the dates will be the same. |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testDatesWithOneRevision() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$editCounterRepo = $this->getMock(EditCounterRepository::class); |
62
|
|
|
$editCounterRepo->expects($this->once()) |
63
|
|
|
->method('getRevisionDates') |
64
|
|
|
->willReturn([ |
65
|
|
|
'first' => '20170510110000', |
66
|
|
|
'last' => '20170510110000', |
67
|
|
|
]); |
68
|
|
|
$project = new Project('TestProject'); |
69
|
|
|
$user = new User('Testuser1'); |
70
|
|
|
$editCounter = new EditCounter($project, $user); |
71
|
|
|
$editCounter->setRepository($editCounterRepo); |
72
|
|
|
$this->assertEquals( |
73
|
|
|
new \DateTime('2017-05-10 11:00'), |
74
|
|
|
$editCounter->datetimeFirstRevision() |
75
|
|
|
); |
76
|
|
|
$this->assertEquals( |
77
|
|
|
new \DateTime('2017-05-10 11:00'), |
78
|
|
|
$editCounter->datetimeLastRevision() |
79
|
|
|
); |
80
|
|
|
$this->assertEquals(1, $editCounter->getDays()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testPageCounts() |
84
|
|
|
{ |
85
|
|
|
$editCounterRepo = $this->getMock(EditCounterRepository::class); |
86
|
|
|
$editCounterRepo->expects($this->once()) |
87
|
|
|
->method('getPageCounts') |
88
|
|
|
->willReturn([ |
89
|
|
|
'edited-live' => '3', |
90
|
|
|
'edited-deleted' => '1', |
91
|
|
|
'created-live' => '6', |
92
|
|
|
'created-deleted' => '2', |
93
|
|
|
]); |
94
|
|
|
$project = new Project('TestProject'); |
95
|
|
|
$user = new User('Testuser1'); |
96
|
|
|
$editCounter = new EditCounter($project, $user); |
97
|
|
|
$editCounter->setRepository($editCounterRepo); |
98
|
|
|
|
99
|
|
|
$this->assertEquals(3, $editCounter->countLivePagesEdited()); |
100
|
|
|
$this->assertEquals(1, $editCounter->countDeletedPagesEdited()); |
101
|
|
|
$this->assertEquals(4, $editCounter->countAllPagesEdited()); |
102
|
|
|
|
103
|
|
|
$this->assertEquals(6, $editCounter->countCreatedPagesLive()); |
104
|
|
|
$this->assertEquals(2, $editCounter->countPagesCreatedDeleted()); |
105
|
|
|
$this->assertEquals(8, $editCounter->countPagesCreated()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testNamespaceTotals() |
109
|
|
|
{ |
110
|
|
|
$namespaceTotals = [ |
111
|
|
|
// Namespace IDs => Edit counts |
112
|
|
|
'1' => '3', |
113
|
|
|
'2' => '6', |
114
|
|
|
'3' => '9', |
115
|
|
|
'4' => '12', |
116
|
|
|
]; |
117
|
|
|
$editCounterRepo = $this->getMock(EditCounterRepository::class); |
118
|
|
|
$editCounterRepo->expects($this->once()) |
119
|
|
|
->method('getNamespaceTotals') |
120
|
|
|
->willReturn($namespaceTotals); |
121
|
|
|
$project = new Project('TestProject'); |
122
|
|
|
$user = new User('Testuser1'); |
123
|
|
|
$editCounter = new EditCounter($project, $user); |
124
|
|
|
$editCounter->setRepository($editCounterRepo); |
125
|
|
|
|
126
|
|
|
$this->assertEquals($namespaceTotals, $editCounter->namespaceTotals()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get all global edit counts, or just the top N, or the overall grand total. |
131
|
|
|
*/ |
132
|
|
|
public function testGlobalEditCounts() |
133
|
|
|
{ |
134
|
|
|
$wiki1 = new Project('wiki1'); |
135
|
|
|
$wiki2 = new Project('wiki2'); |
136
|
|
|
$editCounts = [ |
137
|
|
|
['project' => new Project('wiki0'), 'total' => 30], |
138
|
|
|
['project' => $wiki1, 'total' => 50], |
139
|
|
|
['project' => $wiki2, 'total' => 40], |
140
|
|
|
['project' => new Project('wiki3'), 'total' => 20], |
141
|
|
|
['project' => new Project('wiki4'), 'total' => 10], |
142
|
|
|
['project' => new Project('wiki5'), 'total' => 35], |
143
|
|
|
]; |
144
|
|
|
$editCounterRepo = $this->getMock(EditCounterRepository::class); |
145
|
|
|
$editCounterRepo->expects($this->once()) |
146
|
|
|
->method('globalEditCounts') |
147
|
|
|
->willReturn($editCounts); |
148
|
|
|
$user = new User('Testuser1'); |
149
|
|
|
$editCounter = new EditCounter($wiki1, $user); |
150
|
|
|
$editCounter->setRepository($editCounterRepo); |
151
|
|
|
|
152
|
|
|
// Get the top 2. |
153
|
|
|
$this->assertEquals( |
154
|
|
|
[ |
155
|
|
|
['project' => $wiki1, 'total' => 50], |
156
|
|
|
['project' => $wiki2, 'total' => 40], |
157
|
|
|
], |
158
|
|
|
$editCounter->globalEditCountsTopN(2) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
// Grand total. |
162
|
|
|
$this->assertEquals(185, $editCounter->globalEditCount()); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
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.