diff --git a/ProcessMaker/Events/UserUpdated.php b/ProcessMaker/Events/UserUpdated.php index ca07f9e67e..b5588db64e 100644 --- a/ProcessMaker/Events/UserUpdated.php +++ b/ProcessMaker/Events/UserUpdated.php @@ -34,6 +34,8 @@ public function __construct(User $user, array $changes, array $original) $this->user = $user; $this->changes = array_diff_key($changes, array_flip($this::REMOVE_KEYS)); $this->original = array_diff_key($original, array_flip($this::REMOVE_KEYS)); + $this->changes = ArrayHelper::replaceKeyInArray($changes, 'title', 'job_title'); + $this->original = ArrayHelper::replaceKeyInArray($original, 'title', 'job_title'); } /** diff --git a/ProcessMaker/Helpers/ArrayHelper.php b/ProcessMaker/Helpers/ArrayHelper.php index eb82892974..d3b34d82c7 100644 --- a/ProcessMaker/Helpers/ArrayHelper.php +++ b/ProcessMaker/Helpers/ArrayHelper.php @@ -40,4 +40,19 @@ public static function getArrayDifferencesWithFormat(array $changedArray, array return $arrayDiff; } + + /** + * This method replace an old key with a new key in Array given. + * @param array $array + * @param string $oldKey + * @param string $newKey + * @return array + */ + public static function replaceKeyInArray(array $array, string $oldKey, string $newKey) { + if (array_key_exists($oldKey, $array)) { + $array[$newKey] = $array[$oldKey]; + unset($array[$oldKey]); + } + return $array; + } } diff --git a/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php b/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php index e0a9ce0880..bc0149b62e 100644 --- a/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php +++ b/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php @@ -71,4 +71,37 @@ public function testGetArrayDifferencesWithFormat() ] )); } + + public function testReplaceKeyInArray() + { + //Case 1: Existing Key + $myArray = [ + 'name' => 'John', + 'title' => 'Musician' + ]; + $oldKey = 'title'; + $newKey = 'job_title'; + $this->assertEquals( + [ + 'name' => 'John', + 'job_title' => 'Musician' + ], + ArrayHelper::replaceKeyInArray($myArray, $oldKey, $newKey) + ); + + //Case 2: Not Existing Key + $myArray = [ + 'name' => 'John', + 'title' => 'Musician' + ]; + $oldKey = 'titles'; + $newKey = 'job_title'; + $this->assertEquals( + [ + 'name' => 'John', + 'title' => 'Musician' + ], + ArrayHelper::replaceKeyInArray($myArray, $oldKey, $newKey) + ); + } }