/usr/share/pyshared/zope/app/container/browser/tests/test_contents.py is in python-zope.app.container 3.9.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# 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.
#
##############################################################################
"""Test Container Contents
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.interface import Interface, implements
from zope.security import checker
from zope.traversing.api import traverse
from zope.component.eventtesting import getEvents
from zope.annotation.interfaces import IAnnotations
from zope.copypastemove import ContainerItemRenamer
from zope.copypastemove import ObjectMover, ObjectCopier
from zope.copypastemove import PrincipalClipboard
from zope.copypastemove.interfaces import IContainerItemRenamer
from zope.copypastemove.interfaces import IObjectMover, IObjectCopier
from zope.copypastemove.interfaces import IPrincipalClipboard
from zope.app.component.testing import PlacefulSetup
from zope.app.container.contained import contained
from zope.app.testing import ztapi
from zope.app.container.interfaces import IContainer, IContained
class BaseTestContentsBrowserView(PlacefulSetup):
"""Base class for testing browser contents.
Subclasses need to define a method, '_TestView__newContext', that
takes no arguments and that returns a new empty test view context.
Subclasses need to define a method, '_TestView__newView', that
takes a context object and that returns a new test view.
"""
def setUp(self):
PlacefulSetup.setUp(self)
PlacefulSetup.buildFolders(self)
ztapi.provideAdapter(IContained, IObjectCopier, ObjectCopier)
ztapi.provideAdapter(IContained, IObjectMover, ObjectMover)
ztapi.provideAdapter(IContainer, IContainerItemRenamer,
ContainerItemRenamer)
ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
PrincipalClipboard)
ztapi.provideAdapter(Principal, IAnnotations,
PrincipalAnnotations)
def testInfo(self):
# Do we get the correct information back from ContainerContents?
container = self._TestView__newContext()
subcontainer = self._TestView__newContext()
container['subcontainer'] = subcontainer
document = Document()
container['document'] = document
fc = self._TestView__newView(container)
info_list = fc.listContentInfo()
self.assertEquals(len(info_list), 2)
ids = map(lambda x: x['id'], info_list)
self.assert_('subcontainer' in ids)
objects = map(lambda x: x['object'], info_list)
self.assert_(subcontainer in objects)
urls = map(lambda x: x['url'], info_list)
self.assert_('subcontainer' in urls)
self.failIf(filter(None, map(lambda x: x['icon'], info_list)))
def testInfoUnicode(self):
# If the id contains non-ASCII characters, url has to be quoted
container = self._TestView__newContext()
subcontainer = self._TestView__newContext()
container[u'f\xf6\xf6'] = subcontainer
fc = self._TestView__newView(container)
info_list = fc.listContentInfo()
urls = map(lambda x: x['url'], info_list)
self.assert_('f%C3%B6%C3%B6' in urls)
def testInfoWDublinCore(self):
container = self._TestView__newContext()
document = Document()
container['document'] = document
from datetime import datetime
from zope.dublincore.interfaces import IZopeDublinCore
class FauxDCAdapter(object):
implements(IZopeDublinCore)
__Security_checker__ = checker.Checker(
{"created": "zope.Public",
"modified": "zope.Public",
"title": "zope.Public",
},
{"title": "zope.app.dublincore.change"})
def __init__(self, context):
pass
title = 'faux title'
size = 1024
created = datetime(2001, 1, 1, 1, 1, 1)
modified = datetime(2002, 2, 2, 2, 2, 2)
ztapi.provideAdapter(IDocument, IZopeDublinCore, FauxDCAdapter)
fc = self._TestView__newView(container)
info = fc.listContentInfo()[0]
self.assertEqual(info['id'], 'document')
self.assertEqual(info['url'], 'document')
self.assertEqual(info['object'], document)
self.assertEqual(info['title'], 'faux title')
self.assertEqual(info['created'], '01/01/01 01:01')
self.assertEqual(info['modified'], '02/02/02 02:02')
def testRemove(self):
container = self._TestView__newContext()
subcontainer = self._TestView__newContext()
container['subcontainer'] = subcontainer
document = Document()
container['document'] = document
document2 = Document()
container['document2'] = document2
fc = self._TestView__newView(container)
fc.request.form.update({'ids': ['document2']})
fc.removeObjects()
info_list = fc.listContentInfo()
self.assertEquals(len(info_list), 2)
ids = map(lambda x: x['id'], info_list)
self.assert_('subcontainer' in ids)
objects = map(lambda x: x['object'], info_list)
self.assert_(subcontainer in objects)
urls = map(lambda x: x['url'], info_list)
self.assert_('subcontainer' in urls)
def testChangeTitle(self):
container = self._TestView__newContext()
document = Document()
container['document'] = document
from zope.dublincore.interfaces import IDCDescriptiveProperties
class FauxDCDescriptiveProperties(object):
implements(IDCDescriptiveProperties)
__Security_checker__ = checker.Checker(
{"title": "zope.Public",
},
{"title": "zope.app.dublincore.change"})
def __init__(self, context):
self.context = context
def setTitle(self, title):
self.context.title = title
def getTitle(self):
return self.context.title
title = property(getTitle, setTitle)
ztapi.provideAdapter(IDocument, IDCDescriptiveProperties, FauxDCDescriptiveProperties)
fc = self._TestView__newView(container)
dc = IDCDescriptiveProperties(document)
fc.request.form.update({'retitle_id': 'document', 'new_value': 'new'})
fc.changeTitle()
events = getEvents()
self.assertEquals(dc.title, 'new')
self.failIf('title' not in events[-1].descriptions[0].attributes)
class IDocument(Interface):
pass
class Document(object):
implements(IDocument)
class Principal(object):
id = 'bob'
class PrincipalAnnotations(dict):
implements(IAnnotations)
data = {}
def __new__(class_, context):
try:
annotations = class_.data[context.id]
except KeyError:
annotations = dict.__new__(class_)
class_.data[context.id] = annotations
return annotations
def __init__(self, context):
pass
def __repr__(self):
return "<%s.PrincipalAnnotations object>" % __name__
class TestCutCopyPaste(PlacefulSetup, TestCase):
def setUp(self):
PlacefulSetup.setUp(self)
PlacefulSetup.buildFolders(self)
ztapi.provideAdapter(IContained, IObjectCopier, ObjectCopier)
ztapi.provideAdapter(IContained, IObjectMover, ObjectMover)
ztapi.provideAdapter(IContainer, IContainerItemRenamer,
ContainerItemRenamer)
ztapi.provideAdapter(IAnnotations, IPrincipalClipboard,
PrincipalClipboard)
ztapi.provideAdapter(Principal, IAnnotations,
PrincipalAnnotations)
def testRename(self):
container = traverse(self.rootFolder, 'folder1')
fc = self._TestView__newView(container)
ids=['document1', 'document2']
for id in ids:
document = Document()
container[id] = document
fc.request.form.update({'rename_ids': ids,
'new_value': ['document1_1', 'document2_2']
})
fc.renameObjects()
self.failIf('document1_1' not in container)
self.failIf('document1' in container)
def testCopyPaste(self):
container = traverse(self.rootFolder, 'folder1')
fc = self._TestView__newView(container)
ids=['document1', 'document2']
for id in ids:
document = Document()
container[id] = document
fc.request.form['ids'] = ids
fc.copyObjects()
fc.pasteObjects()
self.failIf('document1' not in container)
self.failIf('document2' not in container)
self.failIf('document1-2' not in container)
self.failIf('document2-2' not in container)
def testCopyFolder(self):
container = traverse(self.rootFolder, 'folder1')
fc = self._TestView__newView(container)
ids = ['folder1_1']
fc.request.form['ids'] = ids
fc.copyObjects()
fc.pasteObjects()
self.failIf('folder1_1' not in container)
self.failIf('folder1_1-2' not in container)
def testCopyFolder2(self):
container = traverse(self.rootFolder, '/folder1/folder1_1')
fc = self._TestView__newView(container)
ids = ['folder1_1_1']
fc.request.form['ids'] = ids
fc.copyObjects()
fc.pasteObjects()
self.failIf('folder1_1_1' not in container)
self.failIf('folder1_1_1-2' not in container)
def testCopyFolder3(self):
container = traverse(self.rootFolder, '/folder1/folder1_1')
target = traverse(self.rootFolder, '/folder2/folder2_1')
fc = self._TestView__newView(container)
tg = self._TestView__newView(target)
ids = ['folder1_1_1']
fc.request.form['ids'] = ids
fc.copyObjects()
tg.pasteObjects()
self.failIf('folder1_1_1' not in container)
self.failIf('folder1_1_1' not in target)
def testCutPaste(self):
container = traverse(self.rootFolder, 'folder1')
fc = self._TestView__newView(container)
ids=['document1', 'document2']
for id in ids:
document = Document()
container[id] = document
fc.request.form['ids'] = ids
fc.cutObjects()
fc.pasteObjects()
self.failIf('document1' not in container)
self.failIf('document2' not in container)
def testCutFolder(self):
container = traverse(self.rootFolder, 'folder1')
fc = self._TestView__newView(container)
ids = ['folder1_1']
fc.request.form['ids'] = ids
fc.cutObjects()
fc.pasteObjects()
self.failIf('folder1_1' not in container)
def testCutFolder2(self):
container = traverse(self.rootFolder, '/folder1/folder1_1')
fc = self._TestView__newView(container)
ids = ['folder1_1_1']
fc.request.form['ids'] = ids
fc.cutObjects()
fc.pasteObjects()
self.failIf('folder1_1_1' not in container)
def testCutFolder3(self):
container = traverse(self.rootFolder, '/folder1/folder1_1')
target = traverse(self.rootFolder, '/folder2/folder2_1')
fc = self._TestView__newView(container)
tg = self._TestView__newView(target)
ids = ['folder1_1_1']
fc.request.form['ids'] = ids
fc.cutObjects()
tg.pasteObjects()
self.failIf('folder1_1_1' in container)
self.failIf('folder1_1_1' not in target)
def _TestView__newView(self, container):
from zope.app.container.browser.contents import Contents
from zope.publisher.browser import TestRequest
request = TestRequest()
request.setPrincipal(Principal())
return Contents(container, request)
class Test(BaseTestContentsBrowserView, TestCase):
def _TestView__newContext(self):
from zope.app.container.sample import SampleContainer
from zope.site.folder import rootFolder
root = rootFolder()
container = SampleContainer()
return contained(container, root, 'sample')
def _TestView__newView(self, container):
from zope.app.container.browser.contents import Contents
from zope.publisher.browser import TestRequest
request = TestRequest()
request.setPrincipal(Principal())
return Contents(container, request)
def test_suite():
return TestSuite((
makeSuite(Test),
makeSuite(TestCutCopyPaste),
))
if __name__=='__main__':
main(defaultTest='test_suite')
|