Completed
Push — master ( e24934...29b94d )
by
unknown
02:51
created

UserTest::testExpiry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
/**
3
 * This file contains only the UserTest 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
use Xtools\UserRepository;
13
use DateTime;
14
15
/**
16
 * Tests for the User class.
17
 */
18
class UserTest extends PHPUnit_Framework_TestCase
19
{
20
21
    /**
22
     * A username should be given an initial capital letter in all cases.
23
     */
24
    public function testUsernameHasInitialCapital()
25
    {
26
        $user = new User('lowercasename');
27
        $this->assertEquals('Lowercasename', $user->getUsername());
28
        $user2 = new User('UPPERCASENAME');
29
        $this->assertEquals('UPPERCASENAME', $user2->getUsername());
30
    }
31
32
    /**
33
     * A user has an integer identifier on a project (and this can differ from project
34
     * to project).
35
     */
36
    public function testUserHasIdOnProject()
37
    {
38
        // Set up stub user and project repositories.
39
        $userRepo = $this->getMock(UserRepository::class);
40
        $userRepo->expects($this->once())
41
            ->method('getId')
42
            ->willReturn(12);
43
        $projectRepo = $this->getMock(ProjectRepository::class);
44
        $projectRepo->expects($this->once())
45
            ->method('getOne')
46
            ->willReturn(['dbname' => 'testWiki']);
47
48
        // Make sure the user has the correct ID.
49
        $user = new User('TestUser');
50
        $user->setRepository($userRepo);
51
        $project = new Project('wiki.example.org');
52
        $project->setRepository($projectRepo);
53
        $this->assertEquals(12, $user->getId($project));
54
    }
55
56
    /**
57
     * Is a user an admin on a given project?
58
     * @dataProvider isAdminProvider
59
     * @param string $username The username.
60
     * @param string[] $groups The groups to test.
61
     * @param bool $isAdmin The desired result.
62
     */
63
    public function testIsAdmin($username, $groups, $isAdmin)
64
    {
65
        $userRepo = $this->getMock(UserRepository::class);
66
        $userRepo->expects($this->once())
67
            ->method('getGroups')
68
            ->willReturn($groups);
69
        $user = new User($username);
70
        $user->setRepository($userRepo);
71
        $this->assertEquals($isAdmin, $user->isAdmin(new Project('testWiki')));
72
    }
73
74
    /**
75
     * Data for self::testIsAdmin().
76
     * @return string[]
77
     */
78
    public function isAdminProvider()
79
    {
80
        return [
81
            ['AdminUser', ['sysop', 'autopatrolled'], true],
82
            ['NormalUser', ['autopatrolled'], false],
83
        ];
84
    }
85
86
    /**
87
     * Get the expiry of the current block of a user on a given project
88
     */
89 View Code Duplication
    public function testExpiry()
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...
90
    {
91
        $userRepo = $this->getMock(UserRepository::class);
92
        $userRepo->expects($this->once())
93
            ->method('getBlockExpiry')
94
            ->willReturn('20500601000000');
95
        $user = new User('TestUser');
96
        $user->setRepository($userRepo);
97
98
        $projectRepo = $this->getMock(ProjectRepository::class);
99
        $project = new Project('wiki.example.org');
100
        $project->setRepository($projectRepo);
101
102
        $this->assertEquals(new DateTime('20500601000000'), $user->getBlockExpiry($project));
103
    }
104
105
    /**
106
     * Is the user currently blocked on a given project?
107
     */
108 View Code Duplication
    public function testIsBlocked()
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...
109
    {
110
        $userRepo = $this->getMock(UserRepository::class);
111
        $userRepo->expects($this->once())
112
            ->method('getBlockExpiry')
113
            ->willReturn('infinity');
114
        $user = new User('TestUser');
115
        $user->setRepository($userRepo);
116
117
        $projectRepo = $this->getMock(ProjectRepository::class);
118
        $project = new Project('wiki.example.org');
119
        $project->setRepository($projectRepo);
120
121
        $this->assertEquals(true, $user->isBlocked($project));
122
    }
123
}
124