From 8d45285b42d3588428fdaa17e30e1ed6ded2df0a Mon Sep 17 00:00:00 2001 From: srosse <stephane.rosse@frentix.com> Date: Thu, 22 Nov 2018 17:01:06 +0100 Subject: [PATCH] no-jira: add some unit tests --- .../manager/CurriculumElementDAO.java | 18 +- .../manager/OrganisationDAOTest.java | 38 +++ .../curriculum/manager/CurriculumDAOTest.java | 21 ++ .../manager/CurriculumElementDAOTest.java | 23 ++ .../manager/CurriculumServiceTest.java | 42 ++- .../portfolio/manager/AssignmentDAOTest.java | 244 ++++++++++++++++++ .../portfolio/manager/BinderDAOTest.java | 2 - .../repository/RepositoryManagerTest.java | 21 +- .../manager/RepositoryEntryDAOTest.java | 36 ++- 9 files changed, 423 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java b/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java index 132c45ea36f..65891aef67d 100644 --- a/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java +++ b/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java @@ -146,7 +146,7 @@ public class CurriculumElementDAO { .getResultList(); } - public String getMaterializedPathKeys(CurriculumElement parent, CurriculumElement element) { + private String getMaterializedPathKeys(CurriculumElement parent, CurriculumElement element) { if(parent != null) { String parentPathOfKeys = parent.getMaterializedPathKeys(); if(parentPathOfKeys == null || "/".equals(parentPathOfKeys)) { @@ -194,20 +194,6 @@ public class CurriculumElementDAO { return elementImpl; } - public boolean hasRelations(CurriculumElementRef curriculumElementRef) { - StringBuilder sb = new StringBuilder(128); - sb.append("select context.key from qualitycontext as context") - .append(" where context.audienceCurriculumElement.key=:curriculumElementKey"); - - List<Long> keys = dbInstance.getCurrentEntityManager() - .createQuery(sb.toString(), Long.class) - .setParameter("curriculumElementKey", curriculumElementRef.getKey()) - .setFirstResult(0) - .setMaxResults(1) - .getResultList(); - return keys != null && !keys.isEmpty() && keys.get(0) != null; - } - public List<CurriculumElement> loadElements(CurriculumRef curriculum, CurriculumElementStatus[] status) { QueryBuilder sb = new QueryBuilder(256); sb.append("select el from curriculumelement el") @@ -636,8 +622,6 @@ public class CurriculumElementDAO { .getResultList(); } - - public List<CurriculumElementMembership> getMembershipInfos(CurriculumRef curriculum, Collection<CurriculumElement> elements, Identity... identities) { StringBuilder sb = new StringBuilder(256); sb.append("select el.key, membership from curriculumelement el") diff --git a/src/test/java/org/olat/basesecurity/manager/OrganisationDAOTest.java b/src/test/java/org/olat/basesecurity/manager/OrganisationDAOTest.java index 67c6387fe51..4106b00832c 100644 --- a/src/test/java/org/olat/basesecurity/manager/OrganisationDAOTest.java +++ b/src/test/java/org/olat/basesecurity/manager/OrganisationDAOTest.java @@ -150,6 +150,26 @@ public class OrganisationDAOTest extends OlatTestCase { Assert.assertEquals(OrganisationRoles.user.name(), organisationMember.getRole()); } + @Test + public void getMembersIdentity() { + Identity member = JunitTestHelper.createAndPersistIdentityAsRndUser("Member-1"); + String identifier = UUID.randomUUID().toString(); + Organisation organisation = organisationDao.createAndPersistOrganisation("OpenOLAT EI", identifier, null, null, null); + dbInstance.commit(); + organisationService.addMember(organisation, member, OrganisationRoles.user); + dbInstance.commitAndCloseSession(); + + // get users + List<Identity> members = organisationDao.getMembersIdentity(organisation, OrganisationRoles.user.name()); + Assert.assertNotNull(members); + Assert.assertEquals(1, members.size()); + + // but there is no managers + List<Identity> rolesManagers = organisationDao.getMembersIdentity(organisation, OrganisationRoles.rolesmanager.name()); + Assert.assertNotNull(rolesManagers); + Assert.assertTrue(rolesManagers.isEmpty()); + } + @Test public void getIdentities_organisationIdentifier() { Identity member1 = JunitTestHelper.createAndPersistIdentityAsRndUser("Member-2"); @@ -266,6 +286,24 @@ public class OrganisationDAOTest extends OlatTestCase { Assert.assertTrue(organisations.isEmpty()); } + @Test + public void getChildren() { + String identifier = UUID.randomUUID().toString(); + Organisation rootOrganisation = organisationDao.createAndPersistOrganisation("Root", identifier, null, null, null); + Organisation organisation_1 = organisationDao.createAndPersistOrganisation("Level 1.1", identifier + ".1", null, rootOrganisation, null); + Organisation organisation_2 = organisationDao.createAndPersistOrganisation("Level 1.2", identifier + ".2", null, rootOrganisation, null); + dbInstance.commitAndCloseSession(); + Organisation organisation2_1 = organisationDao.createAndPersistOrganisation("Level 1.2.1", identifier, null, organisation_2, null); + dbInstance.commitAndCloseSession(); + + // get the children + List<Organisation> children = organisationDao.getChildren(rootOrganisation, new OrganisationStatus[] { OrganisationStatus.active }); + Assert.assertNotNull(children); + Assert.assertTrue(children.contains(organisation_1)); + Assert.assertTrue(children.contains(organisation_2)); + Assert.assertFalse(children.contains(organisation2_1)); + } + @Test public void getDescendants() { String identifier = UUID.randomUUID().toString(); diff --git a/src/test/java/org/olat/modules/curriculum/manager/CurriculumDAOTest.java b/src/test/java/org/olat/modules/curriculum/manager/CurriculumDAOTest.java index a755fb0476f..6fdc2270a59 100644 --- a/src/test/java/org/olat/modules/curriculum/manager/CurriculumDAOTest.java +++ b/src/test/java/org/olat/modules/curriculum/manager/CurriculumDAOTest.java @@ -21,6 +21,7 @@ package org.olat.modules.curriculum.manager; import java.util.Arrays; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -33,6 +34,8 @@ import org.olat.core.commons.persistence.DB; import org.olat.core.id.Identity; import org.olat.core.id.Organisation; import org.olat.modules.curriculum.Curriculum; +import org.olat.modules.curriculum.CurriculumCalendars; +import org.olat.modules.curriculum.CurriculumElement; import org.olat.modules.curriculum.CurriculumRoles; import org.olat.modules.curriculum.CurriculumService; import org.olat.modules.curriculum.model.CurriculumImpl; @@ -238,6 +241,24 @@ public class CurriculumDAOTest extends OlatTestCase { Assert.assertTrue(coaches.isEmpty()); } + @Test + public void getMyCurriculums() { + // add a curriculum with a coach in an element + Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("cur-manager-1"); + Curriculum curriculum = curriculumService.createCurriculum("CUR-MY-1", "My Curriculum 1", "Short desc.", null); + CurriculumElement element = curriculumService.createCurriculumElement("Element-1", "1. Element", new Date(), new Date(), null, + null, CurriculumCalendars.disabled, curriculum); + dbInstance.commitAndCloseSession(); + curriculumService.addMember(element, id, CurriculumRoles.participant); + dbInstance.commitAndCloseSession(); + + // get my curriculums + List<Curriculum> myCurriculums = curriculumDao.getMyCurriculums(id); + Assert.assertNotNull(myCurriculums); + Assert.assertEquals(1, myCurriculums.size()); + Assert.assertEquals(curriculum, myCurriculums.get(0)); + } + @Test public void hasCurriculumRole() { // add a curriculum manager diff --git a/src/test/java/org/olat/modules/curriculum/manager/CurriculumElementDAOTest.java b/src/test/java/org/olat/modules/curriculum/manager/CurriculumElementDAOTest.java index 75ceb2325de..d21b08a413e 100644 --- a/src/test/java/org/olat/modules/curriculum/manager/CurriculumElementDAOTest.java +++ b/src/test/java/org/olat/modules/curriculum/manager/CurriculumElementDAOTest.java @@ -408,6 +408,27 @@ public class CurriculumElementDAOTest extends OlatTestCase { Assert.assertEquals(supervisor, members.get(0)); } + @Test + public void getMembersIdentity_elementKeys() { + Identity supervisor = JunitTestHelper.createAndPersistIdentityAsRndUser("cur-supervisor-1"); + Curriculum curriculum = curriculumService.createCurriculum("cur-for-el-4", "Curriculum for element", "Curriculum", null); + CurriculumElement element = curriculumService.createCurriculumElement("Element-4", "4. Element", null, null, null, null, CurriculumCalendars.disabled, curriculum); + curriculumService.addMember(element, supervisor, CurriculumRoles.curriculummanager); + dbInstance.commitAndCloseSession(); + + // check with something to find + List<Long> elementKeys = Collections.singletonList(element.getKey()); + List<Identity> members = curriculumElementDao.getMembersIdentity(elementKeys, CurriculumRoles.curriculummanager.name()); + Assert.assertNotNull(members); + Assert.assertEquals(1, members.size()); + Assert.assertEquals(supervisor, members.get(0)); + + // negative check + List<Identity> participants = curriculumElementDao.getMembersIdentity(elementKeys, CurriculumRoles.participant.name()); + Assert.assertNotNull(participants); + Assert.assertTrue(participants.isEmpty()); + } + @Test public void getMemberKeys() { Identity coach = JunitTestHelper.createAndPersistIdentityAsRndUser("cur-supervisor-1"); @@ -423,6 +444,8 @@ public class CurriculumElementDAOTest extends OlatTestCase { Assert.assertEquals(coach.getKey(), members.get(0)); } + + @Test public void getMembershipInfos_elements() { Identity supervisor = JunitTestHelper.createAndPersistIdentityAsRndUser("cur-supervisor-1"); diff --git a/src/test/java/org/olat/modules/curriculum/manager/CurriculumServiceTest.java b/src/test/java/org/olat/modules/curriculum/manager/CurriculumServiceTest.java index 7735f7c1a15..153dac175b0 100644 --- a/src/test/java/org/olat/modules/curriculum/manager/CurriculumServiceTest.java +++ b/src/test/java/org/olat/modules/curriculum/manager/CurriculumServiceTest.java @@ -25,9 +25,17 @@ import org.junit.Assert; import org.junit.Test; import org.olat.core.commons.persistence.DB; import org.olat.core.id.Identity; +import org.olat.core.id.Roles; import org.olat.modules.curriculum.Curriculum; +import org.olat.modules.curriculum.CurriculumCalendars; +import org.olat.modules.curriculum.CurriculumElement; +import org.olat.modules.curriculum.CurriculumElementMembership; import org.olat.modules.curriculum.CurriculumRoles; import org.olat.modules.curriculum.CurriculumService; +import org.olat.modules.curriculum.model.CurriculumElementRepositoryEntryViews; +import org.olat.repository.RepositoryEntry; +import org.olat.repository.RepositoryEntryStatusEnum; +import org.olat.repository.RepositoryManager; import org.olat.test.JunitTestHelper; import org.olat.test.OlatTestCase; import org.springframework.beans.factory.annotation.Autowired; @@ -44,6 +52,8 @@ public class CurriculumServiceTest extends OlatTestCase { private DB dbInstance; @Autowired private CurriculumService curriculumService; + @Autowired + private RepositoryManager repositoryManager; @Test public void addCurriculumManagers() { @@ -65,6 +75,36 @@ public class CurriculumServiceTest extends OlatTestCase { Assert.assertTrue(owners.isEmpty()); } - + @Test + public void getCurriculumElements() { + Curriculum curriculum = curriculumService.createCurriculum("CUR-2", "Curriculum 2", "Curriculum", null); + CurriculumElement element = curriculumService.createCurriculumElement("Element-for-rel", "Element for relation", null, null, null, null, CurriculumCalendars.disabled, curriculum); + Identity author = JunitTestHelper.createAndPersistIdentityAsRndUser("cur-el-re-auth"); + RepositoryEntry publishedEntry = JunitTestHelper.createRandomRepositoryEntry(author); + RepositoryEntry reviewedEntry = JunitTestHelper.createRandomRepositoryEntry(author); + dbInstance.commit(); + + publishedEntry = repositoryManager.setAccess(publishedEntry, RepositoryEntryStatusEnum.published, false, false); + reviewedEntry = repositoryManager.setAccess(reviewedEntry, RepositoryEntryStatusEnum.review, false, false); + // add the course and a participant to the curriculum + curriculumService.addRepositoryEntry(element, publishedEntry, false); + Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("cur-el-re-part"); + curriculumService.addMember(element, participant, CurriculumRoles.participant); + dbInstance.commitAndCloseSession(); + List<CurriculumElementRepositoryEntryViews> myElements = curriculumService.getCurriculumElements(participant, Roles.userRoles(), curriculum); + Assert.assertNotNull(myElements); + Assert.assertEquals(1, myElements.size()); + + CurriculumElementRepositoryEntryViews myElement = myElements.get(0); + Assert.assertEquals(element, myElement.getCurriculumElement()); + Assert.assertEquals(1, myElement.getEntries().size()); + Assert.assertEquals(publishedEntry.getKey(), myElement.getEntries().get(0).getKey()); + + CurriculumElementMembership membership = myElement.getCurriculumMembership(); + Assert.assertTrue(membership.isParticipant()); + Assert.assertFalse(membership.isCoach()); + Assert.assertFalse(membership.isRepositoryEntryOwner()); + Assert.assertFalse(membership.isCurriculumManager()); + } } diff --git a/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java b/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java index 0a7be5be15b..6f2aec6d26d 100644 --- a/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java +++ b/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java @@ -20,17 +20,28 @@ package org.olat.modules.portfolio.manager; import java.util.List; +import java.util.Locale; import org.junit.Assert; import org.junit.Test; +import org.olat.basesecurity.OrganisationService; import org.olat.core.commons.persistence.DB; import org.olat.core.id.Identity; +import org.olat.core.id.Organisation; import org.olat.modules.portfolio.Assignment; import org.olat.modules.portfolio.AssignmentStatus; import org.olat.modules.portfolio.AssignmentType; import org.olat.modules.portfolio.Binder; +import org.olat.modules.portfolio.Page; +import org.olat.modules.portfolio.PageBody; import org.olat.modules.portfolio.PortfolioService; import org.olat.modules.portfolio.Section; +import org.olat.modules.portfolio.SectionRef; +import org.olat.modules.portfolio.model.SynchedBinder; +import org.olat.repository.RepositoryEntry; +import org.olat.repository.RepositoryEntryStatusEnum; +import org.olat.repository.RepositoryService; +import org.olat.resource.OLATResource; import org.olat.test.JunitTestHelper; import org.olat.test.OlatTestCase; import org.springframework.beans.factory.annotation.Autowired; @@ -49,6 +60,10 @@ public class AssignmentDAOTest extends OlatTestCase { private AssignmentDAO assignmentDao; @Autowired private PortfolioService portfolioService; + @Autowired + private RepositoryService repositoryService; + @Autowired + private OrganisationService organisationService; @Test public void createBinderWithAssignment() { @@ -89,6 +104,105 @@ public class AssignmentDAOTest extends OlatTestCase { Assert.assertEquals(assignment, assignments.get(0)); } + /** + * the method doesn't load the assignment templates on binder level. + * This test checks that's the case. + */ + @Test + public void loadAssignments_binder_excludedTemplates() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("assign-3"); + Binder binder = portfolioService.createNewBinder("Assignment binder 3", "Difficult!", null, owner); + dbInstance.commit(); + portfolioService.appendNewSection("Section", "Assignment section", null, null, binder); + dbInstance.commit(); + //create assignment + List<Section> sections = portfolioService.getSections(binder); + Assignment assignment = assignmentDao.createAssignment("Load assignment", "Load by binder", "The difficult content", + null, AssignmentType.essay, false, AssignmentStatus.template, sections.get(0), null, false, false, false, null); + dbInstance.commitAndCloseSession(); + + //create assignment (need to relad the binder to have an up-to-date sections list) + binder = portfolioService.getBinderByKey(binder.getKey()); + Assignment template = assignmentDao.createAssignment("Load assignment", "Load by binder", "The difficult content", + null, AssignmentType.document, true, AssignmentStatus.template, null, binder, false, false, false, null); + dbInstance.commitAndCloseSession(); + + //load the assignment + List<Assignment> assignments = assignmentDao.loadAssignments(binder, null); + Assert.assertNotNull(assignments); + Assert.assertEquals(1, assignments.size()); + Assert.assertEquals(assignment, assignments.get(0)); + Assert.assertNotEquals(template, assignments.get(0)); + } + + @Test + public void loadBinderAssignmentsTemplates_binder() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("assign-template-1"); + Binder binder = portfolioService.createNewBinder("Assignment template binder", "Difficult templates!", null, owner); + dbInstance.commit(); + portfolioService.appendNewSection("Section", "Assignment section", null, null, binder); + //create assignment + Assignment assignment = assignmentDao.createAssignment("Load assignment", "Load by binder", "The difficult content", + null, AssignmentType.document, true, AssignmentStatus.template, null, binder, false, false, false, null); + dbInstance.commitAndCloseSession(); + + List<Assignment> assignmentTemplates = assignmentDao.loadBinderAssignmentsTemplates(binder); + Assert.assertNotNull(assignmentTemplates); + Assert.assertEquals(1, assignmentTemplates.size()); + Assert.assertEquals(assignment, assignmentTemplates.get(0)); + } + + /** + * The method only load assignment templates. It must ignore the section's + * assignments. + */ + @Test + public void loadBinderAssignmentsTemplates_binder_excludeSectionAssignment() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("assign-template-1"); + Binder binder = portfolioService.createNewBinder("Assignment template binder", "Difficult templates!", null, owner); + dbInstance.commit(); + portfolioService.appendNewSection("Section", "Assignment section", null, null, binder); + //create assignment + Assignment template = assignmentDao.createAssignment("Load assignment", "Load by binder", "The difficult content", + null, AssignmentType.document, true, AssignmentStatus.template, null, binder, false, false, false, null); + dbInstance.commitAndCloseSession(); + + //create a section's assignment + List<Section> sections = portfolioService.getSections(binder); + Assignment assignment = assignmentDao.createAssignment("Load assignment", "Load by binder", "The difficult content", + null, AssignmentType.essay, false, AssignmentStatus.template, sections.get(0), null, false, false, false, null); + dbInstance.commitAndCloseSession(); + + List<Assignment> assignmentTemplates = assignmentDao.loadBinderAssignmentsTemplates(binder); + Assert.assertNotNull(assignmentTemplates); + Assert.assertEquals(1, assignmentTemplates.size()); + Assert.assertEquals(template, assignmentTemplates.get(0)); + Assert.assertNotEquals(assignment, assignmentTemplates.get(0)); + } + + @Test + public void hasBinderAssignmentTemplate_binderRef() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("assign-template-2"); + Binder binder = portfolioService.createNewBinder("Assignment template binder", "Difficult templates!", null, owner); + dbInstance.commit(); + portfolioService.appendNewSection("Section", "Assignment section", null, null, binder); + dbInstance.commit(); + + // check if there is an assignment, no + boolean forgotenAssignmentTemplates = assignmentDao.hasBinderAssignmentTemplate(binder); + Assert.assertFalse(forgotenAssignmentTemplates); + + //create an assignment + Assignment assignment = assignmentDao.createAssignment("Load assignment", "Load by binder", "The difficult content", + null, AssignmentType.document, true, AssignmentStatus.template, null, binder, false, false, false, null); + dbInstance.commitAndCloseSession(); + Assert.assertNotNull(assignment); + + boolean assignmentTemplates = assignmentDao.hasBinderAssignmentTemplate(binder); + Assert.assertTrue(assignmentTemplates); + } + + @Test public void loadAssignments_binder_search() { Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("assign-3"); @@ -160,4 +274,134 @@ public class AssignmentDAOTest extends OlatTestCase { Assert.assertNotNull(emptyAssignments); Assert.assertEquals(0, emptyAssignments.size()); } + + @Test + public void isAssignmentInUse() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-10"); + Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-11"); + RepositoryEntry templateEntry = createTemplate(owner, "Template", "TE"); + dbInstance.commitAndCloseSession(); + + //1 section + Binder templateBinder = portfolioService.getBinderByResource(templateEntry.getOlatResource()); + SectionRef sectionRef = portfolioService.getSections(templateBinder).get(0); + dbInstance.commit(); + + //make 1 assignment + Section templateSection = portfolioService.getSection(sectionRef); + Assignment assignment = portfolioService.addAssignment("1 Assignment", "", "", AssignmentType.essay, false, templateSection, null, false, false, false, null); + dbInstance.commit(); + + // check the method + boolean assignmentNotInUse = assignmentDao.isAssignmentInUse(assignment); + Assert.assertFalse(assignmentNotInUse); + + // synched and check the sections order + Binder binder = portfolioService.assignBinder(id, templateBinder, templateEntry, null, null); + SynchedBinder synchedBinder = portfolioService.loadAndSyncBinder(binder); + binder = synchedBinder.getBinder(); + dbInstance.commit(); + + List<Assignment> assignments = portfolioService.getSectionsAssignments(binder, null); + Assert.assertEquals(1, assignments.size()); + portfolioService.startAssignment(assignments.get(0).getKey(), id); + dbInstance.commitAndCloseSession(); + + // check the method + boolean assignmentInUse = assignmentDao.isAssignmentInUse(assignment); + Assert.assertTrue(assignmentInUse); + } + + @Test + public void loadAssignment_pageBody() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-10"); + Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-11"); + RepositoryEntry templateEntry = createTemplate(owner, "Template", "TE"); + dbInstance.commitAndCloseSession(); + + //1 section + Binder templateBinder = portfolioService.getBinderByResource(templateEntry.getOlatResource()); + SectionRef sectionRef = portfolioService.getSections(templateBinder).get(0); + dbInstance.commit(); + + //make 1 assignment + Section templateSection = portfolioService.getSection(sectionRef); + Assignment assignment = portfolioService.addAssignment("1 Assignment", "", "", AssignmentType.essay, false, templateSection, null, false, false, false, null); + dbInstance.commit(); + Assert.assertNotNull(assignment); + + // synched and check the sections order + Binder binder = portfolioService.assignBinder(id, templateBinder, templateEntry, null, null); + SynchedBinder synchedBinder = portfolioService.loadAndSyncBinder(binder); + binder = synchedBinder.getBinder(); + dbInstance.commit(); + + List<Assignment> assignments = portfolioService.getSectionsAssignments(binder, null); + Assert.assertEquals(1, assignments.size()); + Assignment startedAssignment = portfolioService.startAssignment(assignments.get(0).getKey(), id); + dbInstance.commit(); + + // get the unique page body + List<Section> sections = portfolioService.getSections(binder); + PageBody body = sections.get(0).getPages().get(0).getBody(); + + // check the method + Assignment assignmentInUse = assignmentDao.loadAssignment(body); + Assert.assertNotNull(assignmentInUse); + Assert.assertEquals(startedAssignment, assignmentInUse); + } + + /** + * Create a portfolio template with an assignment's template, + * use it and the assignment as template. Check that the synched + * binder has a copy of the template, check that the instantiated + * assignment as a reference to the synched template (not the original). + */ + @Test + public void loadAssignmentTemplate_pageBody() { + Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-10"); + Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-11"); + RepositoryEntry templateEntry = createTemplate(owner, "Template", "TE"); + dbInstance.commitAndCloseSession(); + + //make 1 assignment + Binder templateBinder = portfolioService.getBinderByResource(templateEntry.getOlatResource()); + Assignment template = portfolioService.addAssignment("1 Assignment", "", "", AssignmentType.essay, true, null, templateBinder, false, false, false, null); + dbInstance.commit(); + + // synched and check the sections order + Binder binder = portfolioService.assignBinder(id, templateBinder, templateEntry, null, null); + SynchedBinder synchedBinder = portfolioService.loadAndSyncBinder(binder); + binder = synchedBinder.getBinder(); + dbInstance.commit(); + + List<Assignment> templates = assignmentDao.loadBinderAssignmentsTemplates(binder); + Assert.assertEquals(1, templates.size()); + Assert.assertEquals(template, templates.get(0).getTemplateReference()); + + // new page based on an assignment template + List<Section> sections = portfolioService.getSections(binder); + Page page = portfolioService.startAssignmentFromTemplate(template.getKey(), id, + "From template", "Froma template", null, null, sections.get(0)); + dbInstance.commit(); + + // check the method + Assignment assignmentInUse = assignmentDao.loadAssignment(page.getBody()); + Assert.assertNotNull(assignmentInUse); + Assert.assertEquals(template, assignmentInUse.getTemplateReference()); + } + + private RepositoryEntry createTemplate(Identity initialAuthor, String displayname, String description) { + return createTemplate(initialAuthor, RepositoryEntryStatusEnum.preparation, displayname, description); + } + + private RepositoryEntry createTemplate(Identity initialAuthor, RepositoryEntryStatusEnum status, String displayname, String description) { + OLATResource resource = portfolioService.createBinderTemplateResource(); + Organisation defOrganisation = organisationService.getDefaultOrganisation(); + RepositoryEntry re = repositoryService.create(initialAuthor, null, "", displayname, description, + resource, status, defOrganisation); + portfolioService.createAndPersistBinderTemplate(initialAuthor, re, Locale.ENGLISH); + return re; + } + } \ No newline at end of file diff --git a/src/test/java/org/olat/modules/portfolio/manager/BinderDAOTest.java b/src/test/java/org/olat/modules/portfolio/manager/BinderDAOTest.java index 2893aab626f..8b6056db3fd 100644 --- a/src/test/java/org/olat/modules/portfolio/manager/BinderDAOTest.java +++ b/src/test/java/org/olat/modules/portfolio/manager/BinderDAOTest.java @@ -214,7 +214,5 @@ public class BinderDAOTest extends OlatTestCase { Assert.assertEquals(binder.getKey(), stats.getKey()); Assert.assertEquals("open", stats.getStatus()); Assert.assertEquals("My statistical binder", stats.getTitle()); - } - } diff --git a/src/test/java/org/olat/repository/RepositoryManagerTest.java b/src/test/java/org/olat/repository/RepositoryManagerTest.java index f63e25e9363..3a4ec261124 100644 --- a/src/test/java/org/olat/repository/RepositoryManagerTest.java +++ b/src/test/java/org/olat/repository/RepositoryManagerTest.java @@ -936,6 +936,10 @@ public class RepositoryManagerTest extends OlatTestCase { Assert.assertTrue(reSecurity.canLaunch()); } + + /** + * Author is not allowed to launch it + */ @Test public void isAllowed_authorRoles() { Identity author = JunitTestHelper.createAndPersistIdentityAsRndAuthor("allowed-re-1"); @@ -944,11 +948,26 @@ public class RepositoryManagerTest extends OlatTestCase { re = repositoryManager.setAccess(re, RepositoryEntryStatusEnum.published, false, false); dbInstance.commitAndCloseSession(); - Roles roles = Roles.userRoles(); + Roles roles = Roles.authorRoles(); RepositoryEntrySecurity reSecurity = repositoryManager.isAllowed(author, roles, re); Assert.assertFalse(reSecurity.canLaunch()); } + @Test + public void isAllowed_authorRoles_canReference() { + Identity author = JunitTestHelper.createAndPersistIdentityAsRndAuthor("allowed-re-1"); + RepositoryEntry re = JunitTestHelper.createAndPersistRepositoryEntry(); + dbInstance.commit(); + re = repositoryManager.setAccess(re, RepositoryEntryStatusEnum.review, false, false); + re = repositoryManager.setAccess(re, false, true, false); + dbInstance.commitAndCloseSession(); + + Roles roles = Roles.authorRoles(); + RepositoryEntrySecurity reSecurity = repositoryManager.isAllowed(author, roles, re); + Assert.assertTrue(reSecurity.canLaunch()); + } + + @Test public void leave_simpleRepositoryEnty() { Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("leave-re-1"); diff --git a/src/test/java/org/olat/repository/manager/RepositoryEntryDAOTest.java b/src/test/java/org/olat/repository/manager/RepositoryEntryDAOTest.java index dcd83d630da..d1902a66c8d 100644 --- a/src/test/java/org/olat/repository/manager/RepositoryEntryDAOTest.java +++ b/src/test/java/org/olat/repository/manager/RepositoryEntryDAOTest.java @@ -31,11 +31,14 @@ import org.olat.basesecurity.OrganisationService; import org.olat.core.commons.persistence.DB; import org.olat.core.id.OLATResourceable; import org.olat.core.id.Organisation; +import org.olat.core.util.CodeHelper; +import org.olat.core.util.resource.OresHelper; import org.olat.repository.RepositoryEntry; import org.olat.repository.RepositoryEntryStatusEnum; import org.olat.repository.RepositoryManager; import org.olat.repository.RepositoryService; import org.olat.resource.OLATResource; +import org.olat.resource.OLATResourceManager; import org.olat.test.OlatTestCase; import org.springframework.beans.factory.annotation.Autowired; @@ -57,6 +60,8 @@ public class RepositoryEntryDAOTest extends OlatTestCase { private RepositoryEntryDAO repositoryEntryDao; @Autowired private OrganisationService organisationService; + @Autowired + private OLATResourceManager resourceManager; @Test public void loadByKey() { @@ -354,6 +359,35 @@ public class RepositoryEntryDAOTest extends OlatTestCase { Assert.assertEquals(0, emptyRes.size()); } + @Test + public void loadRepositoryEntries() { + // insert test data + Organisation defOrganisation = organisationService.getDefaultOrganisation(); + RepositoryEntry re = repositoryService.create(null, "Rei Ayanami", "-", "Repository entry DAO Test all", "", null, + RepositoryEntryStatusEnum.published, defOrganisation); + dbInstance.commitAndCloseSession(); + Assert.assertNotNull(re); + + List<RepositoryEntry> oneEntry = repositoryEntryDao.loadRepositoryEntries(0, 1); + Assert.assertNotNull(oneEntry); + Assert.assertEquals(1, oneEntry.size()); + } - + @Test + public void getLastUsedRepositoryEntries() { + // insert test data + OLATResourceable resourceable = OresHelper.createOLATResourceableInstance("Wiki", new Long(CodeHelper.getForeverUniqueID())); + OLATResource resource = resourceManager.createAndPersistOLATResourceInstance(resourceable); + Organisation defOrganisation = organisationService.getDefaultOrganisation(); + RepositoryEntry re = repositoryService.create(null, "Rei Ayanami", "-", "Repository entry DAO Test all", "", resource, + RepositoryEntryStatusEnum.published, defOrganisation); + dbInstance.commit(); + repositoryService.setLastUsageNowFor(re); + dbInstance.commitAndCloseSession(); + + List<RepositoryEntry> lastUsed = repositoryEntryDao.getLastUsedRepositoryEntries("Wiki", 0, 100); + Assert.assertNotNull(lastUsed); + Assert.assertTrue(lastUsed.size() <= 100); + Assert.assertTrue(lastUsed.contains(re)); + } } \ No newline at end of file -- GitLab