Failed Conditions
Push — master ( b9670c...4ba57f )
by Zac
16:20 queued 37s
created

ManageUsersTest::testEditUserRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
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(UserFixtures::$users['user-1']->getEmail(), $this->getUsers()[0]->getText());
26
        $this->assertContains('role_super_admin', $this->getUsers(' div.user')[0]->getAttribute('class'));
27
        $this->assertContains(UserFixtures::$users['user-2']->getEmail(), $this->getUsers()[1]->getText());
28
        $this->assertContains('role_user', $this->getUsers(' div.user')[1]->getAttribute('class'));
29
        $this->assertContains(UserFixtures::$users['user-3']->getEmail(), $this->getUsers()[2]->getText());
30
        $this->assertContains('role_admin', $this->getUsers(' div.user')[2]->getAttribute('class'));
31
    }
32
    
33
    public function testCannotEditMe()
34
    {
35
        $this->assertFalse($this->getUsers(':first-child div.user a[title]')[0]->isDisplayed());
36
        $this->assertFalse($this->getUsers(':first-child div.user a[title]')[1]->isDisplayed());
37
        $this->assertFalse($this->getUsers(':first-child div.user a[title]')[2]->isDisplayed());
38
        
39
        $itsyou = $this->getUsers(':first-child div.user a: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 a:nth-child(3)')[0];
47
        $this->assertEquals('Lock', $lockButton->getText());
48
        
49
        $lockButton->click();
50
        $this->waitForLoadingAnimation();
51
        $this->assertEquals('Unlock', $this->getUsers(':nth-child(2) div a:nth-child(3)')[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->assertEquals('User account is locked.', $this->webDriver->findElement(WebDriverBy::cssSelector('#page > div'))->getText());
57
        
58
        $this->logInAsUser1();
59
        $this->getUsers(':nth-child(2) div a:nth-child(3)')[0]->click();
60
        $this->waitForLoadingAnimation();
61
        $this->assertEquals('Lock', $this->getUsers(':nth-child(2) div a:nth-child(3)')[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) div a:nth-child(2)')[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('#page > div'))->getText());
88
    }
89
    
90
    public function testEditUserRole()
91
    {
92
        $this->getUsers(':nth-child(2) div a:nth-child(4)')[0]->click();
93
        $this->webDriver->findElement(
94
            WebDriverBy::cssSelector('div.dialog button:nth-child(6)')
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) div a:nth-child(4)')[0]->click();
104
        $this->webDriver->findElement(
105
            WebDriverBy::cssSelector('div.dialog a:last-child')
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('ul.users li:last-child a')
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('ul.users li:last-child a')
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.ng-scope' . $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