From d1be524875b8e501d2ba1b5d5ea4032d0e5460c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 16 Mar 2022 08:46:41 -0500
Subject: [PATCH] Workflow: add a simple _checkConsistency implementation

For now only checks that error_message is missing, but later we might
extended this to perform more check on the workflow definition.
---
 product/ERP5Type/Core/InteractionWorkflow.py |  3 +++
 product/ERP5Type/Core/Workflow.py            | 21 ++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/product/ERP5Type/Core/InteractionWorkflow.py b/product/ERP5Type/Core/InteractionWorkflow.py
index ccab8d94b1..300b2ba083 100644
--- a/product/ERP5Type/Core/InteractionWorkflow.py
+++ b/product/ERP5Type/Core/InteractionWorkflow.py
@@ -358,6 +358,9 @@ class InteractionWorkflow(Workflow):
   def getStateValueList(self):
     return []
 
+  def _checkConsistency(self, fixit=False):
+    return []
+
   security.declareProtected(Permissions.AccessContentsInformation, 'showAsXML')
   def showAsXML(self, root=None):
     from lxml import etree
diff --git a/product/ERP5Type/Core/Workflow.py b/product/ERP5Type/Core/Workflow.py
index 76d0e7cb4f..72b0ce3929 100644
--- a/product/ERP5Type/Core/Workflow.py
+++ b/product/ERP5Type/Core/Workflow.py
@@ -30,6 +30,7 @@
 ## Used in Products.ERP5Type.patches.DCWorkflow so this needs to go first...
 from Acquisition import aq_parent, aq_inner
 from Products.PageTemplates.Expressions import SecureModuleImporter
+from Products.ERP5Type.ConsistencyMessage import ConsistencyMessage
 from AccessControl import getSecurityManager
 from Products.PageTemplates.Expressions import getEngine
 from six import reraise
@@ -958,6 +959,26 @@ class Workflow(XMLObject):
       raise ObjectMoved(ex.getNewObject(), res)
     return res
 
+  def _checkConsistency(self, fixit=False):
+    """Checks the workflow definition.
+    """
+    consistency_message_list = []
+    # make sure we have necessary variables
+    variable_reference_set = {
+        v.getReference()
+        for v in self.contentValues(portal_type='Workflow Variable')
+    }
+    for variable_reference in 'error_message', :
+      if variable_reference not in variable_reference_set:
+        consistency_message_list.append(
+            ConsistencyMessage(
+                self,
+                object_relative_url=self.getRelativeUrl(),
+                message=
+                'Required variable {variable_reference} missing in workflow.'.
+                format(variable_reference=variable_reference)))
+    return consistency_message_list
+
   security.declareProtected(Permissions.AccessContentsInformation, 'showAsXML')
   def showAsXML(self, root=None):
     from lxml import etree
-- 
2.30.9