testERP5Web.py 29.2 KB
Newer Older
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
##############################################################################
#
# Copyright (c) 2004, 2005, 2006 Nexedi SARL and Contributors. 
# All Rights Reserved.
#          Romain Courteaud <romain@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import os
31
import unittest
32

33
from AccessControl import Unauthorized
34
from AccessControl.SecurityManagement import newSecurityManager
35
from AccessControl.SecurityManagement import getSecurityManager
36 37
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
38
from Products.ERP5Type.tests.utils import DummyLocalizer
39

40
LANGUAGE_LIST = ('en', 'fr', 'de', 'bg',)
41 42 43 44 45

class TestERP5Web(ERP5TypeTestCase, ZopeTestCase.Functional):
  """Test for erp5_web business template.
  """
  run_all_test = 1
46
  quiet = 0
47 48
  manager_username = 'zope'
  manager_password = 'zope'
49
  website_id = 'test'
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64


  def getTitle(self):
    return "ERP5Web"

  def login(self, quiet=0, run=run_all_test):
    uf = self.getPortal().acl_users
    uf._doAddUser(self.manager_username, self.manager_password, ['Manager'], [])
    user = uf.getUserById(self.manager_username).__of__(uf)
    newSecurityManager(None, user)

  def getBusinessTemplateList(self):
    """
    Return the list of required business templates.
    """
65 66
    return ('erp5_base', 'erp5_web',
            'erp5_ingestion', 'erp5_ingestion_mysql_innodb_catalog',
67
            'erp5_dms', 'erp5_dms_mysql_innodb_catalog',)
68 69 70

  def afterSetUp(self):
    self.login()
71
    portal = self.getPortal()
72
    self.web_page_module = self.portal.web_page_module
73
    self.web_site_module = self.portal.web_site_module
74 75
    self.portal_id = self.portal.getId()

76
  def setupWebSite(self, **kw):
77
    """
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      Setup Web Site
    """
    portal = self.getPortal()
    request = self.app.REQUEST
    
    # add supported languages for Localizer
    localizer = portal.Localizer
    for language in LANGUAGE_LIST:
      localizer.manage_addLanguage(language = language)
      
    # create website
    if hasattr(self.web_site_module, self.website_id):
      self.web_site_module.manage_delObjects(self.website_id)
    website = self.getPortal().web_site_module.newContent(portal_type = 'Web Site', 
                                                          id = self.website_id,
                                                          **kw)
    get_transaction().commit()
    self.tic()
    return website
    
  def setupWebSection(self, **kw):
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
      Setup Web Section
    """
    web_site_module = self.portal.getDefaultModule('Web Site')
    website = web_site_module[self.website_id]
    websection = website.newContent(portal_type = 'Web Section', **kw)
    self.websection = websection
    kw = dict(criterion_property_list = 'portal_type',
              membership_criterion_base_category_list = '',
              membership_criterion_category_list = '',)
    websection.edit(**kw)
    websection.setCriterion(property = 'portal_type',
                            identity = ['Web Page'],
                            max = '', 
                            min = '')
                            
    get_transaction().commit()
    self.tic()
    return websection
   

  def setupWebSitePages(self,
                        prefix,
                        suffix = None, 
                        version = '0.1', 
                        language_list = LANGUAGE_LIST):
    """
      Setup some Web Pages.
    """
    webpage_list = []
    portal = self.getPortal()
    request = self.app.REQUEST
    web_site_module = self.portal.getDefaultModule('Web Site')
    website = web_site_module[self.website_id]
    
    # create sample web pages
    for language in language_list:
      if suffix is not None:
        reference = '%s-%s' %(prefix, language)
      else:
        reference = prefix
      webpage = self.web_page_module.newContent(portal_type = 'Web Page', 
                                                reference = reference,
                                                version = version,
                                                language = language)
      webpage.publish()
      webpage.reindexObject()
      self.assertEquals(language, webpage.getLanguage())
      self.assertEquals(reference, webpage.getReference())
      self.assertEquals(version, webpage.getVersion())
      self.assertEquals('published', webpage.getValidationState())
      webpage_list.append(webpage)
    
    get_transaction().commit()
    self.tic()
    return webpage_list
    
156

157 158 159 160 161 162 163 164 165 166 167
  def test_01_WebSiteRecatalog(self, quiet=quiet, run=run_all_test):
    """
      Test that a recataloging works for Web Site documents
    """
    if not run:
      return
    if not quiet:
      message = '\ntest_01_WebSiteRecatalog'
      ZopeTestCase._print(message)
    
    self.setupWebSite()
168
    portal = self.getPortal()
169
    web_site_module = self.portal.getDefaultModule('Web Site')
170 171
    web_site = web_site_module[self.website_id]

172 173 174 175 176 177 178 179
    self.assertTrue(web_site is not None)
    # Recatalog the Web Site document
    portal_catalog = self.getCatalogTool()
    try:
      portal_catalog.catalog_object(web_site)
    except:
      self.fail('Cataloging of the Web Site failed.')

180

181
  def test_02_EditSimpleWebPage(self, quiet=quiet, run=run_all_test):
182 183
    """
      Simple Case of creating a web page.
184
    """
185 186 187 188 189
    if not run:
      return
    if not quiet:
      message = '\ntest_02_EditSimpleWebPage'
      ZopeTestCase._print(message)
190 191 192 193
    page = self.web_page_module.newContent(portal_type='Web Page')
    page.edit(text_content='<b>OK</b>')
    self.assertEquals('text/html', page.getTextFormat())
    self.assertEquals('<b>OK</b>', page.getTextContent())
194
    
195
  def test_03_CreateWebSiteUser(self, quiet=quiet, run=run_all_test):
196 197 198
    """
      Create Web site User.
    """
199 200 201 202 203 204
    if not run:
      return
    if not quiet:
      message = '\ntest_03_CreateWebSiteUser'
      ZopeTestCase._print(message)
    self.setupWebSite()
205 206 207 208 209 210 211 212 213 214 215
    portal = self.getPortal()
    request = self.app.REQUEST
    kw = dict(reference = 'web',
              first_name = 'TestFN',
              last_name = 'TestLN',
              default_email_text = 'test@test.com',
              password = 'abc',
              password_confirm = 'abc',)
    for key, item in kw.items():
      request.set('field_your_%s' %key, item)
    website = portal.web_site_module[self.website_id]
216
    website.WebSite_createWebSiteAccount('WebSite_viewRegistrationDialog')
217
    
218 219 220
    get_transaction().commit()
    self.tic()
    
221 222 223 224 225 226 227 228 229 230 231 232 233
    # find person object by reference
    person = website.ERP5Site_getAuthenticatedMemberPersonValue(kw['reference'])
    self.assertEquals(person.getReference(), kw['reference'])
    self.assertEquals(person.getFirstName(), kw['first_name'])
    self.assertEquals(person.getLastName(), kw['last_name'])
    self.assertEquals(person.getDefaultEmailText(), kw['default_email_text'])
    self.assertEquals(person.getValidationState(), 'validated')

    # check if user account is 'loggable' 
    uf = portal.acl_users
    user = uf.getUserById( kw['reference'])
    self.assertEquals(str(user),  kw['reference'])
    self.assertEquals(1, user.has_role(('Member', 'Authenticated',)))
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
    
  def test_04_WebPageTranslation(self, quiet=quiet, run=run_all_test):
    """
      Simple Case of showing the proper Web Page based on 
      current user selected language in browser.
    """
    if not run:
      return
    if not quiet:
      message = '\ntest_04_WebPageTranslation'
      ZopeTestCase._print(message)
    portal = self.getPortal()
    request = self.app.REQUEST
    website = self.setupWebSite()
    websection = self.setupWebSection()
    page_reference = 'default-webpage'
    webpage_list  = self.setupWebSitePages(prefix = page_reference)
   
    # set default web page for section
    found_by_reference = portal.portal_catalog(name = page_reference,
                                               portal_type = 'Web Page')
    found =  found_by_reference[0].getObject()
    websection.edit(categories_list = ['aggregate/%s' %found.getRelativeUrl(),])
    self.assertEqual([found.getReference(),],
                      websection.getAggregateReferenceList())
    # even though we create many pages we should get only one
    # this is the most recent one since all share the same reference
    self.assertEquals(1, len(websection.WebSection_getDocumentValueList()))
     
    # use already created few pages in different languages with same reference
    # and check that we always get the right one based on selected 
    # by us language
    for language in LANGUAGE_LIST:
      # set default language in Localizer only to check that we get
      # the corresponding web page for language. 
      # XXX: Extend API so we can select language from REQUEST
      portal.Localizer.manage_changeDefaultLang(language = language)
      default_document = websection.getDefaultDocumentValue()
      self.assertEquals(language, default_document.getLanguage())

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
  def test_05_WebPageVersioning(self, quiet=quiet, run=run_all_test):
    """
      Simple Case of showing the proper most recent public Web Page based on 
      (language, version
    """
    if not run:
      return
    if not quiet:
      message = '\ntest_05_WebPageVersioning'
      ZopeTestCase._print(message)
    portal = self.getPortal()
    request = self.app.REQUEST
    website = self.setupWebSite()
    websection = self.setupWebSection()
    page_reference = 'default-webpage-versionning'
    webpage_list  = self.setupWebSitePages(prefix = page_reference)
   
    # set default web page for section
    found_by_reference = portal.portal_catalog(name = page_reference,
                                               language = 'en',
                                               portal_type = 'Web Page')
    en_01 =  found_by_reference[0].getObject()
    # set it as default web page for section
    websection.edit(categories_list = ['aggregate/%s' %en_01.getRelativeUrl(),])
    self.assertEqual([en_01.getReference(),],
                      websection.getAggregateReferenceList())
    
    # create manually a copy of 'en_01' with higher version and check that
    # older version is archived and new one is show as default web page for section
    en_02 = self.web_page_module.newContent(portal_type = 'Web Page', 
                                            reference = page_reference,
                                            version = 0.2,
                                            language = 'en')
    en_02.publish()
    en_02.reindexObject()
    get_transaction().commit()
    self.tic()

    # is old archived?
    self.assertEquals('archived', en_01.getValidationState())

    # is new public and default web page for section?
    portal.Localizer.manage_changeDefaultLang(language = 'en')
    default_document = websection.getDefaultDocumentValue()
    self.assertEquals(en_02, default_document)
    self.assertEquals('en', default_document.getLanguage())
    self.assertEquals('0.2', default_document.getVersion())
    self.assertEquals('published', default_document.getValidationState())
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  def test_06_WebSectionAuthorizationForced(self, quiet=quiet, run=run_all_test):
    """ Check that when a document is requested within a Web Section we have a chance to 
        require user to login.
        Whether or not an user will login is controlled by a property on Web Section (authorization_forced).
    """
    if not run:
      return
    if not quiet:
      message = '\ntest_06_WebSectionAuthorizationForced'
      ZopeTestCase._print(message)
    request = self.app.REQUEST    
    website = self.setupWebSite()
    websection = self.setupWebSection()
    webpage_list  = self.setupWebSitePages(prefix = 'test-web-page')
    webpage = webpage_list[0]
    document_reference = 'default-document-reference'
    document = self.portal.web_page_module.newContent(
                                      portal_type = 'Web Page', 
                                      reference = document_reference)
342
    document.release()
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    website.setAuthorizationForced(0)  
    websection.setAuthorizationForced(0)                                      
    get_transaction().commit()
    self.tic()
    
    # make sure that _getExtensibleContent will return the same document
    # there's not other way to test otherwise URL traversal
    self.assertEqual(document.getUid(), 
                           websection._getExtensibleContent(request,  document_reference).getUid())
                          
    # Anonymous User should have in the request header for not found when 
    # viewing non available document in Web Section (with no authorization_forced)
    self.logout()
    self.assertEqual(None,  websection._getExtensibleContent(request,  document_reference))
    self.assertEqual('404 Not Found',  request.RESPONSE.getHeader('status'))
358

359 360 361 362 363 364 365 366 367
    # set authorization_forced flag
    self.login()
    websection.setAuthorizationForced(1)
    
    # check Unauthorized exception is raised for anonymous
    # this exception is usually caught and user is redirecetd to login form
    self.logout()
    self.assertRaises(Unauthorized,  websection._getExtensibleContent,  request,  document_reference)
    
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
  def test_07_WebPageTextContentSubstituions(self, quiet=quiet, run=run_all_test):
    """
      Simple Case of showing the proper text content with and without a substitution
      mapping method.
    """
    if not run:
      return
    if not quiet:
      message = '\ntest_07_WebPageTextContentSubstituions'
      ZopeTestCase._print(message)

    content = '<a href="${toto}">$titi</a>'
    substituted_content = '<a href="foo">bar</a>'
    mapping = dict(toto='foo', titi='bar')

    portal = self.getPortal()
    document = portal.web_page_module.newContent(portal_type='Web Page', 
            text_content=content)
   
    # No substitution should occur.
    self.assertEquals(document.asStrippedHTML(), content)

    klass = document.__class__
    klass.getTestSubstitutionMapping = lambda self, **kw: mapping
    document.setTextContentSubstitutionMappingMethodId('getTestSubstitutionMapping')

    # Substitutions should occur.
    self.assertEquals(document.asStrippedHTML(), substituted_content)

    klass._getTestSubstitutionMapping = klass.getTestSubstitutionMapping
    document.setTextContentSubstitutionMappingMethodId('_getTestSubstitutionMapping')

    # Even with the same callable object, a restricted method id should not be callable.
    self.assertRaises(Unauthorized, document.asStrippedHTML)
Ivan Tyagov's avatar
Ivan Tyagov committed
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
    
  def test_08_LatestContent(self, quiet=quiet, run=run_all_test):
    """ Test latest content for a Web Section. Test different use case like languaeg, workflow state.
   """
    if not run: return
    if not quiet:
      message = '\ntest_08_LatestContent'
      ZopeTestCase._print(message)    
    portal = self.getPortal()
    website = self.setupWebSite()
    websection = self.setupWebSection()
    portal_categories = portal.portal_categories
    publication_section_category_id_list = ['documentation',  'administration']
    for category_id in publication_section_category_id_list:
      portal_categories.publication_section.newContent(portal_type = 'Category', 
                                                                             id = category_id)
    #set predicate on web section using 'publication_section'
    websection.edit(membership_criterion_base_category = ['publication_section'], 
                            membership_criterion_category=['publication_section/%s' 
                                                                              %publication_section_category_id_list[0]])
    get_transaction().commit()
    self.tic()
    
    self.assertEquals(0,  len(websection.getDocumentValueList())) 
    # create pages belonging to this publication_section 'documentation'
    web_page_en = portal.web_page_module.newContent(portal_type = 'Web Page', 
                                                 language = 'en', 
                                                 publication_section_list=publication_section_category_id_list[:1])
    web_page_en.publish()
    get_transaction().commit()
    self.tic()
433 434
    self.assertEquals(1,  len(websection.getDocumentValueList(anguage='en')))
    self.assertEquals(web_page_en,  websection.getDocumentValueList(anguage='en')[0].getObject())
Ivan Tyagov's avatar
Ivan Tyagov committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    
    # create pages belonging to this publication_section 'documentation' but for 'bg' language
    web_page_bg = portal.web_page_module.newContent(portal_type = 'Web Page', 
                                                 language = 'bg', 
                                                 publication_section_list=publication_section_category_id_list[:1])
    web_page_bg.publish()
    get_transaction().commit()
    self.tic()
    self.assertEquals(1,  len(websection.getDocumentValueList(language='bg')))
    self.assertEquals(web_page_bg,  websection.getDocumentValueList(language='bg')[0].getObject())
    
    # reject page
    web_page_bg.reject()
    get_transaction().commit()
    self.tic()
    self.assertEquals(0,  len(websection.getDocumentValueList(language='bg')))
451 452 453 454 455 456 457
    
    # publish page and search without a language (by default system should return 'en' docs only)
    web_page_bg.publish()
    get_transaction().commit()
    self.tic()
    self.assertEquals(1,  len(websection.getDocumentValueList()))
    self.assertEquals(web_page_en,  websection.getDocumentValueList()[0].getObject())
458

Ivan Tyagov's avatar
Ivan Tyagov committed
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
  def test_09_DefaultDocumentForWebSection(self, quiet=quiet, run=run_all_test):
    """ Testetting default document for a Web Section. Test  use case like workflow state of document.
         Note: due to generic ERP5 Web implementation this test highly depends on WebSection_geDefaulttDocumentValueList
    """
    if not run: return
    if not quiet:
      message = '\ntest_09_DefaultDocumentForWebSection'
      ZopeTestCase._print(message)    
    portal = self.getPortal()
    website = self.setupWebSite()
    websection = self.setupWebSection()
    publication_section_category_id_list = ['documentation',  'administration']
    
    # create pages belonging to this publication_section 'documentation'
    web_page_en = portal.web_page_module.newContent(portal_type = 'Web Page', 
                                                 language = 'en', 
                                                 reference='NXD-DDP', 
                                                 publication_section_list=publication_section_category_id_list[:1])    
    websection.setAggregateValue(web_page_en)
    get_transaction().commit()
    self.tic()
    self.assertEqual(None,   websection.getDefaultDocumentValue())
    # publish it
    web_page_en.publish()
    get_transaction().commit()
    self.tic()
    self.assertEqual(web_page_en,   websection.getDefaultDocumentValue())
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
    
  def test_10_WebSectionAuthorizationForcedForDefaultDocument(self, quiet=quiet, run=run_all_test):
    """ Check that when a Web Section contains a default document not accessible by user we have a chance to 
        require user to login.
        Whether or not an user will login is controlled by a property on Web Section (authorization_forced).
    """
    if not run:   return
    if not quiet:  
      message = '\ntest_10_WebSectionAuthorizationForcedForDefaultDocument'
      ZopeTestCase._print(message)
    request = self.app.REQUEST    
    website = self.setupWebSite()
    websection = self.setupWebSection()
    web_page_reference = 'default-document-reference'
    web_page_en = self.portal.web_page_module.newContent(
                                      portal_type = 'Web Page', 
                                      language = 'en', 
                                      reference = web_page_reference)
    # this way it's not viewable by anonymous and we can test 
    web_page_en.releaseAlive() 
    websection.setAggregateValue(web_page_en)
    websection.setAuthorizationForced(1)                                     
    get_transaction().commit()
    self.tic()
    
    # make sure that getDefaultDocumentValue() will return the same document for logged in user
    # if default document is accessible
    self.assertEqual(web_page_en.getUid(), 
                           websection.getDefaultDocumentValue().getUid())
                          
    # check Unauthorized exception is raised for anonymous when authorization_forced is set 
    self.logout()
    self.assertEqual(None,  websection.getDefaultDocumentValue()) 
    self.assertRaises(Unauthorized,  websection)                           
                          
    # Anonymous User should not get Unauthorized when authorization_forced is not set
    self.login()
    websection.setAuthorizationForced(0)
    get_transaction().commit()
    self.tic()
    self.logout()
    self.assertEqual(None,  websection.getDefaultDocumentValue())
    try:
      websection()
    except Unauthorized:
      self.fail("Web Section should not prompt user for login.")
532

533 534 535 536 537
  def test_11_WebSection_getDocumentValueList(self, quiet=quiet, run=run_all_test):
    """ Check getting getDocumentValueList from Web Section.
    """
    if not run:   return
    if not quiet:  
538 539
      message = '\ntest_11_WebSection_getDocumentValueList'
      ZopeTestCase._print(message)
540 541 542 543 544 545 546 547 548
    portal = self.getPortal()
    website = self.setupWebSite()
    websection = self.setupWebSection()
    publication_section_category_id_list = ['documentation',  'administration']
    
    #set predicate on web section using 'publication_section'
    websection.edit(membership_criterion_base_category = ['publication_section'], 
                            membership_criterion_category=['publication_section/%s' 
                                                                              %publication_section_category_id_list[0]])
549 550 551 552
    # clean up
    self.web_page_module.manage_delObjects(list(self.web_page_module.objectIds()))
    portal.portal_categories.publication_section.manage_delObjects(
                                                                  list(portal.portal_categories.publication_section.objectIds()))   
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
    # create categories
    for category_id in publication_section_category_id_list:
      portal.portal_categories.publication_section.newContent(portal_type = 'Category', 
                                                                             id = category_id)
    web_page_reference = 'default-document-reference-%s'
    web_page_list = []
    for index in range(0, 10):
      web_page = self.portal.web_page_module.newContent(
                                      portal_type = 'Web Page', 
                                      language = 'en', 
                                      reference = web_page_reference %index, 
                                      publication_section_list=publication_section_category_id_list[:1])
      web_page_list.append(web_page)
    get_transaction().commit()
    self.tic()
    # in draft state no documents should belong to this Web Section    
    self.assertEqual(0, len(websection.getDocumentValueList()))
    
    # when published all web pages should belong to it
    for web_page in web_page_list:
      web_page.publish()
    get_transaction().commit()
    self.tic()
    self.assertEqual(len(web_page_list), len(websection.getDocumentValueList()))
    
    # test if limit works
579 580
    self.assertEqual(5, len(websection.getDocumentValueList(limit=5)))

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
class TestERP5WebWithSimpleSecurity(ERP5TypeTestCase):
  """
  Test for erp5_web with simple security.
  """
  run_all_test = 1
  quiet = 1

  def getBusinessTemplateList(self):
    return ('erp5_base',
            'erp5_dms_mysql_innodb_catalog',
            'erp5_web',
            )

  def getTitle(self):
    return "Web"

  def createUser(self, name, role_list):
    user_folder = self.getPortal().acl_users
    user_folder._doAddUser(name, 'password', role_list, [])

  def changeUser(self, name):
    self.old_user = getSecurityManager().getUser()
    user_folder = self.getPortal().acl_users
    user = user_folder.getUserById(name).__of__(user_folder)
    newSecurityManager(None, user)

  def afterSetUp(self):
    self.portal.Localizer = DummyLocalizer()
    self.createUser('admin', ['Manager'])
    self.createUser('erp5user', ['Auditor', 'Author'])
    get_transaction().commit()
    self.tic()

  def beforeTearDown(self):
    get_transaction().abort()
    def clearModule(module):
      module.manage_delObjects(list(module.objectIds()))
      get_transaction().commit()
      self.tic()
    clearModule(self.portal.web_site_module)
    clearModule(self.portal.web_page_module)

624 625 626
  def test_01_AccessWebPageByReference(self, quiet=quiet, run=run_all_test):
    if not run:
      return
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 681 682 683
    self.changeUser('admin')
    site = self.portal.web_site_module.newContent(portal_type='Web Site',
                                                  id='site')
    section = site.newContent(portal_type='Web Section', id='section')

    get_transaction().commit()
    self.tic()

    section.setCriterionProperty('portal_type')
    section.setCriterion('portal_type', max='', identity=['Web Page'], min='')

    get_transaction().commit()
    self.tic()

    self.changeUser('erp5user')
    page_en = self.portal.web_page_module.newContent(portal_type='Web Page')
    page_en.edit(reference='my-first-web-page',
                 language='en',
                 version='1',
                 text_format='text/plain',
                 text_content='Hello, World!')

    get_transaction().commit()
    self.tic()

    page_en.publish()

    get_transaction().commit()
    self.tic()

    page_ja = self.portal.web_page_module.newContent(portal_type='Web Page')
    page_ja.edit(reference='my-first-web-page',
                 language='ja',
                 version='1',
                 text_format='text/plain',
                 text_content='こんにちは、世界!')

    get_transaction().commit()
    self.tic()

    page_ja.publish()

    get_transaction().commit()
    self.tic()

    # By Anonymous
    self.logout()

    self.portal.Localizer.changeLanguage('en')

    target = self.portal.restrictedTraverse('web_site_module/site/section/my-first-web-page')
    self.assertEqual('Hello, World!', target.getTextContent())

    self.portal.Localizer.changeLanguage('ja')

    target = self.portal.restrictedTraverse('web_site_module/site/section/my-first-web-page')
    self.assertEqual('こんにちは、世界!', target.getTextContent())
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    
  def test_02_LocalRolesFromRoleDefinition(self, quiet=quiet, run=run_all_test):
    """ Test setting local roles on Web Site/ Web Sectio using ERP5 Role Definition objects . """
    if not run:
      return
    portal = self.portal
    person_reference = 'webuser'
    site = portal.web_site_module.newContent(portal_type='Web Site',
                                                  id='site')
    section = site.newContent(portal_type='Web Section', id='section')
    person = portal.person_module.newContent(portal_type = 'Person', 
                                                                    reference = person_reference)
    # add Role Definition for site and section
    site_role_definition = site.newContent(portal_type = 'Role Definition', 
                                                           role_name = 'Assignee', 
                                                           agent = person.getRelativeUrl())
    section_role_definition = section.newContent(portal_type = 'Role Definition', 
                                                           role_name = 'Associate', 
                                                           agent = person.getRelativeUrl())                                                           
    get_transaction().commit()
    self.tic()
    # check if Role Definition have create local roles
    self.assertSameSet(('Assignee',),  
                                 site.get_local_roles_for_userid(person_reference))
    self.assertSameSet(('Associate',),  
                                 section.get_local_roles_for_userid(person_reference))
                                 
    # delete Role Definition and check again (local roles must be gone too)
    site.manage_delObjects(site_role_definition.getId())
    section.manage_delObjects(section_role_definition.getId())
    get_transaction().commit()
    self.tic()
    self.assertSameSet((),  
                                 site.get_local_roles_for_userid(person_reference))
    self.assertSameSet((),  
                                 section.get_local_roles_for_userid(person_reference))                                 
720

721 722 723
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestERP5Web))
724
  suite.addTest(unittest.makeSuite(TestERP5WebWithSimpleSecurity))
725
  return suite