|
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 readDataProvider() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
['noread', false], |
|
39
|
|
|
['anonymous', true], |
|
40
|
|
|
['user', true], |
|
41
|
|
|
['admin', true], |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @dataProvider readDataProvider |
|
47
|
|
|
* @group slow |
|
48
|
|
|
* Tests if you can access the /new part which is used to list all entities. Checks if permissions are working |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testListEntries(string $user, bool $read) |
|
51
|
|
|
{ |
|
52
|
|
|
static::ensureKernelShutdown(); |
|
53
|
|
|
|
|
54
|
|
|
//Test read access |
|
55
|
|
|
$client = static::createClient([], [ |
|
56
|
|
|
'PHP_AUTH_USER' => $user, |
|
57
|
|
|
'PHP_AUTH_PW' => 'test', |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
if (false == $read) { |
|
|
|
|
|
|
61
|
|
|
$this->expectException(AccessDeniedException::class); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$client->catchExceptions(false); |
|
65
|
|
|
|
|
66
|
|
|
//Test read/list access by access /new overview page |
|
67
|
|
|
$crawler = $client->request('GET', static::$base_path.'/new'); |
|
|
|
|
|
|
68
|
|
|
$this->assertFalse($client->getResponse()->isRedirect()); |
|
69
|
|
|
$this->assertEquals($read, $client->getResponse()->isSuccessful(), 'Controller was not successful!'); |
|
70
|
|
|
$this->assertEquals($read, !$client->getResponse()->isForbidden(), 'Permission Checking not working!'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @dataProvider readDataProvider |
|
75
|
|
|
* @group slow |
|
76
|
|
|
* Tests if it possible to access an specific entity. Checks if permissions are working. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testReadEntity(string $user, bool $read) |
|
79
|
|
|
{ |
|
80
|
|
|
//Test read access |
|
81
|
|
|
$client = static::createClient([], [ |
|
82
|
|
|
'PHP_AUTH_USER' => $user, |
|
83
|
|
|
'PHP_AUTH_PW' => 'test', |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$client->catchExceptions(false); |
|
87
|
|
|
if (false == $read) { |
|
|
|
|
|
|
88
|
|
|
$this->expectException(AccessDeniedException::class); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//Test read/list access by access /new overview page |
|
92
|
|
|
$crawler = $client->request('GET', static::$base_path.'/1'); |
|
|
|
|
|
|
93
|
|
|
$this->assertFalse($client->getResponse()->isRedirect()); |
|
94
|
|
|
$this->assertEquals($read, $client->getResponse()->isSuccessful(), 'Controller was not successful!'); |
|
95
|
|
|
$this->assertEquals($read, !$client->getResponse()->isForbidden(), 'Permission Checking not working!'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function deleteDataProvider() |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
['noread', false], |
|
102
|
|
|
['anonymous', false], |
|
103
|
|
|
['user', true], |
|
104
|
|
|
['admin', true], |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Tests if deleting an entity is working. |
|
110
|
|
|
* |
|
111
|
|
|
* @group slow |
|
112
|
|
|
* @dataProvider deleteDataProvider |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testDeleteEntity(string $user, bool $delete) |
|
115
|
|
|
{ |
|
116
|
|
|
//Test read access |
|
117
|
|
|
$client = static::createClient([], [ |
|
118
|
|
|
'PHP_AUTH_USER' => $user, |
|
119
|
|
|
'PHP_AUTH_PW' => 'test', |
|
120
|
|
|
]); |
|
121
|
|
|
|
|
122
|
|
|
$client->catchExceptions(false); |
|
123
|
|
|
if (false == $delete) { |
|
|
|
|
|
|
124
|
|
|
$this->expectException(AccessDeniedException::class); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
//Test read/list access by access /new overview page |
|
128
|
|
|
$crawler = $client->request('DELETE', static::$base_path.'/7'); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
//Page is redirected to '/new', when delete was successful |
|
131
|
|
|
$this->assertEquals($delete, $client->getResponse()->isRedirect(static::$base_path.'/new')); |
|
132
|
|
|
$this->assertEquals($delete, !$client->getResponse()->isForbidden(), 'Permission Checking not working!'); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.