From 45f14145a10d36515aa466a09bd86f11029184fc Mon Sep 17 00:00:00 2001
From: nathangray <nathangray.bsc+github@gmail.com>
Date: Tue, 5 Dec 2017 09:44:55 -0700
Subject: [PATCH] * Infolog - Fix project got lost on reload if set via link

---
 infolog/inc/class.infolog_bo.inc.php    |  9 +++--
 infolog/tests/SetProjectManagerTest.php | 50 +++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/infolog/inc/class.infolog_bo.inc.php b/infolog/inc/class.infolog_bo.inc.php
index c7520f67c0..7d913c4fa0 100644
--- a/infolog/inc/class.infolog_bo.inc.php
+++ b/infolog/inc/class.infolog_bo.inc.php
@@ -479,6 +479,8 @@ class infolog_bo
 		$info['info_link_id'] = 0;	// link might have been deleted
 		$info['info_custom_from'] = (int)!!$info['info_from'];
 
+		$this->get_pm_id($info);
+
 		return False;
 	}
 
@@ -1165,10 +1167,11 @@ class infolog_bo
 			}
 			Link::unlink($old_link_id);
 		}
-		// if added link is a project and no other project selected, also add as project
-		if ($app == 'projectmanager' && $id && !$values['pm_id'])
+		// if linked to a project and no other project selected, also add as project
+		$links = Link::get_links('infolog', $values['info_id'], 'projectmanager');
+		if (!$values['pm_id'] && count($links))
 		{
-			$values['old_pm_id'] = $values['pm_id'] = $id;
+			$values['old_pm_id'] = $values['pm_id'] = array_pop($links);
 		}
 	}
 
diff --git a/infolog/tests/SetProjectManagerTest.php b/infolog/tests/SetProjectManagerTest.php
index 840486a85b..48aa2e1aa0 100644
--- a/infolog/tests/SetProjectManagerTest.php
+++ b/infolog/tests/SetProjectManagerTest.php
@@ -6,6 +6,7 @@ namespace EGroupware\Infolog;
 require_once realpath(__DIR__.'/../../api/tests/AppTest.php');	// Application test base
 
 use Egroupware\Api;
+use Egroupware\Api\Link;
 use Egroupware\Api\Etemplate;
 
 /**
@@ -150,6 +151,55 @@ class SetProjectManagerTest extends \EGroupware\Api\AppTest
 		$this->checkElements();
 	}
 
+	/**
+	 * Add a project by only adding it as a link.  First linked project gets
+	 * taken as _the_ project.
+	 */
+	public function testAddProjectViaLink()
+	{
+		// Saving the infolog should try to send a notification
+		$this->bo->tracking->expects($this->exactly(2))
+                ->method('track')
+				->withConsecutive(
+					// First call - creation
+					[$this->callback(function($subject) { return is_null($subject['pm_status']);})],
+					// Second call - after setting project
+					[$this->callback(function($subject) { return $subject['pm_id'] == $this->pm_id;})]
+				);
+
+		$info = $this->getTestInfolog();
+
+		$this->info_id = $this->bo->write($info);
+		$this->assertInternalType('integer', $this->info_id);
+		$this->assertGreaterThan(0, $this->info_id);
+
+		// Force links to run notification now so we get valid testing - it
+		// usually waits until Egw::on_shutdown();
+		Api\Link::run_notifies();
+
+		// Now load it again
+		$info = $this->bo->read($this->info_id);
+
+		// Set project by link
+		Link::link('infolog', $this->info_id, 'projectmanager', $this->pm_id);
+		Api\Link::run_notifies();
+
+		$this->bo->write($info);
+
+		// Now load it again
+		$info = $this->bo->read($this->info_id);
+
+		// Check pm_id is there
+		$this->assertNotNull($info['pm_id'], 'Project was not set');
+
+		// Force links to run notification now so we get valid testing - it
+		// usually waits until Egw::on_shutdown();
+		Api\Link::run_notifies();
+
+		// Check project
+		$this->checkElements();
+	}
+
 	/**
 	 * Create a new infolog entry, set project via info_contact
 	 */