From 718c995a6501c6204f54122055234f584790fb63 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Nowak?= <luke@nexedi.com>
Date: Wed, 12 Apr 2017 14:19:56 +0000
Subject: [PATCH] Link Test Result Node with Test Node

This link allows to navigate from Test Result via its Test Result Node
to Test Node and distributor related to given Test Result.

Note: It happens only, if client will do subscribeNode before calling
createTestResult.
---
 .../base_category_list.xml                    |   3 +
 .../erp5_test_result/TestResultNode_view.xml  |   1 +
 .../my_specialise_title.xml                   | 101 ++++++++++++++++++
 .../test.erp5.testTaskDistribution.py         |  13 +++
 .../template_portal_type_base_category_list   |   1 +
 product/ERP5/Tool/TaskDistributionTool.py     |  18 +++-
 6 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view/my_specialise_title.xml

diff --git a/bt5/erp5_test_result/PortalTypeBaseCategoryTemplateItem/base_category_list.xml b/bt5/erp5_test_result/PortalTypeBaseCategoryTemplateItem/base_category_list.xml
index 297d1e13a0..ac5e29ab9d 100644
--- a/bt5/erp5_test_result/PortalTypeBaseCategoryTemplateItem/base_category_list.xml
+++ b/bt5/erp5_test_result/PortalTypeBaseCategoryTemplateItem/base_category_list.xml
@@ -21,6 +21,9 @@
  <portal_type id="Test Result Module">
   <item>business_application</item>
  </portal_type>
+ <portal_type id="Test Result Node">
+  <item>specialise</item>
+ </portal_type>
  <portal_type id="Test Suite">
   <item>specialise</item>
  </portal_type>
diff --git a/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view.xml b/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view.xml
index f0784d1907..91b03d3e95 100644
--- a/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view.xml
+++ b/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view.xml
@@ -98,6 +98,7 @@
                     <value>
                       <list>
                         <string>my_title</string>
+                        <string>my_specialise_title</string>
                       </list>
                     </value>
                 </item>
diff --git a/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view/my_specialise_title.xml b/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view/my_specialise_title.xml
new file mode 100644
index 0000000000..20e2ec9ab0
--- /dev/null
+++ b/bt5/erp5_test_result/SkinTemplateItem/portal_skins/erp5_test_result/TestResultNode_view/my_specialise_title.xml
@@ -0,0 +1,101 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>delegated_list</string> </key>
+            <value>
+              <list>
+                <string>editable</string>
+                <string>portal_type</string>
+                <string>title</string>
+              </list>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>my_specialise_title</string> </value>
+        </item>
+        <item>
+            <key> <string>message_values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>external_validator_failed</string> </key>
+                    <value> <string>The input failed the external validator.</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>overrides</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>tales</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>editable</string> </key>
+                    <value> <int>0</int> </value>
+                </item>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string>my_relation_field</string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string>Base_viewFieldLibrary</string> </value>
+                </item>
+                <item>
+                    <key> <string>portal_type</string> </key>
+                    <value>
+                      <list>
+                        <tuple>
+                          <string>Test Node</string>
+                          <string>Test Node</string>
+                        </tuple>
+                      </list>
+                    </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string>Test Node</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_test_result/TestTemplateItem/portal_components/test.erp5.testTaskDistribution.py b/bt5/erp5_test_result/TestTemplateItem/portal_components/test.erp5.testTaskDistribution.py
index 8d847b3797..a9424ff13d 100644
--- a/bt5/erp5_test_result/TestTemplateItem/portal_components/test.erp5.testTaskDistribution.py
+++ b/bt5/erp5_test_result/TestTemplateItem/portal_components/test.erp5.testTaskDistribution.py
@@ -383,6 +383,19 @@ class TestTaskDistribution(ERP5TypeTestCase):
     test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
     self.assertEqual("stopped", test_result.getSimulationState())
 
+  def test_05c_createTestResult_with_registered_test_node(self):
+    node_title = 'Node0OinkUink'
+    # "register" Test Node
+    test_node = self.portal.test_node_module.newContent(
+        portal_type='Test Node', title=node_title)
+    self.tic()
+    test_result_path, revision = self._createTestResult(node_title=node_title)
+    # check that Test Node Result used in Test Result is specialised
+    # into registered Test Node
+    test_result = self.getPortalObject().unrestrictedTraverse(test_result_path)
+    test_result_node = test_result.contentValues(portal_type='Test Result Node')[0]
+    self.assertEqual(test_result_node.getSpecialise(), test_node.getRelativeUrl())
+
   def test_06_startStopUnitTest(self):
     """
     We will check methods startUnitTest/stopUnitTest of task distribution tool
diff --git a/bt5/erp5_test_result/bt/template_portal_type_base_category_list b/bt5/erp5_test_result/bt/template_portal_type_base_category_list
index 571542ceb6..97ad5e53b8 100644
--- a/bt5/erp5_test_result/bt/template_portal_type_base_category_list
+++ b/bt5/erp5_test_result/bt/template_portal_type_base_category_list
@@ -5,6 +5,7 @@ SlapOS Software Release Unit Test | specialise
 Test Node | aggregate
 Test Node | specialise
 Test Result Module | business_application
+Test Result Node | specialise
 Test Result | source_project
 Test Suite Repository | aggregate
 Test Suite | specialise
\ No newline at end of file
diff --git a/product/ERP5/Tool/TaskDistributionTool.py b/product/ERP5/Tool/TaskDistributionTool.py
index 6ea7bc5f6e..5456a1dd4a 100644
--- a/product/ERP5/Tool/TaskDistributionTool.py
+++ b/product/ERP5/Tool/TaskDistributionTool.py
@@ -53,6 +53,15 @@ class TaskDistributionTool(BaseTool):
     """
     return 1
 
+  def _getTestNodeRelativeUrl(self, node_title):
+    portal = self.getPortalObject()
+    test_node_list = portal.portal_catalog(
+        portal_type="Test Node",
+        title=SimpleQuery(comparison_operator='=', title=node_title),
+    )
+    if len(test_node_list) == 1:
+      return test_node_list[0].getRelativeUrl()
+
   def _getTestResultNode(self, test_result, node_title):
     node_list = [x for x in test_result.objectValues(
        portal_type='Test Result Node') if x.getTitle() == node_title]
@@ -86,8 +95,15 @@ class TaskDistributionTool(BaseTool):
       if node_title is not None:
         node = self._getTestResultNode(test_result, node_title)
         if node is None:
+          # Note: specialise might not be set. This is on purpose, in order
+          #       to support cases, when client will call createTestResult
+          #       without calling subscribeNode before, and this is required
+          #       to find Test Node document returned by
+          #       _getTestNodeRelativeUrl.
           node = test_result.newContent(portal_type='Test Result Node',
-                                 title=node_title)
+                                 title=node_title,
+                                 specialise=self._getTestNodeRelativeUrl(
+                                   node_title))
           node.start()
     def createTestResultLineList(test_result, test_name_list):
       duration_list = []
-- 
2.30.9