diff --git a/resources/i18n/en.json5 b/resources/i18n/en.json5 index 2197d4257b..8d956d1a0e 100644 --- a/resources/i18n/en.json5 +++ b/resources/i18n/en.json5 @@ -570,6 +570,8 @@ "error.validation.pattern": "This input is restricted by the current pattern: {{ pattern }}.", + "error.validation.filerequired": "The file upload is mandatory", + "footer.copyright": "copyright © 2002-{{ year }}", diff --git a/src/app/shared/mocks/mock-submission.ts b/src/app/shared/mocks/mock-submission.ts index 922e6ad02d..a97d2fb31a 100644 --- a/src/app/shared/mocks/mock-submission.ts +++ b/src/app/shared/mocks/mock-submission.ts @@ -1325,7 +1325,7 @@ export const mockUploadConfigResponse = { }, self: 'https://rest.api/dspace-spring-rest/api/config/submissionforms/bitstream-metadata' }, - required: false, + required: true, maxSize: 536870912, name: 'upload', type: 'submissionupload', @@ -1336,6 +1336,10 @@ export const mockUploadConfigResponse = { self: 'https://rest.api/dspace-spring-rest/api/config/submissionuploads/upload' }; +// Clone the object and change one property +export const mockUploadConfigResponseNotRequired = JSON.parse(JSON.stringify(mockUploadConfigResponse)); +mockUploadConfigResponseNotRequired.required = false; + export const mockAccessConditionOptions = [ { name: 'openaccess', diff --git a/src/app/submission/sections/upload/section-upload.component.spec.ts b/src/app/submission/sections/upload/section-upload.component.spec.ts index fd9f88d939..a58de09b8d 100644 --- a/src/app/submission/sections/upload/section-upload.component.spec.ts +++ b/src/app/submission/sections/upload/section-upload.component.spec.ts @@ -5,6 +5,7 @@ import { TranslateModule } from '@ngx-translate/core'; import { of as observableOf } from 'rxjs'; import { createSuccessfulRemoteDataObject$, createTestComponent } from '../../../shared/testing/utils'; +import { SubmissionObjectState } from '../../objects/submission-objects.reducer'; import { SubmissionService } from '../../submission.service'; import { SubmissionServiceStub } from '../../../shared/testing/submission-service-stub'; import { SectionsService } from '../sections.service'; @@ -18,7 +19,7 @@ import { mockSubmissionId, mockSubmissionState, mockUploadConfigResponse, - mockUploadFiles + mockUploadConfigResponseNotRequired, mockUploadFiles, } from '../../../shared/mocks/mock-submission'; import { BrowserModule } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; @@ -31,7 +32,6 @@ import { cold, hot } from 'jasmine-marbles'; import { Collection } from '../../../core/shared/collection.model'; import { ResourcePolicy } from '../../../core/shared/resource-policy.model'; import { ResourcePolicyService } from '../../../core/data/resource-policy.service'; -import { RemoteData } from '../../../core/data/remote-data'; import { ConfigData } from '../../../core/config/config-data'; import { PageInfo } from '../../../core/shared/page-info.model'; import { Group } from '../../../core/eperson/models/group.model'; @@ -65,17 +65,7 @@ function getMockResourcePolicyService(): ResourcePolicyService { }); } -const sectionObject: SectionDataObject = { - config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/upload', - mandatory: true, - data: { - files: [] - }, - errors: [], - header: 'submit.progressbar.describe.upload', - id: 'upload', - sectionType: SectionsType.Upload -}; +let sectionObject: SectionDataObject; describe('SubmissionSectionUploadComponent test suite', () => { @@ -90,30 +80,48 @@ describe('SubmissionSectionUploadComponent test suite', () => { let uploadsConfigService: any; let bitstreamService: any; - const submissionId = mockSubmissionId; - const collectionId = mockSubmissionCollectionId; - const submissionState = Object.assign({}, mockSubmissionState[mockSubmissionId]); - const mockCollection = Object.assign(new Collection(), { - name: 'Community 1-Collection 1', - id: collectionId, - metadata: [ - { - key: 'dc.title', - language: 'en_US', - value: 'Community 1-Collection 1' - }], - _links: { - defaultAccessConditions: collectionId + '/defaultAccessConditions' - } - }); - const mockDefaultAccessCondition = Object.assign(new ResourcePolicy(), { - name: null, - groupUUID: '11cc35e5-a11d-4b64-b5b9-0052a5d15509', - id: 20, - uuid: 'resource-policy-20' - }); + let submissionId: string; + let collectionId: string; + let submissionState: SubmissionObjectState; + let mockCollection: Collection; + let mockDefaultAccessCondition: ResourcePolicy; beforeEach(async(() => { + sectionObject = { + config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/upload', + mandatory: true, + data: { + files: [] + }, + errors: [], + header: 'submit.progressbar.describe.upload', + id: 'upload', + sectionType: SectionsType.Upload + }; + submissionId = mockSubmissionId; + collectionId = mockSubmissionCollectionId; + submissionState = Object.assign({}, mockSubmissionState[mockSubmissionId]) as any; + mockCollection = Object.assign(new Collection(), { + name: 'Community 1-Collection 1', + id: collectionId, + metadata: [ + { + key: 'dc.title', + language: 'en_US', + value: 'Community 1-Collection 1' + }], + _links: { + defaultAccessConditions: collectionId + '/defaultAccessConditions' + } + }); + + mockDefaultAccessCondition = Object.assign(new ResourcePolicy(), { + name: null, + groupUUID: '11cc35e5-a11d-4b64-b5b9-0052a5d15509', + id: 20, + uuid: 'resource-policy-20' + }); + TestBed.configureTestingModule({ imports: [ BrowserModule, @@ -206,7 +214,7 @@ describe('SubmissionSectionUploadComponent test suite', () => { comp.onSectionInit(); - const expectedGroupsMap = new Map([ + const expectedGroupsMap = new Map([ [mockUploadConfigResponse.accessConditionOptions[1].name, [mockGroup as any]], [mockUploadConfigResponse.accessConditionOptions[2].name, [mockGroup as any]], ]); @@ -215,6 +223,7 @@ describe('SubmissionSectionUploadComponent test suite', () => { expect(comp.collectionName).toBe(mockCollection.name); expect(comp.availableAccessConditionOptions.length).toBe(4); expect(comp.availableAccessConditionOptions).toEqual(mockUploadConfigResponse.accessConditionOptions as any); + expect(comp.required$.getValue()).toBe(true); expect(compAsAny.subs.length).toBe(2); expect(compAsAny.availableGroups.size).toBe(2); expect(compAsAny.availableGroups).toEqual(expectedGroupsMap); @@ -245,7 +254,7 @@ describe('SubmissionSectionUploadComponent test suite', () => { comp.onSectionInit(); - const expectedGroupsMap = new Map([ + const expectedGroupsMap = new Map([ [mockUploadConfigResponse.accessConditionOptions[1].name, [mockGroup as any]], [mockUploadConfigResponse.accessConditionOptions[2].name, [mockGroup as any]], ]); @@ -254,6 +263,7 @@ describe('SubmissionSectionUploadComponent test suite', () => { expect(comp.collectionName).toBe(mockCollection.name); expect(comp.availableAccessConditionOptions.length).toBe(4); expect(comp.availableAccessConditionOptions).toEqual(mockUploadConfigResponse.accessConditionOptions as any); + expect(comp.required$.getValue()).toBe(true); expect(compAsAny.subs.length).toBe(2); expect(compAsAny.availableGroups.size).toBe(2); expect(compAsAny.availableGroups).toEqual(expectedGroupsMap); @@ -263,17 +273,67 @@ describe('SubmissionSectionUploadComponent test suite', () => { }); - it('should the properly section status', () => { - bitstreamService.getUploadedFileList.and.returnValue(hot('-a-b', { + it('should properly read the section status when required is true', () => { + submissionServiceStub.getSubmissionObject.and.returnValue(observableOf(submissionState)); + + collectionDataService.findById.and.returnValue(createSuccessfulRemoteDataObject$(mockCollection)); + + resourcePolicyService.findByHref.and.returnValue(createSuccessfulRemoteDataObject$(mockDefaultAccessCondition)); + + uploadsConfigService.getConfigByHref.and.returnValue(observableOf( + new ConfigData(new PageInfo(), mockUploadConfigResponse as any) + )); + + groupService.findById.and.returnValues( + createSuccessfulRemoteDataObject$(Object.assign(new Group(), mockGroup)), + createSuccessfulRemoteDataObject$(Object.assign(new Group(), mockGroup)) + ); + + bitstreamService.getUploadedFileList.and.returnValue(cold('-a-b', { a: [], b: mockUploadFiles })); + comp.onSectionInit(); + + expect(comp.required$.getValue()).toBe(true); + expect(compAsAny.getSectionStatus()).toBeObservable(cold('-c-d', { c: false, d: true })); }); + + it('should properly read the section status when required is false', () => { + submissionServiceStub.getSubmissionObject.and.returnValue(observableOf(submissionState)); + + collectionDataService.findById.and.returnValue(createSuccessfulRemoteDataObject$(mockCollection)); + + resourcePolicyService.findByHref.and.returnValue(createSuccessfulRemoteDataObject$(mockDefaultAccessCondition)); + + uploadsConfigService.getConfigByHref.and.returnValue(observableOf( + new ConfigData(new PageInfo(), mockUploadConfigResponseNotRequired as any) + )); + + groupService.findById.and.returnValues( + createSuccessfulRemoteDataObject$(Object.assign(new Group(), mockGroup)), + createSuccessfulRemoteDataObject$(Object.assign(new Group(), mockGroup)) + ); + + bitstreamService.getUploadedFileList.and.returnValue(cold('-a-b', { + a: [], + b: mockUploadFiles + })); + + comp.onSectionInit(); + + expect(comp.required$.getValue()).toBe(false); + + expect(compAsAny.getSectionStatus()).toBeObservable(cold('-c-d', { + c: true, + d: true + })); + }); }); }); diff --git a/src/app/submission/sections/upload/section-upload.component.ts b/src/app/submission/sections/upload/section-upload.component.ts index 9dbd1079f4..6c2506b773 100644 --- a/src/app/submission/sections/upload/section-upload.component.ts +++ b/src/app/submission/sections/upload/section-upload.component.ts @@ -1,6 +1,6 @@ import { ChangeDetectorRef, Component, Inject } from '@angular/core'; -import { combineLatest, Observable, Subscription } from 'rxjs'; +import { BehaviorSubject, combineLatest as observableCombineLatest, Observable, Subscription} from 'rxjs'; import { distinctUntilChanged, filter, find, flatMap, map, reduce, take, tap } from 'rxjs/operators'; import { SectionModelComponent } from '../models/section.model'; @@ -104,6 +104,12 @@ export class SubmissionSectionUploadComponent extends SectionModelComponent { */ protected availableGroups: Map; // Groups for any policy + /** + * Is the upload required + * @type {boolean} + */ + public required$ = new BehaviorSubject(true); + /** * Array to track all subscriptions and unsubscribe them onDestroy * @type {Array} @@ -172,6 +178,7 @@ export class SubmissionSectionUploadComponent extends SectionModelComponent { }), flatMap(() => config$), flatMap((config: SubmissionUploadsModel) => { + this.required$.next(config.required); this.availableAccessConditionOptions = isNotEmpty(config.accessConditionOptions) ? config.accessConditionOptions : []; this.collectionPolicyType = this.availableAccessConditionOptions.length > 0 @@ -221,7 +228,7 @@ export class SubmissionSectionUploadComponent extends SectionModelComponent { }), // retrieve submission's bitstreams from state - combineLatest(this.configMetadataForm$, + observableCombineLatest(this.configMetadataForm$, this.bitstreamService.getUploadedFileList(this.submissionId, this.sectionData.id)).pipe( filter(([configMetadataForm, fileList]: [SubmissionFormsModel, any[]]) => { return isNotEmpty(configMetadataForm) && isNotUndefined(fileList) @@ -273,8 +280,13 @@ export class SubmissionSectionUploadComponent extends SectionModelComponent { * the section status */ protected getSectionStatus(): Observable { - return this.bitstreamService.getUploadedFileList(this.submissionId, this.sectionData.id).pipe( - map((fileList: any[]) => (isNotUndefined(fileList) && fileList.length > 0))); + // if not mandatory, always true + // if mandatory, at least one file is required + return observableCombineLatest(this.required$, + this.bitstreamService.getUploadedFileList(this.submissionId, this.sectionData.id), + (required,fileList: any[]) => { + return (!required || (isNotUndefined(fileList) && fileList.length > 0)); + }); } /**