User: add UserToJsonNormalizerTest + fix ArrayCollection

+ fix default SettingsCurrent values
pull/4020/head^2
Julio 3 years ago
parent 35dbb596f0
commit 2680f7601e
  1. 5
      src/CoreBundle/Entity/SettingsCurrent.php
  2. 3
      src/CoreBundle/Entity/User.php
  3. 35
      src/CoreBundle/Serializer/ResourceNormalizer.php
  4. 60
      src/CoreBundle/Serializer/UserToJsonNormalizer.php
  5. 25
      tests/CoreBundle/Serializer/UserToJsonNormalizerTest.php

@ -81,12 +81,12 @@ class SettingsCurrent
/**
* @ORM\Column(name="scope", type="string", length=50, nullable=true)
*/
protected string $scope;
protected ?string $scope = null;
/**
* @ORM\Column(name="subkeytext", type="string", length=255, nullable=true)
*/
protected string $subkeytext;
protected ?string $subkeytext = null;
/**
* @ORM\Column(name="access_url_changeable", type="integer", nullable=false)
@ -103,6 +103,7 @@ class SettingsCurrent
public function __construct()
{
$this->accessUrlLocked = 0;
$this->scope = '';
}
public function setVariable(string $variable): self

@ -117,6 +117,7 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
* @ApiProperty(iri="http://schema.org/contentUrl")
*/
#[Groups([
'user_export',
'user:read',
'resource_node:read',
'document:read',
@ -148,6 +149,7 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
*/
#[Assert\NotBlank]
#[Groups([
'user_export',
'user:read',
'user:write',
'course:read',
@ -836,6 +838,7 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
$this->friends = new ArrayCollection();
$this->friendsWithMe = new ArrayCollection();
$this->gradeBookLinkEvalLogs = new ArrayCollection();
$this->messageTags = new ArrayCollection();
$this->sequenceValues = new ArrayCollection();
$this->trackEExerciseConfirmations = new ArrayCollection();
$this->trackEAccessCompleteList = new ArrayCollection();

@ -42,23 +42,28 @@ final class ResourceNormalizer implements ContextAwareNormalizerInterface, Norma
$context[self::ALREADY_CALLED] = true;
$request = $this->requestStack->getCurrentRequest();
$getFile = false;
$courseId = 0;
$sessionId = 0;
$groupId = 0;
if ($request) {
$getFile = $request->get('getFile');
$courseId = (int) $request->get('cid');
if (empty($courseId)) {
// Try with cid from session
$courseId = (int) $request->getSession()->get('cid');
}
$getFile = $request->get('getFile');
$courseId = (int) $request->get('cid');
if (empty($courseId)) {
// Try with cid from session
$courseId = (int) $request->getSession()->get('cid');
}
$sessionId = (int) $request->get('sid');
if (empty($sessionId)) {
$sessionId = (int) $request->getSession()->get('sid');
}
$sessionId = (int) $request->get('sid');
if (empty($sessionId)) {
$sessionId = (int) $request->getSession()->get('sid');
}
$groupId = (int) $request->get('gid');
if (empty($groupId)) {
$groupId = (int) $request->getSession()->get('gid');
$groupId = (int) $request->get('gid');
if (empty($groupId)) {
$groupId = (int) $request->getSession()->get('gid');
}
}
if ($object->hasResourceNode()) {

@ -43,20 +43,21 @@ use DateTime;
use DateTimeInterface;
use Doctrine\ORM\EntityManager;
use SocialManager;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
final class UserToJsonNormalizer
{
private EntityManager $em;
private UserRepository $userRepository;
private SerializerInterface $serializer;
public function __construct(EntityManager $em, UserRepository $userRepository)
public function __construct(EntityManager $em, UserRepository $userRepository, SerializerInterface $serializer)
{
$this->em = $em;
$this->userRepository = $userRepository;
$this->serializer = $serializer;
}
/**
@ -74,10 +75,10 @@ final class UserToJsonNormalizer
/** @var User $user */
$user = $this->userRepository->find($userId);
$user->setPassword($substitutionTerms['password']);
$user->setSalt($substitutionTerms['salt']);
$user->setPassword($substitutionTerms['password'] ?? '');
$user->setSalt($substitutionTerms['salt'] ?? '');
$noDataLabel = $substitutionTerms['empty'];
$noDataLabel = $substitutionTerms['empty'] ?? '';
// Dummy content
$user->setDateOfBirth(null);
@ -99,22 +100,22 @@ final class UserToJsonNormalizer
$user->setWebsite($noDataLabel);
//$user->setToken($noDataLabel);
$friends = SocialManager::get_friends($userId);
/*$friends = SocialManager::get_friends($userId);
$friendList = [];
if (!empty($friends)) {
foreach ($friends as $friend) {
$friendList[] = $friend['user_info']['complete_name'];
}
}
}*/
$agenda = new Agenda('personal');
/*$agenda = new Agenda('personal');
$events = $agenda->getEvents(0, 0, 0, 0, $userId, 'array');
$eventList = [];
if (!empty($events)) {
foreach ($events as $event) {
$eventList[] = $event['title'].' '.$event['start_date_localtime'].' / '.$event['end_date_localtime'];
}
}
}*/
// GradebookCertificate
$result = $em->getRepository(GradebookCertificate::class)->findBy([
@ -134,7 +135,7 @@ final class UserToJsonNormalizer
// TrackEExercises
$criteria = [
'exeUserId' => $userId,
'user' => $userId,
];
$result = $em->getRepository(TrackExercise::class)->findBy($criteria);
$trackEExercises = [];
@ -189,7 +190,7 @@ final class UserToJsonNormalizer
}
$checkEntities = [
TrackELogin::class => 'loginUserId',
TrackELogin::class => 'user',
TrackEAccess::class => 'accessUserId',
TrackEOnline::class => 'loginUserId',
TrackEDefault::class => 'defaultUserId',
@ -383,7 +384,6 @@ final class UserToJsonNormalizer
];
$cForumThreadList[] = implode(', ', $list);
}
// CForumAttachment
/*$criteria = [
'threadPosterId' => $userId,
@ -758,19 +758,14 @@ final class UserToJsonNormalizer
//$extraFieldValues = new \ExtraFieldValue('user');
$lastLogin = $user->getLastLogin();
if (null === $lastLogin) {
/*if (null === $lastLogin) {
$login = $this->userRepository->getLastLogin($user);
if (null !== $login) {
$lastLogin = $login->getLoginDate();
}
}
}*/
$user->setLastLogin($lastLogin);
/*$dateNormalizer = new GetSetMethodNormalizer();
$dateNormalizer->setCircularReferenceHandler(function ($object) {
return get_class($object);
});*/
$ignore = [
'twoStepVerificationCode',
'biography',
@ -805,30 +800,9 @@ final class UserToJsonNormalizer
'resourceNode',
];
$callback = function ($dateTime) {
return $dateTime instanceof DateTime ? $dateTime->format(DateTimeInterface::ATOM) : '';
};
$defaultContext = [
AbstractNormalizer::CALLBACKS => [
'createdAt' => $callback,
'lastLogin' => $callback,
'registrationDate' => $callback,
'memberSince' => $callback,
],
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
return \get_class($object);
},
];
$normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
$serializer = new Serializer(
[$normalizer],
[new JsonEncoder()]
);
return $serializer->serialize($user, 'json', [
return $this->serializer->serialize($user, 'json', [
AbstractNormalizer::IGNORED_ATTRIBUTES => $ignore,
'groups' => ['user_export'],
]);
}
}

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Serializer;
use Chamilo\CoreBundle\Serializer\UserToJsonNormalizer;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class UserToJsonNormalizerTest extends WebTestCase
{
use ChamiloTestTrait;
public function testSerialize(): void
{
$serializer = static::getContainer()->get(UserToJsonNormalizer::class);
$student = $this->createUser('student');
$result = $serializer->getPersonalDataToJson($student->getId(), []);
$this->assertStringContainsString('student', $result);
}
}
Loading…
Cancel
Save