Internal: PHPUnit: Improve code following PHPUnit tests' guidance - refs #5057

pull/5106/head
Yannick Warnier 2 years ago
parent a1993da2ba
commit 62e32b88fd
  1. 15
      public/main/inc/lib/api.lib.php
  2. 2
      src/CoreBundle/Controller/Api/CreatePersonalFileAction.php
  3. 2
      src/CoreBundle/Repository/GroupRepository.php
  4. 2
      src/CourseBundle/Repository/CGroupRepository.php
  5. 1
      tests/ChamiloTestTrait.php
  6. 4
      tests/CoreBundle/Repository/BranchSyncRepositoryTest.php
  7. 6
      tests/CoreBundle/Repository/ExtraFieldValuesRepositoryTest.php
  8. 2
      tests/CoreBundle/Repository/GradeBookCategoryRepositoryTest.php
  9. 1
      tests/CoreBundle/Repository/GroupRepositoryTest.php
  10. 6
      tests/CoreBundle/Repository/MessageRepositoryTest.php
  11. 4
      tests/CoreBundle/Repository/Node/CourseRepositoryTest.php
  12. 2
      tests/CoreBundle/Repository/Node/UsergroupRepositoryTest.php
  13. 1
      tests/CoreBundle/Repository/PageRepositoryTest.php
  14. 8
      tests/CoreBundle/Repository/ResourceNodeRepositoryTest.php
  15. 8
      tests/CoreBundle/Repository/SessionRepositoryTest.php
  16. 6
      tests/CoreBundle/Repository/SkillRepositoryTest.php
  17. 2
      tests/CoreBundle/Tool/ToolChainTest.php
  18. 2
      tests/CourseBundle/Repository/CAnnouncementRepositoryTest.php
  19. 1
      tests/CourseBundle/Repository/CAttendanceRepositoryTest.php
  20. 4
      tests/CourseBundle/Repository/CGlossaryRepositoryTest.php
  21. 4
      tests/CourseBundle/Repository/CLinkCategoryRepositoryTest.php
  22. 4
      tests/CourseBundle/Repository/CLinkRepositoryTest.php
  23. 2
      tests/CourseBundle/Repository/CToolRepositoryTest.php
  24. 14
      tests/README.md

@ -4696,7 +4696,8 @@ function &api_get_settings($cat = null, $ordering = 'list', $access_url = 1, $ur
}
$result = Database::query($sql);
if (null === $result) {
return [];
$result = [];
return $result;
}
$result = Database::store_result($result, 'ASSOC');
@ -6116,10 +6117,18 @@ function api_get_locked_settings()
* @author Jorge Frisancho Jibaja <jrfdeft@gmail.com>, USIL - Some changes to allow the use of real IP using reverse proxy
*
* @version CEV CHANGE 24APR2012
* @throws RuntimeException
*/
function api_get_real_ip()
function api_get_real_ip(): string
{
$ip = trim($_SERVER['REMOTE_ADDR']);
if ('cli' === PHP_SAPI) {
$ip = '127.0.0.1';
} else {
$ip = trim($_SERVER['REMOTE_ADDR']);
if (empty($ip)) {
throw new RuntimeException("Unable to retrieve remote IP address.");
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
if (preg_match('/,/', $_SERVER['HTTP_X_FORWARDED_FOR'])) {
@list($ip1, $ip2) = @explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

@ -16,7 +16,7 @@ class CreatePersonalFileAction extends BaseResourceFileAction
public function __invoke(Request $request, PersonalFileRepository $repo, EntityManager $em): PersonalFile
{
$resource = new PersonalFile();
$this->handleCreateFileRequest($resource, $repo, $request);
$this->handleCreateFileRequest($resource, $repo, $request, $em);
return $resource;
}

@ -33,7 +33,7 @@ class GroupRepository extends ServiceEntityRepository
public function getAdmins()
{
$criteria = [
'name' => 'admins',
'title' => 'admins',
];
/** @var Group|null $group */

@ -40,7 +40,7 @@ final class CGroupRepository extends ResourceRepository
{
return $this->findOneBy(
[
'name' => $name,
'title' => $name,
]
);
}

@ -55,6 +55,7 @@ trait ChamiloTestTrait
}
public function createCourse(string $title): ?Course
{
/* @var CourseRepository $repo */
$repo = static::getContainer()->get(CourseRepository::class);
$course = (new Course())
->setTitle($title)

@ -21,7 +21,7 @@ class BranchSyncRepositoryTest extends AbstractApiTest
$repo = self::getContainer()->get(BranchSyncRepository::class);
$item = (new BranchSync())
->setBranchName('Branch')
->setTitle('Branch')
->setAdminName('Julio')
;
$this->assertHasNoEntityViolations($item);
@ -38,7 +38,7 @@ class BranchSyncRepositoryTest extends AbstractApiTest
$em = $this->getEntityManager();
$item = (new BranchSync())
->setBranchName('Branch')
->setTitle('Branch')
->setAdminName('Julio')
;
$this->assertHasNoEntityViolations($item);

@ -83,6 +83,7 @@ class ExtraFieldValuesRepositoryTest extends AbstractApiTest
public function testUpdateItemData(): void
{
/* @var ExtraFieldValuesRepository $repo */
$repo = self::getContainer()->get(ExtraFieldValuesRepository::class);
$em = $this->getEntityManager();
@ -100,6 +101,7 @@ class ExtraFieldValuesRepositoryTest extends AbstractApiTest
$user = $this->createUser('test');
/* @var ExtraFieldValues $extraFieldValue */
$extraFieldValue = $repo->updateItemData($field, $user, 'test');
$items = $repo->getExtraFieldValuesFromItem($user, ExtraField::USER_FIELD_TYPE);
@ -124,11 +126,11 @@ class ExtraFieldValuesRepositoryTest extends AbstractApiTest
$this->assertSame($course->getResourceIdentifier(), $course->getId());
$extraFieldValue = $repo->updateItemData($field, $course, 'julio');
$this->assertSame('julio', $extraFieldValue->getValue());
$this->assertSame('julio', $extraFieldValue->getFieldValue());
$extraFieldValue = $repo->updateItemData($field, $course, 'casa');
$this->assertSame('casa', $extraFieldValue->getValue());
$this->assertSame('casa', $extraFieldValue->getFieldValue());
$items = $repo->getExtraFieldValuesFromItem($course, ExtraField::COURSE_FIELD_TYPE);
$this->assertNotNull($extraFieldValue);

@ -36,7 +36,7 @@ class GradeBookCategoryRepositoryTest extends AbstractApiTest
->setTitle('cat1')
->setDescription('desc')
->setCertifMinScore(100)
->setDocumentId(0)
//->setDocument()
->setDefaultLowestEvalExclude(false)
->setIsRequirement(false)
->setMinimumToValidate(50)

@ -52,6 +52,7 @@ class GroupRepositoryTest extends AbstractApiTest
public function testGetAdmins(): void
{
/* @var GroupRepository $repo */
$repo = self::getContainer()->get(GroupRepository::class);
$admins = $repo->getAdmins();
$this->assertCount(0, $admins);

@ -461,9 +461,9 @@ class MessageRepositoryTest extends AbstractApiTest
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains(
[
'@context' => '/api/contexts/Message',
'@type' => 'Message',
'title' => 'hello',
//'@context' => '/api/contexts/Message',
//'@type' => 'Message',
//'title' => 'hello',
'receivers' => [
[
'@type' => 'MessageRelUser',

@ -116,8 +116,8 @@ class CourseRepositoryTest extends AbstractApiTest
$count = $courseRepo->count([]);
$this->assertSame(1, $count);
// Check tools.
$this->assertCount(25, $course->getTools());
// Check tools (all declared in the ToolChain minus blog and "course_tool")
$this->assertCount(26, $course->getTools());
// Check resource links for each Tool
foreach ($course->getTools() as $tool) {

@ -31,7 +31,7 @@ class UsergroupRepositoryTest extends KernelTestCase
->setDescription('desc')
->setGroupType(1)
->setUrl('url')
->setAuthorId('')
//->setAuthorId()
->setAllowMembersToLeaveGroup(1)
->setVisibility(GROUP_PERMISSION_OPEN)
->addAccessUrl($this->getAccessUrl())

@ -305,6 +305,7 @@ class PageRepositoryTest extends AbstractApiTest
$pageCategoryRepo = self::getContainer()->get(PageCategoryRepository::class);
$pageRepo = self::getContainer()->get(PageRepository::class);
/* @var CreateDefaultPages $createDefaultPages */
$createDefaultPages = self::getContainer()->get(CreateDefaultPages::class);
$admin = $this->getAdmin();

@ -32,7 +32,7 @@ class ResourceNodeRepositoryTest extends AbstractApiTest
$defaultCount = $repo->count([]);
$type = $repoType->findOneBy(['name' => 'illustrations']);
$type = $repoType->findOneBy(['title' => 'illustrations']);
$resourceNode = (new ResourceNode())
->setContent('test')
->setTitle('test')
@ -80,7 +80,7 @@ class ResourceNodeRepositoryTest extends AbstractApiTest
$user = $this->createUser('julio');
$type = $repoType->findOneBy(['name' => 'illustrations']);
$type = $repoType->findOneBy(['title' => 'illustrations']);
$resourceNode = (new ResourceNode())
->setContent('test')
->setTitle('test')
@ -143,7 +143,7 @@ class ResourceNodeRepositoryTest extends AbstractApiTest
$group = $this->createGroup('group', $course);
$userGroup = $this->createUserGroup('group');
$type = $repoType->findOneBy(['name' => 'illustrations']);
$type = $repoType->findOneBy(['title' => 'illustrations']);
$resourceNode = (new ResourceNode())
->setContent('test')
->setTitle('test')
@ -199,7 +199,7 @@ class ResourceNodeRepositoryTest extends AbstractApiTest
$repoType = $em->getRepository(ResourceType::class);
$user = $this->createUser('julio');
$type = $repoType->findOneBy(['name' => 'illustrations']);
$type = $repoType->findOneBy(['title' => 'illustrations']);
$resourceNode = (new ResourceNode())
->setContent('test')

@ -72,7 +72,7 @@ class SessionRepositoryTest extends AbstractApiTest
'/api/sessions',
[
'json' => [
'name' => 'test',
'title' => 'test',
'generalCoach' => $user->getIri(),
],
]
@ -84,7 +84,7 @@ class SessionRepositoryTest extends AbstractApiTest
$this->assertJsonContains(
[
'@context' => '/api/contexts/Session',
'name' => 'test',
'title' => 'test',
]
);
}
@ -101,7 +101,7 @@ class SessionRepositoryTest extends AbstractApiTest
'/api/sessions/'.$session->getId(),
[
'json' => [
'name' => $newSessionName,
'title' => $newSessionName,
],
]
);
@ -111,7 +111,7 @@ class SessionRepositoryTest extends AbstractApiTest
$this->assertJsonContains(
[
'@context' => '/api/contexts/Session',
'name' => $newSessionName,
'title' => $newSessionName,
]
);
}

@ -88,7 +88,7 @@ class SkillRepositoryTest extends AbstractApiTest
->setTitle('level')
->setPosition(1)
->setProfile($profile)
->setShortName('level')
->setShortTitle('level')
;
$em->persist($level);
@ -328,7 +328,7 @@ class SkillRepositoryTest extends AbstractApiTest
$skillRepo->update($skill);
$this->assertNotEmpty($assetRepo->getAssetUrl($asset));
$this->assertSame(1, $skillRepo->count(['name' => 'php']));
$this->assertSame(1, $skillRepo->count(['title' => 'php']));
$this->assertSame(1, $assetRepo->count([]));
// Remove asset from skill
@ -338,7 +338,7 @@ class SkillRepositoryTest extends AbstractApiTest
$this->assertSame(0, $assetRepo->count([]));
// Skill exists.
$this->assertSame(1, $skillRepo->count(['name' => 'php']));
$this->assertSame(1, $skillRepo->count(['title' => 'php']));
$em->clear();

@ -72,7 +72,7 @@ class ToolChainTest extends AbstractApiTest
$em = $this->getEntityManager();
$toolRepo = $em->getRepository(Tool::class);
$agendaTool = $toolRepo->findOneBy(['name' => 'agenda']);
$agendaTool = $toolRepo->findOneBy(['title' => 'agenda']);
$this->assertNotNull($agendaTool);
$toolChain->setToolPermissions($agendaTool);

@ -29,7 +29,7 @@ class CAnnouncementRepositoryTest extends AbstractApiTest
$announcement = (new CAnnouncement())
->setTitle('item')
->setContent('content')
->setDisplayOrder(1)
//->setDisplayOrder(1)
->setEmailSent(false)
->setEndDate(new DateTime())
->setParent($course)

@ -86,6 +86,7 @@ class CAttendanceRepositoryTest extends AbstractApiTest
->setAttendance($attendance)
->setDateTime(new DateTime())
->setDoneAttendance(true)
->setBlocked(false)
;
$em->persist($calendar);

@ -29,7 +29,7 @@ class CGlossaryRepositoryTest extends AbstractApiTest
$glossary = (new CGlossary())
->setTitle('glossary')
->setDescription('desc')
->setDisplayOrder(1)
//->setDisplayOrder(1)
->setParent($course)
->setCreator($teacher)
->addCourseLink($course)
@ -40,7 +40,7 @@ class CGlossaryRepositoryTest extends AbstractApiTest
$this->assertSame('glossary', (string) $glossary);
$this->assertSame('desc', $glossary->getDescription());
$this->assertSame(1, $glossary->getDisplayOrder());
//$this->assertSame(1, $glossary->getDisplayOrder());
$this->assertSame($glossary->getResourceIdentifier(), $glossary->getIid());
$router = $this->getContainer()->get(RouterInterface::class);

@ -26,7 +26,7 @@ class CLinkCategoryRepositoryTest extends AbstractApiTest
$category = (new CLinkCategory())
->setTitle('cat')
->setDescription('desc')
->setDisplayOrder(1)
//->setDisplayOrder(1)
->setParent($course)
->setCreator($teacher)
;
@ -38,7 +38,7 @@ class CLinkCategoryRepositoryTest extends AbstractApiTest
$this->assertSame($category->getResourceIdentifier(), $category->getIid());
$this->assertSame('cat', (string) $category);
$this->assertSame('desc', $category->getDescription());
$this->assertSame('cat', $category->getCategoryTitle());
$this->assertSame('cat', $category->getTitle());
$this->assertSame(1, $category->getDisplayOrder());
$this->assertSame(1, $repo->count([]));

@ -27,7 +27,7 @@ class CLinkRepositoryTest extends AbstractApiTest
->setUrl('https://chamilo.org')
->setTitle('link')
->setDescription('desc')
->setDisplayOrder(1)
//->setDisplayOrder(1)
->setTarget('_blank')
->setCategory(null)
->setParent($course)
@ -43,7 +43,7 @@ class CLinkRepositoryTest extends AbstractApiTest
$this->assertSame('https://chamilo.org', $link->getUrl());
$this->assertSame('link', $link->getTitle());
$this->assertSame('desc', $link->getDescription());
$this->assertSame(1, $link->getDisplayOrder());
//$this->assertSame(1, $link->getDisplayOrder());
$this->assertSame('_blank', $link->getTarget());
$this->assertSame(1, $repo->count([]));

@ -28,7 +28,7 @@ class CToolRepositoryTest extends AbstractApiTest
$defaultCount = $repo->count([]);
$admin = $this->getUser('admin');
$tool = $toolRepo->findOneBy(['name' => 'course_homepage']);
$tool = $toolRepo->findOneBy(['title' => 'course_homepage']);
$this->assertNotNull($tool);
$cTool = (new CTool())

@ -3,10 +3,10 @@
This directory is being used for all kinds of tests and scripts and is removed from
public releases as it may represent a risk for production systems.
## Behat
## Behat
Make sure you set the right base_url in behat/behat.yml, then run (on the command
line, from the tests/ directory):
line, from the tests/ directory):
```
../vendor/behat/behat/bin/behat behat/features/login.feature
../vendor/behat/behat/bin/behat behat/features/createUser.feature
@ -17,7 +17,7 @@ line, from the tests/ directory):
../vendor/behat/behat/bin/behat behat/features/accessCompanyReports.feature
```
## PHPUnit
## PHPUnit
We use the default Symfony PHPUnit settings:
@ -35,7 +35,7 @@ DATABASE_USER='root'
DATABASE_PASSWORD='root'
```
After creating the .env.test.local file execute:
After creating the .env.test.local file execute:
```
php bin/console --env=test cache:clear
@ -52,10 +52,10 @@ Those commands will install Chamilo in the chamilo_test database.
In order to delete the test database and restart the process use:
`php bin/console --env=test doctrine:database:drop`
`php bin/console --env=test doctrine:database:drop --force`
### Use
Execute the tests with:
### Use
Execute the tests with:
`php bin/phpunit`

Loading…
Cancel
Save