Minor - format code

pull/3904/head
Julio Montoya 4 years ago
parent c02356d867
commit 2474c55845
  1. 3
      ecs.php
  2. 9
      src/CoreBundle/Security/AccessDeniedHandler.php
  3. 16
      src/CoreBundle/Security/AuthenticationEntryPoint.php
  4. 10
      src/CoreBundle/Security/LoginFormAuthenticator.php
  5. 5
      src/CoreBundle/Traits/ControllerTrait.php
  6. 7
      src/CoreBundle/Traits/CourseControllerTrait.php
  7. 8
      src/CoreBundle/Traits/ResourceControllerTrait.php
  8. 15
      src/CoreBundle/Traits/TimestampableTypedEntity.php
  9. 12
      tests/CoreBundle/Controller/IndexControllerTest.php
  10. 14
      tests/CoreBundle/Repository/Node/AccessUrlRepositoryTest.php
  11. 27
      tests/CoreBundle/Repository/Node/CourseRepositoryTest.php
  12. 14
      tests/CoreBundle/Repository/Node/UserRepositoryTest.php
  13. 12
      tests/CoreBundle/Repository/Node/UsergroupRepositoryTest.php
  14. 17
      tests/CourseBundle/Repository/CDocumentRepositoryTest.php

@ -99,12 +99,13 @@ return static function (ContainerConfigurator $containerConfigurator): void {
__DIR__.'/public/main/admin/db.php',
__DIR__.'/src/CoreBundle/Hook/*',
__DIR__.'/src/CoreBundle/Component/HTMLPurifier/Filter/AllowIframes.php',
__DIR__.'/src/CoreBundle/Traits/*',
__DIR__.'/src/CoreBundle/Traits/Repository/*',
__DIR__.'/src/CourseBundle/Component/*',
__DIR__.'/src/DataFixtures/*',
IncrementStyleFixer::class => 'post',
PropertyTypeHintSniff::class.'.'.PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION,
PropertyTypeHintSniff::class.'.'.PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT,
PhpCsFixer\Fixer\PhpUnit\PhpUnitInternalClassFixer::class,
DoctrineAnnotationArrayAssignmentFixer::class,
SingleLineCommentStyleFixer::class,
NotOperatorWithSuccessorSpaceFixer::class,

@ -6,6 +6,7 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@ -15,6 +16,12 @@ class AccessDeniedHandler implements AccessDeniedHandlerInterface
{
public function handle(Request $request, AccessDeniedException $accessDeniedException)
{
return new Response('403', 403);
error_log('AccessDeniedHandler');
$data = [
'error' => 'aa',
];
return new JsonResponse($data, Response::HTTP_FORBIDDEN, [], true);
//return new Response('403', 403);
}
}

@ -6,9 +6,11 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
@ -26,13 +28,21 @@ class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
{
/*error_log('start');
$message = $authException->getMessage();
if (null !== $authException->getPrevious()) {
$message = $authException->getPrevious()->getMessage();
}
}*/
$session = $this->requestStack->getSession();
$session->getFlashBag()->add('warning', $message);
//$session = $this->requestStack->getSession();
//$session->getFlashBag()->add('warning', $message);
/*$data = [
// you might translate this message
'message' => 'Authentication Required',
];
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);*/
return new RedirectResponse($this->urlGenerator->generate('login'));
}

@ -9,7 +9,6 @@ namespace Chamilo\CoreBundle\Security;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Hook\HookFactory;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
@ -124,14 +123,9 @@ class LoginFormAuthenticator extends AbstractGuardAuthenticator implements Passw
return $user;
}
/**
* @throws Exception
*
* @return bool
*/
public function checkCredentials($credentials, UserInterface $user)
public function checkCredentials($credentials, $user): bool
{
error_log('login form');
//error_log('login form');
return $this->passwordHasher->isPasswordValid($user, $credentials['password']);
/*$hook = $this->hookFactory->build(CheckLoginCredentialsHook::class);

@ -31,12 +31,16 @@ use Chamilo\CourseBundle\Repository\CToolRepository;
use Chamilo\LtiBundle\Repository\ExternalToolRepository;
use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
trait ControllerTrait
{
/**
* @var ContainerInterface
*/
protected $container;
public static function getSubscribedServices(): array
@ -100,7 +104,6 @@ trait ControllerTrait
/**
* Translator shortcut.
*
*
* @return string
*/
public function trans(string $variable)

@ -9,7 +9,7 @@ namespace Chamilo\CoreBundle\Traits;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CourseBundle\Entity\CGroup;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Trait CourseControllerTrait.
@ -19,6 +19,9 @@ trait CourseControllerTrait
{
protected ?Course $course = null;
protected ?Session $session = null;
/**
* @var ContainerInterface
*/
protected $container;
/**
@ -159,6 +162,6 @@ trait CourseControllerTrait
public function getSessionId(): int
{
return $this->session !== null ? $this->getSession()->getId() : 0;
return null !== $this->session ? $this->getSession()->getId() : 0;
}
}

@ -13,13 +13,15 @@ use Chamilo\CoreBundle\Repository\ResourceFactory;
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Doctrine\ORM\EntityNotFoundException;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use function is_object;
trait ResourceControllerTrait
{
/**
* @var ContainerInterface
*/
protected $container;
public function getRepositoryFromRequest(Request $request): ResourceRepository
@ -127,7 +129,7 @@ trait ResourceControllerTrait
/** @var User $user */
$user = $token->getUser();
if (!is_object($user)) {
if (!\is_object($user)) {
// e.g. anonymous authentication
return null;
}

@ -1,7 +1,10 @@
<?php
declare(strict_types=1);
namespace Chamilo\CoreBundle\Traits;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
@ -11,20 +14,20 @@ trait TimestampableTypedEntity
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
protected \DateTime $createdAt;
protected DateTime $createdAt;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
protected \DateTime $updatedAt;
protected DateTime $updatedAt;
/**
* Sets createdAt.
*
* @return $this
*/
public function setCreatedAt(\DateTime $createdAt)
public function setCreatedAt(DateTime $createdAt)
{
$this->createdAt = $createdAt;
@ -34,7 +37,7 @@ trait TimestampableTypedEntity
/**
* Returns createdAt.
*
* @return \DateTime
* @return DateTime
*/
public function getCreatedAt()
{
@ -46,7 +49,7 @@ trait TimestampableTypedEntity
*
* @return $this
*/
public function setUpdatedAt(\DateTime $updatedAt)
public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
@ -56,7 +59,7 @@ trait TimestampableTypedEntity
/**
* Returns updatedAt.
*
* @return \DateTime
* @return DateTime
*/
public function getUpdatedAt()
{

@ -1,19 +1,23 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Controller;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* @coversNothing
*/
class IndexControllerTest extends WebTestCase
{
use ChamiloTestTrait;
public function testIndex()
public function testIndex(): void
{
$client = static::createClient();
$client->request('GET', '/');
@ -21,7 +25,7 @@ class IndexControllerTest extends WebTestCase
$this->assertResponseIsSuccessful();
}
public function testUserAccess()
public function testUserAccess(): void
{
$client = static::createClient();
@ -33,6 +37,6 @@ class IndexControllerTest extends WebTestCase
$client->request('GET', '/account/edit');
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
}
}

@ -1,29 +1,33 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \AccessUrlRepository
*/
class AccessUrlRepositoryTest extends KernelTestCase
{
use ChamiloTestTrait;
public function testCount()
public function testCount(): void
{
self::bootKernel();
$count = self::getContainer()->get(AccessUrlRepository::class)->count([]);
// In a fresh installation, Chamilo has one default AccessUrl.
// Added in AccessUrlFixtures.php
$this->assertEquals(1, $count);
$this->assertSame(1, $count);
}
public function testAdminInAccessUrl()
public function testAdminInAccessUrl(): void
{
self::bootKernel();
$accessUrl = $this->getAccessUrl();
@ -31,6 +35,6 @@ class AccessUrlRepositoryTest extends KernelTestCase
$hasUser = $accessUrl->hasUser($admin);
$this->assertEquals(true, $hasUser);
$this->assertTrue($hasUser);
}
}

@ -1,20 +1,21 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CoreBundle\ToolChain;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
/**
* @covers \CourseRepository
*/
class CourseRepositoryTest extends WebTestCase
{
use ChamiloTestTrait;
@ -22,7 +23,7 @@ class CourseRepositoryTest extends WebTestCase
/**
* Create a course with no creator.
*/
public function testCreateNoCreator()
public function testCreateNoCreator(): void
{
self::bootKernel();
$courseRepo = self::getContainer()->get(CourseRepository::class);
@ -38,25 +39,25 @@ class CourseRepositoryTest extends WebTestCase
/**
* Create a course with a creator.
*/
public function testCreate()
public function testCreate(): void
{
$courseRepo = self::getContainer()->get(CourseRepository::class);
$course = $this->createCourse('Test course');
$count = $courseRepo->count([]);
$this->assertEquals(1, $count);
$this->assertSame(1, $count);
// Check tools.
$this->assertEquals(25, count($course->getTools()));
$this->assertSame(25, \count($course->getTools()));
// Check course code.
$this->assertEquals('TEST-COURSE', $course->getCode());
$this->assertSame('TEST-COURSE', $course->getCode());
// The course should connected with a Access URL
$this->assertEquals(1, $course->getUrls()->count());
$this->assertSame(1, $course->getUrls()->count());
}
public function testCourseAccess()
public function testCourseAccess(): void
{
self::bootKernel();
/** @var CourseRepository $courseRepo */
@ -66,10 +67,10 @@ class CourseRepositoryTest extends WebTestCase
$student = $this->createUser('student', 'student', 'student@student.com');
$course->addUser($student,0, null, 5);
$course->addUser($student, 0, null, 5);
$courseRepo->update($course);
$this->assertEquals(1, $course->getUsers()->count());
$this->assertSame(1, $course->getUsers()->count());
}
}

@ -1,31 +1,35 @@
<?php
declare(strict_types=1);
namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \UserRepository
*/
class UserRepositoryTest extends KernelTestCase
{
use ChamiloTestTrait;
public function testCount()
public function testCount(): void
{
self::bootKernel();
$count = self::getContainer()->get(UserRepository::class)->count([]);
// Admin + anon (registered in the DataFixture\AccessUrlAdminFixtures.php)
$this->assertEquals(2, $count);
$this->assertSame(2, $count);
}
public function testCreateUser()
public function testCreateUser(): void
{
self::bootKernel();
$this->createUser('user', 'user', 'user@example.org');
$count = self::getContainer()->get(UserRepository::class)->count([]);
$this->assertEquals(3, $count);
$this->assertSame(3, $count);
}
}

@ -1,22 +1,24 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\Usergroup;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \UsergroupRepository
*/
class UsergroupRepositoryTest extends KernelTestCase
{
use ChamiloTestTrait;
public function testCount()
public function testCount(): void
{
self::bootKernel();
$repo = self::getContainer()->get(UsergroupRepository::class);
@ -29,6 +31,6 @@ class UsergroupRepositoryTest extends KernelTestCase
$this->assertHasNoEntityViolations($usergroup);
$repo->create($usergroup);
$this->assertEquals(1, $repo->count([]));
$this->assertSame(1, $repo->count([]));
}
}

@ -1,24 +1,26 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @covers \Chamilo\CourseBundle\Repository\CDocumentRepository
*/
class CDocumentRepositoryTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testGetDocuments()
public function testGetDocuments(): void
{
$token = $this->getUserToken([]);
$response = $this->createClientWithCredentials($token)->request('GET', '/api/documents');
@ -47,9 +49,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
{
$courseRepo = self::getContainer()->get(CourseRepository::class);
// Get admin.
$admin = $this->getUser('admin');
// Get access url.
$accessUrl = $this->getAccessUrl();
$course = (new Course())
@ -93,7 +93,6 @@ class CDocumentRepositoryTest extends AbstractApiTest
{
$courseRepo = self::getContainer()->get(CourseRepository::class);
// Get admin.
$admin = $this->getUser('admin');
// Get access url.
$accessUrl = $this->getAccessUrl();
@ -127,7 +126,9 @@ class CDocumentRepositoryTest extends AbstractApiTest
'POST',
'/api/documents',
[
'headers' => ['Content-Type' => 'multipart/form-data'],
'headers' => [
'Content-Type' => 'multipart/form-data',
],
'extra' => [
'files' => [
'uploadFile' => $file,

Loading…
Cancel
Save