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

AbstractAdminControllerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeleteEntity() 0 19 2
A setUp() 0 4 1
A readDataProvider() 0 7 1
A testReadEntity() 0 18 2
A testListEntries() 0 19 2
A deleteDataProvider() 0 7 1
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\AdminPages;
23
24
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
25
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
26
27
/**
28
 * @group slow
29
 */
30
abstract class AbstractAdminControllerTest extends WebTestCase
31
{
32
    protected static $base_path = 'not_valid';
33
    protected static $entity_class = 'not valid';
34
35
    public function setUp()
36
    {
37
        parent::setUp();
38
        self::bootKernel();
39
    }
40
41
    public function readDataProvider()
42
    {
43
        return [
44
            ['noread', false],
45
            ['anonymous', true],
46
            ['user', true],
47
            ['admin', true],
48
        ];
49
    }
50
51
    /**
52
     * @dataProvider readDataProvider
53
     * @group slow
54
     * Tests if you can access the /new part which is used to list all entities. Checks if permissions are working
55
     */
56
    public function testListEntries(string $user, bool $read)
57
    {
58
        //Test read access
59
        $client = static::createClient([], [
60
            'PHP_AUTH_USER' => $user,
61
            'PHP_AUTH_PW' => 'test',
62
        ]);
63
64
        if (false == $read) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
65
            $this->expectException(AccessDeniedException::class);
66
        }
67
68
        $client->catchExceptions(false);
69
70
        //Test read/list access by access /new overview page
71
        $crawler = $client->request('GET', static::$base_path.'/new');
0 ignored issues
show
Unused Code introduced by
The assignment to $crawler is dead and can be removed.
Loading history...
72
        $this->assertFalse($client->getResponse()->isRedirect());
73
        $this->assertEquals($read, $client->getResponse()->isSuccessful(), 'Controller was not successful!');
74
        $this->assertEquals($read, !$client->getResponse()->isForbidden(), 'Permission Checking not working!');
75
    }
76
77
    /**
78
     * @dataProvider readDataProvider
79
     * @group slow
80
     * Tests if it possible to access an specific entity. Checks if permissions are working.
81
     */
82
    public function testReadEntity(string $user, bool $read)
83
    {
84
        //Test read access
85
        $client = static::createClient([], [
86
            'PHP_AUTH_USER' => $user,
87
            'PHP_AUTH_PW' => 'test',
88
        ]);
89
90
        $client->catchExceptions(false);
91
        if (false == $read) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
92
            $this->expectException(AccessDeniedException::class);
93
        }
94
95
        //Test read/list access by access /new overview page
96
        $crawler = $client->request('GET', static::$base_path.'/1');
0 ignored issues
show
Unused Code introduced by
The assignment to $crawler is dead and can be removed.
Loading history...
97
        $this->assertFalse($client->getResponse()->isRedirect());
98
        $this->assertEquals($read, $client->getResponse()->isSuccessful(), 'Controller was not successful!');
99
        $this->assertEquals($read, !$client->getResponse()->isForbidden(), 'Permission Checking not working!');
100
    }
101
102
    public function deleteDataProvider()
103
    {
104
        return [
105
            ['noread', false],
106
            ['anonymous', false],
107
            ['user', true],
108
            ['admin', true],
109
        ];
110
    }
111
112
    /**
113
     * Tests if deleting an entity is working.
114
     *
115
     * @group slow
116
     * @dataProvider deleteDataProvider
117
     */
118
    public function testDeleteEntity(string $user, bool $delete)
119
    {
120
        //Test read access
121
        $client = static::createClient([], [
122
            'PHP_AUTH_USER' => $user,
123
            'PHP_AUTH_PW' => 'test',
124
        ]);
125
126
        $client->catchExceptions(false);
127
        if (false == $delete) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
128
            $this->expectException(AccessDeniedException::class);
129
        }
130
131
        //Test read/list access by access /new overview page
132
        $crawler = $client->request('DELETE', static::$base_path.'/7');
0 ignored issues
show
Unused Code introduced by
The assignment to $crawler is dead and can be removed.
Loading history...
133
134
        //Page is redirected to '/new', when delete was successful
135
        $this->assertEquals($delete, $client->getResponse()->isRedirect(static::$base_path.'/new'));
136
        $this->assertEquals($delete, !$client->getResponse()->isForbidden(), 'Permission Checking not working!');
137
    }
138
}
139