Skip to content

Commit 3c65247

Browse files
TavoNiievezNaktibalda
authored andcommitted
Added seeUserHasRole function
1 parent 1d3020f commit 3c65247

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

documentation.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,18 @@ Checks that the response code is 5xx
12811281
Checks that the response code 2xx
12821282

12831283

1284+
### seeUserHasRole
1285+
1286+
Check that the current user has a role
1287+
1288+
```php
1289+
<?php
1290+
$I->seeUserHasRole('ROLE_ADMIN');
1291+
```
1292+
1293+
* `param string` $role
1294+
1295+
12841296
### selectOption
12851297

12861298
Selects an option in a select tag or in radio button group.

src/Codeception/Module/Symfony.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,4 +960,42 @@ public function seeAuthentication($remembered = false)
960960

961961
$this->assertTrue($security->isGranted($role), 'There is no authenticated user');
962962
}
963+
964+
/**
965+
* Check that the current user has a role
966+
*
967+
* ```php
968+
* <?php
969+
* $I->seeUserHasRole('ROLE_ADMIN');
970+
* ```
971+
*
972+
* @param string $role
973+
*/
974+
public function seeUserHasRole($role)
975+
{
976+
$container = $this->_getContainer();
977+
978+
if (!$container->has('security.helper')) {
979+
$this->fail("Symfony container doesn't have 'security.helper' service");
980+
return;
981+
}
982+
983+
$security = $this->grabService('security.helper');
984+
985+
$user = $security->getUser();
986+
987+
if (!$user) {
988+
$this->fail('There is no user in session');
989+
return;
990+
}
991+
992+
$this->assertTrue(
993+
$security->isGranted($role),
994+
sprintf(
995+
"User %s has no role %s",
996+
$user->getUsername(),
997+
$role
998+
)
999+
);
1000+
}
9631001
}

0 commit comments

Comments
 (0)