Passed
Push — master ( 311a34...7e0e83 )
by Jan
04:48 queued 45s
created

RedirectControllerTest::testRedirectToPasswordChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 10
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
 * @group DB
31
 */
32
class RedirectControllerTest extends WebTestCase
33
{
34
    protected $em;
35
    protected $userRepo;
36
    protected $client;
37
38
    public function setUp() : void
39
    {
40
        $this->client = static::createClient([], [
41
            'PHP_AUTH_USER' => 'user',
42
            'PHP_AUTH_PW' => 'test',
43
        ]);
44
        $this->client->disableReboot();
45
        $this->em = self::$container->get(EntityManagerInterface::class);
46
        $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

46
        /** @scrutinizer ignore-call */ 
47
        $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...
47
    }
48
49
    public function urlMatchDataProvider()
50
    {
51
        return [
52
            ['/', true],
53
            ['/part/2/info', true],
54
            ['/part/de/2', true],
55
            ['/part/en/', true],
56
            ['/de/', false],
57
            ['/de_DE/', false],
58
            ['/en/', false],
59
            ['/en_US/', false],
60
        ];
61
    }
62
63
    /**
64
     * Test if a certain request to an url will be redirected.
65
     *
66
     * @dataProvider urlMatchDataProvider
67
     * @group slow
68
     */
69
    public function testUrlMatch($url, $expect_redirect)
70
    {
71
        //$client = static::createClient();
72
        $this->client->request('GET', $url);
73
        $response = $this->client->getResponse();
74
        if ($expect_redirect) {
75
            $this->assertEquals(302, $response->getStatusCode());
76
        }
77
        $this->assertEquals($expect_redirect, $response->isRedirect());
78
    }
79
80
    public function urlAddLocaleDataProvider()
81
    {
82
        return [
83
            //User locale, original target, redirect target
84
            ['de', '/', '/de/'],
85
            ['de', '/part/3', '/de/part/3'],
86
            ['en', '/', '/en/'],
87
            ['en', '/category/new', '/en/category/new'],
88
            ['en_US', '/part/3', '/en_US/part/3'],
89
            //Without an explicit set value, the user should be redirect to english version
90
            [null, '/', '/en/'],
91
            ['en_US', '/part/3', '/en_US/part/3'],
92
        ];
93
    }
94
95
    /**
96
     * Test if the user is redirected to the localized version of a page, based on his settings.
97
     *
98
     * @dataProvider urlAddLocaleDataProvider
99
     * @group slow
100
     * @depends testUrlMatch
101
     *
102
     * @param $user_locale
103
     * @param $input_path
104
     * @param $redirect_path
105
     */
106
    public function testAddLocale($user_locale, $input_path, $redirect_path)
107
    {
108
        //Redirect path is absolute
109
        $redirect_path = 'http://localhost'.$redirect_path;
110
111
        /** @var User $user */
112
        $user = $this->userRepo->findOneBy(['name' => 'user']);
113
        //Set user locale
114
        $user->setLanguage($user_locale);
115
        $this->em->flush();
116
117
118
119
        $this->client->followRedirects(false);
120
        $this->client->request('GET', $input_path);
121
        $this->assertEquals($redirect_path, $this->client->getResponse()->headers->get('Location'));
122
    }
123
}
124