~launchpad-pqm/launchpad/devel

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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=C0102

__metaclass__ = type

from storm.exceptions import DataError
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy

from lp.testing.layers import ZopelessDatabaseLayer
from lp.app.enums import ServiceUsage
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.testing import TestCaseWithFactory
from lp.translations.interfaces.potemplate import IPOTemplateSet


class TestTranslationSharingPOTemplate(TestCaseWithFactory):
    """Test behaviour of "sharing" PO templates."""

    layer = ZopelessDatabaseLayer

    def setUp(self):
        """Set up context to test in."""
        # Create a product with two series and sharing POTemplates
        # in different series ('devel' and 'stable').
        super(TestTranslationSharingPOTemplate, self).setUp()
        self.foo = self.factory.makeProduct(
            translations_usage=ServiceUsage.LAUNCHPAD)
        self.foo_devel = self.factory.makeProductSeries(
            name='devel', product=self.foo)
        self.foo_stable = self.factory.makeProductSeries(
            name='stable', product=self.foo)

        # POTemplate is a 'sharing' one if it has the same name ('messages').
        self.devel_potemplate = self.factory.makePOTemplate(
            productseries=self.foo_devel, name="messages")
        self.stable_potemplate = self.factory.makePOTemplate(
            self.foo_stable, name="messages")

        # Create a single POTMsgSet that is used across all tests,
        # and add it to only one of the POTemplates.
        self.potmsgset = self.factory.makePOTMsgSet(self.devel_potemplate)

    def test_getPOTMsgSets(self):
        self.potmsgset.setSequence(self.stable_potemplate, 1)

        devel_potmsgsets = list(self.devel_potemplate.getPOTMsgSets())
        stable_potmsgsets = list(self.stable_potemplate.getPOTMsgSets())

        self.assertEquals(devel_potmsgsets, [self.potmsgset])
        self.assertEquals(devel_potmsgsets, stable_potmsgsets)

    def test_getPOTMsgSetByMsgIDText(self):
        potmsgset = self.factory.makePOTMsgSet(self.devel_potemplate,
                                               singular="Open file",
                                               sequence=2)

        # We can retrieve the potmsgset by its ID text.
        read_potmsgset = self.devel_potemplate.getPOTMsgSetByMsgIDText(
            "Open file")
        self.assertEquals(potmsgset, read_potmsgset)

    def test_getPOTMsgSetBySequence(self):
        sequence = 2
        potmsgset = self.factory.makePOTMsgSet(self.devel_potemplate,
                                               sequence=sequence)

        # We can retrieve the potmsgset by its sequence.
        read_potmsgset = self.devel_potemplate.getPOTMsgSetBySequence(
            sequence)
        self.assertEquals(potmsgset, read_potmsgset)

        # It's still not present in different sharing PO template.
        read_potmsgset = self.stable_potemplate.getPOTMsgSetBySequence(
            sequence)
        self.assertEquals(read_potmsgset, None)

    def test_getPOTMsgSetByID(self):
        potmsgset = self.factory.makePOTMsgSet(self.devel_potemplate,
                                               sequence=3)
        id = potmsgset.id

        # We can retrieve the potmsgset by its ID.
        read_potmsgset = self.devel_potemplate.getPOTMsgSetByID(id)
        self.assertEquals(potmsgset, read_potmsgset)

        # Getting this one in a different template doesn't work.
        read_potmsgset = self.stable_potemplate.getPOTMsgSetByID(id)
        self.assertEquals(read_potmsgset, None)

        # Nor can you get an entry with a made up ID.
        random_id = 100000 + self.factory.getUniqueInteger()
        read_potmsgset = self.devel_potemplate.getPOTMsgSetByID(random_id)
        self.assertEquals(read_potmsgset, None)

    def test_hasMessageID(self):
        naked_potemplate = removeSecurityProxy(self.devel_potemplate)
        # Let's get details we need for a POTMsgSet that is
        # already in the POTemplate.
        present_msgid_singular = self.potmsgset.msgid_singular
        present_msgid_plural = self.potmsgset.msgid_plural
        present_context = self.potmsgset.context
        has_message_id = naked_potemplate.hasMessageID(
            present_msgid_singular, present_msgid_plural, present_context)
        self.assertEquals(has_message_id, True)

    def test_hasPluralMessage(self):
        # At the moment, a POTemplate has no plural form messages.
        self.assertEquals(self.devel_potemplate.hasPluralMessage(), False)

        # Let's add a POTMsgSet with plural forms.
        self.factory.makePOTMsgSet(self.devel_potemplate,
                                   singular="singular",
                                   plural="plural",
                                   sequence=4)

        # Now, template contains a plural form message.
        self.assertEquals(self.devel_potemplate.hasPluralMessage(), True)

    def test_expireAllMessages(self):
        devel_potmsgsets = list(self.devel_potemplate.getPOTMsgSets())
        self.assertEquals(len(devel_potmsgsets) > 0, True)

        # Expiring all messages brings the count back to zero.
        self.devel_potemplate.expireAllMessages()
        devel_potmsgsets = list(self.devel_potemplate.getPOTMsgSets())
        self.assertEquals(len(devel_potmsgsets), 0)

        # Expiring all messages even when all are already expired still works.
        self.devel_potemplate.expireAllMessages()
        devel_potmsgsets = list(self.devel_potemplate.getPOTMsgSets())
        self.assertEquals(len(devel_potmsgsets), 0)

    def test_createPOTMsgSetFromMsgIDs(self):
        # We need a 'naked' potemplate to make use of getOrCreatePOMsgID
        # private method.
        naked_potemplate = removeSecurityProxy(self.devel_potemplate)

        # Let's create a new POTMsgSet.
        singular_text = self.factory.getUniqueString()
        msgid_singular = naked_potemplate.getOrCreatePOMsgID(singular_text)
        potmsgset = self.devel_potemplate.createPOTMsgSetFromMsgIDs(
            msgid_singular=msgid_singular)
        self.assertEquals(potmsgset.msgid_singular, msgid_singular)

        # And let's add it to the devel_potemplate.
        potmsgset.setSequence(self.devel_potemplate, 5)
        devel_potmsgsets = list(self.devel_potemplate.getPOTMsgSets())
        self.assertEquals(len(devel_potmsgsets), 2)

        # Creating it with a different context also works.
        msgid_context = self.factory.getUniqueString()
        potmsgset_context = self.devel_potemplate.createPOTMsgSetFromMsgIDs(
            msgid_singular=msgid_singular, context=msgid_context)
        self.assertEquals(potmsgset_context.msgid_singular, msgid_singular)
        self.assertEquals(potmsgset_context.context, msgid_context)

    def test_getOrCreateSharedPOTMsgSet(self):
        # Let's create a new POTMsgSet.
        singular_text = self.factory.getUniqueString()
        potmsgset = self.devel_potemplate.getOrCreateSharedPOTMsgSet(
            singular_text, None)

        # If we try to add a POTMsgSet with identical strings,
        # we get back the existing one.
        same_potmsgset = self.devel_potemplate.getOrCreateSharedPOTMsgSet(
            singular_text, None)
        self.assertEquals(potmsgset, same_potmsgset)

        # And even if we do it in the shared template, existing
        # POTMsgSet is returned.
        shared_potmsgset = self.stable_potemplate.getOrCreateSharedPOTMsgSet(
            singular_text, None)
        self.assertEquals(potmsgset, shared_potmsgset)

    def test_getOrCreateSharedPOTMsgSet_initializes_file_references(self):
        # When creating a POTMsgSet, getOrCreateSharedPOTMsgSet
        # initializes its filereferences to initial_file_references.
        singular = self.factory.getUniqueString()
        file_references = self.factory.getUniqueString()
        potmsgset = self.devel_potemplate.getOrCreateSharedPOTMsgSet(
            singular, None, initial_file_references=file_references)
        self.assertEqual(file_references, potmsgset.filereferences)

    def test_getOrCreateSharedPOTMsgSet_leaves_file_references_intact(self):
        # In returning an existing POTMsgSet, getOrCreateSharedPOTMsgSet
        # leaves its existing filereferences unchanged.  The
        # initial_file_references argument is ignored.
        potmsgset = self.factory.makePOTMsgSet(
            potemplate=self.devel_potemplate)
        potmsgset.filereferences = None
        new_file_references = self.factory.getUniqueString()
        updated_potmsgset = self.devel_potemplate.getOrCreateSharedPOTMsgSet(
            potmsgset.singular_text, potmsgset.plural_text,
            initial_file_references=new_file_references)
        self.assertEqual(potmsgset, updated_potmsgset)
        self.assertIs(None, potmsgset.filereferences)

    def test_getOrCreateSharedPOTMsgSet_initializes_source_comment(self):
        # When creating a POTMsgSet, getOrCreateSharedPOTMsgSet
        # initializes its sourcecomment to initial_source_comment.
        singular = self.factory.getUniqueString()
        source_comment = self.factory.getUniqueString()
        potmsgset = self.devel_potemplate.getOrCreateSharedPOTMsgSet(
            singular, None, initial_source_comment=source_comment)
        self.assertEqual(source_comment, potmsgset.sourcecomment)

    def test_getOrCreateSharedPOTMsgSet_leaves_source_comment_intact(self):
        # In returning an existing POTMsgSet, getOrCreateSharedPOTMsgSet
        # leaves its existing sourcecomment unchanged.  The
        # initial_source_comment argument is ignored.
        potmsgset = self.factory.makePOTMsgSet(
            potemplate=self.devel_potemplate)
        potmsgset.sourcecomment = None
        new_source_comment = self.factory.getUniqueString()
        updated_potmsgset = self.devel_potemplate.getOrCreateSharedPOTMsgSet(
            potmsgset.singular_text, potmsgset.plural_text,
            initial_source_comment=new_source_comment)
        self.assertEqual(potmsgset, updated_potmsgset)
        self.assertIs(None, potmsgset.sourcecomment)


class TestSharingPOTemplatesByRegex(TestCaseWithFactory):
    """Isolate tests for regular expression use in SharingSubset."""

    layer = ZopelessDatabaseLayer

    def setUp(self):
        super(TestSharingPOTemplatesByRegex, self).setUp()

    def _makeAndFind(self, names, name_pattern=None):
        product = self.factory.makeProduct()
        trunk = product.getSeries('trunk')
        for name in names:
            self.factory.makePOTemplate(productseries=trunk, name=name)
        subset = getUtility(IPOTemplateSet).getSharingSubset(product=product)
        return [
            template.name
            for template in subset.getSharingPOTemplatesByRegex(name_pattern)]

    def test_getSharingPOTemplatesByRegex_baseline(self):
        # Baseline test.
        self.assertContentEqual(
            ['foo', 'foo-bar', 'foo-two'],
            self._makeAndFind(['foo', 'foo-bar', 'foo-two'], 'foo.*'))

    def test_getSharingPOTemplatesByRegex_not_all(self):
        # A template may not match.
        self.assertContentEqual(
            ['foo-bar', 'foo-two'],
            self._makeAndFind(['foo', 'foo-bar', 'foo-two'], 'foo-.*'))

    def test_getSharingPOTemplatesByRegex_all(self):
        # Not passing a pattern returns all templates.
        self.assertContentEqual(
            ['foo', 'foo-bar', 'foo-two'],
            self._makeAndFind(['foo', 'foo-bar', 'foo-two']))

    def test_getSharingPOTemplatesByRegex_no_match(self):
        # A not matching pattern returns no templates.
        self.assertContentEqual(
            [],
            self._makeAndFind(['foo', 'foo-bar', 'foo-two'], "doo.+dle"))

    def test_getSharingPOTemplatesByRegex_robustness_single_quotes(self):
        # Single quotes do not confuse the regex match.
        self.assertContentEqual(
            [],
            self._makeAndFind(['foo', 'foo-bar', 'foo-two'], "'"))

    def test_getSharingPOTemplatesByRegex_robustness_double_quotes(self):
        # Double quotes do not confuse the regex match.
        self.assertContentEqual(
            [],
            self._makeAndFind(['foo', 'foo-bar', 'foo-two'], '"'))

    def test_getSharingPOTemplatesByRegex_robustness_backslash(self):
        # A backslash at the end could escape enclosing quotes without
        # proper escaping, leading to a SyntaxError or even a successful
        # exploit. Instead, storm should complain about an invalid expression
        # by raising DataError.
        product = self.factory.makeProduct()
        subset = getUtility(IPOTemplateSet).getSharingSubset(product=product)
        self.assertRaises(
            DataError, list, subset.getSharingPOTemplatesByRegex("foo.*\\"))


class TestMessageSharingProductPackage(TestCaseWithFactory):
    """Test message sharing between a product and a package.

    Each test uses assertStatementCount to make sure the number of SQL
    queries does not change. This was integrated here to avoid having
    a second test case just for statement counts.
    The current implementation is good and only needs one statement.
    """

    layer = ZopelessDatabaseLayer

    def setUp(self):
        super(TestMessageSharingProductPackage, self).setUp()

        self.ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        self.hoary = self.ubuntu['hoary']
        self.warty = self.ubuntu['warty']
        self.ubuntu.translation_focus = self.hoary
        self.packagename = self.factory.makeSourcePackageName()

        self.product = self.factory.makeProduct()
        self.trunk = self.product.getSeries('trunk')
        self.stable = self.factory.makeProductSeries(
            product=self.product)

        self.templatename = self.factory.getUniqueString()
        self.trunk_template = self.factory.makePOTemplate(
            productseries=self.trunk, name=self.templatename)
        self.hoary_template = self.factory.makePOTemplate(
            distroseries=self.hoary, sourcepackagename=self.packagename,
            name=self.templatename)

        self.owner = self.factory.makePerson()
        self.potemplateset = getUtility(IPOTemplateSet)

    def _assertStatements(self, no_of_statements, resultset):
        """Assert constant number of SQL statements when iterating result set.

        This encapsulates using the 'list' function to feed the iterator to
        the assert method. This iterates the resultset, triggering SQL
        statement execution."""
        return self.assertStatementCount(no_of_statements, list, resultset)

    def test_getSharingPOTemplates_product(self):
        # Sharing templates for a product include the same templates from
        # a linked source package.
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename,
            distroseries=self.hoary)
        self.trunk.setPackaging(self.hoary, self.packagename, self.owner)
        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.trunk_template, self.hoary_template], templates)

    def test_getSharingPOTemplates_package(self):
        # Sharing templates for a source package include the same templates
        # from a linked product.
        sourcepackage = self.factory.makeSourcePackage(
            self.packagename, self.hoary)
        sourcepackage.setPackaging(self.trunk, self.owner)
        subset = self.potemplateset.getSharingSubset(
            distribution=self.ubuntu,
            sourcepackagename=self.packagename)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.trunk_template, self.hoary_template], templates)

    def test_getSharingPOTemplates_product_multiple_series(self):
        # Sharing templates for a product include the same templates from
        # a linked source package, even from multiple product series and
        # multiple distro series.
        stable_template = self.factory.makePOTemplate(
            productseries=self.stable, name=self.templatename)
        warty_template = self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=self.packagename,
            name=self.templatename)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename,
            distroseries=self.hoary)
        self.trunk.setPackaging(self.hoary, self.packagename, self.owner)
        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        expected_templates = [
            self.trunk_template,
            self.hoary_template,
            stable_template,
            warty_template,
            ]
        self.assertContentEqual(expected_templates, templates)

    def test_getSharingPOTemplates_package_multiple_series(self):
        # Sharing templates for a source package include the same templates
        # from a linked product, even with multiple product series.
        stable_template = self.factory.makePOTemplate(
            productseries=self.stable, name=self.templatename)
        warty_template = self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=self.packagename,
            name=self.templatename)
        hoary_sourcepackage = self.factory.makeSourcePackage(
            self.packagename, self.hoary)
        hoary_sourcepackage.setPackaging(self.trunk, self.owner)
        warty_sourcepackage = self.factory.makeSourcePackage(
            self.packagename, self.warty)
        warty_sourcepackage.setPackaging(self.stable, self.owner)
        subset = self.potemplateset.getSharingSubset(
            distribution=self.ubuntu,
            sourcepackagename=self.packagename)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        expected_templates = [
            self.trunk_template,
            self.hoary_template,
            stable_template,
            warty_template,
            ]
        self.assertContentEqual(expected_templates, templates)

    def test_getSharingPOTemplates_many_series(self):
        # The number of queries for a call to getSharingPOTemplates must
        # remain constant.

        all_templates = [self.trunk_template, self.hoary_template]
        hoary_sourcepackage = self.factory.makeSourcePackage(
            self.packagename, self.hoary)
        hoary_sourcepackage.setPackaging(self.trunk, self.owner)
        # Add a greater number of series and sharing templates on either side.
        seriesnames = (
            ('0.1', 'feisty'),
            ('0.2', 'gutsy'),
            ('0.3', 'hardy'),
            ('0.4', 'intrepid'),
            ('0.5', 'jaunty'),
            ('0.6', 'karmic'),
            )
        for pseries_name, dseries_name in seriesnames:
            productseries = self.factory.makeProductSeries(
                self.product, pseries_name)
            all_templates.append(self.factory.makePOTemplate(
                productseries=productseries, name=self.templatename))
            distroseries = self.factory.makeDistroSeries(
                self.ubuntu, name=dseries_name)
            all_templates.append(self.factory.makePOTemplate(
                distroseries=distroseries, sourcepackagename=self.packagename,
                name=self.templatename))
            sourcepackage = self.factory.makeSourcePackage(
                self.packagename, distroseries)
            sourcepackage.setPackaging(productseries, self.owner)
        # Don't forget warty and stable.
        all_templates.append(self.factory.makePOTemplate(
            productseries=self.stable, name=self.templatename))
        all_templates.append(self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=self.packagename,
            name=self.templatename))
        warty_sourcepackage = self.factory.makeSourcePackage(
            self.packagename, self.warty)
        warty_sourcepackage.setPackaging(self.stable, self.owner)

        # Looking from the product side.
        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))
        self.assertContentEqual(all_templates, templates)

        # Looking from the sourcepackage side.
        subset = self.potemplateset.getSharingSubset(
            distribution=self.ubuntu,
            sourcepackagename=self.packagename)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))
        self.assertContentEqual(all_templates, templates)

    def test_getSharingPOTemplates_product_unrelated_templates(self):
        # Sharing templates for a product must not include other templates
        # from a linked source package.
        self.factory.makePOTemplate(
            distroseries=self.hoary, sourcepackagename=self.packagename,
            name=self.factory.getUniqueString())
        self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=self.packagename,
            name=self.factory.getUniqueString())
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename,
            distroseries=self.hoary)
        self.trunk.setPackaging(self.hoary, self.packagename, self.owner)
        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.trunk_template, self.hoary_template],
            templates)

    def test_getSharingPOTemplates_product_different_names_and_series(self):
        # A product may be packaged into differently named packages in
        # different distroseries.
        warty_packagename = self.factory.makeSourcePackageName()
        warty_template = self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=warty_packagename,
            name=self.templatename)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename,
            distroseries=self.hoary)
        self.trunk.setPackaging(self.hoary, self.packagename, self.owner)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=warty_packagename,
            distroseries=self.warty)
        self.trunk.setPackaging(self.warty, warty_packagename, self.owner)
        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.trunk_template, self.hoary_template, warty_template],
            templates)

    def test_getSharingPOTemplates_package_different_products(self):
        # A package from different distroseries can be packaging different
        # products. These products may also have templates in different
        # series which are not linked to the package but have the same name.
        warty_template = self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=self.packagename,
            name=self.templatename)
        warty_sourcepackage = self.factory.makeSourcePackage(
                self.packagename, self.warty)
        warty_productseries = self.factory.makeProductSeries()
        warty_productseries_template = self.factory.makePOTemplate(
            productseries=warty_productseries, name=self.templatename)
        warty_sourcepackage.setPackaging(warty_productseries, self.owner)

        other_productseries = self.factory.makeProductSeries(
            product=warty_productseries.product)
        other_productseries_template = self.factory.makePOTemplate(
            productseries=other_productseries, name=self.templatename)

        hoary_sourcepackage = self.factory.makeSourcePackage(
                self.packagename, self.hoary)
        hoary_sourcepackage.setPackaging(self.trunk, self.owner)

        subset = self.potemplateset.getSharingSubset(
                distribution=self.ubuntu, sourcepackagename=self.packagename)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        expected_templates = [
            self.hoary_template,
            self.trunk_template,
            warty_template,
            warty_productseries_template,
            other_productseries_template,
            ]
        self.assertContentEqual(expected_templates, templates)

    def test_getSharingPOTemplates_product_different_packages(self):
        # A product can be packaged in different packages which may also have
        # sharing templates in series that are not linked to this product.
        other_sourcepackagename = self.factory.makeSourcePackageName()
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=other_sourcepackagename,
            distroseries=self.warty)
        self.stable.setPackaging(
            self.warty, other_sourcepackagename, self.owner)
        other_warty_sourcepackage_template = self.factory.makePOTemplate(
            distroseries=self.warty,
            sourcepackagename=other_sourcepackagename,
            name=self.templatename)
        stable_template = self.factory.makePOTemplate(
            productseries=self.stable, name=self.templatename)

        other_distroseries = self.factory.makeDistroSeries(
            distribution=self.ubuntu)
        other_distroseries_template = self.factory.makePOTemplate(
            distroseries=other_distroseries,
            sourcepackagename=other_sourcepackagename,
            name=self.templatename)

        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename, distroseries=self.hoary)
        self.trunk.setPackaging(
            self.hoary, self.packagename, self.owner)

        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        expected_templates = [
            self.hoary_template,
            self.trunk_template,
            other_warty_sourcepackage_template,
            stable_template,
            other_distroseries_template,
            ]
        self.assertContentEqual(expected_templates, templates)

    def test_getSharingPOTemplates_product_different_names_same_series(self):
        # A product may be packaged into differently named packages even in
        # the same distroseries. Must use different product series, though.
        other_packagename = self.factory.makeSourcePackageName()
        other_template = self.factory.makePOTemplate(
            distroseries=self.hoary, sourcepackagename=other_packagename,
            name=self.templatename)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename,
            distroseries=self.hoary)
        self.trunk.setPackaging(self.hoary, self.packagename, self.owner)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=other_packagename,
            distroseries=self.hoary)
        self.stable.setPackaging(self.hoary, other_packagename, self.owner)
        subset = self.potemplateset.getSharingSubset(product=self.product)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.trunk_template, self.hoary_template, other_template],
            templates)

    def test_getSharingPOTemplates_package_unrelated_template(self):
        # Sharing templates for a source package must not include other
        # templates from a linked product.
        self.factory.makePOTemplate(
            productseries=self.trunk, name=self.factory.getUniqueString())
        self.factory.makePOTemplate(
            productseries=self.stable, name=self.factory.getUniqueString())
        sourcepackage = self.factory.makeSourcePackage(
            self.packagename, self.hoary)
        sourcepackage.setPackaging(self.trunk, self.owner)
        subset = self.potemplateset.getSharingSubset(
            distribution=self.ubuntu,
            sourcepackagename=self.packagename)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.trunk_template, self.hoary_template],
            templates)

    def test_getSharingPOTemplates_package_only(self):
        # Sharing templates for a source package only, is done by the
        # sourcepackagename.
        warty_template = self.factory.makePOTemplate(
            distroseries=self.warty, sourcepackagename=self.packagename,
            name=self.templatename)
        other_series = self.factory.makeDistroSeries(self.ubuntu)
        other_template = self.factory.makePOTemplate(
            distroseries=other_series, sourcepackagename=self.packagename,
            name=self.templatename)
        subset = self.potemplateset.getSharingSubset(
            distribution=self.ubuntu,
            sourcepackagename=self.packagename)
        templates = self._assertStatements(
            1, subset.getSharingPOTemplates(self.templatename))

        self.assertContentEqual(
            [self.hoary_template, other_template, warty_template], templates)

    def test_getOrCreateSharedPOTMsgSet_product(self):
        # Trying to create an identical POTMsgSet in a product as exists
        # in a linked sourcepackage will return the existing POTMsgset.
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.packagename,
            distroseries=self.hoary)
        self.trunk.setPackaging(self.hoary, self.packagename, self.owner)
        hoary_potmsgset = self.factory.makePOTMsgSet(
            potemplate=self.hoary_template)

        trunk_potmsgset = self.trunk_template.getOrCreateSharedPOTMsgSet(
                singular_text=hoary_potmsgset.singular_text,
                plural_text=hoary_potmsgset.plural_text)
        self.assertEqual(hoary_potmsgset, trunk_potmsgset)

    def test_getOrCreateSharedPOTMsgSet_package(self):
        # Trying to create an identical POTMsgSet in a product as exists
        # in a linked sourcepackage will return the existing POTMsgset.
        sourcepackage = self.factory.makeSourcePackage(
                self.packagename, self.hoary)
        sourcepackage.setPackaging(self.trunk, self.owner)
        trunk_potmsgset = self.factory.makePOTMsgSet(
            potemplate=self.trunk_template)

        hoary_potmsgset = self.trunk_template.getOrCreateSharedPOTMsgSet(
                singular_text=trunk_potmsgset.singular_text,
                plural_text=trunk_potmsgset.plural_text)
        self.assertEqual(trunk_potmsgset, hoary_potmsgset)