Resources: Add tests, remove unused code.

pull/4020/head^2
Julio 4 years ago
parent 4b65b7a58e
commit 050f6e78d9
  1. 89
      src/CoreBundle/Controller/ResourceController.php
  2. 3
      src/CoreBundle/Entity/ResourceFile.php
  3. 20
      tests/CoreBundle/Controller/IndexControllerTest.php
  4. 99
      tests/CoreBundle/Controller/ResourceControllerTest.php
  5. 30
      tests/CoreBundle/Controller/SessionControllerTest.php
  6. 4
      tests/CoreBundle/Controller/UserControllerTest.php
  7. 1
      tests/CoreBundle/Repository/SessionRepositoryTest.php

@ -6,10 +6,7 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Controller;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CoreBundle\Form\Type\ResourceCommentType;
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
use Chamilo\CoreBundle\Traits\ControllerTrait;
@ -19,7 +16,6 @@ use Chamilo\CourseBundle\Controller\CourseControllerInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -116,91 +112,6 @@ class ResourceController extends AbstractResourceController implements CourseCon
);
}
/**
* Shows resource information.
*
* @Route("/{tool}/{type}/{id}/info", methods={"GET", "POST"}, name="chamilo_core_resource_info")
*/
public function infoAction(Request $request): Response
{
$nodeId = (int) $request->get('id');
$repository = $this->getRepositoryFromRequest($request);
$resource = $repository->getResourceFromResourceNode($nodeId);
$this->denyAccessUnlessValidResource($resource);
$resourceNode = $resource->getResourceNode();
$this->denyAccessUnlessGranted(
ResourceNodeVoter::VIEW,
$resourceNode,
$this->trans(sprintf('Unauthorised access to resource #%s', $nodeId))
);
//$this->setBreadCrumb($request, $resourceNode);
$tool = $request->get('tool');
$type = $request->get('type');
$form = $this->createForm(ResourceCommentType::class, null);
$params = [
'resource' => $resource,
'course' => $this->getCourse(),
'tool' => $tool,
'type' => $type,
'comment_form' => $form->createView(),
];
return $this->render(
'@ChamiloCore/Resource/info.html.twig',
$params
);
}
/**
* @deprecated use vue
*
* @Route("/{tool}/{type}/{id}/change_visibility", name="chamilo_core_resource_change_visibility")
*/
public function changeVisibilityAction(Request $request): Response
{
$id = (int) $request->get('id');
$repository = $this->getRepositoryFromRequest($request);
$resource = $repository->getResourceFromResourceNode($id);
$this->denyAccessUnlessValidResource($resource);
/** @var AbstractResource $resource */
$resourceNode = $resource->getResourceNode();
$this->denyAccessUnlessGranted(
ResourceNodeVoter::EDIT,
$resourceNode,
$this->trans('Unauthorised access to resource')
);
if ($this->hasCourse()) {
$link = $resource->getFirstResourceLinkFromCourseSession($this->getCourse(), $this->getSession());
} else {
$link = $resource->getFirstResourceLink();
}
// Use repository to change settings easily.
if ($link && ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility()) {
$repository->setVisibilityDraft($resource);
} else {
$repository->setVisibilityPublished($resource);
}
$result = [
'visibility' => $link->getVisibility(),
'ok' => true,
];
return new JsonResponse($result);
}
/**
* View file of a resource node.
*/

@ -242,6 +242,9 @@ class ResourceFile
return $this->crop;
}
/**
* $crop example: 100,100,100,100 = width,height,x,y.
*/
public function setCrop(string $crop): self
{
$this->crop = $crop;

@ -24,6 +24,26 @@ class IndexControllerTest extends WebTestCase
$this->assertResponseIsSuccessful();
}
public function testLoginJsonWrongFormat(): void
{
$client = static::createClient();
$params = [
'username' => 'admin',
'password' => 'admin',
];
$client->request(
'POST',
'/login_json',
[
'headers' => ['Content-Type' => 'application/test'],
'body' => json_encode($params),
]
);
$this->assertResponseStatusCodeSame(400);
}
public function testLoginPage(): void
{
$client = static::createClient();

@ -7,7 +7,9 @@ declare(strict_types=1);
namespace Chamilo\Tests\CoreBundle\Controller;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Entity\CLp;
use Chamilo\CourseBundle\Repository\CDocumentRepository;
use Chamilo\CourseBundle\Repository\CLpRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -43,9 +45,106 @@ class ResourceControllerTest extends WebTestCase
$id = $node->getUuid()->toRfc4122();
// Download document.
$urlDownload = '/r/document/files/'.$id.'/download';
$client->request('GET', $urlDownload);
$this->assertResponseIsSuccessful();
// Download all documents.
$id = $course->getResourceNode()->getUuid()->toRfc4122();
$urlDownload = '/r/document/files/'.$id.'/download';
$client->request('GET', $urlDownload);
$this->assertResponseIsSuccessful();
}
public function testViewAction(): void
{
$client = static::createClient();
$em = $this->getEntityManager();
$admin = $this->getUser('admin');
$client->loginUser($admin);
$documentRepo = self::getContainer()->get(CDocumentRepository::class);
$course = $this->createCourse('Test');
$document = (new CDocument())
->setFiletype('file')
->setTitle('title 123')
->setTemplate(false)
->setReadonly(false)
->setParent($course)
->setCreator($admin)
->addCourseLink($course)
;
$documentRepo->create($document);
$documentRepo->addFileFromString($document, 'test', 'text/html', 'my file', true);
/** @var CDocument $document */
$document = $documentRepo->find($document->getIid());
$node = $document->getResourceNode();
$this->assertTrue($node->hasResourceFile());
$id = $document->getResourceNode()->getUuid()->toRfc4122();
// View HTML document.
$url = '/r/document/files/'.$id.'/view';
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
$document = (new CDocument())
->setFiletype('file')
->setTitle('title')
->setParent($course)
->setCreator($admin)
->addCourseLink($course)
;
$documentRepo->create($document);
$resourceFile = $documentRepo->addFile($document, $this->getUploadedFile());
$resourceFile->setCrop('100,100,100,100');
$em->persist($resourceFile);
$em->flush();
$node = $document->getResourceNode();
$this->assertTrue($node->hasResourceFile());
$id = $document->getResourceNode()->getUuid()->toRfc4122();
// View image document.
$url = '/r/document/files/'.$id.'/view';
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
// View image document with params.
$url = '/r/document/files/'.$id.'/view';
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
}
public function testLinkAction(): void
{
$client = static::createClient();
$em = $this->getEntityManager();
$admin = $this->getUser('admin');
$client->loginUser($admin);
$course = $this->createCourse('Test');
$lpRepo = self::getContainer()->get(CLpRepository::class);
$lp = (new CLp())
->setName('lp')
->setParent($course)
->setCreator($admin)
->setLpType(CLp::LP_TYPE)
;
$lpRepo->createLp($lp);
$url = '/r/learnpath/lps/'.$lp->getResourceNode()->getId().'/link?cid='.$course->getId();
$client->request('GET', $url);
$redirects = '/main/lp/lp_controller.php?lp_id='.$lp->getIid().'&action=view&cid='.$course->getId().'&sid=0';
$this->assertResponseRedirects($redirects);
$url = '/r/document/files/'.$lp->getResourceNode()->getId().'/link';
$client->request('GET', $url);
$this->assertResponseStatusCodeSame(404);
}
}

@ -6,6 +6,9 @@ declare(strict_types=1);
namespace Chamilo\Tests\CoreBundle\Controller;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Repository\SessionRepository;
use Chamilo\CourseBundle\Entity\CCourseDescription;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
@ -15,9 +18,32 @@ class SessionControllerTest extends AbstractApiTest
public function testAbout(): void
{
self::bootKernel();
$session = $this->createSession('session 1');
$course = $this->createCourse('course title');
$admin = $this->getUser('admin');
$em = $this->getEntityManager();
$courseCoach = $this->createUser('course_coach', '', '', 'ROLE_TEACHER');
$sessionRepo = self::getContainer()->get(SessionRepository::class);
$session->addCourse($course);
$sessionRepo->update($session);
$sessionRepo->addUserInCourse(Session::COURSE_COACH, $courseCoach, $course, $session);
$sessionRepo->update($session);
$item = (new CCourseDescription())
->setTitle('title')
->setContent('content')
->setDescriptionType(1)
->setProgress(100)
->setParent($course)
->setCreator($admin)
->addCourseLink($course, $session)
;
$this->assertHasNoEntityViolations($item);
$em->persist($item);
$em->flush();
$tokenFrom = $this->getUserToken(
[

@ -25,7 +25,9 @@ class UserControllerTest extends WebTestCase
$client->request('GET', '/user/admin');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('#sectionMainContent', 'admin');
$client->request('GET', '/user/nothing');
$this->assertResponseStatusCodeSame(302);
}
}

@ -179,7 +179,6 @@ class SessionRepositoryTest extends AbstractApiTest
$session = $this->createSession('test session');
$sessionRepo = self::getContainer()->get(SessionRepository::class);
$session->addCourse($course);
$sessionRepo->update($session);

Loading…
Cancel
Save