[ACS-4448] Adding selectable options of mime types in folder rules add conditions (#3098)

* [ACS-4448] Adding selectable options of mime types in folder rules conditions.

* [ACS-4448] Restructuring

* PR comments

* added unit tests

* PR comments

* unit tests

* unit tests
This commit is contained in:
Aayush Rohila
2023-04-11 14:34:48 +05:30
committed by GitHub
parent 494c2f753c
commit 2787ed97fb
7 changed files with 936 additions and 7 deletions

View File

@@ -32,6 +32,18 @@ const simpleConditionMock: RuleSimpleCondition = {
parameter: ''
};
export const mimeTypeMock: RuleSimpleCondition = {
field: 'mimetype',
comparator: 'equals',
parameter: ''
};
export const categoryMock: RuleSimpleCondition = {
field: 'category',
comparator: 'equals',
parameter: ''
};
export const simpleConditionUnknownFieldMock: RuleSimpleCondition = {
field: 'unknown-field',
comparator: 'equals',

View File

@@ -23,7 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
export type RuleConditionFieldType = 'string' | 'number' | 'date' | 'type' | 'special';
export type RuleConditionFieldType = 'string' | 'number' | 'date' | 'type' | 'special' | 'mimeType';
export interface RuleConditionField {
name: string;
@@ -31,6 +31,8 @@ export interface RuleConditionField {
type: RuleConditionFieldType;
}
export const comparatorHiddenForConditionFieldType: string[] = ['special', 'mimeType'];
export const ruleConditionFields: RuleConditionField[] = [
{
name: 'cm:name',
@@ -45,7 +47,7 @@ export const ruleConditionFields: RuleConditionField[] = [
{
name: 'mimetype',
label: 'ACA_FOLDER_RULES.RULE_DETAILS.FIELDS.MIMETYPE',
type: 'string'
type: 'mimeType'
},
{
name: 'encoding',

View File

@@ -0,0 +1,29 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
export interface MimeType {
label: string;
value: string;
}

View File

@@ -22,6 +22,14 @@
</mat-form-field>
<mat-form-field class="aca-rule-simple-condition__form__parameter-input">
<input matInput placeholder="{{ 'ACA_FOLDER_RULES.RULE_DETAILS.PLACEHOLDER.VALUE' | translate }}" type="text" formControlName="parameter" data-automation-id="value-input">
<mat-select formControlName="parameter" data-automation-id="simple-condition-value-select" *ngIf="selectedField?.type === 'mimeType'; else valueInput">
<mat-option *ngFor="let mimeType of mimeTypes"
[value]="mimeType.value">
{{ mimeType.label }}
</mat-option>
</mat-select>
<ng-template #valueInput>
<input matInput placeholder="{{ 'ACA_FOLDER_RULES.RULE_DETAILS.PLACEHOLDER.VALUE' | translate }}" type="text" formControlName="parameter" data-automation-id="value-input">
</ng-template>
</mat-form-field>
</form>

View File

@@ -28,7 +28,8 @@ import { RuleSimpleConditionUiComponent } from './rule-simple-condition.ui-compo
import { CoreTestingModule } from '@alfresco/adf-core';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { simpleConditionUnknownFieldMock } from '../../mock/conditions.mock';
import { categoryMock, mimeTypeMock, simpleConditionUnknownFieldMock } from '../../mock/conditions.mock';
import { MimeType } from './rule-mime-types';
describe('RuleSimpleConditionUiComponent', () => {
let fixture: ComponentFixture<RuleSimpleConditionUiComponent>;
@@ -75,6 +76,19 @@ describe('RuleSimpleConditionUiComponent', () => {
expect(getComputedStyle(comparatorFormField).display).toBe('none');
});
it('should hide the comparator select box if the type of the field is mimeType', () => {
fixture.detectChanges();
const comparatorFormField = getByDataAutomationId('comparator-form-field').nativeElement;
expect(fixture.componentInstance.isComparatorHidden).toBeFalsy();
expect(getComputedStyle(comparatorFormField).display).not.toBe('none');
changeMatSelectValue('field-select', 'mimetype');
expect(fixture.componentInstance.isComparatorHidden).toBeTruthy();
expect(getComputedStyle(comparatorFormField).display).toBe('none');
});
it('should set the comparator to equals if the field is set to a type with different comparators', () => {
const onChangeFieldSpy = spyOn(fixture.componentInstance, 'onChangeField').and.callThrough();
fixture.detectChanges();
@@ -84,7 +98,7 @@ describe('RuleSimpleConditionUiComponent', () => {
changeMatSelectValue('field-select', 'mimetype');
expect(onChangeFieldSpy).toHaveBeenCalledTimes(1);
expect(getByDataAutomationId('comparator-select').componentInstance.value).toBe('contains');
expect(getByDataAutomationId('comparator-select').componentInstance.value).toBe('equals');
changeMatSelectValue('field-select', 'size');
expect(onChangeFieldSpy).toHaveBeenCalledTimes(2);
@@ -116,4 +130,46 @@ describe('RuleSimpleConditionUiComponent', () => {
const unknownOptionMatOption = getByDataAutomationId('unknown-field-option');
expect(unknownOptionMatOption).toBeNull();
});
it('should provide select option when mimeType is selected and value filled', () => {
const mockMimeTypes: MimeType[] = [
{
value: 'video/3gpp',
label: '3G Video'
},
{
value: 'video/3gpp2',
label: '3G2 Video'
},
{
value: 'application/vnd.alfresco.ai.features.v1+json',
label: 'AI-Features'
},
{
value: 'application/vnd.alfresco.ai.labels.v1+json',
label: 'AI-Labels'
}
];
fixture.componentInstance.writeValue(mimeTypeMock);
fixture.componentInstance.mimeTypes = mockMimeTypes;
fixture.componentInstance.onChangeField();
fixture.detectChanges();
expect(getByDataAutomationId('simple-condition-value-select')).toBeTruthy();
expect(fixture.componentInstance.form.get('parameter').value).toEqual(mockMimeTypes[0].value);
});
it('should set value to empty when any condition is selected after mimeType', () => {
fixture.componentInstance.writeValue(mimeTypeMock);
fixture.detectChanges();
expect(getByDataAutomationId('simple-condition-value-select')).toBeTruthy();
fixture.componentInstance.writeValue(categoryMock);
fixture.detectChanges();
expect(getByDataAutomationId('value-input').nativeElement.value).toBe('');
});
});

View File

@@ -26,8 +26,10 @@
import { Component, forwardRef, Input, OnDestroy, ViewEncapsulation } from '@angular/core';
import { AbstractControl, ControlValueAccessor, FormControl, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { RuleSimpleCondition } from '../../model/rule-simple-condition.model';
import { RuleConditionField, ruleConditionFields } from './rule-condition-fields';
import { comparatorHiddenForConditionFieldType, RuleConditionField, ruleConditionFields } from './rule-condition-fields';
import { RuleConditionComparator, ruleConditionComparators } from './rule-condition-comparators';
import { AppConfigService } from '@alfresco/adf-core';
import { MimeType } from './rule-mime-types';
@Component({
selector: 'aca-rule-simple-condition',
@@ -52,6 +54,8 @@ export class RuleSimpleConditionUiComponent implements ControlValueAccessor, OnD
parameter: new FormControl()
});
mimeTypes: MimeType[] = [];
private _readOnly = false;
@Input()
get readOnly(): boolean {
@@ -61,6 +65,10 @@ export class RuleSimpleConditionUiComponent implements ControlValueAccessor, OnD
this.setDisabledState(isReadOnly);
}
constructor(private config: AppConfigService) {
this.mimeTypes = this.config.get<Array<MimeType>>('mimeTypes');
}
private formSubscription = this.form.valueChanges.subscribe((value: any) => {
this.onChange(value);
this.onTouch();
@@ -86,12 +94,16 @@ export class RuleSimpleConditionUiComponent implements ControlValueAccessor, OnD
return ruleConditionComparators.filter((comparator) => Object.keys(comparator.labels).includes(this.selectedField.type));
}
get isComparatorHidden(): boolean {
return this.selectedField?.type === 'special';
return comparatorHiddenForConditionFieldType.includes(this.selectedField?.type);
}
get comparatorControl(): AbstractControl {
return this.form.get('comparator');
}
private get parameterControl(): AbstractControl<string> {
return this.form.get('parameter');
}
onChange: (condition: RuleSimpleCondition) => void = () => undefined;
onTouch: () => void = () => undefined;
@@ -121,6 +133,11 @@ export class RuleSimpleConditionUiComponent implements ControlValueAccessor, OnD
if (!this.selectedFieldComparators.find((comparator) => comparator.name === this.comparatorControl.value)) {
this.comparatorControl.setValue('equals');
}
if (!this.parameterControl.value && this.selectedField?.type === 'mimeType') {
this.parameterControl.setValue(this.mimeTypes[0]?.value);
} else if (this.parameterControl.value) {
this.parameterControl.setValue('');
}
}
ngOnDestroy() {