From 6ea6c6182d365203b53f7540209414a1cb788dda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Thu, 21 Apr 2022 14:14:01 +0200
Subject: [PATCH 1/2] test: check that fields of loaded examples are not empty
 when 'empty fields on calculator creation' is enabled

refs #530
---
 e2e/examples-empty-fields.e2e-spec.ts | 61 +++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 e2e/examples-empty-fields.e2e-spec.ts

diff --git a/e2e/examples-empty-fields.e2e-spec.ts b/e2e/examples-empty-fields.e2e-spec.ts
new file mode 100644
index 000000000..573012930
--- /dev/null
+++ b/e2e/examples-empty-fields.e2e-spec.ts
@@ -0,0 +1,61 @@
+import { browser, by, element } from "protractor";
+import { CalculatorPage } from "./calculator.po";
+import { ListPage } from "./list.po";
+import { Navbar } from "./navbar.po";
+import { PreferencesPage } from "./preferences.po"
+
+/**
+ * check that fields are empty on creation
+ */
+describe("ngHyd - Check that examples fields are not empty with 'empty fields on calculator creation' is enabled - ", () => {
+    let prefPage: PreferencesPage;
+    let navBar: Navbar;
+    let calcPage: CalculatorPage;
+
+    beforeEach(async () => {
+        prefPage = new PreferencesPage();
+        navBar = new Navbar();
+        calcPage = new CalculatorPage();
+
+        // enable evil option "empty fields on module creation"
+        await prefPage.navigateTo();
+        await browser.sleep(200);
+        await prefPage.enableEvilEmptyFields();
+        await browser.sleep(200);
+    });
+
+    /**
+     * check that a input set is in a given (empty/filled) state
+     * @param inputIds id of the inputs to check
+     * @param emptys empty/not empty state array
+     */
+    async function checkFields(inputIds: string[], emptys: boolean[]) {
+        let n = 0;
+        for (const id of inputIds) {
+            const inp = await calcPage.getInputById(id);
+            const txt = await inp.getAttribute("value");
+            expect(txt === "").toEqual(emptys[n]);
+            n++;
+        }
+    }
+
+    it("when a standard fish ladder calculator is created", async () => {
+        // start page
+        await navBar.clickNewCalculatorButton();
+        await browser.sleep(200);
+
+        // open 1st example
+        const examples = await element.all(by.css("#examples-list .load-example"));
+        await examples[0].click();
+        await browser.sleep(50);
+
+        // select wall module
+        await navBar.openNthCalculator(4);
+        await browser.sleep(50);
+
+        // check fields are not empty
+        const inputIds = ["Z1", "LB", "PB", "0_L", "0_CdWSL"];
+        const emptys = [false, false, false, false, false];
+        await checkFields(inputIds, emptys);
+    });
+});
-- 
GitLab


From d8504a8a541589f51d496c1cdd7ea1379e32a41f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Thu, 21 Apr 2022 16:23:13 +0200
Subject: [PATCH 2/2] fix: empty fields on session loading when 'empty fields
 on calculator creation' option is enabled

refs #530
---
 src/app/formulaire/elements/fieldset.ts        | 11 +++++------
 src/app/formulaire/elements/formulaire-node.ts |  2 +-
 src/app/services/formulaire.service.ts         |  9 +++++++++
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/app/formulaire/elements/fieldset.ts b/src/app/formulaire/elements/fieldset.ts
index cf23d79c9..e5a9ca15a 100644
--- a/src/app/formulaire/elements/fieldset.ts
+++ b/src/app/formulaire/elements/fieldset.ts
@@ -400,9 +400,6 @@ export class FieldSet extends FormulaireElement implements Observer {
                         }
                     } else {
                         if (this.parentForm instanceof FormulaireFixedVar) {
-                            // backup parameters
-                            const oldParams = this.backupParameters();
-
                             // for all select fields known by the form, apply received value
                             // to associated property
                             const selectIds = this.parentForm.selectids;
@@ -421,11 +418,13 @@ export class FieldSet extends FormulaireElement implements Observer {
                                     }
                                 }
                             }
-
-                            // restore parameters
-                            this.restoreParameters(oldParams, FormulaireNode.NeverEmptyFields);
                         }
                     }
+
+                    if (ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit) {
+                        const f = this.parentForm;
+                        f.emptyFields();
+                    }
                     break; // switch (data.action)
             }
         }
diff --git a/src/app/formulaire/elements/formulaire-node.ts b/src/app/formulaire/elements/formulaire-node.ts
index 4ce064883..0ab8a88df 100644
--- a/src/app/formulaire/elements/formulaire-node.ts
+++ b/src/app/formulaire/elements/formulaire-node.ts
@@ -156,7 +156,7 @@ export abstract class FormulaireNode implements IObservable {
             if (p instanceof NgParameter) {
                 if (
                     [ParamValueMode.SINGLE, ParamValueMode.CALCUL].includes(p.valueMode)
-                    && !except.includes(p.id)
+                    && !except.includes(p.id) && !p.isValueModified
                 ) {
                     if (p.valueMode === ParamValueMode.CALCUL) {
                         calcP = p;
diff --git a/src/app/services/formulaire.service.ts b/src/app/services/formulaire.service.ts
index 5d3910bbf..1bfdbe14f 100644
--- a/src/app/services/formulaire.service.ts
+++ b/src/app/services/formulaire.service.ts
@@ -50,6 +50,7 @@ import { FormulairePAR } from "../formulaire/definition/form-par";
 import { FormulaireVerificateur } from "../formulaire/definition/form-verificateur";
 import { FormulaireEspece } from "../formulaire/definition/form-espece";
 import { FormulairePrebarrage } from "../formulaire/definition/form-prebarrage";
+import { ServiceFactory } from "./service-factory";
 
 @Injectable()
 export class FormulaireService extends Observable {
@@ -628,6 +629,10 @@ export class FormulaireService extends Observable {
      */
     public async loadSession(f: File, formInfos: any[] = []): Promise<{ hasErrors: boolean, loaded: string[] }> {
         try {
+            // disable "empty fields" flag temporarly
+            const emptyFields = ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit;
+            ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit = false;
+
             const s = await this.readSingleFile(f);
             const uids: string[] = [];
             formInfos.forEach((fi) => {
@@ -646,6 +651,10 @@ export class FormulaireService extends Observable {
                 }
                 await this.createFormulaire(nn.nub.calcType, nn.nub, title); // await guarantees loading order
             }
+
+            // restore "empty fields" flag
+            ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit = emptyFields;
+
             // apply settings
             if (res.settings) {
                 // model based settings
-- 
GitLab