diff --git a/Migrators/Importer/Services/BaseWorkItemService.cs b/Migrators/Importer/Services/BaseWorkItemService.cs index 5dc7670..a96f1e3 100644 --- a/Migrators/Importer/Services/BaseWorkItemService.cs +++ b/Migrators/Importer/Services/BaseWorkItemService.cs @@ -59,6 +59,84 @@ private static object ConvertAttributeValue(TmsAttribute tmsAttribute, CaseAttri return caseAttribute.Value.ToString(); } + /// + /// Searches for a specific value within an HTML string (input). + /// If the value is found inside an HTML tag, the function moves this value to a position + /// after the closing tag of the current HTML element. + /// + private static string UpImageLinkIfNeeded(string source, string matchValue) + { + try + { + // Find the position of the value in the string + int valueIndex = source.IndexOf(matchValue, StringComparison.Ordinal); + if (valueIndex == -1) + { + return source; // If value is not found, return input unchanged + } + + // Find the last opening tag before the value + int openingTagStart = source.LastIndexOf('<', valueIndex); + int openingTagEnd = source.IndexOf('>', openingTagStart); + if (openingTagStart == -1 || openingTagEnd == -1) + { + return source; // If the structure is invalid, return input unchanged + } + + // Extract the tag name + string tagName = source.Substring(openingTagStart + 1, + openingTagEnd - openingTagStart - 1).Split(' ')[0]; + + // Find the corresponding closing tag + string closingTag = $""; + int closingTagIndex = source.IndexOf(closingTag, valueIndex, StringComparison.Ordinal); + if (closingTagIndex == -1) + { + return source; // If no closing tag found, return input unchanged + } + + int closingTagLength = closingTag.Length; + + // Remove the value from its current position + source = source.Remove(valueIndex, matchValue.Length); + + // Recalculate the position of the closing tag after the removal + closingTagIndex = source.IndexOf(closingTag, openingTagStart, + StringComparison.Ordinal) + closingTagLength; + + // Insert the value after the identified closing tag + source = source.Insert(closingTagIndex, matchValue); + + return source; + } + catch (Exception) + { + return source; + } + } + + private static string HandleStepImageLink(string source, string a, Dictionary attachments) + { + if (source.Contains($"<<<{a}>>>")) + { + source = source.Replace($"<<<{a}>>>", $"%%%{a}%%%"); + source = UpImageLinkIfNeeded(source, $"%%%{a}%%%"); + source = source.Replace($"%%%{a}%%%", $"

"); + } + else + { + if (IsImage(a)) + { + source += $"

"; + } + else + { + source += $"

File attached to test case: {a}

"; + } + } + return source; + } + protected static List AddAttachmentsToSteps(List steps, Dictionary attachments) { steps.ToList().ForEach( @@ -66,59 +144,17 @@ protected static List AddAttachmentsToSteps(List steps, Dictionary { - if (s.Action.Contains($"<<<{a}>>>")) - { - s.Action = s.Action.Replace($"<<<{a}>>>", $"

"); - } - else - { - if (IsImage(a)) - { - s.Action += $"

"; - } - else - { - s.Action += $"

File attached to test case: {a}

"; - } - } + s.Action = HandleStepImageLink(s.Action, a, attachments); }); s.ExpectedAttachments?.ForEach(a => { - if (s.Expected.Contains($"<<<{a}>>>")) - { - s.Expected = s.Expected.Replace($"<<<{a}>>>", $"

"); - } - else - { - if (IsImage(a)) - { - s.Expected += $"

"; - } - else - { - s.Expected += $"

File attached to test case: {a}

"; - } - } + s.Expected = HandleStepImageLink(s.Expected, a, attachments); }); s.TestDataAttachments?.ForEach(a => { - if (s.TestData.Contains($"<<<{a}>>>")) - { - s.TestData = s.TestData.Replace($"<<<{a}>>>", $"

"); - } - else - { - if (IsImage(a)) - { - s.TestData += $"

"; - } - else - { - s.TestData += $"

File attached to test case: {a}

"; - } - } + s.TestData = HandleStepImageLink(s.TestData, a, attachments); }); }); diff --git a/Migrators/Importer/Services/TestCaseService.cs b/Migrators/Importer/Services/TestCaseService.cs index 9a14bf4..a19cc30 100644 --- a/Migrators/Importer/Services/TestCaseService.cs +++ b/Migrators/Importer/Services/TestCaseService.cs @@ -97,6 +97,8 @@ private async Task ImportTestCase(Guid projectId, TestCase testCase) tmsTestCase.Attachments = attachments.Select(a => a.Value.ToString()).ToList(); tmsTestCase.Steps = AddAttachmentsToSteps(tmsTestCase.Steps, attachments); + tmsTestCase.PreconditionSteps = AddAttachmentsToSteps(tmsTestCase.PreconditionSteps, attachments); + tmsTestCase.PostconditionSteps = AddAttachmentsToSteps(tmsTestCase.PostconditionSteps, attachments); await _client.ImportTestCase(projectId, sectionId, tmsTestCase);