##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

import unittest


class TestPropertySheet(unittest.TestCase):

    def _makeOne(self, *args, **kw):
        from OFS.PropertySheets import PropertySheet
        return PropertySheet(*args, **kw)

    def test_dav__propstat_nullns(self):
        # Tests 15 (propnullns) and 16 (propget) from the props suite
        # of litmus version 10.5 (http://www.webdav.org/neon/litmus/)
        # expose a bug in Zope propertysheet access via DAV.  If a
        # proppatch command sets a property with a null xmlns,
        # e.g. with a PROPPATCH body like:
        #
        # <?xml version="1.0" encoding="utf-8" ?>
        # <propertyupdate xmlns="DAV:">
        # <set>
        # <prop>
        # <nonamespace xmlns="">randomvalue</nonamespace>
        # </prop>
        # </set>
        # </propertyupdate>
        #
        # When we set properties in the null namespace, Zope turns
        # around and creates (or finds) a propertysheet with the
        # xml_namespace of None and sets the value on it.  The
        # response to a subsequent PROPFIND for the resource will fail
        # because the XML generated by dav__propstat included a bogus
        # namespace declaration (xmlns="None").
        #
        inst = self._makeOne('foo')

        inst._md = {'xmlns': None}
        resultd = {}
        inst._setProperty('foo', 'bar')
        inst.dav__propstat('foo', resultd)
        self.assertEqual(len(resultd['200 OK']), 1)
        self.assertEqual(resultd['200 OK'][0], '<foo xmlns="">bar</foo>\n')

    def test_dav__propstat_notnullns(self):
        # see test_dav__propstat_nullns
        inst = self._makeOne('foo')

        inst._md = {'xmlns': 'http://www.example.com/props'}
        resultd = {}
        inst._setProperty('foo', 'bar')
        inst.dav__propstat('foo', resultd)
        self.assertEqual(len(resultd['200 OK']), 1)
        self.assertEqual(resultd['200 OK'][0],
                         '<n:foo xmlns:n="http://www.example.com/props">bar'
                         '</n:foo>\n')
