<commit_before># Copyright (c) 2011 gocept gmbh & co. kg
# See also LICENSE.txt

import mock
import unittest2 as unittest
import zeit.cms.testing
import zope.component
import zope.security.management
import zope.testbrowser.testing


class TestTags(unittest.TestCase,
               zeit.cms.tagging.testing.TaggingHelper):

    def get_content(self):
        from zeit.cms.tagging.tag import Tags

        class Content(object):
            tags = Tags()
        return Content()

    def test_get_without_tagger_should_be_empty(self):
        self.assertEqual((), self.get_content().tags)

    def test_set_should_raise_without_tagger(self):
        def set():
            self.get_content().tags = ()
        self.assertRaises(TypeError, lambda: set())

    def test_get_should_return_tagger_values(self):
        t1 = mock.Mock()
        t2 = mock.Mock()
        t1.disabled = False
        t2.disabled = False
        with mock.patch('zeit.cms.tagging.interfaces.ITagger') as tagger:
            tagger().values.return_value = [t1, t2]
            result = self.get_content().tags
        self.assertEqual((t1, t2), result)
        tagger().values.assert_called_with()

    def test_set_should_add_new_values_to_tagger(self):
        tags = self.setup_tags('t1', 't2')
        t1 = tags['t1']
        t2 = tags['t2']
        del tags['t2']
        self.get_content().tags = [t1, t2]
        result = self.get_content().tags
        self.assertEqual(['t1', 't2'], [x.code for x in result])

    def test_set_should_remove_remaining_values_from_tagger(self):
        tags = self.setup_tags('t1', 't2')
        self.get_content().tags = [tags['t1']]
        result = self.get_content().tags
        self.assertEqual(['t1'], [x.code for x in result])


class TestCMSContentWiring(zeit.cms.testing.FunctionalTestCase):

    # This test checks that the Tag object and its views etc are wired up
    # properly so that they can be addressed as ICMSContent and traversed to.
    # We need these things so we can use the ObjectSequenceWidget to edit tags.

    def setUp(self):
        super(TestCMSContentWiring, self).setUp()
        zope.security.management.endInteraction()
        self.browser = zope.testbrowser.testing.Browser()
        self.browser.addHeader('Authorization', 'Basic user:userpw')

    def test_object_details(self):
        from zeit.cms.tagging.tag import Tag

        whitelist = zope.component.getUtility(
            zeit.cms.tagging.interfaces.IWhitelist)
        whitelist['foo'] = Tag('foo', 'foo')

        base = 'http://localhost/++skin++vivi/'
        b = self.browser
        b.handleErrors = False
        b.open(
            base + '@@redirect_to?unique_id=tag://foo&view=@@object-details')
        self.assertEqual('<h3>foo</h3>', b.contents)<commit_msg>User proper testcase<commit_after># Copyright (c) 2011 gocept gmbh & co. kg
# See also LICENSE.txt

import mock
import unittest2 as unittest
import zeit.cms.testing
import zope.component
import zope.security.management
import zope.testbrowser.testing


class TestTags(unittest.TestCase,
               zeit.cms.tagging.testing.TaggingHelper):

    def get_content(self):
        from zeit.cms.tagging.tag import Tags

        class Content(object):
            tags = Tags()
        return Content()

    def test_get_without_tagger_should_be_empty(self):
        self.assertEqual((), self.get_content().tags)

    def test_set_should_raise_without_tagger(self):
        def set():
            self.get_content().tags = ()
        self.assertRaises(TypeError, lambda: set())

    def test_get_should_return_tagger_values(self):
        t1 = mock.Mock()
        t2 = mock.Mock()
        t1.disabled = False
        t2.disabled = False
        with mock.patch('zeit.cms.tagging.interfaces.ITagger') as tagger:
            tagger().values.return_value = [t1, t2]
            result = self.get_content().tags
        self.assertEqual((t1, t2), result)
        tagger().values.assert_called_with()

    def test_set_should_add_new_values_to_tagger(self):
        tags = self.setup_tags('t1', 't2')
        t1 = tags['t1']
        t2 = tags['t2']
        del tags['t2']
        self.get_content().tags = [t1, t2]
        result = self.get_content().tags
        self.assertEqual(['t1', 't2'], [x.code for x in result])

    def test_set_should_remove_remaining_values_from_tagger(self):
        tags = self.setup_tags('t1', 't2')
        self.get_content().tags = [tags['t1']]
        result = self.get_content().tags
        self.assertEqual(['t1'], [x.code for x in result])



class TestCMSContentWiring(zeit.cms.testing.BrowserTestCase):

    # This test checks that the Tag object and its views etc are wired up
    # properly so that they can be addressed as ICMSContent and traversed to.
    # We need these things so we can use the ObjectSequenceWidget to edit tags.

    def test_object_details(self):
        from zeit.cms.tagging.tag import Tag

        whitelist = zope.component.getUtility(
            zeit.cms.tagging.interfaces.IWhitelist)
        whitelist['foo'] = Tag('foo', 'foo')

        base = 'http://localhost/++skin++vivi/'
        b = self.browser
        b.open(
            base + '@@redirect_to?unique_id=tag://foo&view=@@object-details')
        self.assertEqual('<h3>foo</h3>', b.contents)
{'fraction_non_alphanumeric': 0.11856738925541942, 'fraction_numerical': 0.011310084825636193, 'mean_word_length': 2.7471751412429377, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5093331', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1638, 'n_words': 363}