Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File edit component updated to work for forms without access conditions. #2647

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ describe('SubmissionSectionUploadFileEditComponent test suite', () => {
const fileData: any = mockUploadFiles[0];
const pathCombiner = new JsonPatchOperationPathCombiner('sections', sectionId, 'files', fileIndex);

let noAccessConditionsMock = Object.assign({}, mockFileFormData);
delete noAccessConditionsMock.accessConditions;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
Expand Down Expand Up @@ -299,6 +302,28 @@ describe('SubmissionSectionUploadFileEditComponent test suite', () => {

}));

it('should update Bitstream data properly when access options are omitted', fakeAsync(() => {
compAsAny.formRef = {formGroup: null};
compAsAny.fileData = fileData;
compAsAny.pathCombiner = pathCombiner;
formService.validateAllFormFields.and.callFake(() => null);
formService.isValid.and.returnValue(of(true));
formService.getFormData.and.returnValue(of(noAccessConditionsMock));
const response = [
Object.assign(mockSubmissionObject, {
sections: {
upload: {
files: mockUploadFiles
}
}
})
];
operationsService.jsonPatchByResourceID.and.returnValue(of(response));
comp.saveBitstreamData();
tick();
expect(uploadService.updateFileData).toHaveBeenCalled();
}));

it('should not save Bitstream File data properly when form is not valid', fakeAsync(() => {
compAsAny.formRef = {formGroup: null};
compAsAny.pathCombiner = pathCombiner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,56 +416,58 @@
this.operationsBuilder.remove(this.pathCombiner.getPath(path));
});
const accessConditionsToSave = [];
formData.accessConditions
.map((accessConditions) => accessConditions.accessConditionGroup)
.filter((accessCondition) => isNotEmpty(accessCondition))
.forEach((accessCondition) => {
let accessConditionOpt;

this.availableAccessConditionOptions
.filter((element) => isNotNull(accessCondition.name) && element.name === accessCondition.name[0].value)
.forEach((element) => accessConditionOpt = element);

if (accessConditionOpt) {
const currentAccessCondition = Object.assign({}, accessCondition);
currentAccessCondition.name = this.retrieveValueFromField(accessCondition.name);

/* When start and end date fields are deactivated, their values may be still present in formData,
therefore it is necessary to delete them if they're not allowed by the current access condition option. */
if (!accessConditionOpt.hasStartDate) {
delete currentAccessCondition.startDate;
} else if (accessCondition.startDate) {
const startDate = this.retrieveValueFromField(accessCondition.startDate);
// Clamp the start date to the maximum, if any, since the
// datepicker sometimes exceeds it.
let startDateDate = new Date(startDate);
if (accessConditionOpt.maxStartDate) {
if (formData.hasOwnProperty('accessConditions')) {
formData.accessConditions
.filter((accessConditions) => isNotNull(accessConditions))
.map((accessConditions) => accessConditions.accessConditionGroup)
.filter((accessCondition) => isNotEmpty(accessCondition))
.forEach((accessCondition) => {
let accessConditionOpt;

this.availableAccessConditionOptions
.filter((element) => isNotNull(accessCondition.name) && element.name === accessCondition.name[0].value)
.forEach((element) => accessConditionOpt = element);

if (accessConditionOpt) {
const currentAccessCondition = Object.assign({}, accessCondition);
currentAccessCondition.name = this.retrieveValueFromField(accessCondition.name);

/* When start and end date fields are deactivated, their values may be still present in formData,
therefore it is necessary to delete them if they're not allowed by the current access condition option. */
if (!accessConditionOpt.hasStartDate) {
delete currentAccessCondition.startDate;
} else if (accessCondition.startDate) {
const startDate = this.retrieveValueFromField(accessCondition.startDate);
// Clamp the start date to the maximum, if any, since the
// datepicker sometimes exceeds it.
let startDateDate = new Date(startDate);
if (accessConditionOpt.maxStartDate) {
const maxStartDateDate = new Date(accessConditionOpt.maxStartDate);
if (startDateDate > maxStartDateDate) {
startDateDate = maxStartDateDate;
startDateDate = maxStartDateDate;

Check warning on line 447 in src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts#L447

Added line #L447 was not covered by tests
}
}
currentAccessCondition.startDate = dateToISOFormat(startDateDate);
}
currentAccessCondition.startDate = dateToISOFormat(startDateDate);
}
if (!accessConditionOpt.hasEndDate) {
delete currentAccessCondition.endDate;
} else if (accessCondition.endDate) {
const endDate = this.retrieveValueFromField(accessCondition.endDate);
// Clamp the end date to the maximum, if any, since the
// datepicker sometimes exceeds it.
let endDateDate = new Date(endDate);
if (accessConditionOpt.maxEndDate) {
if (!accessConditionOpt.hasEndDate) {
delete currentAccessCondition.endDate;
} else if (accessCondition.endDate) {
const endDate = this.retrieveValueFromField(accessCondition.endDate);
// Clamp the end date to the maximum, if any, since the
// datepicker sometimes exceeds it.
let endDateDate = new Date(endDate);
if (accessConditionOpt.maxEndDate) {
const maxEndDateDate = new Date(accessConditionOpt.maxEndDate);
if (endDateDate > maxEndDateDate) {
endDateDate = maxEndDateDate;
endDateDate = maxEndDateDate;

Check warning on line 462 in src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts#L462

Added line #L462 was not covered by tests
}
}
currentAccessCondition.endDate = dateToISOFormat(endDateDate);
}
currentAccessCondition.endDate = dateToISOFormat(endDateDate);
accessConditionsToSave.push(currentAccessCondition);
}
accessConditionsToSave.push(currentAccessCondition);
}
});

});
}
if (isNotEmpty(accessConditionsToSave)) {
this.operationsBuilder.add(this.pathCombiner.getPath('accessConditions'), accessConditionsToSave, true);
}
Expand Down
Loading