Exemplo n.º 1
0
    def setUpTestData(cls):
        academic_year = AcademicYearFactory()
        minor_list_type = EducationGroupTypeFactory(
            category=education_group_categories.GROUP,
            name=education_group_types.GroupType.MINOR_LIST_CHOICE.name,
        )
        common_type = EducationGroupTypeFactory(
            category=education_group_categories.GROUP,
            name=education_group_types.GroupType.COMMON_CORE,
        )

        cls.base_1 = GroupFactory(education_group_type=common_type,
                                  acronym="BASE",
                                  academic_year=academic_year)
        child_1 = GroupFactory(education_group_type=common_type,
                               acronym="CHILD",
                               academic_year=academic_year)
        minor_list_choice = GroupFactory(education_group_type=minor_list_type,
                                         acronym="MINOR LIST",
                                         academic_year=academic_year)

        minor_content_1 = MiniTrainingFactory(
            education_group_type=minor_list_type, academic_year=academic_year)
        minor_content_2 = MiniTrainingFactory(
            education_group_type=minor_list_type, academic_year=academic_year)

        cls.groupe_element_yr_1 = GroupElementYearFactory(
            parent=cls.base_1, child_branch=minor_list_choice)
        cls.groupe_element_yr_2 = GroupElementYearFactory(parent=cls.base_1,
                                                          child_branch=child_1)
        cls.groupe_element_yr_3 = GroupElementYearFactory(
            parent=minor_list_choice, child_branch=minor_content_1)
        cls.groupe_element_yr_4 = GroupElementYearFactory(
            parent=minor_list_choice, child_branch=minor_content_2)
Exemplo n.º 2
0
 def setUpTestData(cls):
     cls.academic_year = AcademicYearFactory()
     cls.parent = TrainingFactory(
         academic_year=cls.academic_year,
         education_group_type__learning_unit_child_allowed=True)
     cls.child_leaf = LearningUnitYearFactory()
     cls.child_branch = MiniTrainingFactory(academic_year=cls.academic_year)
Exemplo n.º 3
0
    def setUpTestData(cls):
        """
        |LBIOL212O - Cours au choix
        |--Common Core
               |-- Learning unit year
        """
        cls.academic_year = AcademicYearFactory(year=2018)
        cls.mini_training = MiniTrainingFactory(
            acronym="CCHOIXM60",
            partial_acronym='LBIOL212O',
            academic_year=cls.academic_year)
        cls.common_core = GroupFactory(
            education_group_type__name=GroupType.COMMON_CORE.name,
            academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.mini_training,
                                child_branch=cls.common_core,
                                child_leaf=None)
        cls.learning_unit_year = LearningUnitYearFactory(
            academic_year=cls.academic_year,
            learning_container_year__academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.common_core,
                                child_branch=None,
                                child_leaf=cls.learning_unit_year)

        cls.person = PersonFactory()
        url_kwargs = {
            'partial_acronym': cls.mini_training.partial_acronym,
            'year': cls.mini_training.academic_year.year
        }
        cls.url = reverse('education_group_api_v1:' +
                          MiniTrainingTreeView.name,
                          kwargs=url_kwargs)
Exemplo n.º 4
0
    def setUpTestData(cls):
        cls.academic_year = AcademicYearFactory()
        cls.education_group_year_root = TrainingFactory(academic_year=cls.academic_year)
        cls.education_group_year_childs = [MiniTrainingFactory(academic_year=cls.academic_year) for _ in range(0, 3)]

        cls.group_element_years_root_to_child = [
            GroupElementYearFactory(parent=cls.education_group_year_root,
                                    child_leaf=None,
                                    child_branch=cls.education_group_year_childs[i])
            for i in range(0, len(cls.education_group_year_childs))
        ]

        cls.group_element_years_child_0 = [
            GroupElementYearFactory(parent=cls.education_group_year_childs[0],
                                    child_leaf=LearningUnitYearFakerFactory(
                                        learning_container_year__academic_year=cls.academic_year),
                                    child_branch=None)
            for i in range(0, 2)
        ]

        cls.group_element_years_child_2 = [
            GroupElementYearFactory(parent=cls.education_group_year_childs[2],
                                    child_leaf=LearningUnitYearFakerFactory(
                                        learning_container_year__academic_year=cls.academic_year),
                                    child_branch=None)
            for i in range(0, 4)
        ]

        cls.all_learning_units_acronym = [
            gey.child_leaf.acronym for gey in itertools.chain(cls.group_element_years_child_0,
                                                              cls.group_element_years_child_2)
        ]
Exemplo n.º 5
0
    def test_update_with_parent_when_existing_group_element_year(
            self, mock_find_authorized_types):
        parent = EducationGroupYearFactory(
            academic_year=self.expected_educ_group_year.academic_year,
            education_group__end_year=None)

        entity_version = MainEntityVersionFactory()
        initial_educ_group_year = MiniTrainingFactory(
            management_entity=entity_version.entity,
            academic_year=self.expected_educ_group_year.academic_year,
            education_group__start_year=starting_academic_year())

        GroupElementYearFactory(parent=parent,
                                child_branch=initial_educ_group_year)
        initial_count = GroupElementYear.objects.all().count()

        form = self.form_class(data=self.post_data,
                               instance=initial_educ_group_year,
                               parent=parent,
                               user=self.user)
        self.assertTrue(form.is_valid(), form.errors)
        updated_education_group_year = form.save()

        # Assert existing GroupElementYear is reused.
        self.assertEqual(initial_count, GroupElementYear.objects.all().count())
        self._assert_all_fields_correctly_saved(updated_education_group_year)
        self.assertTrue(form.forms[ModelForm].fields["academic_year"].disabled)
Exemplo n.º 6
0
    def setUpTestData(cls):
        cls.academic_year = AcademicYearFactory()
        cls.master_120 = TrainingFactory(
            education_group_type__name=TrainingType.PGRM_MASTER_120.name,
            academic_year=cls.academic_year)

        cls.option_in_parent = MiniTrainingFactory(
            acronym="OPT1",
            education_group_type__name=MiniTrainingType.OPTION.name,
            academic_year=cls.academic_year)
        cls.master_120_link_option = GroupElementYearFactory(
            parent=cls.master_120, child_branch=cls.option_in_parent)

        # Create finality structure
        cls.finality_group = GroupFactory(
            education_group_type__name=GroupType.FINALITY_120_LIST_CHOICE.name,
            academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.master_120,
                                child_branch=cls.finality_group)

        cls.master_120_specialized = GroupFactory(
            education_group_type__name=TrainingType.MASTER_MS_120.name,
            academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.finality_group,
                                child_branch=cls.master_120_specialized)
Exemplo n.º 7
0
    def test_mini_training_template_used(self):
        mini_training = MiniTrainingFactory()
        url = reverse("education_group_read",
                      args=[mini_training.pk, mini_training.pk])
        expected_template = "education_group/identification_mini_training_details.html"

        response = self.client.get(url)
        self.assertTemplateUsed(response, expected_template)
Exemplo n.º 8
0
 def test_li_tag_case_mini_training_disabled(self):
     """
     This test ensure that as faculty manager, the li tag is disabled for mini training
     Faculty manager must enter in proposition mode for mini training
     """
     self.context['education_group_year'] = MiniTrainingFactory()
     result = li_with_create_perm_mini_training(self.context, self.url, "")
     msg = _("The user has not permission to create a %(category)s.") % {"category": _(MINI_TRAINING)}
     msg = msg.capitalize()
     self.assertHTMLEqual(result, DISABLED_LI.format("link_create_mini_training", msg, ""))
Exemplo n.º 9
0
 def test_education_group_year_is_not_training_type(self):
     type_not_allowed = (MiniTrainingFactory(
         academic_year=self.academic_year),
                         GroupFactory(academic_year=self.academic_year))
     for education_group_year in type_not_allowed:
         with self.subTest(education_group_year=education_group_year):
             perm = CertificateAimsPerms(
                 user=SuperUserFactory(),
                 education_group_year=education_group_year)
             self.assertFalse(perm.is_eligible())
Exemplo n.º 10
0
    def test_when_link_deepening_to_minor_list_choice(self):
        minor_list_choice = GroupFactory(
            education_group_type__name=GroupType.MINOR_LIST_CHOICE.name)
        deepening = MiniTrainingFactory(
            education_group_type__name=MiniTrainingType.DEEPENING.name)

        link = GroupElementYear(parent=minor_list_choice,
                                child_branch=deepening,
                                link_type=None)
        link._clean_link_type()
        self.assertEqual(link.link_type, LinkTypes.REFERENCE.name)
Exemplo n.º 11
0
    def test_when_link_major_to_major_list_choice(self):
        major_list_choice = GroupFactory(
            education_group_type__name=GroupType.MAJOR_LIST_CHOICE.name)
        major = MiniTrainingFactory(
            education_group_type__name=MiniTrainingType.FSA_SPECIALITY.name)

        link = GroupElementYear(parent=major_list_choice,
                                child_branch=major,
                                link_type=None)
        link._clean_link_type()
        self.assertEqual(link.link_type, LinkTypes.REFERENCE.name)
Exemplo n.º 12
0
 def test_get_publish_url_case_not_common_and_fsa_speciality_case(self):
     fsa_speciality = MiniTrainingFactory(
         education_group_type__name=MiniTrainingType.FSA_SPECIALITY.name)
     expected_url = "{api_url}/{endpoint}".format(
         api_url=settings.ESB_API_URL,
         endpoint=settings.ESB_REFRESH_PEDAGOGY_ENDPOINT.format(
             year=fsa_speciality.academic_year.year,
             code='fsa1ba-{partial_acronym}'.format(
                 partial_acronym=fsa_speciality.partial_acronym)),
     )
     self.assertEqual(expected_url, _get_url_to_publish(fsa_speciality))
Exemplo n.º 13
0
    def test_when_link_minor_to_minor_list_choice(self):
        minor_list_choice = GroupFactory(
            education_group_type__name=GroupType.MINOR_LIST_CHOICE.name)
        minor = MiniTrainingFactory(education_group_type__name=random.choice(
            MiniTrainingType.minors()))

        link = GroupElementYear(parent=minor_list_choice,
                                child_branch=minor,
                                link_type=None)
        link._clean_link_type()
        self.assertEqual(link.link_type, LinkTypes.REFERENCE.name)
Exemplo n.º 14
0
 def test_get_publish_url_case_not_common_and_mini_training_case(self):
     mini_training = MiniTrainingFactory(
         education_group_type__name=random.choice(
             [t.name for t in MiniTrainingType]))
     print(mini_training.education_group_type)
     expected_url = "{api_url}/{endpoint}".format(
         api_url=settings.ESB_API_URL,
         endpoint=settings.ESB_REFRESH_PEDAGOGY_ENDPOINT.format(
             year=mini_training.academic_year.year,
             code=self._get_correct_mini_training_code(mini_training)),
     )
     self.assertEqual(expected_url, _get_url_to_publish(mini_training))
Exemplo n.º 15
0
    def setUpTestData(cls):
        cls.academic_year = AcademicYearFactory(year=2018)
        cls.mini_training = MiniTrainingFactory(
            partial_acronym='LGENR100I',
            academic_year=cls.academic_year,
        )

        cls.user = UserFactory()
        cls.url = reverse('education_group_api_v1:mini_training_read', kwargs={
            'partial_acronym': cls.mini_training.partial_acronym,
            'year': cls.academic_year.year
        })
Exemplo n.º 16
0
    def setUpTestData(cls):
        anac = AcademicYearFactory()

        cls.egy = MiniTrainingFactory(
            academic_year=anac,
        )

        cls.person = PersonFactory()
        cls.url = reverse('education_group_api_v1:minitrainingstitle_read', kwargs={
            'partial_acronym': cls.egy.partial_acronym,
            'year': cls.egy.academic_year.year
        })
Exemplo n.º 17
0
 def setUpTestData(cls):
     cls.current_acy, cls.previous_acy = AcademicYearFactory.produce_in_past(
         from_year=2019, quantity=2)
     cls.group_element_year = GroupElementYearFactory(
         parent=TrainingFactory(academic_year=cls.current_acy),
         child_branch=MiniTrainingFactory(academic_year=cls.current_acy),
     )
     cls.faculty_manager = FacultyManagerFactory()
     cls.faculty_manager.user.user_permissions.add(
         Permission.objects.get(codename="change_educationgroup"))
     PersonEntityFactory(
         entity=cls.group_element_year.parent.management_entity,
         person=cls.faculty_manager)
Exemplo n.º 18
0
 def setUpTestData(cls):
     cls.current_acy, cls.previous_acy = AcademicYearFactory.produce_in_past(
         from_year=2019, quantity=2)
     cls.group_element_year = GroupElementYearFactory(
         parent=TrainingFactory(academic_year=cls.current_acy),
         child_branch=MiniTrainingFactory(
             academic_year=cls.current_acy,
             education_group_type__name=MiniTrainingType.DEEPENING.name),
     )
     cls.faculty_manager = FacultyManagerFactory(
         'change_educationgroup', 'change_educationgroupcontent')
     PersonEntityFactory(
         entity=cls.group_element_year.parent.management_entity,
         person=cls.faculty_manager)
Exemplo n.º 19
0
    def test_is_not_valid_case_attach_option_which_are_not_within_master_120(self):
        """
        In this test, we ensure that we CANNOT add an option at specialized finality because
        it is not present in root master 2m level
        """
        option_which_are_not_in_2m = MiniTrainingFactory(education_group_type__name=MiniTrainingType.OPTION.name,
                                                         academic_year=self.academic_year)
        strategy = AttachEducationGroupYearStrategy(
            parent=self.master_120_specialized,
            child=option_which_are_not_in_2m
        )

        with self.assertRaises(ValidationError):
            strategy.is_valid()
Exemplo n.º 20
0
 def setUpTestData(cls):
     cls.academic_year = AcademicYearFactory(year=2018)
     cls.entity_version = EntityVersionFactory(
         entity__organization__type=organization_type.MAIN
     )
     cls.mini_training = MiniTrainingFactory(
         partial_acronym='LGENR100I',
         academic_year=cls.academic_year,
         management_entity=cls.entity_version.entity,
     )
     url = reverse('education_group_api_v1:mini_training_read', kwargs={
         'partial_acronym': cls.mini_training.partial_acronym,
         'year': cls.academic_year.year
     })
     cls.serializer = MiniTrainingDetailSerializer(cls.mini_training, context={
         'request': RequestFactory().get(url),
         'language': settings.LANGUAGE_CODE_FR
     })
Exemplo n.º 21
0
    def test_is_not_valid_case_attach_group_which_contains_option_which_are_not_within_master_120(self):
        """
        In this test, we ensure that we CANNOT add a groups which contains options at specialized finality because
        this options are not present in root master 2m level
        """
        subgroup = GroupFactory(education_group_type__name=GroupType.SUB_GROUP.name, academic_year=self.academic_year)
        option_which_are_not_in_2m = MiniTrainingFactory(education_group_type__name=MiniTrainingType.OPTION.name,
                                                         academic_year=self.academic_year)
        # Error case
        GroupElementYearFactory(parent=subgroup, child_branch=option_which_are_not_in_2m)
        # Good case (present in 2M)
        GroupElementYearFactory(parent=subgroup, child_branch=self.option_in_parent)

        strategy = AttachEducationGroupYearStrategy(
            parent=self.master_120_specialized,
            child=subgroup
        )
        with self.assertRaises(ValidationError):
            strategy.is_valid()
Exemplo n.º 22
0
    def test_is_not_valid_case_detach_option_which_are_within_finality_master_120(
            self):
        """
        In this test, we ensure that we CANNOT detach an option at 2m level because
        it is present in one finality of this 2m
        """
        option = MiniTrainingFactory(
            education_group_type__name=MiniTrainingType.OPTION.name,
            academic_year=self.academic_year)
        GroupElementYearFactory(parent=self.master_120_specialized,
                                child_branch=option)
        master_120_link_option = GroupElementYearFactory(
            parent=self.master_120,
            child_branch=option,
        )

        strategy = DetachEducationGroupYearStrategy(
            link=master_120_link_option)
        self.assertFalse(strategy.is_valid())
Exemplo n.º 23
0
    def setUpTestData(cls):
        cls.academic_year = AcademicYearFactory()
        cls.master_120 = TrainingFactory(
            education_group_type__name=TrainingType.PGRM_MASTER_120.name,
            education_group__end_year=None,
            academic_year=cls.academic_year
        )

        cls.option_in_parent = MiniTrainingFactory(education_group_type__name=MiniTrainingType.OPTION.name,
                                                   academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.master_120, child_branch=cls.option_in_parent)

        # Create finality and attach some options
        cls.finality_group = GroupFactory(education_group_type__name=GroupType.FINALITY_120_LIST_CHOICE.name,
                                          academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.master_120, child_branch=cls.finality_group)

        cls.master_120_specialized = TrainingFactory(education_group_type__name=TrainingType.MASTER_MS_120.name,
                                                     academic_year=cls.academic_year)
        GroupElementYearFactory(parent=cls.finality_group, child_branch=cls.master_120_specialized)
Exemplo n.º 24
0
    def test_li_tag_case_mini_training_disabled(self):
        """
        This test ensure that as faculty manager, the li tag is disabled for mini training
        Faculty manager must enter in proposition mode for mini training
        """
        self.context['education_group_year'] = MiniTrainingFactory()
        result = li_with_create_perm_mini_training(self.context, self.url, "")
        msg = _("The user has not permission to create a %(category)s.") % {
            "category": _(MINI_TRAINING)
        }
        msg = msg.capitalize()

        self.assertEqual(
            result, {
                'load_modal': False,
                'title': msg,
                'class_li': 'disabled',
                'id_li': 'link_create_mini_training',
                'url': "#",
                'text': ''
            })
Exemplo n.º 25
0
    def test_is_not_valid_case_detach_group_which_contains_option_which_are_within_finality_master_120(
            self):
        """
        In this test, we ensure that we CANNOT detach a group which contains options at 2m level because
        this options are present in one finality of this 2m
        """
        subgroup = GroupFactory(
            education_group_type__name=GroupType.SUB_GROUP.name,
            academic_year=self.academic_year)
        GroupElementYearFactory(parent=self.master_120_specialized,
                                child_branch=subgroup)
        option = MiniTrainingFactory(
            education_group_type__name=MiniTrainingType.OPTION.name,
            academic_year=self.academic_year)
        GroupElementYearFactory(parent=subgroup, child_branch=option)

        master_120_link_option = GroupElementYearFactory(
            parent=self.master_120, child_branch=option)

        strategy = DetachEducationGroupYearStrategy(
            link=master_120_link_option)
        with self.assertRaises(ValidationError):
            strategy.is_valid()
Exemplo n.º 26
0
 def setUp(self):
     self.parent = TrainingFactory()
     self.child_leaf = LearningUnitYearFactory()
     self.child_branch = MiniTrainingFactory()
Exemplo n.º 27
0
 def setUpTestData(cls):
     cls.current_acy, cls.previous_acy = AcademicYearFactory.produce_in_past(
         quantity=2)
     cls.group_element_year = GroupElementYearFactory(
         parent=TrainingFactory(academic_year=cls.current_acy),
         child_branch=MiniTrainingFactory(academic_year=cls.current_acy))
Exemplo n.º 28
0
    def setUp(self):
        self.current_academic_year = create_current_academic_year()
        self.start_date_ay_1 = self.current_academic_year.start_date.replace(
            year=self.current_academic_year.year + 1)
        self.end_date_ay_1 = self.current_academic_year.end_date.replace(
            year=self.current_academic_year.year + 2)
        self.previous_academic_year = AcademicYearFactory(
            year=self.current_academic_year.year - 1)
        academic_year_1 = AcademicYearFactory.build(
            start_date=self.start_date_ay_1,
            end_date=self.end_date_ay_1,
            year=self.current_academic_year.year + 1)
        academic_year_1.save()

        self.start_date_ay_2 = self.current_academic_year.start_date.replace(
            year=self.current_academic_year.year + 2)
        self.end_date_ay_2 = self.current_academic_year.end_date.replace(
            year=self.current_academic_year.year + 3)
        academic_year_2 = AcademicYearFactory.build(
            start_date=self.start_date_ay_2,
            end_date=self.end_date_ay_2,
            year=self.current_academic_year.year + 2)
        academic_year_2.save()

        self.education_group_year = GroupFactory()

        EntityVersionFactory(
            entity=self.education_group_year.management_entity,
            start_date=self.education_group_year.academic_year.start_date)

        EntityVersionFactory(
            entity=self.education_group_year.administration_entity,
            start_date=self.education_group_year.academic_year.start_date)

        AuthorizedRelationshipFactory(
            parent_type=self.education_group_year.education_group_type,
            child_type=self.education_group_year.education_group_type)

        self.url = reverse(update_education_group,
                           kwargs={
                               "root_id":
                               self.education_group_year.pk,
                               "education_group_year_id":
                               self.education_group_year.pk
                           })
        self.person = CentralManagerFactory()
        PersonEntityFactory(person=self.person,
                            entity=self.education_group_year.management_entity)
        self.client.force_login(self.person.user)
        permission = Permission.objects.get(codename='change_educationgroup')
        self.person.user.user_permissions.add(permission)
        self.perm_patcher = mock.patch(
            "base.business.education_groups.perms._is_eligible_certificate_aims",
            return_value=True)
        self.mocked_perm = self.perm_patcher.start()

        self.an_training_education_group_type = EducationGroupTypeFactory(
            category=education_group_categories.TRAINING)

        self.previous_training_education_group_year = TrainingFactory(
            academic_year=self.previous_academic_year,
            education_group_type=self.an_training_education_group_type,
            education_group__start_year=1968)

        EntityVersionFactory(
            entity=self.previous_training_education_group_year.
            management_entity,
            start_date=self.previous_training_education_group_year.
            academic_year.start_date)

        EntityVersionFactory(
            entity=self.previous_training_education_group_year.
            administration_entity,
            start_date=self.previous_training_education_group_year.
            academic_year.start_date)

        self.training_education_group_year = TrainingFactory(
            academic_year=self.current_academic_year,
            education_group_type=self.an_training_education_group_type,
            education_group__start_year=1968)

        self.training_education_group_year_1 = TrainingFactory(
            academic_year=academic_year_1,
            education_group_type=self.an_training_education_group_type,
            education_group=self.training_education_group_year.education_group)

        self.training_education_group_year_2 = TrainingFactory(
            academic_year=academic_year_2,
            education_group_type=self.an_training_education_group_type,
            education_group=self.training_education_group_year.education_group)

        AuthorizedRelationshipFactory(
            parent_type=self.an_training_education_group_type,
            child_type=self.an_training_education_group_type,
        )

        EntityVersionFactory(
            entity=self.training_education_group_year.management_entity,
            start_date=self.education_group_year.academic_year.start_date)

        EntityVersionFactory(
            entity=self.training_education_group_year.administration_entity,
            start_date=self.education_group_year.academic_year.start_date)

        self.training_url = reverse(update_education_group,
                                    args=[
                                        self.training_education_group_year.pk,
                                        self.training_education_group_year.pk
                                    ])
        PersonEntityFactory(
            person=self.person,
            entity=self.training_education_group_year.management_entity)

        self.domains = [DomainFactory() for x in range(10)]

        self.a_mini_training_education_group_type = EducationGroupTypeFactory(
            category=education_group_categories.MINI_TRAINING)

        self.mini_training_education_group_year = MiniTrainingFactory(
            academic_year=self.current_academic_year,
            education_group_type=self.a_mini_training_education_group_type)

        self.mini_training_url = reverse(
            update_education_group,
            args=[
                self.mini_training_education_group_year.pk,
                self.mini_training_education_group_year.pk
            ])
        PersonEntityFactory(
            person=self.person,
            entity=self.mini_training_education_group_year.management_entity)

        EntityVersionFactory(
            entity=self.mini_training_education_group_year.management_entity,
            start_date=self.education_group_year.academic_year.start_date)
Exemplo n.º 29
0
class TestUpdate(TestCase):
    def setUp(self):
        self.current_academic_year = create_current_academic_year()
        self.start_date_ay_1 = self.current_academic_year.start_date.replace(
            year=self.current_academic_year.year + 1)
        self.end_date_ay_1 = self.current_academic_year.end_date.replace(
            year=self.current_academic_year.year + 2)
        self.previous_academic_year = AcademicYearFactory(
            year=self.current_academic_year.year - 1)
        academic_year_1 = AcademicYearFactory.build(
            start_date=self.start_date_ay_1,
            end_date=self.end_date_ay_1,
            year=self.current_academic_year.year + 1)
        academic_year_1.save()

        self.start_date_ay_2 = self.current_academic_year.start_date.replace(
            year=self.current_academic_year.year + 2)
        self.end_date_ay_2 = self.current_academic_year.end_date.replace(
            year=self.current_academic_year.year + 3)
        academic_year_2 = AcademicYearFactory.build(
            start_date=self.start_date_ay_2,
            end_date=self.end_date_ay_2,
            year=self.current_academic_year.year + 2)
        academic_year_2.save()

        self.education_group_year = GroupFactory()

        EntityVersionFactory(
            entity=self.education_group_year.management_entity,
            start_date=self.education_group_year.academic_year.start_date)

        EntityVersionFactory(
            entity=self.education_group_year.administration_entity,
            start_date=self.education_group_year.academic_year.start_date)

        AuthorizedRelationshipFactory(
            parent_type=self.education_group_year.education_group_type,
            child_type=self.education_group_year.education_group_type)

        self.url = reverse(update_education_group,
                           kwargs={
                               "root_id":
                               self.education_group_year.pk,
                               "education_group_year_id":
                               self.education_group_year.pk
                           })
        self.person = CentralManagerFactory()
        PersonEntityFactory(person=self.person,
                            entity=self.education_group_year.management_entity)
        self.client.force_login(self.person.user)
        permission = Permission.objects.get(codename='change_educationgroup')
        self.person.user.user_permissions.add(permission)
        self.perm_patcher = mock.patch(
            "base.business.education_groups.perms._is_eligible_certificate_aims",
            return_value=True)
        self.mocked_perm = self.perm_patcher.start()

        self.an_training_education_group_type = EducationGroupTypeFactory(
            category=education_group_categories.TRAINING)

        self.previous_training_education_group_year = TrainingFactory(
            academic_year=self.previous_academic_year,
            education_group_type=self.an_training_education_group_type,
            education_group__start_year=1968)

        EntityVersionFactory(
            entity=self.previous_training_education_group_year.
            management_entity,
            start_date=self.previous_training_education_group_year.
            academic_year.start_date)

        EntityVersionFactory(
            entity=self.previous_training_education_group_year.
            administration_entity,
            start_date=self.previous_training_education_group_year.
            academic_year.start_date)

        self.training_education_group_year = TrainingFactory(
            academic_year=self.current_academic_year,
            education_group_type=self.an_training_education_group_type,
            education_group__start_year=1968)

        self.training_education_group_year_1 = TrainingFactory(
            academic_year=academic_year_1,
            education_group_type=self.an_training_education_group_type,
            education_group=self.training_education_group_year.education_group)

        self.training_education_group_year_2 = TrainingFactory(
            academic_year=academic_year_2,
            education_group_type=self.an_training_education_group_type,
            education_group=self.training_education_group_year.education_group)

        AuthorizedRelationshipFactory(
            parent_type=self.an_training_education_group_type,
            child_type=self.an_training_education_group_type,
        )

        EntityVersionFactory(
            entity=self.training_education_group_year.management_entity,
            start_date=self.education_group_year.academic_year.start_date)

        EntityVersionFactory(
            entity=self.training_education_group_year.administration_entity,
            start_date=self.education_group_year.academic_year.start_date)

        self.training_url = reverse(update_education_group,
                                    args=[
                                        self.training_education_group_year.pk,
                                        self.training_education_group_year.pk
                                    ])
        PersonEntityFactory(
            person=self.person,
            entity=self.training_education_group_year.management_entity)

        self.domains = [DomainFactory() for x in range(10)]

        self.a_mini_training_education_group_type = EducationGroupTypeFactory(
            category=education_group_categories.MINI_TRAINING)

        self.mini_training_education_group_year = MiniTrainingFactory(
            academic_year=self.current_academic_year,
            education_group_type=self.a_mini_training_education_group_type)

        self.mini_training_url = reverse(
            update_education_group,
            args=[
                self.mini_training_education_group_year.pk,
                self.mini_training_education_group_year.pk
            ])
        PersonEntityFactory(
            person=self.person,
            entity=self.mini_training_education_group_year.management_entity)

        EntityVersionFactory(
            entity=self.mini_training_education_group_year.management_entity,
            start_date=self.education_group_year.academic_year.start_date)

    def tearDown(self):
        self.perm_patcher.stop()

    def test_login_required(self):
        self.client.logout()
        response = self.client.get(self.url)

        self.assertRedirects(response, '/login/?next={}'.format(self.url))

    def test_template_used(self):
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "education_group/update_groups.html")

    def test_response_context(self):
        response = self.client.get(self.url)

        form_education_group_year = response.context[
            "form_education_group_year"]

        self.assertIsInstance(form_education_group_year, GroupYearModelForm)

    def test_post(self):
        new_entity_version = MainEntityVersionFactory()
        PersonEntityFactory(person=self.person,
                            entity=new_entity_version.entity)
        self.education_group_year.management_entity = new_entity_version.entity
        self.education_group_year.save()

        data = {
            'title':
            'Cours au choix',
            'title_english':
            'deaze',
            'education_group_type':
            self.education_group_year.education_group_type.id,
            'credits':
            42,
            'acronym':
            'CRSCHOIXDVLD',
            'partial_acronym':
            'LDVLD101R',
            'management_entity':
            new_entity_version.pk,
            'main_teaching_campus':
            "",
            'academic_year':
            self.education_group_year.academic_year.pk,
            "constraint_type":
            "",
        }
        response = self.client.post(self.url, data=data)

        self.assertEqual(response.status_code, 302)
        self.education_group_year.refresh_from_db()
        self.assertEqual(self.education_group_year.title, 'Cours au choix')
        self.assertEqual(self.education_group_year.title_english, 'deaze')
        self.assertEqual(self.education_group_year.credits, 42)
        self.assertEqual(self.education_group_year.acronym, 'CRSCHOIXDVLD')
        self.assertEqual(self.education_group_year.partial_acronym,
                         'LDVLD101R')
        self.assertEqual(self.education_group_year.management_entity,
                         new_entity_version.entity)

    def test_template_used_for_training(self):
        response = self.client.get(self.training_url)
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response,
                                "education_group/update_trainings.html")

    def test_template_used_for_certificate_edition(self):
        faculty_managers_group = Group.objects.get(name='faculty_managers')
        self.faculty_user = UserFactory()
        self.faculty_user.groups.add(faculty_managers_group)
        self.faculty_person = PersonFactory(user=self.faculty_user)
        self.client.force_login(self.faculty_user)
        permission = Permission.objects.get(codename='change_educationgroup')
        self.faculty_user.user_permissions.add(permission)
        response = self.client.get(
            reverse(update_education_group,
                    args=[
                        self.previous_training_education_group_year.pk,
                        self.previous_training_education_group_year.pk
                    ]))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(
            response, "education_group/blocks/form/training_certificate.html")

        certificate_aims = [
            CertificateAimFactory(code=code) for code in range(100, 103)
        ]
        response = self.client.post(
            reverse(update_education_group,
                    args=[
                        self.previous_training_education_group_year.pk,
                        self.previous_training_education_group_year.pk
                    ]),
            data={'certificate_aims': str(certificate_aims[0].id)})
        self.assertEqual(response.status_code, 302)

    def test_post_training(self):
        old_domain = DomainFactory()
        EducationGroupYearDomainFactory(
            education_group_year=self.training_education_group_year,
            domain=old_domain)

        new_entity_version = MainEntityVersionFactory()
        PersonEntityFactory(person=self.person,
                            entity=new_entity_version.entity)
        list_domains = [domain.pk for domain in self.domains]
        isced_domain = DomainIscedFactory()
        data = {
            'title':
            'Cours au choix',
            'title_english':
            'deaze',
            'education_group_type':
            self.an_training_education_group_type.pk,
            'credits':
            42,
            'acronym':
            'CRSCHOIXDVLD',
            'partial_acronym':
            'LDVLD101R',
            'management_entity':
            new_entity_version.pk,
            'administration_entity':
            new_entity_version.pk,
            'main_teaching_campus':
            "",
            'academic_year':
            self.training_education_group_year.academic_year.pk,
            'secondary_domains': [
                '|' + ('|'.join([str(domain.pk)
                                 for domain in self.domains])) + '|'
            ],
            'isced_domain':
            isced_domain.pk,
            'active':
            ACTIVE,
            'schedule_type':
            DAILY,
            "internship":
            internship_presence.NO,
            "primary_language":
            LanguageFactory().pk,
            "start_year":
            2010,
            "constraint_type":
            "",
            "diploma_printing_title":
            "Diploma Title",
        }
        response = self.client.post(self.training_url, data=data)
        self.assertEqual(response.status_code, 302)

        self.training_education_group_year.refresh_from_db()
        self.assertEqual(self.training_education_group_year.title,
                         'Cours au choix')
        self.assertEqual(self.training_education_group_year.title_english,
                         'deaze')
        self.assertEqual(self.training_education_group_year.credits, 42)
        self.assertEqual(self.training_education_group_year.acronym,
                         'CRSCHOIXDVLD')
        self.assertEqual(self.training_education_group_year.partial_acronym,
                         'LDVLD101R')
        self.assertEqual(self.training_education_group_year.management_entity,
                         new_entity_version.entity)
        self.assertEqual(
            self.training_education_group_year.administration_entity,
            new_entity_version.entity)
        self.assertEqual(self.training_education_group_year.isced_domain,
                         isced_domain)
        self.assertCountEqual(
            list(
                self.training_education_group_year.secondary_domains.
                values_list('id', flat=True)), list_domains)
        self.assertNotIn(old_domain,
                         self.education_group_year.secondary_domains.all())

    def test_post_mini_training(self):
        old_domain = DomainFactory()
        EducationGroupYearDomainFactory(
            education_group_year=self.mini_training_education_group_year,
            domain=old_domain)

        new_entity_version = MainEntityVersionFactory()
        PersonEntityFactory(person=self.person,
                            entity=new_entity_version.entity)
        data = {
            'title':
            'Cours au choix',
            'title_english':
            'deaze',
            'education_group_type':
            self.a_mini_training_education_group_type.pk,
            'credits':
            42,
            'acronym':
            'CRSCHOIXDVLD',
            'partial_acronym':
            'LDVLD101R',
            'management_entity':
            new_entity_version.pk,
            'main_teaching_campus':
            "",
            'academic_year':
            self.mini_training_education_group_year.academic_year.pk,
            'active':
            ACTIVE,
            'schedule_type':
            DAILY,
            "primary_language":
            LanguageFactory().pk,
            "start_year":
            2010,
            "constraint_type":
            "",
            "diploma_printing_title":
            "Diploma Title",
        }
        response = self.client.post(self.mini_training_url, data=data)
        self.assertEqual(response.status_code,
                         HttpResponseRedirect.status_code)

        self.mini_training_education_group_year.refresh_from_db()
        self.assertEqual(self.mini_training_education_group_year.title,
                         'Cours au choix')
        self.assertEqual(self.mini_training_education_group_year.title_english,
                         'deaze')
        self.assertEqual(self.mini_training_education_group_year.credits, 42)
        self.assertEqual(self.mini_training_education_group_year.acronym,
                         'CRSCHOIXDVLD')
        self.assertEqual(
            self.mini_training_education_group_year.partial_acronym,
            'LDVLD101R')
        self.assertEqual(
            self.mini_training_education_group_year.management_entity,
            new_entity_version.entity)

    def test_post_training_with_end_year(self):
        new_entity_version = MainEntityVersionFactory()
        PersonEntityFactory(person=self.person,
                            entity=new_entity_version.entity)
        data = {
            'title':
            'Cours au choix',
            'title_english':
            'deaze',
            'education_group_type':
            self.an_training_education_group_type.pk,
            'credits':
            42,
            'acronym':
            'CRSCHOIXDVLD',
            'partial_acronym':
            'LDVLD101R',
            'management_entity':
            new_entity_version.pk,
            'administration_entity':
            new_entity_version.pk,
            'main_teaching_campus':
            "",
            'academic_year':
            self.training_education_group_year.academic_year.pk,
            'secondary_domains': [
                '|' + ('|'.join([str(domain.pk)
                                 for domain in self.domains])) + '|'
            ],
            'active':
            ACTIVE,
            'schedule_type':
            DAILY,
            "internship":
            internship_presence.NO,
            "primary_language":
            LanguageFactory().pk,
            "start_year":
            2010,
            "end_year":
            2018,
            "constraint_type":
            "",
            "diploma_printing_title":
            "Diploma Title",
        }
        response = self.client.post(self.training_url, data=data)
        messages = [m.message for m in get_messages(response.wsgi_request)]

        self.assertEqual(
            messages[1],
            _("Education group year %(acronym)s (%(academic_year)s) successfuly deleted."
              ) % {
                  "acronym":
                  self.training_education_group_year_1.acronym,
                  "academic_year":
                  self.training_education_group_year_1.academic_year,
              })
        self.assertEqual(
            messages[2],
            _("Education group year %(acronym)s (%(academic_year)s) successfuly deleted."
              ) % {
                  "acronym":
                  self.training_education_group_year_2.acronym,
                  "academic_year":
                  self.training_education_group_year_2.academic_year,
              })