Add MessageTag.position + add test

pull/3924/head
Julio Montoya 5 years ago
parent e4aa3eddb8
commit 50a5dfe2fb
  1. 8
      src/CoreBundle/Entity/Message.php
  2. 20
      src/CoreBundle/Entity/MessageTag.php
  3. 2
      src/CoreBundle/Migrations/Schema/V200/Version20200821224242.php
  4. 8
      src/CoreBundle/Repository/MessageTagRepository.php
  5. 35
      tests/CoreBundle/Repository/MessageTagRepositoryTest.php

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
@ -89,6 +90,7 @@ use Symfony\Component\Validator\Constraints as Assert;
'msgType' => 'exact',
'userSender' => 'exact',
'userReceiver' => 'exact',
'tags' => 'exact',
])]
class Message
{
@ -128,6 +130,12 @@ class Message
self::MESSAGE_TYPE_OUTBOX,
self::MESSAGE_TYPE_PROMOTED,
])]
/*#[ApiProperty(attributes: [
'openapi_context' => [
'type' => 'int',
'enum' => [self::MESSAGE_TYPE_INBOX, self::MESSAGE_TYPE_OUTBOX],
],
])]*/
#[Groups(['message:read', 'message:write'])]
protected int $msgType;

@ -13,6 +13,7 @@ use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
@ -81,6 +82,7 @@ class MessageTag
protected ?int $id = null;
/**
* @Gedmo\SortableGroup()
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="messageTags")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="cascade")
*/
@ -102,6 +104,12 @@ class MessageTag
#[Groups(['message_tag:read', 'message_tag:write'])]
protected string $color;
/**
* @Gedmo\SortablePosition()
* @ORM\Column(name="position", type="integer")
*/
protected int $position;
/**
* @var Collection|Message[]
*
@ -156,6 +164,18 @@ class MessageTag
return $this;
}
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
public function getPosition(): int
{
return $this->position;
}
public function getMessages(): Collection
{
return $this->messages;

@ -142,7 +142,7 @@ final class Version20200821224242 extends AbstractMigrationChamilo
}
if (!$schema->hasTable('message_tag')) {
$this->addSql("CREATE TABLE message_tag (id BIGINT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, tag VARCHAR(255) NOT NULL, color VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', updated_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', INDEX IDX_2ABC3D6FA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;");
$this->addSql("CREATE TABLE message_tag (id BIGINT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, tag VARCHAR(255) NOT NULL, color VARCHAR(255) NOT NULL, position INT NOT NULL, created_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', updated_at DATETIME NOT NULL COMMENT '(DC2Type:datetime)', INDEX IDX_2ABC3D6FA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;");
$this->addSql('CREATE TABLE message_rel_tags (message_id BIGINT NOT NULL, message_tag_id BIGINT NOT NULL, INDEX IDX_D07232D6537A1329 (message_id), INDEX IDX_D07232D68DF5FE1E (message_tag_id), PRIMARY KEY(message_id, message_tag_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;');
$this->addSql('ALTER TABLE message_tag ADD CONSTRAINT FK_2ABC3D6FA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE message_rel_tags ADD CONSTRAINT FK_D07232D6537A1329 FOREIGN KEY (message_id) REFERENCES message (id) ON DELETE CASCADE');

@ -8,13 +8,15 @@ namespace Chamilo\CoreBundle\Repository;
use Chamilo\CoreBundle\Entity\MessageTag;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Gedmo\Sortable\Entity\Repository\SortableRepository;
class MessageTagRepository extends ServiceEntityRepository
class MessageTagRepository extends SortableRepository
{
public function __construct(ManagerRegistry $registry)
public function __construct(EntityManagerInterface $em)
{
parent::__construct($registry, MessageTag::class);
parent::__construct($em, $em->getClassMetadata(MessageTag::class));
}
public function update(MessageTag $message, $andFlush = true): void

@ -20,7 +20,7 @@ class MessageTagRepositoryTest extends WebTestCase
{
use ChamiloTestTrait;
public function testCreateTag(): void
public function testCreateTagAndDeleteUser(): void
{
self::bootKernel();
$tagRepo = self::getContainer()->get(MessageTagRepository::class);
@ -48,6 +48,39 @@ class MessageTagRepositoryTest extends WebTestCase
$count = $tagRepo->count([]);
$this->assertSame(0, $count);
$this->assertSame(0, $tag->getPosition());
}
public function testCreateTags(): void
{
$tagRepo = self::getContainer()->get(MessageTagRepository::class);
$testUser = $this->createUser('test');
// Create first tag.
$tag =
(new MessageTag())
->setTag('tag 1')
->setUser($testUser)
;
$this->assertHasNoEntityViolations($tag);
$tagRepo->update($tag);
$this->assertSame(0, $tag->getPosition());
// Create second tag
$tag2 =
(new MessageTag())
->setTag('tag 2')
->setUser($testUser)
;
$this->assertHasNoEntityViolations($tag2);
$tagRepo->update($tag2);
$count = $tagRepo->count([]);
$this->assertSame(2, $count);
$this->assertSame(1, $tag2->getPosition());
}
public function testCreateTagWithSameName(): void

Loading…
Cancel
Save