From 6ca443a2556e5d4bade9a9ef7d6d877bf1d6fc45 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 5 Sep 2017 17:39:45 -0400 Subject: PLT-7522 Cleaned up translation of templates (#7351) * PLT-7522 Cleaned up translation of templates * Added unit tests * Changed TranslateAsHtml to not be variadic --- utils/html_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 utils/html_test.go (limited to 'utils/html_test.go') diff --git a/utils/html_test.go b/utils/html_test.go new file mode 100644 index 000000000..8dc70242a --- /dev/null +++ b/utils/html_test.go @@ -0,0 +1,53 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "html/template" + "testing" +) + +func TestTranslateAsHtml(t *testing.T) { + TranslationsPreInit() + + translateFunc := TfuncWithFallback("en") + + expected := "To finish updating your email address for YOUR TEAM HERE, please click the link below to confirm this is the right address." + if actual := TranslateAsHtml(translateFunc, "api.templates.email_change_verify_body.info", + map[string]interface{}{"TeamDisplayName": "YOUR TEAM HERE"}); actual != template.HTML(expected) { + t.Fatalf("Incorrectly translated template, got %v, expected %v", actual, expected) + } + + expected = "To finish updating your email address for <b>YOUR TEAM HERE</b>, please click the link below to confirm this is the right address." + if actual := TranslateAsHtml(translateFunc, "api.templates.email_change_verify_body.info", + map[string]interface{}{"TeamDisplayName": "YOUR TEAM HERE"}); actual != template.HTML(expected) { + t.Fatalf("Incorrectly translated template, got %v, expected %v", actual, expected) + } +} + +func TestEscapeForHtml(t *testing.T) { + input := "abc" + expected := "abc" + if actual := escapeForHtml(input).(string); actual != expected { + t.Fatalf("incorrectly escaped %v, got %v expected %v", input, actual, expected) + } + + input = "abc" + expected = "<b>abc</b>" + if actual := escapeForHtml(input).(string); actual != expected { + t.Fatalf("incorrectly escaped %v, got %v expected %v", input, actual, expected) + } + + inputMap := map[string]interface{}{ + "abc": "abc", + "123": "123", + } + expectedMap := map[string]interface{}{ + "abc": "abc", + "123": "<b>123</b>", + } + if actualMap := escapeForHtml(inputMap).(map[string]interface{}); actualMap["abc"] != expectedMap["abc"] || actualMap["123"] != expectedMap["123"] { + t.Fatalf("incorrectly escaped %v, got %v expected %v", inputMap, actualMap, expectedMap) + } +} -- cgit v1.2.3-1-g7c22