Tests: Add phpunit tests, add legacy calls in phpunit tests

pull/3965/head
Julio 3 years ago
parent b10d98304f
commit f9cd930eeb
  1. 2
      config/routes.yaml
  2. 5
      public/main/inc/global.inc.php
  3. 25
      src/CoreBundle/Controller/IndexController.php
  4. 17
      src/CoreBundle/EventListener/LegacyListener.php
  5. 27
      tests/ApplicationAvailabilityFunctionalTest.php
  6. 17
      tests/CoreBundle/Controller/Admin/AdminControllerTest.php

@ -7,7 +7,7 @@ authentication_token:
legacy_main:
path: /main/{name}
defaults: {_controller: 'ChamiloCoreBundle:Legacy:classic'}
defaults: {_controller: 'Chamilo\CoreBundle\Controller\IndexController::classic'}
requirements:
name: .+

@ -10,6 +10,11 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpKernel\HttpKernelInterface;
// Use when running PHPUnit tests.
if (isset($fileToLoad)) {
return;
}
/**
* All legacy Chamilo scripts should include this important file.
*/

@ -55,4 +55,29 @@ class IndexController extends BaseController
return new Response($content);
}
/**
* Use only in PHPUnit tests.
*/
public function classic($name): Response
{
if (1 !== (int) $_ENV['APP_DEBUG']) {
exit;
}
$rootDir = $this->getParameter('kernel.project_dir');
$mainPath = $rootDir.'/public/main/';
$fileToLoad = $mainPath.$name;
ob_start();
require_once $fileToLoad;
$content = ob_get_contents();
ob_end_clean();
return $this->render(
'@ChamiloCore/Layout/layout_one_col.html.twig',
['content' => $content]
);
}
}

@ -8,6 +8,7 @@ namespace Chamilo\CoreBundle\EventListener;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Exception;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
@ -28,11 +29,13 @@ class LegacyListener
private Environment $twig;
private TokenStorageInterface $tokenStorage;
private UserRepository $userRepository;
public function __construct(Environment $twig, TokenStorageInterface $tokenStorage)
public function __construct(Environment $twig, TokenStorageInterface $tokenStorage, UserRepository $userRepository)
{
$this->twig = $twig;
$this->tokenStorage = $tokenStorage;
$this->userRepository = $userRepository;
}
public function onKernelRequest(RequestEvent $event): void
@ -73,7 +76,6 @@ class LegacyListener
$twig = $this->twig;
$token = $this->tokenStorage->getToken();
$userObject = null;
if (null !== $token) {
/** @var User $userObject */
@ -83,15 +85,12 @@ class LegacyListener
$userInfo = [];
$isAdmin = false;
$allowedCreateCourse = false;
$userStatus = null;
if ($userObject instanceof UserInterface) {
$userInfo = api_get_user_info_from_entity($userObject);
if ($userInfo) {
$userStatus = $userObject->getStatus();
$isAdmin = $userInfo['is_admin'];
}
$allowedCreateCourse = 1 === $userStatus;
$isAdmin = $userObject->isAdmin();
$allowedCreateCourse = $userObject->isTeacher();
}
// @todo remove _user/is_platformAdmin/is_allowedCreateCourse
$session->set('_user', $userInfo);
$session->set('is_platformAdmin', $isAdmin);
$session->set('is_allowedCreateCourse', $allowedCreateCourse);
@ -130,7 +129,7 @@ class LegacyListener
// We set cid_reset = true if we enter inside a main/admin url
// CourseListener check this variable and deletes the course session
if (false !== strpos((string) $request->get('name'), 'admin/')) {
if (str_contains((string) $request->get('name'), 'admin/')) {
$session->set('cid_reset', true);
} else {
$session->set('cid_reset', false);

@ -0,0 +1,27 @@
<?php
namespace Chamilo\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
/**
* @dataProvider urlProvider
*/
public function testPageIsSuccessful($url)
{
$client = self::createClient();
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
}
public function urlProvider()
{
yield ['/'];
yield ['/login'];
yield ['/main/auth/lostPassword.php'];
}
}

@ -23,6 +23,23 @@ class AdminControllerTest extends WebTestCase
// simulate $testUser being logged in
$client->loginUser($admin);
$client->request('GET', '/main/admin/index.php');
$content = (string) $client->getResponse()->getContent();
$this->assertResponseIsSuccessful();
$this->assertStringContainsString('User management', $content);
}
public function testAdminSettings(): void
{
$client = static::createClient();
// retrieve the admin
$admin = $this->getUser('admin');
// simulate $testUser being logged in
$client->loginUser($admin);
$client->request('GET', '/admin/settings/platform');
$this->assertResponseIsSuccessful();
}

Loading…
Cancel
Save