Tests: Add new tests

pull/3959/head
Julio 4 years ago
parent b96d83574b
commit 2a84c60a87
  1. 4
      tests/CoreBundle/Repository/AssetRepositoryTest.php
  2. 2
      tests/CoreBundle/Repository/BranchSyncRepositoryTest.php
  3. 2
      tests/CoreBundle/Repository/CareerRepositoryTest.php
  4. 9
      tests/CoreBundle/Repository/Node/AccessUrlRepositoryTest.php
  5. 4
      tests/CoreBundle/Repository/Node/CourseRepositoryTest.php
  6. 55
      tests/CoreBundle/Repository/Node/UserRepositoryTest.php
  7. 12
      tests/CoreBundle/Repository/Node/UsergroupRepositoryTest.php
  8. 33
      tests/CoreBundle/Repository/ResourceFactoryTest.php
  9. 1
      tests/CoreBundle/Settings/SettingsManagerTest.php
  10. 77
      tests/CoreBundle/Tool/HandlerCollectionTest.php
  11. 121
      tests/CoreBundle/Tool/ToolChainTest.php
  12. 28
      tests/CoreBundle/Twig/Extension/ChamiloExtensionTest.php
  13. 33
      tests/CoreBundle/Twig/SettingsHelperTest.php
  14. 54
      tests/CourseBundle/Repository/CCalendarEventRepositoryTest.php

@ -19,7 +19,7 @@ class AssetRepositoryTest extends AbstractApiTest
{
self::bootKernel();
$em = self::getContainer()->get('doctrine')->getManager();
$em = $this->getManager();
$assetRepo = self::getContainer()->get(AssetRepository::class);
$file = $this->getUploadedFile();
@ -41,7 +41,7 @@ class AssetRepositoryTest extends AbstractApiTest
{
self::bootKernel();
$em = self::getContainer()->get('doctrine')->getManager();
$em = $this->getManager();
$assetRepo = self::getContainer()->get(AssetRepository::class);
$file = $this->getUploadedFile();

@ -19,7 +19,7 @@ class BranchSyncRepositoryTest extends AbstractApiTest
{
self::bootKernel();
$em = self::getContainer()->get('doctrine')->getManager();
$em = $this->getManager();
$repo = self::getContainer()->get(BranchSyncRepository::class);
$item = (new BranchSync())

@ -19,7 +19,7 @@ class CareerRepositoryTest extends AbstractApiTest
{
self::bootKernel();
$em = self::getContainer()->get('doctrine')->getManager();
$em = $this->getManager();
$repo = self::getContainer()->get(CareerRepository::class);
$item = (new Career())

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\ResourceType;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@ -18,10 +19,16 @@ class AccessUrlRepositoryTest extends KernelTestCase
public function testCount(): void
{
self::bootKernel();
$count = self::getContainer()->get(AccessUrlRepository::class)->count([]);
$repo = self::getContainer()->get(AccessUrlRepository::class);
$count = $repo->count([]);
// In a fresh installation, Chamilo has one default AccessUrl.
// Added in AccessUrlFixtures.php
$this->assertSame(1, $count);
$this->assertIsInt($repo->getFirstId());
$this->assertTrue($repo->getFirstId() > 0);
$this->assertInstanceOf(ResourceType::class, $repo->getResourceType());
}
public function testAdminInAccessUrl(): void

@ -58,7 +58,7 @@ class CourseRepositoryTest extends WebTestCase
$this->assertSame(1, $count);
// Check tools.
$this->assertSame(23, \count($course->getTools()));
$this->assertSame(22, \count($course->getTools()));
// Check resource links for each Tool
foreach ($course->getTools() as $tool) {
@ -177,6 +177,6 @@ class CourseRepositoryTest extends WebTestCase
$this->assertResponseIsSuccessful();
// Create a user.
$student2 = $this->createUser('student2');
//$student2 = $this->createUser('student2');
}
}

@ -28,7 +28,7 @@ class UserRepositoryTest extends AbstractApiTest
$userRepo = self::getContainer()->get(UserRepository::class);
$count = $userRepo->count([]);
// By default there are 2 users: admin + anon.
// By default, there are 2 users: admin + anon.
$this->assertSame(3, $count);
$this->assertHasNoEntityViolations($student);
@ -38,11 +38,15 @@ class UserRepositoryTest extends AbstractApiTest
$student->addRole('ROLE_STUDENT');
$userRepo->update($student);
$this->assertTrue($student->hasRole('ROLE_STUDENT'));
$this->assertTrue($student->isEqualTo($student));
$this->assertSame(2, \count($student->getRoles()));
$student->addRole('ROLE_STUDENT');
$userRepo->update($student);
$this->assertTrue($student->isStudent());
$this->assertSame(2, \count($student->getRoles()));
$student->removeRole('ROLE_STUDENT');
@ -55,9 +59,58 @@ class UserRepositoryTest extends AbstractApiTest
$this->assertTrue($student->isActive());
$this->assertTrue($student->isEnabled());
$this->assertFalse($student->isAdmin());
$this->assertFalse($student->isStudentBoss());
$this->assertFalse($student->isSuperAdmin());
$this->assertTrue($student->isCredentialsNonExpired());
}
public function testCreateAdmin(): void
{
self::bootKernel();
$admin = $this->createUser('admin2');
$userRepo = self::getContainer()->get(UserRepository::class);
$this->assertHasNoEntityViolations($admin);
$admin->addRole('ROLE_ADMIN');
$userRepo->update($admin);
$this->assertTrue($admin->isActive());
$this->assertTrue($admin->isEnabled());
$this->assertTrue($admin->isAdmin());
$this->assertFalse($admin->isStudentBoss());
$this->assertFalse($admin->isSuperAdmin());
$this->assertTrue($admin->isCredentialsNonExpired());
}
public function testCreateUserSkipResourceNode(): void
{
$manager = $this->getManager();
$userRepo = self::getContainer()->get(UserRepository::class);
$user = (new User())
->setSkipResourceNode(true)
->setLastname('Doe')
->setFirstname('Joe')
->setUsername('admin2')
->setStatus(1)
->setPlainPassword('admin2')
->setEmail('admin@example.org')
->setOfficialCode('ADMIN')
->setCreatorId(1)
->addUserAsAdmin()//->addGroup($group)
;
$manager->persist($user);
$userRepo->updateUser($user);
$userRepo->addUserToResourceNode($user->getId(), $user->getId());
$manager->flush();
//$this->assertTrue($user->isAdmin());
//$this->assertTrue($user->isSuperAdmin());
$this->assertSame(3, $userRepo->count([]));
}
public function testCreateUserWithApi(): void
{
$token = $this->getUserToken([]);

@ -20,22 +20,22 @@ class UsergroupRepositoryTest extends KernelTestCase
self::bootKernel();
$repo = self::getContainer()->get(UsergroupRepository::class);
$usergroup = (new Usergroup())
$group = (new Usergroup())
->setName('test')
->addAccessUrl($this->getAccessUrl())
->setCreator($this->getUser('admin'))
;
$this->assertHasNoEntityViolations($usergroup);
$repo->create($usergroup);
$this->assertHasNoEntityViolations($group);
$repo->create($group);
$this->assertSame(1, $repo->count([]));
$usergroup->setName('test2');
$repo->update($usergroup);
$group->setName('test2');
$repo->update($group);
$this->assertSame(1, $repo->count([]));
$repo->delete($usergroup);
$repo->delete($group);
$this->assertSame(0, $repo->count([]));
}
}

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Repository;
use Chamilo\CoreBundle\Repository\ResourceFactory;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Repository\CDocumentRepository;
use Chamilo\Tests\ChamiloTestTrait;
use InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ResourceFactoryTest extends WebTestCase
{
use ChamiloTestTrait;
public function testGetRepositoryService(): void
{
self::bootKernel();
$factory = self::getContainer()->get(ResourceFactory::class);
$this->expectException(InvalidArgumentException::class);
$factory->getRepositoryService('aaa', 'bbb');
$repository = $factory->getRepositoryService('document', 'files');
$this->assertInstanceOf(ResourceRepository::class, $repository);
$this->assertInstanceOf(CDocumentRepository::class, $repository);
}
}

@ -19,7 +19,6 @@ class SettingsManagerTest extends AbstractApiTest
{
self::bootKernel();
$em = self::getContainer()->get('doctrine')->getManager();
$settingsManager = self::getContainer()->get(SettingsManager::class);
$this->expectException(InvalidArgumentException::class);

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Tool;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CoreBundle\Tool\AbstractTool;
use Chamilo\CoreBundle\Tool\Agenda;
use Chamilo\CoreBundle\Tool\HandlerCollection;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use InvalidArgumentException;
class HandlerCollectionTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testGetCollection(): void
{
self::bootKernel();
$handler = self::getContainer()->get(HandlerCollection::class);
$collection = $handler->getCollection();
$this->assertNotEmpty($collection);
}
public function testGetHandler(): void
{
self::bootKernel();
$handler = self::getContainer()->get(HandlerCollection::class);
$this->expectException(InvalidArgumentException::class);
$handler->getHandler('bla bla');
$tool = $handler->getHandler('agenda');
$this->assertInstanceOf(AbstractTool::class, $tool);
$this->assertInstanceOf(Agenda::class, $tool);
}
public function testRepositoryHandlers(): void
{
self::bootKernel();
$handler = self::getContainer()->get(HandlerCollection::class);
$collection = $handler->getCollection();
foreach ($collection as $tool) {
$name = $tool->getName();
$this->assertNotEmpty($name);
$types = $tool->getResourceTypes();
//$icon = $tool->getIcon();
//$this->assertNotEmpty($icon, sprintf("Icons for tool %s doesnt exists", $name));
$em = $this->getManager();
/*if (!empty($types)) {
foreach ($types as $entityName) {
$repo = $em->getRepository($entityName);
//var_dump($repo->getClassName());
$msg = sprintf(
'Error in tool %s, entity: %s repo: %s not instance of ResourceRepository',
$name,
$entityName,
\get_class($repo)
);
$this->assertInstanceOf(ResourceRepository::class, $repo, $msg);
}
}*/
}
}
}

@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Tool;
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\BranchSync;
use Chamilo\CoreBundle\Entity\PersonalFile;
use Chamilo\CoreBundle\Entity\ResourceType;
use Chamilo\CoreBundle\Tool\AbstractTool;
use Chamilo\CoreBundle\Tool\GlobalTool;
use Chamilo\CoreBundle\Tool\ToolChain;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
class ToolChainTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testGetToolFromName(): void
{
self::bootKernel();
$toolChain = self::getContainer()->get(ToolChain::class);
$tool = $toolChain->getToolFromName('global');
$this->assertInstanceOf(AbstractTool::class, $tool);
$this->assertInstanceOf(GlobalTool::class, $tool);
}
public function testResourceType(): void
{
self::bootKernel();
$toolChain = self::getContainer()->get(ToolChain::class);
$tool = $toolChain->getToolFromName('global');
$entity = $tool->getEntityByResourceType('urls');
$this->assertSame($entity, AccessUrl::class);
$typeName = 'urls';
$type = $tool->getTypeNameByEntity(AccessUrl::class);
$this->assertSame($typeName, $type);
$type = $toolChain->getResourceTypeNameByEntity(AccessUrl::class);
$this->assertSame($typeName, $type);
$typeName = 'files';
$tool = $toolChain->getToolFromName('user');
$type = $tool->getTypeNameByEntity(PersonalFile::class);
$this->assertSame($typeName, $type);
$type = $toolChain->getResourceTypeNameByEntity(PersonalFile::class);
$this->assertSame($typeName, $type);
}
public function testGetTools(): void
{
self::bootKernel();
$toolChain = self::getContainer()->get(ToolChain::class);
$count = $toolChain->getTools();
$this->assertTrue(\count($count) > 0);
}
public function testCreateTools(): void
{
self::bootKernel();
$toolChain = self::getContainer()->get(ToolChain::class);
$countBefore = \count($toolChain->getTools());
$toolChain->createTools();
$tools = $toolChain->getTools();
$this->assertSame($countBefore, \count($tools));
$em = $this->getManager();
// Delete BranchSync
$branchRepo = $em->getRepository(BranchSync::class);
$items = $branchRepo->findAll();
foreach ($items as $item) {
$em->remove($item);
}
$em->flush();
// Delete AccessUrl
$urlRepo = $em->getRepository(AccessUrl::class);
$items = $urlRepo->findAll();
foreach ($items as $item) {
$em->remove($item);
}
$em->flush();
$resourceTypeRepo = $em->getRepository(ResourceType::class);
$items = $resourceTypeRepo->findAll();
foreach ($items as $item) {
$em->remove($item);
}
$em->flush();
$items = $resourceTypeRepo->findAll();
$this->assertSame([], $items);
$toolChain = self::getContainer()->get(ToolChain::class);
$toolChain->createTools();
$items = $resourceTypeRepo->findAll();
$this->assertNotEmpty($items);
}
}

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Twig\Extension;
use Chamilo\CoreBundle\Twig\Extension\ChamiloExtension;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
class ChamiloExtensionTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testGetIllustration(): void
{
self::bootKernel();
$extension = self::getContainer()->get(ChamiloExtension::class);
$user = $this->createUser('test');
$illustrationUrl = $extension->getIllustration($user);
$this->assertSame('/img/icons/32/unknown.png', $illustrationUrl);
}
}

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Twig;
use Chamilo\CoreBundle\Twig\SettingsHelper;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Sylius\Bundle\SettingsBundle\Model\SettingsInterface;
class SettingsHelperTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testGetSettings(): void
{
self::bootKernel();
$helper = self::getContainer()->get(SettingsHelper::class);
$settings = $helper->getSettings('admin');
$this->assertInstanceOf(SettingsInterface::class, $settings);
$this->assertSame('chamilo_settings', $helper->getName());
$defaultTheme = $helper->getSettingsParameter('platform.theme');
$this->assertSame('chamilo', $defaultTheme);
}
}

@ -40,6 +40,48 @@ class CCalendarEventRepositoryTest extends AbstractApiTest
}
public function testCreatePersonalEvent(): void
{
self::bootKernel();
$user = $this->createUser('test');
$resourceNodeId = $user->getResourceNode()->getId();
$em = $this->getManager();
$repo = self::getContainer()->get(CCalendarEventRepository::class);
// Current server local time (check your php.ini).
$start = new Datetime('2040-06-30 11:00');
$end = new Datetime('2040-06-30 15:00');
// 1. Add event.
$event = (new CCalendarEvent())
->setTitle('hello')
->setContent('content hello')
->setStartDate($start)
->setEndDate($end)
->setCreator($user)
->setParent($user)
->setParentResourceNode($resourceNodeId)
;
$this->assertHasNoEntityViolations($event);
$repo->create($event);
$token = $this->getUserToken(
[
'username' => 'test',
'password' => 'test',
],
true
);
/*$this->createClientWithCredentials($token)->request(
'GET',
'/r/agenda/events/'.$event->getResourceNode()->getId().'/info'
);
$this->assertResponseIsSuccessful();*/
}
public function testCreatePersonalEventApi(): void
{
$user = $this->createUser('test');
$token = $this->getUserToken(
@ -55,7 +97,7 @@ class CCalendarEventRepositoryTest extends AbstractApiTest
$end = new Datetime('2040-06-30 15:00');
// 1. Add event.
$this->createClientWithCredentials($token)->request(
$responseEvent = $this->createClientWithCredentials($token)->request(
'POST',
'/api/c_calendar_events',
[
@ -285,10 +327,12 @@ class CCalendarEventRepositoryTest extends AbstractApiTest
{
$course = $this->createCourse('Test');
$resourceLinkList = [[
'cid' => $course->getId(),
'visibility' => ResourceLink::VISIBILITY_PUBLISHED,
]];
$resourceLinkList = [
[
'cid' => $course->getId(),
'visibility' => ResourceLink::VISIBILITY_PUBLISHED,
],
];
$user = $this->createUser('test');
$token = $this->getUserToken(

Loading…
Cancel
Save