ManageUsersTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 137
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testDisplaysUsers() 0 11 1
A testCannotEditMe() 0 10 1
A testLockUser() 0 23 1
A testDeleteUser() 0 21 1
A testEditUserRole() 0 9 1
A testEditRoleAndCancel() 0 10 1
A testRegisterNewUser() 0 20 1
A getUsers() 0 6 1
A logInAsUser1() 0 10 1
1
<?php
2
3
namespace Overwatch\UserBundle\Tests\E2E;
4
5
use Facebook\WebDriver\WebDriverBy;
6
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures;
7
use Overwatch\UserBundle\Tests\Base\WebDriverTestCase;
8
9
/**
10
 * ManageUsersTest
11
 * Tests the Manage Users screen
12
 */
13
class ManageUsersTest extends WebDriverTestCase
14
{
15
    public function setUp()
16
    {
17
        parent::setUp();
18
        $this->logInAsUser1();
19
    }
20
21
    public function testDisplaysUsers()
22
    {
23
        $this->assertEquals('Manage Users', $this->getHeaderText());
24
        $this->assertCount(3, $this->getUsers());
25
        $this->assertContains(strtoupper(UserFixtures::$users['user-1']->getEmail()), $this->getUsers()[0]->getText());
26
        $this->assertContains('role_super_admin', $this->getUsers(' div.avatar')[0]->getAttribute('class'));
27
        $this->assertContains(strtoupper(UserFixtures::$users['user-2']->getEmail()), $this->getUsers()[1]->getText());
28
        $this->assertContains('role_user', $this->getUsers(' div.avatar')[1]->getAttribute('class'));
29
        $this->assertContains(strtoupper(UserFixtures::$users['user-3']->getEmail()), $this->getUsers()[2]->getText());
30
        $this->assertContains('role_admin', $this->getUsers(' div.avatar')[2]->getAttribute('class'));
31
    }
32
33
    public function testCannotEditMe()
34
    {
35
        $this->assertFalse($this->getUsers(':first-child div.user .buttons a[title]')[0]->isDisplayed());
36
        $this->assertFalse($this->getUsers(':first-child div.user .buttons a[title]')[1]->isDisplayed());
37
        $this->assertFalse($this->getUsers(':first-child div.user .buttons a[title]')[2]->isDisplayed());
38
39
        $itsyou = $this->getUsers(':first-child div.user .its-you:not([title])')[0];
40
        $this->assertTrue($itsyou->isDisplayed());
41
        $this->assertContains("It's you!", $itsyou->getText());
42
    }
43
44
    public function testLockUser()
45
    {
46
        $lockButton = $this->getUsers(':nth-child(2) div.user .buttons a:nth-child(2)')[0];
47
        $this->assertEquals('Lock', $lockButton->getText());
48
49
        $lockButton->click();
50
        $this->waitForLoadingAnimation();
51
        $this->assertEquals('Unlock', $this->getUsers(':nth-child(2) .buttons a:nth-child(2)')[0]->getText());
52
53
        $this->webDriver->get('http://127.0.0.1:8000/logout');
54
        $this->logInAsUser('user-2');
55
        $this->assertEquals('http://127.0.0.1:8000/login', $this->webDriver->getCurrentURL());
56
        $this->assertContains('User account is locked.', $this->webDriver->findElement(WebDriverBy::cssSelector('main'))->getText());
57
58
        $this->logInAsUser1();
59
        $this->getUsers(':nth-child(2) .buttons a:nth-child(2)')[0]->click();
60
        $this->waitForLoadingAnimation();
61
        $this->assertEquals('Lock', $this->getUsers(':nth-child(2) .buttons a:nth-child(2)')[0]->getText());
62
63
        $this->webDriver->get('http://127.0.0.1:8000/logout');
64
        $this->logInAsUser('user-2');
65
        $this->assertNotEquals('http://127.0.0.1:8000/login', $this->webDriver->getCurrentURL());
66
    }
67
68
    public function testDeleteUser()
69
    {
70
        $deleteButton = $this->getUsers(':nth-child(2) .buttons a:nth-child(3)')[0];
71
        $this->assertEquals('Delete', $deleteButton->getText());
72
73
        $deleteButton->click();
74
        $this->waitForAlert();
75
        $this->webDriver->switchTo()->alert()->dismiss();
76
        $this->assertCount(3, $this->getUsers());
77
78
        $deleteButton->click();
79
        $this->waitForAlert();
80
        $this->webDriver->switchTo()->alert()->accept();
81
        $this->waitForLoadingAnimation();
82
        $this->assertCount(2, $this->getUsers());
83
84
        $this->webDriver->get('http://127.0.0.1:8000/logout');
85
        $this->logInAsUser('user-2');
86
        $this->assertEquals('http://127.0.0.1:8000/login', $this->webDriver->getCurrentURL());
87
        $this->assertEquals('Bad credentials.', $this->webDriver->findElement(WebDriverBy::cssSelector('main > div'))->getText());
88
    }
89
90
    public function testEditUserRole()
91
    {
92
        $this->getUsers(':nth-child(2) .user .buttons a:nth-child(1)')[0]->click();
93
        $this->webDriver->findElement(
94
            WebDriverBy::cssSelector('.dialog .row button:nth-child(2)')
95
        )->click();
96
        $this->waitForLoadingAnimation();
97
        $this->assertContains('role_admin', $this->getUsers(' div.user')[2]->getAttribute('class'));
98
    }
99
100
    public function testEditRoleAndCancel()
101
    {
102
        $before = $this->getUsers(' div.user')[2]->getAttribute('class');
103
        $this->getUsers(':nth-child(2) .user .buttons a:first-child')[0]->click();
104
        $this->webDriver->findElement(
105
            WebDriverBy::cssSelector('div.dialog .close-dialog')
106
        )->click();
107
        $this->waitForLoadingAnimation();
108
        $this->assertEquals($before, $this->getUsers(' div.user')[2]->getAttribute('class'));
109
    }
110
111
    public function testRegisterNewUser()
112
    {
113
        $this->webDriver->findElement(
114
            //Register button
115
            WebDriverBy::cssSelector('.widget-box .row .create-user')
116
        )->click();
117
        $this->waitForAlert();
118
        $this->webDriver->switchTo()->alert()->dismiss();
119
        $this->assertCount(3, $this->getUsers());
120
121
        $this->webDriver->findElement(
122
            //Register button
123
            WebDriverBy::cssSelector('.widget-box .row .create-user')
124
        )->click();
125
        $this->waitForAlert();
126
        $this->webDriver->switchTo()->alert()->sendKeys('[email protected]');
127
        $this->webDriver->switchTo()->alert()->accept();
128
        $this->waitForLoadingAnimation();
129
        $this->assertCount(4, $this->getUsers());
130
    }
131
132
    private function getUsers($suffix = '')
133
    {
134
        return $this->webDriver->findElements(
135
            WebDriverBy::cssSelector('.users li' . $suffix)
136
        );
137
    }
138
139
    private function logInAsUser1()
140
    {
141
        $this->logInAsUser('user-1');
142
        $this->waitForLoadingAnimation();
143
144
        $this->webDriver->findElement(
145
            WebDriverBy::cssSelector('.sidebar li:nth-child(2) a')
146
        )->click();
147
        $this->waitForLoadingAnimation();
148
    }
149
}
150