Merge pull request #1239 from atmire/Fix-hardcoded-submission-section-IDs

Fix hardcoded submission section IDs
This commit is contained in:
Art Lowel
2021-07-08 15:14:47 +02:00
committed by GitHub
9 changed files with 114 additions and 67 deletions

View File

@@ -10,6 +10,8 @@ export class SectionsServiceStub {
isSectionEnabled = jasmine.createSpy('isSectionEnabled'); isSectionEnabled = jasmine.createSpy('isSectionEnabled');
isSectionReadOnly = jasmine.createSpy('isSectionReadOnly'); isSectionReadOnly = jasmine.createSpy('isSectionReadOnly');
isSectionAvailable = jasmine.createSpy('isSectionAvailable'); isSectionAvailable = jasmine.createSpy('isSectionAvailable');
isSectionTypeAvailable = jasmine.createSpy('isSectionTypeAvailable');
isSectionType = jasmine.createSpy('isSectionType');
addSection = jasmine.createSpy('addSection'); addSection = jasmine.createSpy('addSection');
removeSection = jasmine.createSpy('removeSection'); removeSection = jasmine.createSpy('removeSection');
updateSectionData = jasmine.createSpy('updateSectionData'); updateSectionData = jasmine.createSpy('updateSectionData');

View File

@@ -120,7 +120,7 @@ describe('SubmissionFormCollectionComponent Component', () => {
}); });
const sectionsService: any = jasmine.createSpyObj('sectionsService', { const sectionsService: any = jasmine.createSpyObj('sectionsService', {
isSectionAvailable: of(true) isSectionTypeAvailable: of(true)
}); });
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {

View File

@@ -28,6 +28,7 @@ import { CollectionDataService } from '../../../core/data/collection-data.servic
import { CollectionDropdownComponent } from '../../../shared/collection-dropdown/collection-dropdown.component'; import { CollectionDropdownComponent } from '../../../shared/collection-dropdown/collection-dropdown.component';
import { SectionsService } from '../../sections/sections.service'; import { SectionsService } from '../../sections/sections.service';
import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators'; import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators';
import { SectionsType } from '../../sections/sections-type';
/** /**
* This component allows to show the current collection the submission belonging to and to change it. * This component allows to show the current collection the submission belonging to and to change it.
@@ -142,7 +143,7 @@ export class SubmissionFormCollectionComponent implements OnChanges, OnInit {
*/ */
ngOnInit() { ngOnInit() {
this.pathCombiner = new JsonPatchOperationPathCombiner('sections', 'collection'); this.pathCombiner = new JsonPatchOperationPathCombiner('sections', 'collection');
this.available$ = this.sectionsService.isSectionAvailable(this.submissionId, 'collection'); this.available$ = this.sectionsService.isSectionTypeAvailable(this.submissionId, SectionsType.collection);
} }
/** /**

View File

@@ -3,7 +3,6 @@
<div *ngIf="(uploadEnabled$ | async)" class="w-100"> <div *ngIf="(uploadEnabled$ | async)" class="w-100">
<ds-submission-upload-files [submissionId]="submissionId" <ds-submission-upload-files [submissionId]="submissionId"
[collectionId]="collectionId" [collectionId]="collectionId"
[sectionId]="'upload'"
[uploadFilesOptions]="uploadFilesOptions"></ds-submission-upload-files> [uploadFilesOptions]="uploadFilesOptions"></ds-submission-upload-files>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>

View File

@@ -83,7 +83,6 @@ describe('SubmissionUploadFilesComponent Component', () => {
const html = ` const html = `
<ds-submission-upload-files [submissionId]="submissionId" <ds-submission-upload-files [submissionId]="submissionId"
[collectionId]="collectionId" [collectionId]="collectionId"
[sectionId]="'upload'"
[uploadFilesOptions]="uploadFilesOptions"></ds-submission-upload-files>`; [uploadFilesOptions]="uploadFilesOptions"></ds-submission-upload-files>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>; testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
@@ -108,11 +107,11 @@ describe('SubmissionUploadFilesComponent Component', () => {
compAsAny = comp; compAsAny = comp;
submissionServiceStub = TestBed.inject(SubmissionService as any); submissionServiceStub = TestBed.inject(SubmissionService as any);
sectionsServiceStub = TestBed.inject(SectionsService as any); sectionsServiceStub = TestBed.inject(SectionsService as any);
sectionsServiceStub.isSectionTypeAvailable.and.returnValue(observableOf(true));
notificationsServiceStub = TestBed.inject(NotificationsService as any); notificationsServiceStub = TestBed.inject(NotificationsService as any);
translateService = TestBed.inject(TranslateService); translateService = TestBed.inject(TranslateService);
comp.submissionId = submissionId; comp.submissionId = submissionId;
comp.collectionId = collectionId; comp.collectionId = collectionId;
comp.sectionId = 'upload';
comp.uploadFilesOptions = Object.assign(new UploaderOptions(),{ comp.uploadFilesOptions = Object.assign(new UploaderOptions(),{
url: '', url: '',
authToken: null, authToken: null,
@@ -133,7 +132,7 @@ describe('SubmissionUploadFilesComponent Component', () => {
}); });
it('should init uploadEnabled properly', () => { it('should init uploadEnabled properly', () => {
sectionsServiceStub.isSectionAvailable.and.returnValue(hot('-a-b', { sectionsServiceStub.isSectionTypeAvailable.and.returnValue(hot('-a-b', {
a: false, a: false,
b: true b: true
})); }));
@@ -149,53 +148,54 @@ describe('SubmissionUploadFilesComponent Component', () => {
expect(compAsAny.uploadEnabled).toBeObservable(expected); expect(compAsAny.uploadEnabled).toBeObservable(expected);
}); });
it('should show a success notification and call updateSectionData on upload complete', () => { describe('on upload complete', () => {
beforeEach(() => {
const expectedErrors: any = mockUploadResponse1ParsedErrors; sectionsServiceStub.isSectionType.and.callFake((_, sectionId, __) => observableOf(sectionId === 'upload'));
compAsAny.uploadEnabled = observableOf(true); compAsAny.uploadEnabled = observableOf(true);
fixture.detectChanges();
comp.onCompleteItem(Object.assign({}, uploadRestResponse, { sections: mockSectionsData }));
Object.keys(mockSectionsData).forEach((sectionId) => {
expect(sectionsServiceStub.updateSectionData).toHaveBeenCalledWith(
submissionId,
sectionId,
mockSectionsData[sectionId],
expectedErrors[sectionId]
);
}); });
expect(notificationsServiceStub.success).toHaveBeenCalled(); it('should show a success notification and call updateSectionData if successful', () => {
const expectedErrors: any = mockUploadResponse1ParsedErrors;
fixture.detectChanges();
}); comp.onCompleteItem(Object.assign({}, uploadRestResponse, { sections: mockSectionsData }));
it('should show an error notification and call updateSectionData on upload complete', () => { Object.keys(mockSectionsData).forEach((sectionId) => {
expect(sectionsServiceStub.updateSectionData).toHaveBeenCalledWith(
submissionId,
sectionId,
mockSectionsData[sectionId],
expectedErrors[sectionId]
);
});
const responseErrors = mockUploadResponse2Errors; expect(notificationsServiceStub.success).toHaveBeenCalled();
const expectedErrors: any = mockUploadResponse2ParsedErrors;
compAsAny.uploadEnabled = observableOf(true);
fixture.detectChanges();
comp.onCompleteItem(Object.assign({}, uploadRestResponse, {
sections: mockSectionsData,
errors: responseErrors.errors
}));
Object.keys(mockSectionsData).forEach((sectionId) => {
expect(sectionsServiceStub.updateSectionData).toHaveBeenCalledWith(
submissionId,
sectionId,
mockSectionsData[sectionId],
expectedErrors[sectionId]
);
}); });
expect(notificationsServiceStub.success).not.toHaveBeenCalled(); it('should show an error notification and call updateSectionData if unsuccessful', () => {
const responseErrors = mockUploadResponse2Errors;
const expectedErrors: any = mockUploadResponse2ParsedErrors;
fixture.detectChanges();
comp.onCompleteItem(Object.assign({}, uploadRestResponse, {
sections: mockSectionsData,
errors: responseErrors.errors
}));
Object.keys(mockSectionsData).forEach((sectionId) => {
expect(sectionsServiceStub.updateSectionData).toHaveBeenCalledWith(
submissionId,
sectionId,
mockSectionsData[sectionId],
expectedErrors[sectionId]
);
});
expect(notificationsServiceStub.success).not.toHaveBeenCalled();
});
}); });
}); });
}); });
@@ -208,7 +208,6 @@ class TestComponent {
submissionId = mockSubmissionId; submissionId = mockSubmissionId;
collectionId = mockSubmissionCollectionId; collectionId = mockSubmissionCollectionId;
sectionId = 'upload';
uploadFilesOptions = Object.assign(new UploaderOptions(), { uploadFilesOptions = Object.assign(new UploaderOptions(), {
url: '', url: '',
authToken: null, authToken: null,

View File

@@ -2,7 +2,7 @@ import { Component, Input, OnChanges } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { Observable, of as observableOf, Subscription } from 'rxjs'; import { Observable, of as observableOf, Subscription } from 'rxjs';
import { first } from 'rxjs/operators'; import { first, take } from 'rxjs/operators';
import { SectionsService } from '../../sections/sections.service'; import { SectionsService } from '../../sections/sections.service';
import { hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util'; import { hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util';
@@ -13,6 +13,7 @@ import { UploaderOptions } from '../../../shared/uploader/uploader-options.model
import parseSectionErrors from '../../utils/parseSectionErrors'; import parseSectionErrors from '../../utils/parseSectionErrors';
import { SubmissionJsonPatchOperationsService } from '../../../core/submission/submission-json-patch-operations.service'; import { SubmissionJsonPatchOperationsService } from '../../../core/submission/submission-json-patch-operations.service';
import { WorkspaceItem } from '../../../core/submission/models/workspaceitem.model'; import { WorkspaceItem } from '../../../core/submission/models/workspaceitem.model';
import { SectionsType } from '../../sections/sections-type';
/** /**
* This component represents the drop zone that provides to add files to the submission. * This component represents the drop zone that provides to add files to the submission.
@@ -35,12 +36,6 @@ export class SubmissionUploadFilesComponent implements OnChanges {
*/ */
@Input() submissionId: string; @Input() submissionId: string;
/**
* The upload section id
* @type {string}
*/
@Input() sectionId: string;
/** /**
* The uploader configuration options * The uploader configuration options
* @type {UploaderOptions} * @type {UploaderOptions}
@@ -110,7 +105,7 @@ export class SubmissionUploadFilesComponent implements OnChanges {
* Check if upload functionality is enabled * Check if upload functionality is enabled
*/ */
ngOnChanges() { ngOnChanges() {
this.uploadEnabled = this.sectionService.isSectionAvailable(this.submissionId, this.sectionId); this.uploadEnabled = this.sectionService.isSectionTypeAvailable(this.submissionId, SectionsType.Upload);
} }
/** /**
@@ -136,14 +131,18 @@ export class SubmissionUploadFilesComponent implements OnChanges {
.forEach((sectionId) => { .forEach((sectionId) => {
const sectionData = normalizeSectionData(sections[sectionId]); const sectionData = normalizeSectionData(sections[sectionId]);
const sectionErrors = errorsList[sectionId]; const sectionErrors = errorsList[sectionId];
if (sectionId === 'upload') { this.sectionService.isSectionType(this.submissionId, sectionId, SectionsType.Upload)
// Look for errors on upload .pipe(take(1))
if ((isEmpty(sectionErrors))) { .subscribe((isUpload) => {
this.notificationsService.success(null, this.translate.get('submission.sections.upload.upload-successful')); if (isUpload) {
} else { // Look for errors on upload
this.notificationsService.error(null, this.translate.get('submission.sections.upload.upload-failed')); if ((isEmpty(sectionErrors))) {
} this.notificationsService.success(null, this.translate.get('submission.sections.upload.upload-successful'));
} } else {
this.notificationsService.error(null, this.translate.get('submission.sections.upload.upload-failed'));
}
}
});
this.sectionService.updateSectionData(this.submissionId, sectionId, sectionData, sectionErrors); this.sectionService.updateSectionData(this.submissionId, sectionId, sectionData, sectionErrors);
}); });
} }

View File

@@ -334,6 +334,38 @@ describe('SectionsService test suite', () => {
}); });
}); });
describe('isSectionType', () => {
it('should return true if the section matches the provided type', () => {
store.select.and.returnValue(observableOf(submissionState));
const expected = cold('(b|)', {
b: true
});
expect(service.isSectionType(submissionId, 'upload', SectionsType.Upload)).toBeObservable(expected);
});
it('should return false if the section doesn\'t match the provided type', () => {
store.select.and.returnValue(observableOf(submissionState));
const expected = cold('(b|)', {
b: false
});
expect(service.isSectionType(submissionId, sectionId, SectionsType.Upload)).toBeObservable(expected);
});
it('should return false if the provided sectionId doesn\'t exist', () => {
store.select.and.returnValue(observableOf(submissionState));
const expected = cold('(b|)', {
b: false
});
expect(service.isSectionType(submissionId, 'no-such-id', SectionsType.Upload)).toBeObservable(expected);
});
});
describe('addSection', () => { describe('addSection', () => {
it('should dispatch a new EnableSectionAction a move target to new section', () => { it('should dispatch a new EnableSectionAction a move target to new section', () => {

View File

@@ -328,6 +328,22 @@ export class SectionsService {
distinctUntilChanged()); distinctUntilChanged());
} }
/**
* Check if given section id is of a given section type
* @param submissionId
* @param sectionId
* @param sectionType
*/
public isSectionType(submissionId: string, sectionId: string, sectionType: SectionsType): Observable<boolean> {
return this.store.select(submissionObjectFromIdSelector(submissionId)).pipe(
filter((submissionState: SubmissionObjectEntry) => isNotUndefined(submissionState)),
map((submissionState: SubmissionObjectEntry) => {
return isNotUndefined(submissionState.sections) && isNotUndefined(submissionState.sections[sectionId])
&& submissionState.sections[sectionId].sectionType === sectionType;
}),
distinctUntilChanged());
}
/** /**
* Dispatch a new [EnableSectionAction] to add a new section and move page target to it * Dispatch a new [EnableSectionAction] to add a new section and move page target to it
* *

View File

@@ -288,14 +288,13 @@ export class SubmissionSectionUploadFileComponent implements OnChanges, OnInit {
this.pathCombiner.subRootElement); this.pathCombiner.subRootElement);
}) })
).subscribe((result: SubmissionObject[]) => { ).subscribe((result: SubmissionObject[]) => {
if (result[0].sections.upload) { if (result[0].sections[this.sectionId]) {
Object.keys((result[0].sections.upload as WorkspaceitemSectionUploadObject).files) const uploadSection = (result[0].sections[this.sectionId] as WorkspaceitemSectionUploadObject);
.filter((key) => (result[0].sections.upload as WorkspaceitemSectionUploadObject).files[key].uuid === this.fileId) Object.keys(uploadSection.files)
.filter((key) => uploadSection.files[key].uuid === this.fileId)
.forEach((key) => this.uploadService.updateFileData( .forEach((key) => this.uploadService.updateFileData(
this.submissionId, this.submissionId, this.sectionId, this.fileId, uploadSection.files[key])
this.sectionId, );
this.fileId,
(result[0].sections.upload as WorkspaceitemSectionUploadObject).files[key]));
} }
this.switchMode(); this.switchMode();
})); }));