MessageRelUser - add cascade remove + add tests

pull/4020/head^2
Julio 3 years ago
parent 239825ace7
commit 2854f71ec4
  1. 2
      src/CoreBundle/Entity/MessageRelUser.php
  2. 6
      src/CoreBundle/Repository/MessageTagRepository.php
  3. 117
      tests/CoreBundle/Repository/MessageRepositoryTest.php
  4. 16
      tests/CoreBundle/Repository/Node/AccessUrlRepositoryTest.php
  5. 3
      tests/CourseBundle/Repository/CDocumentRepositoryTest.php

@ -86,7 +86,7 @@ class MessageRelUser
/**
* @var Collection|MessageTag[]
*
* @ORM\ManyToMany(targetEntity="MessageTag", inversedBy="messageRelUsers", cascade={"persist"})
* @ORM\ManyToMany(targetEntity="MessageTag", inversedBy="messageRelUsers", cascade={"persist", "remove"})
* @ORM\JoinTable(name="message_rel_user_rel_tags")
*/
#[Assert\Valid]

@ -24,4 +24,10 @@ class MessageTagRepository extends SortableRepository
$this->getEntityManager()->flush();
}
}
public function delete(MessageTag $messageTag): void
{
$this->getEntityManager()->remove($messageTag);
$this->getEntityManager()->flush();
}
}

@ -8,6 +8,7 @@ namespace Chamilo\Tests\CoreBundle\Repository;
use Chamilo\CoreBundle\Entity\Message;
use Chamilo\CoreBundle\Entity\MessageAttachment;
use Chamilo\CoreBundle\Entity\MessageFeedback;
use Chamilo\CoreBundle\Entity\MessageRelUser;
use Chamilo\CoreBundle\Entity\MessageTag;
use Chamilo\CoreBundle\Entity\User;
@ -77,7 +78,58 @@ class MessageRepositoryTest extends AbstractApiTest
$this->assertSame(1, $testUser->getReceivedMessages()->count());
}
public function testCreateMessageWithTags(): void
public function testCreateMessageWithFeedback(): void
{
$em = $this->getEntityManager();
$messageRepo = self::getContainer()->get(MessageRepository::class);
$messageFeedbackRepo = $em->getRepository(MessageFeedback::class);
$admin = $this->getUser('admin');
$testUser = $this->createUser('test');
$message = (new Message())
->setTitle('hello')
->setContent('content')
->setMsgType(Message::MESSAGE_TYPE_INBOX)
->setSender($admin)
->addReceiver($testUser)
->setSendDate(new DateTime())
->setVotes(0)
->setGroup(null)
;
$messageRepo->update($message);
// 1. Message exists in the inbox.
$this->assertSame(1, $messageRepo->count([]));
$feedback = (new MessageFeedback())
->setMessage($message)
->setUser($testUser)
->setUpdatedAt(new DateTime())
->setDisliked(true)
->setLiked(true)
;
$em->persist($feedback);
$em->flush();
$em->clear();
$this->assertSame(1, $messageFeedbackRepo->count([]));
$this->assertNotNull($feedback->getUser());
$this->assertNotNull($feedback->getUpdatedAt());
$this->assertNotNull($feedback->getMessage());
/** @var Message $message */
$message = $messageRepo->find($message->getId());
$this->assertSame(1, $message->getLikes()->count());
$messageRepo->delete($message);
$this->assertSame(0, $messageRepo->count([]));
$this->assertSame(0, $messageFeedbackRepo->count([]));
}
public function testCreateMessageWithTags(): Message
{
$em = $this->getEntityManager();
@ -135,9 +187,6 @@ class MessageRepositoryTest extends AbstractApiTest
->addMessage($receivedMessage)
;
$this->assertSame(1, $this->getViolations($tag)->count());
/*$this->assertHasNoEntityViolations($tag);
$tagRepo->update($tag);*/
$em->clear();
/** @var User $testUser */
@ -145,7 +194,7 @@ class MessageRepositoryTest extends AbstractApiTest
/** @var MessageRelUser $receivedMessage */
$receivedMessage = $testUser->getReceivedMessages()->first();
// Add new tag.
// Add second tag.
$tag = (new MessageTag())
->setTag('my tag 2')
->setUser($testUser)
@ -154,10 +203,64 @@ class MessageRepositoryTest extends AbstractApiTest
$this->assertHasNoEntityViolations($tag);
$messageTagRepo->update($tag);
$this->assertSame(2, $receivedMessage->getTags()->count());
// Tag exists
$this->assertSame(2, $receivedMessage->getTags()->count());
$this->assertSame(1, $messageRepo->count([]));
$this->assertSame(1, $messageRelUserRepo->count([]));
$this->assertSame(2, $messageTagRepo->count([]));
return $message;
}
public function testDeleteMessageTag(): void
{
$em = $this->getEntityManager();
$messageTagRepo = self::getContainer()->get(MessageTagRepository::class);
$messageRepo = self::getContainer()->get(MessageRepository::class);
$messageRelUserRepo = $em->getRepository(MessageRelUser::class);
$message = $this->testCreateMessageWithTags();
/** @var Message $message */
$message = $messageRepo->find($message->getId());
$this->assertSame(1, $message->getReceivers()->count());
/** @var MessageRelUser $messageRelUser */
$messageRelUser = $message->getReceivers()->first();
$tag = $messageTagRepo->find($messageRelUser->getTags()->first());
$messageTagRepo->delete($tag);
/** @var Message $message */
$message = $messageRepo->find($message->getId());
$this->assertSame(1, $message->getReceivers()->count());
$this->assertSame(1, $messageRepo->count([]));
$this->assertSame(1, $messageRelUserRepo->count([]));
$this->assertSame(1, $messageTagRepo->count([]));
}
public function testDeleteMessageWithTag(): void
{
$em = $this->getEntityManager();
$userRepo = self::getContainer()->get(UserRepository::class);
$messageTagRepo = self::getContainer()->get(MessageTagRepository::class);
$messageRepo = self::getContainer()->get(MessageRepository::class);
$messageRelUserRepo = $em->getRepository(MessageRelUser::class);
$message = $this->testCreateMessageWithTags();
/** @var Message $message */
$message = $messageRepo->find($message->getId());
$messageRepo->delete($message);
$this->assertSame(0, $messageRepo->count([]));
$this->assertSame(0, $messageRelUserRepo->count([]));
$this->assertSame(0, $messageTagRepo->count([]));
$this->assertNotNull($this->getUser('admin'));
$this->assertNotNull($this->getUser('test'));
}
public function testCreateMessageWithAttachment(): void

@ -11,6 +11,7 @@ use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
use Chamilo\CoreBundle\Entity\ResourceType;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\Tests\ChamiloTestTrait;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@ -66,6 +67,9 @@ class AccessUrlRepositoryTest extends KernelTestCase
->setLimitTeachers(1000)
->setLimitUsers(1000)
->setLimitActiveCourses(1000)
->setTms(new DateTime())
->setCreatedBy(1)
->setUrlType(true)
;
$this->assertHasNoEntityViolations($accessUrl);
@ -77,9 +81,19 @@ class AccessUrlRepositoryTest extends KernelTestCase
/** @var AccessUrl $accessUrl */
$accessUrl = $repo->find($accessUrl->getId());
$this->assertNotNull($accessUrl->getTms());
$this->assertSame(1, $accessUrl->getLft());
$this->assertSame(2, $accessUrl->getRgt());
$this->assertSame(0, $accessUrl->getLvl());
$this->assertSame(1, $accessUrl->getCreatedBy());
$this->assertTrue($accessUrl->getUrlType());
$this->assertSame(1000, $accessUrl->getLimitActiveCourses());
$this->assertSame(1000, $accessUrl->getLimitCourses());
$this->assertSame(1000, $accessUrl->getLimitSessions());
$this->assertSame(1000, $accessUrl->getLimitTeachers());
$this->assertSame(1000, $accessUrl->getLimitUsers());
$this->assertSame(1000, $accessUrl->getLimitDiskSpace());
$this->assertTrue($accessUrl->hasUser($user));
$this->assertSame($accessUrl->getId(), $accessUrl->getResourceIdentifier());
@ -87,8 +101,6 @@ class AccessUrlRepositoryTest extends KernelTestCase
$this->assertSame('test', $accessUrl->getDescription());
$this->assertSame('https://example.org', (string) $accessUrl);
$this->assertSame('test@example.com', $accessUrl->getEmail());
$this->assertSame(1000, $accessUrl->getLimitDiskSpace());
$this->assertSame(1000, $accessUrl->getLimitCourses());
$this->assertSame(2, $repo->count([]));
$this->assertSame(0, $accessUrl->getSettings()->count());
}

@ -962,6 +962,9 @@ class CDocumentRepositoryTest extends AbstractApiTest
public function testSeparateUsersGroups(): void
{
$usersAndGroupsSeparated = CDocument::separateUsersGroups([]);
$this->assertSame(['groups' => [], 'users' => []], $usersAndGroupsSeparated);
$usersAndGroupsSeparated = CDocument::separateUsersGroups(['USER:1']);
$this->assertCount(1, $usersAndGroupsSeparated['users']);

Loading…
Cancel
Save