Skip to content

Commit 1d3020f

Browse files
TavoNiievezNaktibalda
authored andcommitted
Added seeAuthentication function
1 parent b24e2b6 commit 1d3020f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

documentation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,20 @@ For checking the raw source code, use `seeInSource()`.
890890
* `param array|string` $selector optional
891891

892892

893+
### seeAuthentication
894+
895+
Checks that a user is authenticated.
896+
You can check users logged in with the option 'remember me' passing true as parameter.
897+
898+
```php
899+
<?php
900+
$I->seeAuthentication();
901+
$I->seeAuthentication(true);
902+
```
903+
904+
* `param bool` $remembered
905+
906+
893907
### seeCheckboxIsChecked
894908

895909
Checks that the specified checkbox is checked.

src/Codeception/Module/Symfony.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ public function amOnAction($action, $params = [])
911911
$controller = basename($route->getDefault('_controller'));
912912
if ($controller === $action) {
913913
$resource = $router->match($route->getPath());
914-
$url = $router->generate(
914+
$url = $router->generate(
915915
$resource['_route'],
916916
$params,
917917
UrlGeneratorInterface::ABSOLUTE_PATH
@@ -921,4 +921,43 @@ public function amOnAction($action, $params = [])
921921
}
922922
}
923923
}
924+
925+
/**
926+
* Checks that a user is authenticated.
927+
* You can check users logged in with the option 'remember me' passing true as parameter.
928+
*
929+
* ```php
930+
* <?php
931+
* $I->seeAuthentication();
932+
* $I->seeAuthentication(true);
933+
* ```
934+
*
935+
* @param bool $remembered
936+
*/
937+
public function seeAuthentication($remembered = false)
938+
{
939+
$container = $this->_getContainer();
940+
941+
if (!$container->has('security.helper')) {
942+
$this->fail("Symfony container doesn't have 'security.helper' service");
943+
return;
944+
}
945+
946+
$security = $this->grabService('security.helper');
947+
948+
$user = $security->getUser();
949+
950+
if (!$user) {
951+
$this->fail('There is no user in session');
952+
return;
953+
}
954+
955+
if ($remembered) {
956+
$role = 'IS_AUTHENTICATED_REMEMBERED';
957+
} else {
958+
$role = 'IS_AUTHENTICATED_FULLY';
959+
}
960+
961+
$this->assertTrue($security->isGranted($role), 'There is no authenticated user');
962+
}
924963
}

0 commit comments

Comments
 (0)