phpunit: Fix listeners, add tests

pull/3904/head
Julio Montoya 4 years ago
parent da9d92b297
commit 5fd1d8b66a
  1. 11
      composer.json
  2. 2
      ecs.php
  3. 3
      public/main/inc/lib/add_course.lib.inc.php
  4. 2
      src/CoreBundle/DataFixtures/LanguageFixtures.php
  5. 9
      src/CoreBundle/Entity/AbstractResource.php
  6. 6
      src/CoreBundle/Entity/Course.php
  7. 3
      src/CoreBundle/Entity/Listener/CourseListener.php
  8. 10
      src/CoreBundle/Entity/Listener/ResourceListener.php
  9. 2
      src/CoreBundle/Entity/Listener/ResourceNodeListener.php
  10. 1
      src/CoreBundle/Entity/ResourceToRootInterface.php
  11. 1
      src/CoreBundle/ToolChain.php
  12. 6
      tests/CoreBundle/Controller/IndexControllerTest.php
  13. 18
      tests/CoreBundle/Repository/Node/AccessUrlRepositoryTest.php
  14. 8
      tests/CoreBundle/Repository/Node/CourseRepositoryTest.php

@ -32,6 +32,11 @@
"public/main/survey" "public/main/survey"
] ]
}, },
"autoload-dev": {
"psr-4": {
"Chamilo\\Tests\\": "tests/"
}
},
"require": { "require": {
"php": "^7.4 || ^8.0", "php": "^7.4 || ^8.0",
"ext-bcmath": "*", "ext-bcmath": "*",
@ -151,10 +156,12 @@
"require-dev": { "require-dev": {
"behat/behat": "@stable", "behat/behat": "@stable",
"behat/mink": "dev-master", "behat/mink": "dev-master",
"behat/mink-selenium2-driver": "@stable",
"behat/mink-goutte-driver": "dev-master", "behat/mink-goutte-driver": "dev-master",
"behat/mink-selenium2-driver": "@stable",
"dama/doctrine-test-bundle": "^6.5", "dama/doctrine-test-bundle": "^6.5",
"friends-of-behat/mink-extension": "dev-master", "friends-of-behat/mink-extension": "dev-master",
"hautelook/alice-bundle": "^2.9",
"justinrainbow/json-schema": "^5.2",
"phpstan/phpstan": "^0.12", "phpstan/phpstan": "^0.12",
"phpstan/phpstan-doctrine": "^0.12", "phpstan/phpstan-doctrine": "^0.12",
"phpstan/phpstan-symfony": "^0.12", "phpstan/phpstan-symfony": "^0.12",
@ -171,7 +178,7 @@
"symfony/test-pack": "*", "symfony/test-pack": "*",
"symfony/var-dumper": "^5.2", "symfony/var-dumper": "^5.2",
"symfony/web-profiler-bundle": "^5.3", "symfony/web-profiler-bundle": "^5.3",
"symplify/easy-coding-standard": "^9.0", "symplify/easy-coding-standard": "9.3.15",
"vimeo/psalm": "^4.0", "vimeo/psalm": "^4.0",
"weirdan/doctrine-psalm-plugin": "^1.0" "weirdan/doctrine-psalm-plugin": "^1.0"
}, },

@ -87,6 +87,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
Option::PATHS, Option::PATHS,
[ [
__DIR__.'/src', __DIR__.'/src',
__DIR__.'/tests/CoreBundle',
__DIR__.'/tests/CourseBundle',
//__DIR__.'/public/main/admin', //__DIR__.'/public/main/admin',
] ]
); );

@ -767,8 +767,9 @@ class AddCourse
->setSubscribe($subscribe) ->setSubscribe($subscribe)
->setUnsubscribe($unsubscribe) ->setUnsubscribe($unsubscribe)
->setVisualCode($visual_code) ->setVisualCode($visual_code)
->addAccessUrl(api_get_url_entity())
->setCreator(api_get_user_entity()) ->setCreator(api_get_user_entity())
->addAccessUrl(api_get_url_entity()); ;
if (!empty($categories)) { if (!empty($categories)) {
if (!is_array($categories)) { if (!is_array($categories)) {

@ -388,7 +388,7 @@ class LanguageFixtures extends Fixture
->setOriginalName($data['original_name']) ->setOriginalName($data['original_name'])
->setEnglishName($data['english_name']) ->setEnglishName($data['english_name'])
->setIsocode($data['isocode']) ->setIsocode($data['isocode'])
->setAvailable(1 === (int) $data['available']) ->setAvailable(1 === $data['available'])
; ;
$manager->persist($lang); $manager->persist($lang);
} }

@ -77,21 +77,18 @@ abstract class AbstractResource
/** /**
* Use when sending a request to Api platform. * Use when sending a request to Api platform.
* Temporal array that saves the resource link list that will be filled by CreateResourceNodeFileAction.php. * Temporal array that saves the resource link list that will be filled by CreateDocumentFileAction.php.
*/ */
public array $resourceLinkList = []; public array $resourceLinkList = [];
/** /**
* Use when sending request to Chamilo. * Use when sending request to Chamilo.
* Temporal array of objects locates the resource link list that will be filled by CreateResourceNodeFileAction.php. * Temporal array of objects locates the resource link list that will be filled by CreateDocumentFileAction.php.
* *
* @var ResourceLink[] * @var ResourceLink[]
*/ */
public array $resourceLinkEntityList = []; public array $resourceLinkEntityList = [];
/**
* @Assert\NotNull
*/
public ?User $resourceNodeCreator = null; public ?User $resourceNodeCreator = null;
abstract public function getResourceName(): string; abstract public function getResourceName(): string;
@ -112,7 +109,7 @@ abstract class AbstractResource
return $this; return $this;
} }
public function getResourceNodeCreator() public function getResourceNodeCreator(): ?User
{ {
return $this->resourceNodeCreator; return $this->resourceNodeCreator;
} }

@ -47,7 +47,7 @@ use Symfony\Component\Validator\Constraints as Assert;
* @ORM\Entity * @ORM\Entity
* @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener", "Chamilo\CoreBundle\Entity\Listener\CourseListener"}) * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener", "Chamilo\CoreBundle\Entity\Listener\CourseListener"})
*/ */
class Course extends AbstractResource implements ResourceInterface, ResourceWithAccessUrlInterface, ResourceToRootInterface, ResourceIllustrationInterface class Course extends AbstractResource implements ResourceInterface, ResourceWithAccessUrlInterface, ResourceIllustrationInterface
{ {
public const CLOSED = 0; public const CLOSED = 0;
public const REGISTERED = 1; public const REGISTERED = 1;
@ -419,7 +419,7 @@ class Course extends AbstractResource implements ResourceInterface, ResourceWith
public function addTool(CTool $tool) public function addTool(CTool $tool)
{ {
$tool->setCourse($this); $tool->setCourse($this);
$this->tools[] = $tool; $this->tools->add($tool);
return $this; return $this;
} }
@ -450,7 +450,7 @@ class Course extends AbstractResource implements ResourceInterface, ResourceWith
return $this; return $this;
} }
public function addAccessUrl(AccessUrl $url) public function addAccessUrl(AccessUrl $url): self
{ {
$urlRelCourse = new AccessUrlRelCourse(); $urlRelCourse = new AccessUrlRelCourse();
$urlRelCourse->setCourse($this); $urlRelCourse->setCourse($this);

@ -48,7 +48,7 @@ class CourseListener
//error_log('Course listener prePersist'); //error_log('Course listener prePersist');
if ($course) { if ($course) {
// $this->checkLimit($repo, $course, $url); // $this->checkLimit($repo, $course, $url);
//$this->toolChain->addToolsInCourse($this->toolRepository, $course); $this->toolChain->addToolsInCourse($course);
} }
} }
@ -64,7 +64,6 @@ class CourseListener
//$repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course'); //$repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course');
///$this->checkLimit($repo, $course, $url); ///$this->checkLimit($repo, $course, $url);
//$this->toolChain->addToolsInCourse($course); //$this->toolChain->addToolsInCourse($course);
$this->toolChain->addToolsInCourse($course);
} }
} }

@ -9,7 +9,6 @@ namespace Chamilo\CoreBundle\Entity\Listener;
use Chamilo\CoreBundle\Entity\AbstractResource; use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\AccessUrl; use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\EntityAccessUrl;
use Chamilo\CoreBundle\Entity\EntityAccessUrlInterface; use Chamilo\CoreBundle\Entity\EntityAccessUrlInterface;
use Chamilo\CoreBundle\Entity\ResourceFile; use Chamilo\CoreBundle\Entity\ResourceFile;
use Chamilo\CoreBundle\Entity\ResourceLink; use Chamilo\CoreBundle\Entity\ResourceLink;
@ -58,7 +57,7 @@ class ResourceListener
/** /**
* Only in creation. * Only in creation.
*/ */
public function prePersist(AbstractResource $resource, LifecycleEventArgs $event) public function prePersist(AbstractResource $resource, LifecycleEventArgs $event): void
{ {
error_log('Resource listener prePersist for obj: '.\get_class($resource)); error_log('Resource listener prePersist for obj: '.\get_class($resource));
$em = $event->getEntityManager(); $em = $event->getEntityManager();
@ -83,6 +82,7 @@ class ResourceListener
// Check if creator is set with $resource->setCreator() // Check if creator is set with $resource->setCreator()
$creator = $resource->getResourceNodeCreator(); $creator = $resource->getResourceNodeCreator();
if (null === $creator) { if (null === $creator) {
/** @var User|null $creator */ /** @var User|null $creator */
$defaultCreator = $this->security->getUser(); $defaultCreator = $this->security->getUser();
@ -226,7 +226,7 @@ class ResourceListener
} }
// Use by Chamilo. // Use by Chamilo.
$this->setLinks($resourceNode, $resource, $em); $this->setLinks($resource, $em);
if (null !== $resource->getParent()) { if (null !== $resource->getParent()) {
$resourceNode->setParent($resource->getParent()->getResourceNode()); $resourceNode->setParent($resource->getParent()->getResourceNode());
@ -247,7 +247,7 @@ class ResourceListener
public function preUpdate(AbstractResource $resource, PreUpdateEventArgs $event): void public function preUpdate(AbstractResource $resource, PreUpdateEventArgs $event): void
{ {
error_log('Resource listener preUpdate'); error_log('Resource listener preUpdate');
$this->setLinks($resource->getResourceNode(), $resource, $event->getEntityManager()); $this->setLinks($resource, $event->getEntityManager());
if ($resource->hasUploadFile()) { if ($resource->hasUploadFile()) {
$uploadedFile = $resource->getUploadFile(); $uploadedFile = $resource->getUploadFile();
@ -290,7 +290,7 @@ class ResourceListener
$resource->getResourceNode()->setTitle($resourceName); $resource->getResourceNode()->setTitle($resourceName);
} }
public function setLinks(ResourceNode $resourceNode, AbstractResource $resource, $em): void public function setLinks(AbstractResource $resource, $em): void
{ {
error_log('Resource listener setLinks'); error_log('Resource listener setLinks');
$links = $resource->getResourceLinkEntityList(); $links = $resource->getResourceLinkEntityList();

@ -51,7 +51,7 @@ class ResourceNodeListener
*/ */
public function preUpdate(ResourceNode $resourceNode, PreUpdateEventArgs $event) public function preUpdate(ResourceNode $resourceNode, PreUpdateEventArgs $event)
{ {
error_log('resource node preUpdate'); //error_log('resource node preUpdate');
if ($resourceNode->hasResourceFile() && $resourceNode->hasEditableTextContent()) { if ($resourceNode->hasResourceFile() && $resourceNode->hasEditableTextContent()) {
$fileName = $this->resourceNodeRepository->getFilename($resourceNode->getResourceFile()); $fileName = $this->resourceNodeRepository->getFilename($resourceNode->getResourceFile());
error_log(sprintf('fileName: %s', $fileName)); error_log(sprintf('fileName: %s', $fileName));

@ -8,6 +8,7 @@ namespace Chamilo\CoreBundle\Entity;
/** /**
* This will attach the resource to the main resource node root (For example a Course). * This will attach the resource to the main resource node root (For example a Course).
*
* @deprecated use ResourceWithAccessUrlInterface * @deprecated use ResourceWithAccessUrlInterface
*/ */
interface ResourceToRootInterface interface ResourceToRootInterface

@ -210,6 +210,7 @@ class ToolChain
->setPosition($position) ->setPosition($position)
->setVisibility($visibility) ->setVisibility($visibility)
->setParent($course) ->setParent($course)
->setCreator($course->getCreator())
->addCourseLink($course) ->addCourseLink($course)
; ;
$course->addTool($courseTool); $course->addTool($courseTool);

@ -24,11 +24,11 @@ class IndexControllerTest extends WebTestCase
/** @var UserRepository $userRepository */ /** @var UserRepository $userRepository */
$userRepository = $this->getContainer()->get(UserRepository::class); $userRepository = $this->getContainer()->get(UserRepository::class);
// retrieve the test user // retrieve the admin
$testUser = $userRepository->findByUsername('admin'); $admin = $userRepository->findByUsername('admin');
// simulate $testUser being logged in // simulate $testUser being logged in
$client->loginUser($testUser); $client->loginUser($admin);
$client->request('GET', '/account/edit'); $client->request('GET', '/account/edit');

@ -4,7 +4,9 @@
namespace Chamilo\Tests\CoreBundle\Repository\Node; namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository; use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class AccessUrlRepositoryTest extends KernelTestCase class AccessUrlRepositoryTest extends KernelTestCase
@ -13,7 +15,21 @@ class AccessUrlRepositoryTest extends KernelTestCase
{ {
self::bootKernel(); self::bootKernel();
$count = self::getContainer()->get(AccessUrlRepository::class)->count([]); $count = self::getContainer()->get(AccessUrlRepository::class)->count([]);
// localhost default URL (Added in AccessUrlFixtures.php) // In a fresh installation, Chamilo has one default AccessUrl.
// Added in AccessUrlFixtures.php
$this->assertEquals(1, $count); $this->assertEquals(1, $count);
} }
public function testAdminInAccessUrl()
{
self::bootKernel();
$urlRepo = self::getContainer()->get(AccessUrlRepository::class);
/** @var AccessUrl $accessUrl */
$accessUrl = $urlRepo->findOneBy(['url' => AccessUrl::DEFAULT_ACCESS_URL]);
$userRepository = self::getContainer()->get(UserRepository::class);
$admin = $userRepository->findByUsername('admin');
$hasUser = $accessUrl->hasUser($admin);
$this->assertEquals(true, $hasUser);
}
} }

@ -4,11 +4,11 @@
namespace Chamilo\Tests\CoreBundle\Repository\Node; namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository; use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository; use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\Exception\UserNotFoundException;
@ -20,7 +20,7 @@ class CourseRepositoryTest extends WebTestCase
$urlRepo = self::getContainer()->get(AccessUrlRepository::class); $urlRepo = self::getContainer()->get(AccessUrlRepository::class);
$courseRepo = self::getContainer()->get(CourseRepository::class); $courseRepo = self::getContainer()->get(CourseRepository::class);
$accessUrl = $urlRepo->findOneBy(['url' => 'http://localhost/']); $accessUrl = $urlRepo->findOneBy(['url' => AccessUrl::DEFAULT_ACCESS_URL]);
$this->expectException(UserNotFoundException::class); $this->expectException(UserNotFoundException::class);
$course = (new Course()) $course = (new Course())
@ -36,14 +36,14 @@ class CourseRepositoryTest extends WebTestCase
public function testCreate() public function testCreate()
{ {
/** @var UserRepository $userRepository */ /** @var UserRepository $userRepository */
$userRepository = $this->getContainer()->get(UserRepository::class); $userRepository = self::getContainer()->get(UserRepository::class);
$admin = $userRepository->findByUsername('admin'); $admin = $userRepository->findByUsername('admin');
$urlRepo = self::getContainer()->get(AccessUrlRepository::class); $urlRepo = self::getContainer()->get(AccessUrlRepository::class);
$courseRepo = self::getContainer()->get(CourseRepository::class); $courseRepo = self::getContainer()->get(CourseRepository::class);
$accessUrl = $urlRepo->findOneBy(['url' => 'http://localhost/']); $accessUrl = $urlRepo->findOneBy(['url' => AccessUrl::DEFAULT_ACCESS_URL]);
$course = (new Course()) $course = (new Course())
->setTitle('Test course') ->setTitle('Test course')

Loading…
Cancel
Save