def _get_toolbar(self, content_obj, **kwargs): """Helper method to set up the toolbar """ request = RequestFactory().get('/') request.user = UserFactory() request.session = {} cms_toolbar = CMSToolbar(request) toolbar = VersioningToolbar(request, toolbar=cms_toolbar, is_current_app=True, app_path='/') toolbar.toolbar.obj = content_obj if kwargs.get('edit_mode', False): toolbar.toolbar.edit_mode_active = True toolbar.toolbar.content_mode_active = False toolbar.toolbar.structure_mode_active = False elif kwargs.get('preview_mode', False): toolbar.toolbar.edit_mode_active = False toolbar.toolbar.content_mode_active = True toolbar.toolbar.structure_mode_active = False elif kwargs.get('structure_mode', False): toolbar.toolbar.edit_mode_active = False toolbar.toolbar.content_mode_active = False toolbar.toolbar.structure_mode_active = True return toolbar
def test_disable_edit_button_when_content_is_locked(self): user = self.get_superuser() user_2 = UserFactory( is_staff=True, is_superuser=True, username='******', email='*****@*****.**', ) version = PageVersionFactory(created_by=user) toolbar = get_toolbar(version.content, user_2, content_mode=True) toolbar.post_template_populate() btn_name = format_html( '<span style="vertical-align:middle;position:relative;top:-1px" class="cms-icon cms-icon-lock"></span>{name}', # noqa: E501 name=_('Edit'), ) edit_button = find_toolbar_buttons(btn_name, toolbar.toolbar)[0] self.assertEqual(edit_button.name, btn_name) self.assertEqual(edit_button.url, 'javascript:void(0)') self.assertTrue(edit_button.disabled) self.assertListEqual( edit_button.extra_classes, ['cms-btn-action', 'cms-version-locking-btn-icon'] )
def test_version_menu_for_non_version_content(self): # User objects are not registered with versioning, so attempting # to populate toolbar shouldn't contain a version menu toolbar = get_toolbar(UserFactory(), edit_mode=True) toolbar.post_template_populate() version_menu = toolbar.toolbar.get_menu('version') self.assertIsNone(version_menu)
def test_dont_add_publish_for_models_not_registered_with_versioning(self): # User objects are not registered with versioning, so attempting # to populate a user toolbar should not attempt to add a publish # button toolbar = get_toolbar(UserFactory(), edit_mode=True) toolbar.post_template_populate() self.assertFalse(toolbar_button_exists("Publish", toolbar.toolbar))
def test_version_menu_for_version_content_no_permission(self): """Manage versions entry shouldn't appear if user doesn't have access to that endpoint""" user = UserFactory(is_staff=True) version = PollVersionFactory() toolbar = get_toolbar(version.content, user=user, preview_mode=True) toolbar.post_template_populate() version_menu = toolbar.toolbar.get_menu("version") self.assertFalse(version_menu.get_items())
def test_dont_add_publish_for_models_not_registered_with_versioning(self): # User objects are not registered with versioning, so attempting # to populate a user toolbar should not attempt to add a publish # button toolbar = self._get_toolbar(UserFactory(), edit_mode=True) toolbar.post_template_populate() buttons = toolbar.toolbar.get_right_items() self.assertListEqual(buttons, [])
def test_dont_add_edit_for_models_not_registered_with_versioning(self): # User objects are not registered with versioning, so attempting # to populate a user toolbar should not attempt to add a edit # button toolbar = get_toolbar(UserFactory(), user=self.get_superuser(), preview_mode=True) toolbar.post_template_populate() self.assertFalse(toolbar_button_exists("Edit", toolbar.toolbar))
def test_version_menu_for_version_content(self): # Versioned item should have versioning menu user = UserFactory(is_staff=True) user.user_permissions.add( Permission.objects.get( content_type__app_label="djangocms_versioning", codename="change_pollcontentversion", )) version = PollVersionFactory() toolbar = get_toolbar(version.content, user=user, preview_mode=True) toolbar.post_template_populate() version_menu = toolbar.toolbar.get_menu("version") self.assertEqual(version_menu.get_items()[0].name, "Manage Versions...")
def get_toolbar(content_obj, user=None, **kwargs): """ Helper method to set up the toolbar Warning: Other apps may use this helper to improve readability and stability of tests. Be sure to keep backwards compatibility where possible """ # Set the user if none are sent if not user: user = UserFactory(is_staff=True) request = kwargs.get('request', RequestFactory().get('/')) request.user = user request.session = kwargs.get('session', {}) request.current_page = getattr(content_obj, 'page', None) request.toolbar = CMSToolbar(request) # Set the toolbar class if kwargs.get('toolbar_class', False): toolbar_class = kwargs.get('toolbar_class') else: toolbar_class = VersioningToolbar toolbar = toolbar_class( request, toolbar=request.toolbar, is_current_app=True, app_path='/', ) toolbar.toolbar.set_object(content_obj) # Set the toolbar mode if kwargs.get('edit_mode', False): toolbar.toolbar.edit_mode_active = True toolbar.toolbar.content_mode_active = False toolbar.toolbar.structure_mode_active = False elif kwargs.get('preview_mode', False): toolbar.toolbar.edit_mode_active = False toolbar.toolbar.content_mode_active = True toolbar.toolbar.structure_mode_active = False elif kwargs.get('structure_mode', False): toolbar.toolbar.edit_mode_active = False toolbar.toolbar.content_mode_active = False toolbar.toolbar.structure_mode_active = True toolbar.populate() return toolbar
def test_show_menu_only_visible_for_user(self): from django.contrib.auth.models import Group from cms.models import ACCESS_PAGE, PagePermission group = Group.objects.create(name='test_group') user = UserFactory() user.groups.add(group) q_args = { 'grant_on': ACCESS_PAGE, 'group': group, } # Restrict pages for the user so that we can test # whether the can_view=True only pages are visible in the menu. PagePermission.objects.create(can_view=False, page=self._page_1.content.page, **q_args) PagePermission.objects.create(can_view=True, page=self._page_2.content.page, **q_args) PagePermission.objects.create(can_view=False, page=self._page_2_1.content.page, **q_args) PagePermission.objects.create(can_view=True, page=self._page_2_2.content.page, **q_args) PagePermission.objects.create(can_view=True, page=self._page_3.content.page, **q_args) context = self._render_menu(user=user, preview_mode=True) nodes = context['children'] # At this point, only Page 2, Page 2_2 and Page 3 should # be rendered in the menu. self.assertEqual(len(nodes), 2) self._assert_node(nodes[0], self._page_2) children = nodes[0].children self.assertEqual(len(children), 1) self._assert_node(children[0], self._page_2_2) self._assert_node(nodes[1], self._page_3)
def test_edit_button_when_content_is_locked_users_full_name_used(self): user = self.get_superuser() user.first_name = "Firstname" user.last_name = "Lastname" user.save() user_2 = UserFactory( is_staff=True, is_superuser=True, username='******', email='*****@*****.**', ) version = PageVersionFactory(created_by=user) toolbar = get_toolbar(version.content, user_2, content_mode=True) toolbar.post_template_populate() btn_name = format_html( '<span style="vertical-align:middle;position:relative;top:-1px" class="cms-icon cms-icon-lock"></span>{name}', # noqa: E501 name=_('Edit'), ) edit_button = find_toolbar_buttons(btn_name, toolbar.toolbar)[0] self.assertEqual(edit_button.html_attributes, {'title': "Locked with {}".format(user.get_full_name())})