From 3de491135083701136f86cafe0a8bb6c1c6ec5e1 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 24 Dec 2014 15:01:17 +0100 Subject: [PATCH] Adding course-user tests. --- composer.json | 9 +- .../course/CourseUserRelationship.feature | 19 ++ .../CoreBundle/Behat/CourseContext.php | 170 ++++++++++++++++++ .../Controller/BaseResourceController.php | 2 - src/Chamilo/CoreBundle/Entity/Course.php | 31 ++++ .../CoreBundle/Entity/CourseManager.php | 34 ---- .../CoreBundle/Entity/CourseRelUser.php | 4 +- .../Entity/Manager/CourseManager.php | 64 +++++++ .../Entity/Manager/SessionManager.php | 37 ++++ .../Entity/Repository/CourseRepository.php | 36 +--- .../CoreBundle/Resources/config/services.yml | 20 ++- .../Authorization/Voter/CourseVoter.php | 9 +- .../Tests/Controller/IndexControllerTest.php | 5 + .../Controller/NotebookController.php | 4 +- src/Chamilo/UserBundle/Entity/User.php | 7 + web/app.php | 9 +- web/app_dev.php | 5 +- 17 files changed, 376 insertions(+), 89 deletions(-) create mode 100644 features/backend/course/CourseUserRelationship.feature create mode 100644 src/Chamilo/CoreBundle/Behat/CourseContext.php delete mode 100644 src/Chamilo/CoreBundle/Entity/CourseManager.php create mode 100644 src/Chamilo/CoreBundle/Entity/Manager/CourseManager.php create mode 100644 src/Chamilo/CoreBundle/Entity/Manager/SessionManager.php diff --git a/composer.json b/composer.json index 756c6ced88..5dcb550195 100755 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/features/backend/course/CourseUserRelationship.feature b/features/backend/course/CourseUserRelationship.feature new file mode 100644 index 0000000000..4d7a9bc61d --- /dev/null +++ b/features/backend/course/CourseUserRelationship.feature @@ -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" diff --git a/src/Chamilo/CoreBundle/Behat/CourseContext.php b/src/Chamilo/CoreBundle/Behat/CourseContext.php new file mode 100644 index 0000000000..a08b34f4f6 --- /dev/null +++ b/src/Chamilo/CoreBundle/Behat/CourseContext.php @@ -0,0 +1,170 @@ +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); + } +} diff --git a/src/Chamilo/CoreBundle/Controller/BaseResourceController.php b/src/Chamilo/CoreBundle/Controller/BaseResourceController.php index d081903fd4..98416397cb 100644 --- a/src/Chamilo/CoreBundle/Controller/BaseResourceController.php +++ b/src/Chamilo/CoreBundle/Controller/BaseResourceController.php @@ -228,6 +228,4 @@ abstract class BaseResourceController extends ResourceController { return $this->get('security.authorization_checker')->isGranted($attributes, $object); } - - } diff --git a/src/Chamilo/CoreBundle/Entity/Course.php b/src/Chamilo/CoreBundle/Entity/Course.php index 89b5bde392..a4f6bca258 100644 --- a/src/Chamilo/CoreBundle/Entity/Course.php +++ b/src/Chamilo/CoreBundle/Entity/Course.php @@ -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 * diff --git a/src/Chamilo/CoreBundle/Entity/CourseManager.php b/src/Chamilo/CoreBundle/Entity/CourseManager.php deleted file mode 100644 index eabb840988..0000000000 --- a/src/Chamilo/CoreBundle/Entity/CourseManager.php +++ /dev/null @@ -1,34 +0,0 @@ -getRepository()->findOneByCode($code); - } - - /** - * @param $name - * @return mixed - */ - public function findByTitle($name) - { - return $this->getRepository()->findByTitle($name); - } -} diff --git a/src/Chamilo/CoreBundle/Entity/CourseRelUser.php b/src/Chamilo/CoreBundle/Entity/CourseRelUser.php index f5d5cab56a..3fb206a677 100644 --- a/src/Chamilo/CoreBundle/Entity/CourseRelUser.php +++ b/src/Chamilo/CoreBundle/Entity/CourseRelUser.php @@ -1,7 +1,9 @@ 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; + } +} diff --git a/src/Chamilo/CoreBundle/Entity/Manager/SessionManager.php b/src/Chamilo/CoreBundle/Entity/Manager/SessionManager.php new file mode 100644 index 0000000000..30066a4091 --- /dev/null +++ b/src/Chamilo/CoreBundle/Entity/Manager/SessionManager.php @@ -0,0 +1,37 @@ +create(); + } + + /** + * @param $name + * @return Session + */ + public function findOneByName($name) + { + return $this->getRepository()->findOneByName($name); + } +} diff --git a/src/Chamilo/CoreBundle/Entity/Repository/CourseRepository.php b/src/Chamilo/CoreBundle/Entity/Repository/CourseRepository.php index 02fd5a2171..0d7c9b34c6 100644 --- a/src/Chamilo/CoreBundle/Entity/Repository/CourseRepository.php +++ b/src/Chamilo/CoreBundle/Entity/Repository/CourseRepository.php @@ -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; - - } } diff --git a/src/Chamilo/CoreBundle/Resources/config/services.yml b/src/Chamilo/CoreBundle/Resources/config/services.yml index ece0ab4c90..8ba41c353f 100644 --- a/src/Chamilo/CoreBundle/Resources/config/services.yml +++ b/src/Chamilo/CoreBundle/Resources/config/services.yml @@ -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 diff --git a/src/Chamilo/CoreBundle/Security/Authorization/Voter/CourseVoter.php b/src/Chamilo/CoreBundle/Security/Authorization/Voter/CourseVoter.php index 5f7268fc35..aa98d4ae56 100644 --- a/src/Chamilo/CoreBundle/Security/Authorization/Voter/CourseVoter.php +++ b/src/Chamilo/CoreBundle/Security/Authorization/Voter/CourseVoter.php @@ -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; } } diff --git a/src/Chamilo/CoreBundle/Tests/Controller/IndexControllerTest.php b/src/Chamilo/CoreBundle/Tests/Controller/IndexControllerTest.php index e5cfc77e20..2d63ceeb6e 100644 --- a/src/Chamilo/CoreBundle/Tests/Controller/IndexControllerTest.php +++ b/src/Chamilo/CoreBundle/Tests/Controller/IndexControllerTest.php @@ -1,9 +1,14 @@ 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!'); } diff --git a/src/Chamilo/UserBundle/Entity/User.php b/src/Chamilo/UserBundle/Entity/User.php index 41eae23234..ac82c3f326 100644 --- a/src/Chamilo/UserBundle/Entity/User.php +++ b/src/Chamilo/UserBundle/Entity/User.php @@ -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 * diff --git a/web/app.php b/web/app.php index 2adff20d18..8d90972b85 100644 --- a/web/app.php +++ b/web/app.php @@ -1,10 +1,9 @@ enableHttpMethodParameterOverride(); diff --git a/web/app_dev.php b/web/app_dev.php index 097e520bb6..280eb3dbdb 100644 --- a/web/app_dev.php +++ b/web/app_dev.php @@ -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);