diff --git a/lib/core/form/components/widgets/core/form-field.model.ts b/lib/core/form/components/widgets/core/form-field.model.ts index d29905b140..098190e30d 100644 --- a/lib/core/form/components/widgets/core/form-field.model.ts +++ b/lib/core/form/components/widgets/core/form-field.model.ts @@ -74,6 +74,7 @@ export class FormFieldModel extends FormWidgetModel { dateDisplayFormat: string = this.defaultDateFormat; selectionType: 'single' | 'multiple' = null; rule?: FormFieldRule; + selectLoggedUser: boolean; // container model members numberOfColumns: number = 1; @@ -181,6 +182,7 @@ export class FormFieldModel extends FormWidgetModel { this.tooltip = json.tooltip; this.selectionType = json.selectionType; this.rule = json.rule; + this.selectLoggedUser = json.selectLoggedUser; if (json.placeholder && json.placeholder !== '' && json.placeholder !== 'null') { this.placeholder = json.placeholder; diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.spec.ts b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.spec.ts new file mode 100644 index 0000000000..52d04a9b0e --- /dev/null +++ b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.spec.ts @@ -0,0 +1,64 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { CoreTestingModule, FormFieldModel, FormModel, IdentityUserService, setupTestBed } from '@alfresco/adf-core'; +import { TranslateModule } from '@ngx-translate/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { PeopleCloudWidgetComponent } from './people-cloud.widget'; +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; + +describe('PeopleCloudWidgetComponent', () => { + let fixture: ComponentFixture; + let widget: PeopleCloudWidgetComponent; + let identityUserService: IdentityUserService; + const currentUser = { id: 'id', username: 'user' }; + const fakeUser = { id: 'fake-id', username: 'fake' }; + + setupTestBed({ + imports: [ + TranslateModule.forRoot(), + CoreTestingModule + ], + declarations: [ + PeopleCloudWidgetComponent + ], + schemas: [ + CUSTOM_ELEMENTS_SCHEMA + ] + }); + + beforeEach(() => { + identityUserService = TestBed.inject(IdentityUserService); + fixture = TestBed.createComponent(PeopleCloudWidgetComponent); + widget = fixture.componentInstance; + spyOn(identityUserService, 'getCurrentUserInfo').and.returnValue(fakeUser); + }); + + it('should preselect the current user', () => { + widget.field = new FormFieldModel(new FormModel(), { value: null, selectLoggedUser: true }); + fixture.detectChanges(); + expect(widget.preSelectUsers).toEqual([fakeUser]); + expect(identityUserService.getCurrentUserInfo).toHaveBeenCalled(); + }); + + it('should not preselect the current user if value exist', () => { + widget.field = new FormFieldModel(new FormModel(), { value: [currentUser], selectLoggedUser: true }); + fixture.detectChanges(); + expect(widget.preSelectUsers).toEqual([currentUser]); + expect(identityUserService.getCurrentUserInfo).not.toHaveBeenCalled(); + }); +}); diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts index d13e574fa0..25e171f70f 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts @@ -16,7 +16,7 @@ */ import { Component, OnInit, ViewEncapsulation } from '@angular/core'; -import { WidgetComponent, IdentityUserModel, FormService } from '@alfresco/adf-core'; +import { WidgetComponent, IdentityUserModel, FormService, IdentityUserService } from '@alfresco/adf-core'; import { FormControl } from '@angular/forms'; import { filter, takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs'; @@ -52,7 +52,7 @@ export class PeopleCloudWidgetComponent extends WidgetComponent implements OnIni preSelectUsers: IdentityUserModel[]; search: FormControl; - constructor(formService: FormService) { + constructor(formService: FormService, private identityUserService: IdentityUserService) { super(formService); } @@ -88,6 +88,12 @@ export class PeopleCloudWidgetComponent extends WidgetComponent implements OnIni this.field.validate(); this.field.form.validateForm(); }); + + if (this.field.selectLoggedUser && !this.field.value) { + const userInfo = this.identityUserService.getCurrentUserInfo(); + this.preSelectUsers = [ userInfo ]; + this.onChangedUser(this.preSelectUsers); + } } ngOnDestroy() {