Skip to content

Commit

Permalink
Merge pull request #1707 from atmire/fix-1705
Browse files Browse the repository at this point in the history
Fix curation tasks started from the edit community or collection page
  • Loading branch information
tdonohue authored Jun 24, 2022
2 parents e0109b9 + b55e432 commit 9bf0320
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/app/curation-form/curation-form.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { By } from '@angular/platform-browser';
import { ConfigurationDataService } from '../core/data/configuration-data.service';
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
import { getProcessDetailRoute } from '../process-page/process-page-routing.paths';
import { HandleService } from '../shared/handle.service';

describe('CurationFormComponent', () => {
let comp: CurationFormComponent;
Expand All @@ -23,6 +24,7 @@ describe('CurationFormComponent', () => {
let scriptDataService: ScriptDataService;
let processDataService: ProcessDataService;
let configurationDataService: ConfigurationDataService;
let handleService: HandleService;
let notificationsService;
let router;

Expand Down Expand Up @@ -51,18 +53,23 @@ describe('CurationFormComponent', () => {
}))
});

handleService = {
normalizeHandle: (a) => a
} as any;

notificationsService = new NotificationsServiceStub();
router = new RouterStub();

TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), FormsModule, ReactiveFormsModule],
declarations: [CurationFormComponent],
providers: [
{provide: ScriptDataService, useValue: scriptDataService},
{provide: ProcessDataService, useValue: processDataService},
{provide: NotificationsService, useValue: notificationsService},
{provide: Router, useValue: router},
{provide: ConfigurationDataService, useValue: configurationDataService},
{ provide: ScriptDataService, useValue: scriptDataService },
{ provide: ProcessDataService, useValue: processDataService },
{ provide: NotificationsService, useValue: notificationsService },
{ provide: HandleService, useValue: handleService },
{ provide: Router, useValue: router},
{ provide: ConfigurationDataService, useValue: configurationDataService },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
Expand Down Expand Up @@ -143,4 +150,13 @@ describe('CurationFormComponent', () => {
{name: '-i', value: 'all'},
], []);
});

it(`should show an error notification and return when an invalid dsoHandle is provided`, () => {
comp.dsoHandle = 'test-handle';
spyOn(handleService, 'normalizeHandle').and.returnValue(null);
comp.submit();

expect(notificationsService.error).toHaveBeenCalled();
expect(scriptDataService.invoke).not.toHaveBeenCalled();
});
});
15 changes: 11 additions & 4 deletions src/app/curation-form/curation-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getFirstCompletedRemoteData } from '../core/shared/operators';
import { find, map } from 'rxjs/operators';
import { NotificationsService } from '../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
import { hasValue, isEmpty, isNotEmpty } from '../shared/empty.util';
import { hasValue, isEmpty, isNotEmpty, hasNoValue } from '../shared/empty.util';
import { RemoteData } from '../core/data/remote-data';
import { Router } from '@angular/router';
import { ProcessDataService } from '../core/data/processes/process-data.service';
Expand All @@ -14,9 +14,9 @@ import { ConfigurationDataService } from '../core/data/configuration-data.servic
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
import { Observable } from 'rxjs';
import { getProcessDetailRoute } from '../process-page/process-page-routing.paths';
import { HandleService } from '../shared/handle.service';

export const CURATION_CFG = 'plugin.named.org.dspace.curate.CurationTask';

/**
* Component responsible for rendering the Curation Task form
*/
Expand All @@ -39,6 +39,7 @@ export class CurationFormComponent implements OnInit {
private processDataService: ProcessDataService,
private notificationsService: NotificationsService,
private translateService: TranslateService,
private handleService: HandleService,
private router: Router
) {
}
Expand Down Expand Up @@ -76,13 +77,19 @@ export class CurationFormComponent implements OnInit {
const taskName = this.form.get('task').value;
let handle;
if (this.hasHandleValue()) {
handle = this.dsoHandle;
handle = this.handleService.normalizeHandle(this.dsoHandle);
if (isEmpty(handle)) {
this.notificationsService.error(this.translateService.get('curation.form.submit.error.head'),
this.translateService.get('curation.form.submit.error.invalid-handle'));
return;
}
} else {
handle = this.form.get('handle').value;
handle = this.handleService.normalizeHandle(this.form.get('handle').value);
if (isEmpty(handle)) {
handle = 'all';
}
}

this.scriptDataService.invoke('curate', [
{ name: '-t', value: taskName },
{ name: '-i', value: handle },
Expand Down
47 changes: 47 additions & 0 deletions src/app/shared/handle.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { HandleService } from './handle.service';

describe('HandleService', () => {
let service: HandleService;

beforeEach(() => {
service = new HandleService();
});

describe(`normalizeHandle`, () => {
it(`should simply return an already normalized handle`, () => {
let input, output;

input = '123456789/123456';
output = service.normalizeHandle(input);
expect(output).toEqual(input);

input = '12.3456.789/123456';
output = service.normalizeHandle(input);
expect(output).toEqual(input);
});

it(`should normalize a handle url`, () => {
let input, output;

input = 'https://hdl.handle.net/handle/123456789/123456';
output = service.normalizeHandle(input);
expect(output).toEqual('123456789/123456');

input = 'https://rest.api/server/handle/123456789/123456';
output = service.normalizeHandle(input);
expect(output).toEqual('123456789/123456');
});

it(`should return null if the input doesn't contain a handle`, () => {
let input, output;

input = 'https://hdl.handle.net/handle/123456789';
output = service.normalizeHandle(input);
expect(output).toBeNull();

input = 'something completely different';
output = service.normalizeHandle(input);
expect(output).toBeNull();
});
});
});
41 changes: 41 additions & 0 deletions src/app/shared/handle.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable } from '@angular/core';
import { isNotEmpty, isEmpty } from './empty.util';

const PREFIX_REGEX = /handle\/([^\/]+\/[^\/]+)$/;
const NO_PREFIX_REGEX = /^([^\/]+\/[^\/]+)$/;

@Injectable({
providedIn: 'root'
})
export class HandleService {


/**
* Turns a handle string into the default 123456789/12345 format
*
* @param handle the input handle
*
* normalizeHandle('123456789/123456') // '123456789/123456'
* normalizeHandle('12.3456.789/123456') // '12.3456.789/123456'
* normalizeHandle('https://hdl.handle.net/handle/123456789/123456') // '123456789/123456'
* normalizeHandle('https://rest.api/server/handle/123456789/123456') // '123456789/123456'
* normalizeHandle('https://rest.api/server/handle/123456789') // null
*/
normalizeHandle(handle: string): string {
let matches: string[];
if (isNotEmpty(handle)) {
matches = handle.match(PREFIX_REGEX);
}

if (isEmpty(matches) || matches.length < 2) {
matches = handle.match(NO_PREFIX_REGEX);
}

if (isEmpty(matches) || matches.length < 2) {
return null;
} else {
return matches[1];
}
}

}
2 changes: 2 additions & 0 deletions src/assets/i18n/en.json5
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,8 @@

"curation.form.submit.error.content": "An error occured when trying to start the curation task.",

"curation.form.submit.error.invalid-handle": "Couldn't determine the handle for this object",

"curation.form.handle.label": "Handle:",

"curation.form.handle.hint": "Hint: Enter [your-handle-prefix]/0 to run a task across entire site (not all tasks may support this capability)",
Expand Down

0 comments on commit 9bf0320

Please sign in to comment.