Completed
Push — master ( be0721...cced22 )
by Matthew
02:48
created

ProjectTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 163
rs 10
c 3
b 0
f 1
1
<?php
2
/**
3
 * This file contains only the ProjectTest class.
4
 */
5
6
namespace Tests\Xtools;
7
8
use PHPUnit_Framework_TestCase;
9
use Xtools\Project;
10
use Xtools\ProjectRepository;
11
use Xtools\User;
12
13
/**
14
 * Tests for the Project class.
15
 */
16
class ProjectTest extends PHPUnit_Framework_TestCase
17
{
18
19
    /**
20
     * A project has its own domain name, database name, URL, script path, and article path.
21
     */
22
    public function testBasicMetadata()
23
    {
24
        $projectRepo = $this->getMock(ProjectRepository::class);
25
        $projectRepo->expects($this->once())
26
            ->method('getOne')
27
            ->willReturn([
28
                'url' => 'https://test.example.org',
29
                'dbName' => 'test_wiki',
30
                'lang' => 'en',
31
            ]);
32
        $projectRepo->expects($this->once())
33
            ->method('getMetadata')
34
            ->willReturn([
35
                'general' => [
36
                    'articlePath' => '/test_wiki/$1',
37
                    'scriptPath' => '/test_w',
38
                ],
39
            ]);
40
41
        $project = new Project('testWiki');
42
        $project->setRepository($projectRepo);
43
        $this->assertEquals('test.example.org', $project->getDomain());
44
        $this->assertEquals('test_wiki', $project->getDatabaseName());
45
        $this->assertEquals('https://test.example.org/', $project->getUrl());
46
        $this->assertEquals('en', $project->getLang());
47
        $this->assertEquals('/test_w', $project->getScriptPath());
48
        $this->assertEquals('/test_wiki/$1', $project->getArticlePath());
49
        $this->assertTrue($project->exists());
50
    }
51
52
    /**
53
     * Make sure there's an error when trying to get project metadata without a Repository.
54
     * @expectedException \Exception
55
     * @expectedExceptionMessage Repository for Xtools\Project must be set before using.
56
     */
57
    public function testNoRepository()
58
    {
59
        $project2 = new Project('test.example.wiki');
60
        $project2->getTitle();
61
    }
62
63
    /**
64
     * A project has a set of namespaces, comprising integer IDs and string titles.
65
     */
66
    public function testNamespaces()
67
    {
68
        $projectRepo = $this->getMock(ProjectRepository::class);
69
        $projectRepo->expects($this->once())
70
            ->method('getMetadata')
71
            ->willReturn(['namespaces' => [0 => 'Main', 1 => 'Article_talk']]);
72
73
        $project = new Project('testWiki');
74
        $project->setRepository($projectRepo);
75
        $this->assertCount(2, $project->getNamespaces());
76
77
        // Tests that getMetadata was in fact called only once and cached afterwards
78
        $this->assertEquals($project->getNamespaces()[0], 'Main');
79
    }
80
81
    /**
82
     * XTools can be run in single-wiki mode, where there is only one project.
83
     */
84
    public function testSingleWiki()
85
    {
86
        $projectRepo = new ProjectRepository();
87
        $projectRepo->setSingleBasicInfo([
88
            'url' => 'https://example.org/a-wiki/',
89
            'dbName' => 'example_wiki',
90
            'lang' => 'en',
91
        ]);
92
        $project = new Project('disregarded_wiki_name');
93
        $project->setRepository($projectRepo);
94
        $this->assertEquals('example_wiki', $project->getDatabaseName());
95
        $this->assertEquals('https://example.org/a-wiki/', $project->getUrl());
96
        $this->assertEquals('en', $project->getLang());
97
    }
98
99
    /**
100
     * A project is considered to exist if it has at least a domain name.
101
     */
102
    public function testExists()
103
    {
104
        $projectRepo = $this->getMock(ProjectRepository::class);
105
        $projectRepo->expects($this->once())
106
            ->method('getOne')
107
            ->willReturn([]);
108
109
        $project = new Project('testWiki');
110
        $project->setRepository($projectRepo);
111
        $this->assertFalse($project->exists());
112
    }
113
114
    /**
115
     * A user or a whole project can opt in to displaying restricted statistics.
116
     * @dataProvider optedInProvider
117
     * @param string[] $optedInProjects List of projects.
118
     * @param string $dbname The database name.
119
     * @param bool $hasOptedIn The result to check against.
120
     */
121
    public function testOptedIn($optedInProjects, $dbname, $hasOptedIn)
122
    {
123
        $projectRepo = $this->getMock(ProjectRepository::class);
124
        $projectRepo->expects($this->once())
125
            ->method('optedIn')
126
            ->willReturn($optedInProjects);
127
        $projectRepo->expects($this->once())
128
            ->method('getOne')
129
            ->willReturn(['dbName' => $dbname]);
130
131
        $project = new Project($dbname);
132
        $project->setRepository($projectRepo);
133
134
        // Check that the user has opted in or not.
135
        $user = new User('TestUser');
136
        $this->assertEquals($hasOptedIn, $project->userHasOptedIn($user));
137
    }
138
139
    /**
140
     * Whether page assessments are supported for the project
141
     */
142
    public function testHasPageAssessments()
143
    {
144
        $projectRepo = $this->getMock(ProjectRepository::class);
145
146
        $project = new Project('testWiki');
147
        $project->setRepository($projectRepo);
148
149
        // Unconfigured project
150
        $this->assertEquals(false, $project->hasPageAssessments());
151
152
        // Mock and re-test
153
        $projectRepo
154
            ->method('getAssessmentsConfig')
155
            ->with()
156
            ->willReturn([
157
                [
158
                    'wikiproject_prefix' => 'Wikipedia:WikiProject_',
159
                ],
160
            ]);
161
162
        $this->assertEquals(true, $project->hasPageAssessments());
163
    }
164
165
    /**
166
     * Data for self::testOptedIn().
167
     * @return array
168
     */
169
    public function optedInProvider()
170
    {
171
        $optedInProjects = ['project1'];
172
        return [
173
            [$optedInProjects, 'project1', true],
174
            [$optedInProjects, 'project2', false],
175
            [$optedInProjects, 'project3', false],
176
        ];
177
    }
178
}
179