Adding course-user tests.

1.10.x
Julio Montoya 10 years ago
parent a938a967f4
commit 3de4911350
  1. 9
      composer.json
  2. 19
      features/backend/course/CourseUserRelationship.feature
  3. 170
      src/Chamilo/CoreBundle/Behat/CourseContext.php
  4. 2
      src/Chamilo/CoreBundle/Controller/BaseResourceController.php
  5. 31
      src/Chamilo/CoreBundle/Entity/Course.php
  6. 34
      src/Chamilo/CoreBundle/Entity/CourseManager.php
  7. 4
      src/Chamilo/CoreBundle/Entity/CourseRelUser.php
  8. 64
      src/Chamilo/CoreBundle/Entity/Manager/CourseManager.php
  9. 37
      src/Chamilo/CoreBundle/Entity/Manager/SessionManager.php
  10. 36
      src/Chamilo/CoreBundle/Entity/Repository/CourseRepository.php
  11. 20
      src/Chamilo/CoreBundle/Resources/config/services.yml
  12. 9
      src/Chamilo/CoreBundle/Security/Authorization/Voter/CourseVoter.php
  13. 5
      src/Chamilo/CoreBundle/Tests/Controller/IndexControllerTest.php
  14. 4
      src/Chamilo/NotebookBundle/Controller/NotebookController.php
  15. 7
      src/Chamilo/UserBundle/Entity/User.php
  16. 9
      web/app.php
  17. 5
      web/app_dev.php

@ -160,7 +160,14 @@
"elao/web-profiler-extra-bundle" : "~2.3@dev",
"lexik/maintenance-bundle": "dev-master",
"jns/xhprof-bundle": "1.0.*@dev",
"facebook/xhprof": "dev-master@dev"
"facebook/xhprof": "dev-master@dev",
"behat/behat": "~3.0",
"behat/symfony2-extension": "~2.0",
"behat/mink-extension": "~2.0",
"behat/mink-browserkit-driver": "~1.2",
"behat/mink-selenium2-driver": "~1.2",
"behat/mink": "~1.6",
"phpunit/phpunit": "4.4.0"
},
"scripts": {
"post-install-cmd": [

@ -0,0 +1,19 @@
Feature: Course User Relationship
In order to setup a course subscription
As a teacher
I need a working relationship
Background:
Given there are following users:
| username | email | plain_password | enabled |
| student | student@example.com | student | yes |
| teacher | teacher@example.com | teacher | yes |
Given I have a course "My course"
Scenario: A course contains a user
When I add student "student" to course "My course"
Then I should find a user "student" in course "My course"
Scenario: A course contains a user
When I add teacher "teacher" to course "My course"
Then I should find a user "teacher" in course "My course"

@ -0,0 +1,170 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Behat;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\EntityRepository;
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext;
use PHPUnit_Framework_TestCase;
/**
* Defines application features from the specific context.
*/
class CourseContext extends DefaultContext implements Context, SnippetAcceptingContext
{
// only in php 5.4
//use KernelDictionary;
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
/**
* @Given there are following users:
*/
public function thereAreFollowingUsers(TableNode $userTable)
{
$userManager = $this->getContainer()->get('fos_user.user_manager');
$em = $this->getEntityManager();
foreach ($userTable as $userHash) {
$user = $userManager->createUser();
$user->setUsername($userHash['username']);
$user->setEmail($userHash['email']);
$user->setPassword($userHash['plain_password']);
$user->setEnabled(1);
$em->persist($user);
}
$em->flush();
}
/**
* @Given I have a course :arg1
*/
public function iHaveACourse($arg1)
{
$em = $this->getEntityManager();
$entity = new Course();
$entity->setTitle($arg1);
$em->persist($entity);
$em->flush();
}
/**
* @Given I have a user :arg1
*/
/*public function iHaveAUser($arg1)
{
$userManager = $this->getContainer()->get('fos_user.user_manager');
$em = $this->getEntityManager();
$user = $userManager->createUser();
$user->setUsername($arg1);
$em->persist($user);
$em->flush();
}*/
/**
* @When I add user :arg1 to course :arg2
*/
public function iAddUserToCourse($username, $courseTitle)
{
$user = $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($username);
/** @var Course $course */
$course = $this->getContainer()->get('chamilo_core.manager.course')->findOneByTitle($courseTitle);
$course->addStudent($user);
$this->getEntityManager()->persist($course);
$this->getEntityManager()->flush();
}
/**
* @When I add teacher :arg1 to course :arg2
*/
public function iAddTeacherToCourse($username, $courseTitle)
{
$user = $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($username);
/** @var Course $course */
$course = $this->getContainer()->get('chamilo_core.manager.course')->findOneByTitle($courseTitle);
$course->addTeacher($user);
$this->getEntityManager()->persist($course);
$this->getEntityManager()->flush();
}
/**
* @When I add student :arg1 to course :arg2
*/
public function iAddStudentToCourse($username, $courseTitle)
{
$user = $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($username);
/** @var Course $course */
$course = $this->getContainer()->get('chamilo_core.manager.course')->findOneByTitle($courseTitle);
$course->addStudent($user);
$this->getEntityManager()->persist($course);
$this->getEntityManager()->flush();
}
/**
* @Then I should find a user :arg1 in course :arg2
*/
public function iShouldFindAUserInCourse($username, $courseTitle)
{
/** @var Course $course */
$course = $this->getRepository('ChamiloCoreBundle:Course')->findOneByTitle($courseTitle);
$found = false;
/** @var CourseRelUser $user */
foreach ($course->getUsers() as $user) {
if ($username === $user->getUser()->getUserName()) {
$found = true;
break;
}
}
PHPUnit_Framework_TestCase::assertTrue($found);
}
/**
* @return \Doctrine\Common\Persistence\ObjectManager|object
*/
public function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getManager();
}
/**
* Returns the Doctrine repository manager for a given entity.
*
* @param string $entityName The name of the entity.
*
* @return EntityRepository
*/
protected function getRepository($entityName)
{
return $this->getEntityManager()->getRepository($entityName);
}
}

@ -228,6 +228,4 @@ abstract class BaseResourceController extends ResourceController
{
return $this->get('security.authorization_checker')->isGranted($attributes, $object);
}
}

@ -9,6 +9,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Chamilo\UserBundle\Entity\User;
/**
* Course
@ -396,6 +397,36 @@ class Course
}
}
/**
* @param User $user
*/
public function addUser(User $user, $relationType, $role, $status)
{
$courseRelUser = new CourseRelUser();
$courseRelUser->setCourse($this);
$courseRelUser->setUser($user);
$courseRelUser->setRelationType($relationType);
$courseRelUser->setRole($role);
$courseRelUser->setStatus($status);
$this->addUsers($courseRelUser);
}
/**
* @param User $user
*/
public function addTeacher(User $user)
{
$this->addUser($user, User::COURSE_MANAGER, "ROLE_TEACHER", "");
}
/**
* @param User $user
*/
public function addStudent(User $user)
{
$this->addUser($user, User::STUDENT, "ROLE_STUDENT", "");
}
/**
* Set id
*

@ -1,34 +0,0 @@
<?php
namespace Chamilo\CoreBundle\Entity;
use Sonata\CoreBundle\Model\BaseEntityManager;
use Sonata\DatagridBundle\Pager\Doctrine\pager;
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
/**
* Class CourseManager
* @package Chamilo\CoreBundle\Entity
*/
class CourseManager extends BaseEntityManager
{
/**
* @param $code
* @return mixed
*/
public function findOneByCode($code)
{
return $this->getRepository()->findOneByCode($code);
}
/**
* @param $name
* @return mixed
*/
public function findByTitle($name)
{
return $this->getRepository()->findByTitle($name);
}
}

@ -1,7 +1,9 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
@ -171,7 +173,7 @@ class CourseRelUser
/**
* Get User
*
* @return string
* @return User
*/
public function getUser()
{

@ -0,0 +1,64 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity\Manager;
use Chamilo\CoreBundle\Entity\Repository\CourseRepository;
use Sonata\CoreBundle\Model\BaseEntityManager;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\UserBundle\Entity\User;
use Sonata\DatagridBundle\Pager\Doctrine\pager;
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
use Doctrine\Common\Collections\Criteria;
/**
* Class CourseManager
* @package Chamilo\CoreBundle\Entity\Manager
*/
class CourseManager extends BaseEntityManager
{
/**
* @return Course
*/
public function createCourse()
{
return $this->create();
}
/**
* @param $code
* @return mixed
*/
public function findOneByCode($code)
{
return $this->getRepository()->findOneByCode($code);
}
/**
* @param $name
* @return mixed
*/
public function findOneByTitle($name)
{
return $this->getRepository()->findOneByTitle($name);
}
/**
* @param User $user
* @param Course $course
* @return bool
*/
public function isUserSubscribedInCourse(User $user, Course $course)
{
$userCollection = $course->getUsers();
$criteria = Criteria::create()
->where(Criteria::expr()->eq("user", $user));
$userCollection = $userCollection->matching($criteria);
if ($userCollection->count()) {
return true;
}
return false;
}
}

@ -0,0 +1,37 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity\Manager;
use Chamilo\CoreBundle\Entity\Repository\CourseRepository;
use Chamilo\CoreBundle\Entity\Session;
use Sonata\CoreBundle\Model\BaseEntityManager;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\UserBundle\Entity\User;
use Sonata\DatagridBundle\Pager\Doctrine\pager;
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
use Doctrine\Common\Collections\Criteria;
/**
* Class SessionManager
* @package Chamilo\CoreBundle\Entity\Manager
*/
class SessionManager extends BaseEntityManager
{
/**
* @return Session
*/
public function createSession()
{
return $this->create();
}
/**
* @param $name
* @return Session
*/
public function findOneByName($name)
{
return $this->getRepository()->findOneByName($name);
}
}

@ -5,13 +5,12 @@ namespace Chamilo\CoreBundle\Entity\Repository;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\UserBundle\Entity\User;
use Chamilo\UserBundle\ChamiloUserBundle;
use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Collections\Criteria;
use Sonata\CoreBundle\Model\BaseEntityManager;
use Doctrine\ORM\QueryBuilder;
/**
* Class CourseRepository
* The functions inside this class must return an instance of QueryBuilder
* @package Chamilo\CoreBundle\Entity\Repository
*/
class CourseRepository extends EntityRepository
@ -57,7 +56,7 @@ class CourseRepository extends EntityRepository
*
* @param Course $course
*
* @return \Doctrine\ORM\QueryBuilder
* @return QueryBuilder
*/
public function getSubscribedStudents(Course $course)
{
@ -68,7 +67,7 @@ class CourseRepository extends EntityRepository
* Gets the students subscribed in the course
* @param Course $course
*
* @return \Doctrine\ORM\QueryBuilder
* @return QueryBuilder
*/
public function getSubscribedCoaches(Course $course)
{
@ -82,7 +81,7 @@ class CourseRepository extends EntityRepository
* Gets the teachers subscribed in the course
* @param Course $course
*
* @return \Doctrine\ORM\QueryBuilder
* @return QueryBuilder
*/
public function getSubscribedTeachers(Course $course)
{
@ -92,7 +91,7 @@ class CourseRepository extends EntityRepository
/**
* @param Course $course
* @param int $status use legacy chamilo constants COURSEMANAGER|STUDENT
* @return \Doctrine\ORM\QueryBuilder
* @return QueryBuilder
*/
public function getSubscribedUsersByStatus(Course $course, $status)
{
@ -102,27 +101,4 @@ class CourseRepository extends EntityRepository
return $queryBuilder;
}
/**
* @param User $user
* @param Course $course
* @return bool
*/
public function isUserSubscribedInCourse(User $user, Course $course)
{
// $queryBuilder = $this->getSubscribedUsers($course);
$userCollection = $course->getUsers();
$criteria = Criteria::create()
->where(Criteria::expr()->eq("user", $user));
$userCollection = $userCollection->matching($criteria);
if ($userCollection->count()) {
return true;
}
return false;
}
}

@ -1,8 +1,20 @@
parameters:
chamilo_core.manager.course.class: Chamilo\CoreBundle\Entity\CourseManager
chamilo_core.manager.course.entity: Chamilo\CoreBundle\Entity\Course
chamilo_core.entity.manager.course_manager.class: Chamilo\CoreBundle\Entity\Manager\CourseManager
chamilo_core.entity.course: Chamilo\CoreBundle\Entity\Course
chamilo_core.entity.manager.session_manager.class: Chamilo\CoreBundle\Entity\Manager\SessionManager
chamilo_core.entity.session: Chamilo\CoreBundle\Entity\Session
services:
chamilo_core.manager.course:
class: %chamilo_core.entity.manager.course_manager.class%
arguments: [%chamilo_core.entity.course%, @doctrine]
chamilo_core.manager.session:
class: %chamilo_core.entity.manager.session_manager.class%
arguments: [%chamilo_core.entity.session%, @doctrine]
twig.extension.chamilo_extension:
class: Chamilo\CoreBundle\Twig\Extension\ChamiloExtension
tags:
@ -25,9 +37,7 @@ services:
chamilo_core.directory_namer.user_image:
class: Chamilo\CoreBundle\Naming\UserImage
chamilo_core.manager.course:
class: %chamilo_core.manager.course.class%
arguments: [%chamilo_core.manager.course.entity%, @doctrine]
chamilo_core.form.type.yes_no:
class: Chamilo\CoreBundle\Form\Type\YesNoType

@ -86,22 +86,21 @@ class CourseVoter extends AbstractVoter
if ($course->isActive()) {
//return true;
}
} else {
// Course in a session.
if ($session->isActive() && $course->isActive()) {
return true;
}
}
return false;
break;
case self::EDIT:
// Teacher
if ($user->getId() === $course->getOwner()->getId()) {
return true;
}
return false;
break;
}
return false;
dump('Course voter false!');
return true;
}
}

@ -1,9 +1,14 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Class IndexControllerTest
* @package Chamilo\CoreBundle\Tests\Controller
*/
class IndexControllerTest extends WebTestCase
{
public function testIndex()

@ -60,10 +60,8 @@ class NotebookController extends ToolBaseCrudController
}
);*/
$this->createNew();
/** @var NotebookRepository $repository */
$repository = $this->getRepository();
$resources = $repository->getResourceByCourse($course);
$source->setData($resources);
@ -155,7 +153,7 @@ class NotebookController extends ToolBaseCrudController
$resource = $this->findOr404($request);
$resourceNode = $resource->getResourceNode();
$link = $this->detectLink($resourceNode);
if (false === $this->get('security.authorization_checker')->isGranted(ResourceLinkVoter::VIEW, $link)) {
if (false === $this->isGranted(ResourceLinkVoter::VIEW, $link)) {
throw new AccessDeniedException('Unauthorised access!');
}

@ -55,6 +55,13 @@ use Chamilo\CoreBundle\Entity\ExtraFieldValues;
*/
class User extends BaseUser implements ParticipantInterface, ThemeUser
{
const COURSE_MANAGER = 1;
const TEACHER = 1;
const SESSION_ADMIN = 3;
const DRH = 4;
const STUDENT = 5;
const ANONYMOUS = 6;
/**
* @var integer
*

@ -1,10 +1,9 @@
<?php
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
umask(0000);
use Symfony\Component\ClassLoader\ApcClassLoader;
//use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
// Use APC for autoloading to improve performance.
@ -20,12 +19,12 @@ require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
require_once __DIR__.'/legacy.php';
use Sonata\PageBundle\Request\RequestFactory;
// if you want to use the SonataPageBundle with multisite
// using different relative paths, you must change the request
// object to use the SiteRequest
use Sonata\PageBundle\Request\RequestFactory;
$request = RequestFactory::createFromGlobals('host_with_path_by_locale');
// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
$request->enableHttpMethodParameterOverride();

@ -25,12 +25,11 @@ require_once __DIR__.'/legacy.php';
/*use Symfony\Component\HttpFoundation\Request;
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();*/
$request = Sonata\PageBundle\Request\RequestFactory::createFromGlobals('host_with_path_by_locale');
use Sonata\PageBundle\Request\RequestFactory;
$request = RequestFactory::createFromGlobals('host_with_path_by_locale');
$request->enableHttpMethodParameterOverride();
$kernel = new AppKernel('dev', true);
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Loading…
Cancel
Save