Completed
Push — 1.4-password-hashing-2 ( 165f8b...d02581 )
by Kamil
13:26
created

UpdatingUserPasswordEncoderTest::submitForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\Tests\Functional;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Fidry\AliceDataFixtures\LoaderInterface;
9
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
10
use PHPUnit\Framework\Assert;
11
use Sylius\Component\User\Repository\UserRepositoryInterface;
12
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
13
use Symfony\Component\BrowserKit\Client;
14
15
final class UpdatingUserPasswordEncoderTest extends WebTestCase
16
{
17
    /** @var Client */
18
    private $client;
19
20
    protected function setUp(): void
21
    {
22
        $this->client = static::createClient();
23
        $this->client->followRedirects(true);
24
25
        /** @var LoaderInterface $fixtureLoader */
26
        $fixtureLoader = $this->client->getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');
27
28
        $fixtureLoader->load(
29
            [
30
                __DIR__ . '/../DataFixtures/ORM/resources/channels.yml',
31
                __DIR__ . '/../DataFixtures/ORM/resources/customers.yml',
32
                __DIR__ . '/../DataFixtures/ORM/resources/admin_users.yml',
33
            ],
34
            [],
35
            [],
36
            PurgeMode::createDeleteMode()
37
        );
38
    }
39
40
    /** @test */
41
    public function it_updates_the_encoder_when_the_shop_user_logs_in(): void
42
    {
43
        /** @var UserRepositoryInterface $shopUserRepository */
44
        $shopUserRepository = $this->client->getContainer()->get('sylius.repository.shop_user');
45
46
        /** @var ObjectManager $shopUserManager */
47
        $shopUserManager = $this->client->getContainer()->get('sylius.manager.shop_user');
48
49
        $shopUser = $shopUserRepository->findOneByEmail('[email protected]');
50
        $shopUser->setPlainPassword('testpassword');
51
        $shopUser->setEncoderName('sha512');
52
53
        $shopUserManager->persist($shopUser);
54
        $shopUserManager->flush();
55
56
        $this->client->request('GET', '/en_US/login');
57
58
        $this->submitForm('Login', [
59
            '_username' => '[email protected]',
60
            '_password' => 'testpassword'
61
        ]);
62
63
        Assert::assertSame(200, $this->client->getResponse()->getStatusCode());
64
        Assert::assertSame('/en_US/', parse_url($this->client->getCrawler()->getUri(), \PHP_URL_PATH));
65
        Assert::assertSame('argon2i', $shopUserRepository->findOneByEmail('[email protected]')->getEncoderName());
66
    }
67
68
    /** @test */
69
    public function it_updates_the_encoder_when_the_admin_user_logs_in(): void
70
    {
71
        /** @var UserRepositoryInterface $adminUserRepository */
72
        $adminUserRepository = $this->client->getContainer()->get('sylius.repository.admin_user');
73
74
        /** @var ObjectManager $adminUserManager */
75
        $adminUserManager = $this->client->getContainer()->get('sylius.manager.admin_user');
76
77
        $adminUser = $adminUserRepository->findOneByEmail('[email protected]');
78
        $adminUser->setPlainPassword('testpassword');
79
        $adminUser->setEncoderName('sha512');
80
81
        $adminUserManager->persist($adminUser);
82
        $adminUserManager->flush();
83
84
        $this->client->request('GET', '/admin/login');
85
86
        $this->submitForm('Login', [
87
            '_username' => '[email protected]',
88
            '_password' => 'testpassword'
89
        ]);
90
91
        Assert::assertSame(200, $this->client->getResponse()->getStatusCode());
92
        Assert::assertSame('/admin/', parse_url($this->client->getCrawler()->getUri(), \PHP_URL_PATH));
93
        Assert::assertSame('argon2i', $adminUserRepository->findOneByEmail('[email protected]')->getEncoderName());
94
    }
95
96
    private function submitForm(string $button, array $fieldValues = []): void
97
    {
98
        $buttonNode = $this->client->getCrawler()->selectButton($button);
99
100
        $form = $buttonNode->form($fieldValues);
101
102
        $this->client->submit($form);
103
    }
104
}
105