~abompard/mailman/mailman-templates-utf8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright (C) 2011-2015 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Testing i18n template search and interpolation."""

__all__ = [
    'TestFind',
    'TestMake',
    'TestSearchOrder',
    ]


import os
import locale
import shutil
import tempfile
import unittest

from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.interfaces.languages import ILanguageManager
from mailman.testing.layers import ConfigLayer
from mailman.utilities.i18n import TemplateNotFoundError, find, make, search
from pkg_resources import resource_filename
from zope.component import getUtility



class TestSearchOrder(unittest.TestCase):
    """Test internal search order for language templates."""

    layer = ConfigLayer

    def setUp(self):
        self.var_dir = tempfile.mkdtemp()
        config.push('no template dir', """\
        [mailman]
        default_language: fr
        [paths.testing]
        var_dir: {0}
        """.format(self.var_dir))
        language_manager = getUtility(ILanguageManager)
        language_manager.add('de', 'utf-8', 'German')
        language_manager.add('it', 'utf-8', 'Italian')
        self.mlist = create_list('l@example.com')
        self.mlist.preferred_language = 'de'

    def tearDown(self):
        config.pop('no template dir')
        shutil.rmtree(self.var_dir)

    def _stripped_search_order(self, template_file,
                               mailing_list=None, language=None):
        # Return the search path order for a given template, possibly using
        # the mailing list and the language as context.  Note that this only
        # returns the search path, and does not check for whether the paths
        # exist or not.
        #
        # Replace the tempdir prefix with a placeholder for more readable and
        # reproducible tests.  Essentially the paths below are rooted at
        # $var_dir, except those files that live within Mailman's source
        # tree.  The former will use /v/ as the root and the latter will use
        # /m/ as the root.
        in_tree = os.path.dirname(resource_filename('mailman', 'templates'))
        raw_search_order = search(template_file, mailing_list, language)
        for path in raw_search_order:
            if path.startswith(self.var_dir):
                path = '/v' + path[len(self.var_dir):]
            elif path.startswith(in_tree):
                path = '/m' + path[len(in_tree):]
            else:
                # This will cause tests to fail, so keep the full bogus
                # pathname for better debugging.
                pass
            yield path

    def test_fully_specified_search_order(self):
        search_order = self._stripped_search_order('foo.txt', self.mlist, 'it')
        # For convenience.
        def nexteq(path):
            self.assertEqual(next(search_order), path)
        # 1: Use the given language argument
        nexteq('/v/templates/lists/l@example.com/it/foo.txt')
        nexteq('/v/templates/domains/example.com/it/foo.txt')
        nexteq('/v/templates/site/it/foo.txt')
        # 2: Use mlist.preferred_language
        nexteq('/v/templates/lists/l@example.com/de/foo.txt')
        nexteq('/v/templates/domains/example.com/de/foo.txt')
        nexteq('/v/templates/site/de/foo.txt')
        # 3: Use the site's default language
        nexteq('/v/templates/lists/l@example.com/fr/foo.txt')
        nexteq('/v/templates/domains/example.com/fr/foo.txt')
        nexteq('/v/templates/site/fr/foo.txt')
        # 4: English
        nexteq('/v/templates/lists/l@example.com/en/foo.txt')
        nexteq('/v/templates/domains/example.com/en/foo.txt')
        nexteq('/v/templates/site/en/foo.txt')
        # 5: After all the site-admin override paths have been searched, the
        # Mailman in-tree paths are searched.  Note that Mailman only ships
        # one set of English templates.
        nexteq('/m/templates/en/foo.txt')

    def test_no_language_argument_search_order(self):
        search_order = self._stripped_search_order('foo.txt', self.mlist)
        # For convenience.
        def nexteq(path):
            self.assertEqual(next(search_order), path)
        # 1: Use mlist.preferred_language
        nexteq('/v/templates/lists/l@example.com/de/foo.txt')
        nexteq('/v/templates/domains/example.com/de/foo.txt')
        nexteq('/v/templates/site/de/foo.txt')
        # 2: Use the site's default language
        nexteq('/v/templates/lists/l@example.com/fr/foo.txt')
        nexteq('/v/templates/domains/example.com/fr/foo.txt')
        nexteq('/v/templates/site/fr/foo.txt')
        # 3: English
        nexteq('/v/templates/lists/l@example.com/en/foo.txt')
        nexteq('/v/templates/domains/example.com/en/foo.txt')
        nexteq('/v/templates/site/en/foo.txt')
        # 4: After all the site-admin override paths have been searched, the
        # Mailman in-tree paths are searched.  Note that Mailman only ships
        # one set of English templates.
        nexteq('/m/templates/en/foo.txt')

    def test_no_mailing_list_argument_search_order(self):
        search_order = self._stripped_search_order('foo.txt', language='it')
        # For convenience.
        def nexteq(path):
            self.assertEqual(next(search_order), path)
        # 1: Use the given language argument
        nexteq('/v/templates/site/it/foo.txt')
        # 2: Use the site's default language
        nexteq('/v/templates/site/fr/foo.txt')
        # 3: English
        nexteq('/v/templates/site/en/foo.txt')
        # 4: After all the site-admin override paths have been searched, the
        # Mailman in-tree paths are searched.  Note that Mailman only ships
        # one set of English templates.
        nexteq('/m/templates/en/foo.txt')

    def test_no_optional_arguments_search_order(self):
        search_order = self._stripped_search_order('foo.txt')
        # For convenience.
        def nexteq(path):
            self.assertEqual(next(search_order), path)
        # 1: Use the site's default language
        nexteq('/v/templates/site/fr/foo.txt')
        # 2: English
        nexteq('/v/templates/site/en/foo.txt')
        # 3: After all the site-admin override paths have been searched, the
        # Mailman in-tree paths are searched.  Note that Mailman only ships
        # one set of English templates.
        nexteq('/m/templates/en/foo.txt')



class TestFind(unittest.TestCase):
    """Test template search."""

    layer = ConfigLayer

    def setUp(self):
        self.var_dir = tempfile.mkdtemp()
        config.push('template config', """\
        [paths.testing]
        var_dir: {0}
        """.format(self.var_dir))
        # The following MUST happen AFTER the push() above since pushing a new
        # config also clears out the language manager.
        getUtility(ILanguageManager).add('xx', 'utf-8', 'Xlandia')
        self.mlist = create_list('test@example.com')
        self.mlist.preferred_language = 'xx'
        self.fp = None
        # Populate the template directories with a few fake templates.
        def write(text, path):
            os.makedirs(os.path.dirname(path))
            with open(path, 'w') as fp:
                fp.write(text)
        self.xxsite = os.path.join(
            self.var_dir, 'templates', 'site', 'xx', 'site.txt')
        write('Site template', self.xxsite)
        self.xxdomain = os.path.join(
              self.var_dir, 'templates',
              'domains', 'example.com', 'xx', 'domain.txt')
        write('Domain template', self.xxdomain)
        self.xxlist = os.path.join(
              self.var_dir, 'templates',
              'lists', 'test@example.com', 'xx', 'list.txt')
        write('List template', self.xxlist)

    def tearDown(self):
        if self.fp is not None:
            self.fp.close()
        config.pop('template config')
        shutil.rmtree(self.var_dir)

    def test_find_site_template(self):
        filename, self.fp = find('site.txt', language='xx')
        self.assertEqual(filename, self.xxsite)
        self.assertEqual(self.fp.read(), 'Site template')

    def test_find_domain_template(self):
        filename, self.fp = find('domain.txt', self.mlist)
        self.assertEqual(filename, self.xxdomain)
        self.assertEqual(self.fp.read(), 'Domain template')

    def test_find_list_template(self):
        filename, self.fp = find('list.txt', self.mlist)
        self.assertEqual(filename, self.xxlist)
        self.assertEqual(self.fp.read(), 'List template')

    def test_template_not_found(self):
        with self.assertRaises(TemplateNotFoundError) as cm:
            find('missing.txt', self.mlist)
        self.assertEqual(cm.exception.template_file, 'missing.txt')

    def test_encoding(self):
        with open(self.xxsite, 'w', encoding="utf-8") as fp:
            fp.write('Ol\ufffd!')
        # Settings LC_ALL to 'C' will clear locale.getpreferredencoding() from
        # references to UTF-8 that it would have caught up reading the
        # environment.
        locale.setlocale(locale.LC_ALL, 'C')
        filename, self.fp = find('site.txt', language='xx')
        try:
            content = self.fp.read()
        except UnicodeDecodeError:
            self.fail("Templates should be considered UTF-8 by default")
        self.assertEqual(content, 'Ol\ufffd!')



class TestMake(unittest.TestCase):
    """Test template interpolation."""

    layer = ConfigLayer

    def setUp(self):
        self.var_dir = tempfile.mkdtemp()
        config.push('template config', """\
        [paths.testing]
        var_dir: {0}
        """.format(self.var_dir))
        # The following MUST happen AFTER the push() above since pushing a new
        # config also clears out the language manager.
        getUtility(ILanguageManager).add('xx', 'utf-8', 'Xlandia')
        self.mlist = create_list('test@example.com')
        self.mlist.preferred_language = 'xx'
        # Populate the template directories with a few fake templates.
        path = os.path.join(self.var_dir, 'templates', 'site', 'xx')
        os.makedirs(path)
        with open(os.path.join(path, 'nosub.txt'), 'w') as fp:
            print("""\
This is a global template.
It has no substitutions.
It will be wrapped.
""", file=fp)
        with open(os.path.join(path, 'subs.txt'), 'w') as fp:
            print("""\
This is a $kind template.
It has $howmany substitutions.
It will be wrapped.
""", file=fp)
        with open(os.path.join(path, 'nowrap.txt'), 'w') as fp:
            print("""\
This is a $kind template.
It has $howmany substitutions.
It will not be wrapped.
""", file=fp)

    def tearDown(self):
        config.pop('template config')
        shutil.rmtree(self.var_dir)

    def test_no_substitutions(self):
        self.assertEqual(make('nosub.txt', self.mlist), """\
This is a global template.  It has no substitutions.  It will be
wrapped.""")

    def test_substitutions(self):
        self.assertEqual(make('subs.txt', self.mlist,
                              kind='very nice',
                              howmany='a few'), """\
This is a very nice template.  It has a few substitutions.  It will be
wrapped.""")

    def test_substitutions_no_wrap(self):
        self.assertEqual(make('nowrap.txt', self.mlist, wrap=False,
                              kind='very nice',
                              howmany='a few'), """\
This is a very nice template.
It has a few substitutions.
It will not be wrapped.
""")