cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,122 +1,56 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\ReportBundle\Tests\Security\Authorization;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Chill\MainBundle\Test\PrepareUserTrait;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Test\PrepareCenterTrait;
use Chill\MainBundle\Test\PrepareScopeTrait;
use Chill\ReportBundle\Entity\Report;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Test\PrepareUserTrait;
use Chill\PersonBundle\Entity\Person;
use Chill\ReportBundle\Entity\Report;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @internal
* @coversNothing
*/
class ReportVoterTest extends KernelTestCase
{
use PrepareUserTrait, PrepareCenterTrait, PrepareScopeTrait;
{
use PrepareUserTrait;
use PrepareCenterTrait;
use PrepareScopeTrait;
/**
*
* @var \Chill\ReportBundle\Security\Authorization\ReportVoter
*/
protected $voter;
/**
*
* @var \Prophecy\Prophet
*/
protected $prophet;
/**
* @var \Chill\ReportBundle\Security\Authorization\ReportVoter
*/
protected $voter;
public static function setUpBeforeClass()
{
}
public function setUp()
{
static::bootKernel();
$this->voter = static::$kernel->getContainer()
->get('chill.report.security.authorization.report_voter');
->get('chill.report.security.authorization.report_voter');
$this->prophet = new \Prophecy\Prophet();
}
/**
* @dataProvider dataProvider
* @param type $expectedResult
* @param Report $report
* @param User $user
* @param type $action
* @param type $message
*/
public function testAccess($expectedResult, Report $report, $action,
$message, User $user = null)
{
$token = $this->prepareToken($user);
$result = $this->voter->vote($token, $report, [$action]);
$this->assertEquals($expectedResult, $result, $message);
}
/**
* prepare a person
*
* The only properties set is the center, others properties are ignored.
*
* @param Center $center
* @return Person
*/
protected function preparePerson(Center $center)
{
return (new Person())
->setCenter($center)
;
}
/**
* prepare a token interface with correct rights
*
* if $permissions = null, user will be null (no user associated with token
*
* @param User $user
* @return \Symfony\Component\Security\Core\Authentication\Token\TokenInterface
*/
protected function prepareToken(User $user = null)
{
$token = $this->prophet->prophesize();
$token
->willImplement('\Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
if ($user === NULL) {
$token->getUser()->willReturn(null);
} else {
$token->getUser()->willReturn($user);
}
return $token->reveal();
}
public function dataProvider()
{
$centerA = $this->prepareCenter(1, 'center A');
@@ -124,70 +58,120 @@ class ReportVoterTest extends KernelTestCase
$scopeA = $this->prepareScope(1, 'scope default');
$scopeB = $this->prepareScope(2, 'scope B');
$scopeC = $this->prepareScope(3, 'scope C');
$userA = $this->prepareUser(array(
array(
'center' => $centerA,
'permissionsGroup' => array(
$userA = $this->prepareUser([
[
'center' => $centerA,
'permissionsGroup' => [
['scope' => $scopeB, 'role' => 'CHILL_REPORT_SEE'],
['scope' => $scopeA, 'role' => 'CHILL_REPORT_UPDATE']
)
),
array(
'center' => $centerB,
'permissionsGroup' => array(
['scope' => $scopeA, 'role' => 'CHILL_REPORT_SEE'],
)
)
));
$reportA = (new Report)
->setScope($scopeA)
->setPerson($this->preparePerson($centerA))
;
['scope' => $scopeA, 'role' => 'CHILL_REPORT_UPDATE'],
],
],
[
'center' => $centerB,
'permissionsGroup' => [
['scope' => $scopeA, 'role' => 'CHILL_REPORT_SEE'],
],
],
]);
$reportA = (new Report())
->setScope($scopeA)
->setPerson($this->preparePerson($centerA));
$reportB = (new Report())
->setScope($scopeB)
->setPerson($this->preparePerson($centerA))
;
->setScope($scopeB)
->setPerson($this->preparePerson($centerA));
$reportC = (new Report())
->setScope($scopeC)
->setPerson($this->preparePerson($centerB))
;
return array(
array(
->setScope($scopeC)
->setPerson($this->preparePerson($centerB));
return [
[
VoterInterface::ACCESS_DENIED,
$reportA,
'CHILL_REPORT_SEE',
"assert is denied to a null user",
null
),
array(
'assert is denied to a null user',
null,
],
[
VoterInterface::ACCESS_GRANTED,
$reportA,
'CHILL_REPORT_SEE',
"assert access is granted to a user with inheritance UPDATE > SEE",
$userA
),
array(
'assert access is granted to a user with inheritance UPDATE > SEE',
$userA,
],
[
VoterInterface::ACCESS_GRANTED,
$reportB,
'CHILL_REPORT_SEE',
"assert access is granted to a user without inheritance",
$userA
),
array(
'assert access is granted to a user without inheritance',
$userA,
],
[
VoterInterface::ACCESS_DENIED,
$reportC,
'CHILL_REPORT_SEE',
'assert access is denied to a report',
$userA
)
);
$userA,
],
];
}
/**
* @dataProvider dataProvider
*
* @param type $expectedResult
* @param User $user
* @param type $action
* @param type $message
*/
public function testAccess(
$expectedResult,
Report $report,
$action,
$message,
?User $user = null
)
{
$token = $this->prepareToken($user);
$result = $this->voter->vote($token, $report, [$action]);
$this->assertEquals($expectedResult, $result, $message);
}
/**
* prepare a person.
*
* The only properties set is the center, others properties are ignored.
*
* @return Person
*/
protected function preparePerson(Center $center)
{
return (new Person())
->setCenter($center);
}
/**
* prepare a token interface with correct rights.
*
* if $permissions = null, user will be null (no user associated with token
*
* @param User $user
*
* @return \Symfony\Component\Security\Core\Authentication\Token\TokenInterface
*/
protected function prepareToken(?User $user = null)
{
$token = $this->prophet->prophesize();
$token
->willImplement('\Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
if (null === $user) {
$token->getUser()->willReturn(null);
} else {
$token->getUser()->willReturn($user);
}
return $token->reveal();
}
}