Passed
Push — master ( d286b7...24e4a5 )
by Jan
04:36
created

testRedirectToPasswordChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Tests\Controller;
23
24
use Doctrine\ORM\EntityManagerInterface;
25
use Proxies\__CG__\App\Entity\UserSystem\User;
0 ignored issues
show
Bug introduced by
The type Proxies\__CG__\App\Entity\UserSystem\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27
28
/**
29
 * @group slow
30
 */
31
class RedirectControllerTest extends WebTestCase
32
{
33
    protected $em;
34
    protected $userRepo;
35
36
    public function setUp()
37
    {
38
        self::bootKernel();
39
        $this->em = self::$container->get(EntityManagerInterface::class);
40
        $this->userRepo = $this->em->getRepository(User::class);
0 ignored issues
show
Bug introduced by
The method getRepository() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        /** @scrutinizer ignore-call */ 
41
        $this->userRepo = $this->em->getRepository(User::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
    }
42
43
    public function urlMatchDataProvider()
44
    {
45
        return [
46
            ['/', true],
47
            ['/part/2/info', true],
48
            ['/part/de/2', true],
49
            ['/part/en/', true],
50
            ['/de/', false],
51
            ['/de_DE/', false],
52
            ['/en/', false],
53
            ['/en_US/', false],
54
        ];
55
    }
56
57
    /**
58
     * Test if a certain request to an url will be redirected.
59
     *
60
     * @dataProvider urlMatchDataProvider
61
     * @group slow
62
     */
63
    public function testUrlMatch($url, $expect_redirect)
64
    {
65
        $client = static::createClient();
66
        $client->request('GET', $url);
67
        $response = $client->getResponse();
68
        if ($expect_redirect) {
69
            $this->assertEquals(302, $response->getStatusCode());
70
        }
71
        $this->assertEquals($expect_redirect, $response->isRedirect());
72
    }
73
74
    public function urlAddLocaleDataProvider()
75
    {
76
        return [
77
            //User locale, original target, redirect target
78
            ['de', '/', '/de/'],
79
            ['de', '/part/3', '/de/part/3'],
80
            ['en', '/', '/en/'],
81
            ['en', '/category/new', '/en/category/new'],
82
            ['en_US', '/part/3', '/en_US/part/3'],
83
            //Without an explicit set value, the user should be redirect to english version
84
            [null, '/', '/en/'],
85
            ['en_US', '/part/3', '/en_US/part/3'],
86
        ];
87
    }
88
89
    /**
90
     * Test if the user is redirected to the localized version of a page, based on his settings.
91
     *
92
     * @dataProvider urlAddLocaleDataProvider
93
     * @group slow
94
     * @depends testUrlMatch
95
     *
96
     * @param $user_locale
97
     * @param $input_path
98
     * @param $redirect_path
99
     */
100
    public function testAddLocale($user_locale, $input_path, $redirect_path)
101
    {
102
        //Redirect path is absolute
103
        $redirect_path = 'http://localhost'.$redirect_path;
104
105
        /** @var User $user */
106
        $user = $this->userRepo->findOneBy(['name' => 'user']);
107
        //Set user locale
108
        $user->setLanguage($user_locale);
109
        $this->em->flush();
110
111
        $client = static::createClient([], [
112
            'PHP_AUTH_USER' => 'user',
113
            'PHP_AUTH_PW' => 'test',
114
        ]);
115
116
        $client->followRedirects(false);
117
        $client->request('GET', $input_path);
118
        $this->assertEquals($redirect_path, $client->getResponse()->headers->get('Location'));
119
    }
120
121
    /**
122
     * Test if the user is redirected to password change page if he should do that.
123
     *
124
     * @depends testAddLocale
125
     * @group slow
126
     * @testWith    ["de"]
127
     *              ["en"]
128
     */
129
    public function testRedirectToPasswordChange(string $locale)
130
    {
131
        /** @var User $user */
132
        $user = $this->userRepo->findOneBy(['name' => 'user']);
133
134
        //Test for german user
135
        $user->setLanguage($locale);
136
        $user->setNeedPwChange(true);
137
        $this->em->flush();
138
139
        $client = static::createClient([], [
140
            'PHP_AUTH_USER' => 'user',
141
            'PHP_AUTH_PW' => 'test',
142
        ]);
143
        $client->followRedirects(false);
144
145
        $client->request('GET', '/part/3');
146
        $this->assertEquals("/$locale/user/settings", $client->getResponse()->headers->get('Location'));
147
    }
148
}
149