File: SecurityTest.php

package info (click to toggle)
symfony 7.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,424 kB
  • sloc: php: 1,510,651; xml: 7,039; javascript: 979; sh: 586; makefile: 242; pascal: 70
file content (87 lines) | stat: -rw-r--r-- 3,299 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Component\Security\Core\User\InMemoryUser;

class SecurityTest extends AbstractWebTestCase
{
    #[\PHPUnit\Framework\Attributes\DataProvider('getUsers')]
    public function testLoginUser(string $username, array $roles, ?string $firewallContext)
    {
        $user = new InMemoryUser($username, 'the-password', $roles);
        $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);

        if (null === $firewallContext) {
            $client->loginUser($user);
        } else {
            $client->loginUser($user, $firewallContext);
        }

        $client->request('GET', '/'.($firewallContext ?? 'main').'/user_profile');
        $this->assertEquals('Welcome '.$username.'!', $client->getResponse()->getContent());
    }

    public static function getUsers()
    {
        yield ['the-username', ['ROLE_FOO'], null];
        yield ['the-username', ['ROLE_FOO'], 'main'];
        yield ['other-username', ['ROLE_FOO'], 'custom'];
        yield ['stateless-username', ['ROLE_FOO'], 'stateless'];

        yield ['the-username', ['ROLE_FOO'], null];
        yield ['no-role-username', [], null];
    }

    public function testLoginUserMultipleRequests()
    {
        $user = new InMemoryUser('the-username', 'the-password', ['ROLE_FOO']);
        $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
        $client->loginUser($user);

        $client->request('GET', '/main/user_profile');
        $this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());

        $client->request('GET', '/main/user_profile');
        $this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
    }

    public function testLoginInBetweenRequests()
    {
        $user = new InMemoryUser('the-username', 'the-password', ['ROLE_FOO']);
        $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);

        $client->request('GET', '/main/user_profile');
        $this->assertTrue($client->getResponse()->isRedirect('http://localhost/login'));

        $client->loginUser($user);

        $client->request('GET', '/main/user_profile');
        $this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
    }

    public function testLoginUserMultipleTimes()
    {
        $userFoo = new InMemoryUser('the-username', 'the-password', ['ROLE_FOO']);
        $userBar = new InMemoryUser('no-role-username', 'the-password');
        $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
        $client->loginUser($userFoo);

        $client->request('GET', '/main/user_profile');
        $this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());

        $client->loginUser($userBar);

        $client->request('GET', '/main/user_profile');
        $this->assertEquals('Welcome no-role-username!', $client->getResponse()->getContent());
    }
}