Skip to content

Commit 696e3e7

Browse files
Mattia VianelliAndrea Barbasso
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2764 (pull request DSpace#4334)
Task/dspace cris 2023 02 x/DSC-2764 Approved-by: Andrea Barbasso
2 parents 5f03a06 + c70e4e3 commit 696e3e7

5 files changed

Lines changed: 101 additions & 19 deletions

File tree

src/app/lucky-search/search/lucky-search.component.spec.ts

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {getBitstreamDownloadRoute} from '../../app-routing-paths';
2525
import {PLATFORM_ID} from '@angular/core';
2626
import {NotificationsService} from '../../shared/notifications/notifications.service';
2727
import {NotificationsServiceStub} from '../../shared/testing/notifications-service.stub';
28+
import {APP_CONFIG} from '../../../config/app-config.interface';
2829

2930
describe('LuckySearchComponent', () => {
3031
let fixture: ComponentFixture<LuckySearchComponent>;
@@ -101,6 +102,7 @@ describe('LuckySearchComponent', () => {
101102
{provide: HardRedirectService, useValue: hardRedirectService},
102103
{provide: PLATFORM_ID, useValue: 'browser'},
103104
{provide: NotificationsService, useValue: new NotificationsServiceStub()},
105+
{provide: APP_CONFIG, useValue: {}},
104106
],
105107
})
106108
.compileComponents();
@@ -288,30 +290,83 @@ describe('LuckySearchComponent', () => {
288290
component = fixture.componentInstance;
289291
});
290292

291-
it('should not redirect when no bitstreams are found', () => {
292-
const item = Object.assign(new Item(), {uuid: 'item-uuid-1', name: 'Test item 1'});
293-
const data = createSuccessfulRemoteDataObject(createPaginatedList([
294-
{indexableObject: item, hitHighlights: {}}
295-
])) as any;
296-
component.resultsRD$.next(data);
297-
component.bitstreamFilters$.next([{metadataName: 'dc.title', metadataValue: 'Non-existent bitstream'}]);
298-
bitstreamDataService.findByItem.and.returnValue(createSuccessfulRemoteDataObject$(createPaginatedList([])));
299-
spyOn(component, 'redirect');
300-
fixture.detectChanges();
301-
expect(component.redirect).not.toHaveBeenCalled();
302-
});
293+
it('should not redirect when no bitstreams are found', () => {
294+
const item = Object.assign(new Item(), {uuid: 'item-uuid-1', name: 'Test item 1'});
295+
const data = createSuccessfulRemoteDataObject(createPaginatedList([
296+
{indexableObject: item, hitHighlights: {}}
297+
])) as any;
298+
component.resultsRD$.next(data);
299+
component.bitstreamFilters$.next([{metadataName: 'dc.title', metadataValue: 'Non-existent bitstream'}]);
300+
bitstreamDataService.findByItem.and.returnValue(createSuccessfulRemoteDataObject$(createPaginatedList([])));
301+
spyOn(component, 'redirect');
302+
fixture.detectChanges();
303+
expect(component.redirect).not.toHaveBeenCalled();
304+
});
303305

304-
it('should update showEmptySearchSection$ when no results are found', () => {
306+
it('should update showEmptySearchSection$ when no results are found', () => {
305307
fixture.detectChanges();
306-
const emptyResults = createSuccessfulRemoteDataObject(createPaginatedList([]));
308+
const emptyResults = createSuccessfulRemoteDataObject(createPaginatedList([]));
307309

308-
spyOn(component as any, 'getLuckySearchResults').and.returnValue(observableOf(emptyResults));
309-
spyOn(component as any, 'processSearchResults').and.returnValue(observableOf(emptyResults));
310+
spyOn(component as any, 'getLuckySearchResults').and.returnValue(observableOf(emptyResults));
311+
spyOn(component as any, 'processSearchResults').and.returnValue(observableOf(emptyResults));
310312

311-
component.getSearchResults();
313+
component.getSearchResults();
314+
315+
expect(component.showEmptySearchSection$.getValue()).toBe(true);
316+
});
312317

313-
expect(component.showEmptySearchSection$.getValue()).toBe(true);
314318
});
315319

320+
describe('', () => {
321+
beforeEach(() => {
322+
fixture = TestBed.createComponent(LuckySearchComponent);
323+
component = fixture.componentInstance;
324+
});
325+
326+
it('should return default code when no specific identifier is found', () => {
327+
// @ts-ignore: Accessing private method for testing
328+
component.appConfig = {
329+
luckySearchRedirects: {
330+
default: 301
331+
}
332+
} as any;
333+
// @ts-ignore: Accessing private method for testing
334+
component.currentFilter = {identifier: 'unknown'};
335+
336+
// @ts-ignore: Accessing private method for testing
337+
const result = component.getRedirectCode();
338+
339+
expect(result).toBe(301);
340+
});
341+
342+
it('should return 302 when default is not set and identifier is not found', () => {
343+
// @ts-ignore: Accessing private method for testing
344+
component.appConfig = {
345+
luckySearchRedirects: {}
346+
} as any;
347+
// @ts-ignore: Accessing private method for testing
348+
component.currentFilter = {identifier: 'unknown'};
349+
350+
// @ts-ignore: Accessing private method for testing
351+
const result = component.getRedirectCode();
352+
expect(result).toBe(302);
353+
});
354+
355+
it('should return specific code for known identifier', () => {
356+
// @ts-ignore: Accessing private method for testing
357+
component.appConfig = {
358+
luckySearchRedirects: {
359+
default: 302,
360+
'legacy-id': 301
361+
}
362+
};
363+
// @ts-ignore: Accessing private method for testing
364+
component.currentFilter = {identifier: 'legacy-id'};
365+
366+
// @ts-ignore: Accessing private method for testing
367+
const result = component.getRedirectCode();
368+
expect(result).toBe(301);
369+
});
370+
316371
});
317372
});

src/app/lucky-search/search/lucky-search.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {HardRedirectService} from '../../core/services/hard-redirect.service';
2525
import {isPlatformServer} from '@angular/common';
2626
import {NotificationsService} from '../../shared/notifications/notifications.service';
2727
import {TranslateService} from '@ngx-translate/core';
28+
import {APP_CONFIG, AppConfig} from '../../../config/app-config.interface';
2829

2930
@Component({
3031
selector: 'ds-lucky-search',
@@ -77,6 +78,7 @@ export class LuckySearchComponent implements OnInit {
7778
private hardRedirectService: HardRedirectService,
7879
private notificationService: NotificationsService,
7980
private translateService: TranslateService,
81+
@Inject(APP_CONFIG) private appConfig: AppConfig,
8082
) {}
8183

8284
ngOnInit(): void {
@@ -186,12 +188,21 @@ export class LuckySearchComponent implements OnInit {
186188

187189
public redirect(url: string): void {
188190
if (isPlatformServer(this.platformId)) {
189-
this.hardRedirectService.redirect(url, 302);
191+
this.hardRedirectService.redirect(url, this.getRedirectCode());
190192
} else {
191193
this.router.navigateByUrl(url, { replaceUrl: true });
192194
}
193195
}
194196

197+
private getRedirectCode(): number {
198+
let code: number = this.appConfig?.luckySearchRedirects?.default || 302;
199+
if (Object.keys(this.appConfig?.luckySearchRedirects).includes(this.currentFilter.identifier)) {
200+
code = this.appConfig.luckySearchRedirects[this.currentFilter.identifier];
201+
}
202+
203+
return code;
204+
}
205+
195206
private parseBitstreamFilters(queryParams: Params): MetadataFilter[] {
196207
const metadataName = queryParams?.bitstreamMetadata;
197208
const metadataValue = queryParams?.bitstreamValue;

src/config/app-config.interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { MetaTagsConfig } from './meta-tags.config';
3939
import { MetadataLinkViewPopoverDataConfig } from './metadata-link-view-popoverdata-config.interface';
4040
import { IdentifierSubtypesConfig } from './identifier-subtypes-config.interface';
4141
import { DatadogRumConfig } from './datadog-rum-config.interfaces';
42+
import {LuckySearchRedirectConfig} from './lucky-search-redirect-config';
4243

4344
interface AppConfig extends Config {
4445
ui: UIServerConfig;
@@ -86,6 +87,9 @@ interface AppConfig extends Config {
8687
metadataLinkViewPopoverData: MetadataLinkViewPopoverDataConfig;
8788
identifierSubtypes: IdentifierSubtypesConfig[];
8889
datadogRum?: DatadogRumConfig;
90+
permanentRedirectPaths?: string[];
91+
luckySearchRedirects?: LuckySearchRedirectConfig;
92+
8993
}
9094

9195
/**

src/config/default-app-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { MetaTagsConfig } from './meta-tags.config';
4242
import { MetadataLinkViewPopoverDataConfig } from './metadata-link-view-popoverdata-config.interface';
4343
import { IdentifierSubtypesConfig, IdentifierSubtypesIconPositionEnum } from './identifier-subtypes-config.interface';
4444
import { DatadogRumConfig } from './datadog-rum-config.interfaces';
45+
import {LuckySearchRedirectConfig} from './lucky-search-redirect-config';
4546

4647
export class DefaultAppConfig implements AppConfig {
4748
production = false;
@@ -885,4 +886,9 @@ export class DefaultAppConfig implements AppConfig {
885886
trackLongTasks: true,
886887
defaultPrivacyLevel: 'mask-user-input',
887888
};
889+
890+
luckySearchRedirects: LuckySearchRedirectConfig = {
891+
'legacy-id': 301,
892+
default: 302
893+
};
888894
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Config} from './config.interface';
2+
3+
export interface LuckySearchRedirectConfig extends Config {
4+
[indexName: string]: number;
5+
default: number;
6+
}

0 commit comments

Comments
 (0)