Exemple #1
0
    def get_heading_counters_latex(self):
        if self.context != self.layout.context:
            # Only set the heading counters when exporting this chapter
            # directly. Otherwise it is not the first content.
            return ''

        counters = (
            'chapter',
            'section',
            'subsection',
            'subsubsection',
            'paragraph',
            'subparagraph',
            )

        helper = BookHelper()
        heading_numbers = helper.get_chapter_level(self.context)
        latex = []
        if heading_numbers[-1] == 1:
            del heading_numbers[-1]
        else:
            heading_numbers[-1] -= 1

        for level, num in enumerate(heading_numbers):
            latex.append(r'\setcounter{%s}{%s}' % (
                    counters[level], num))

        if latex:
            latex.append('')

        return '\n'.join(latex)
Exemple #2
0
    def get_heading_counters_latex(self):
        if self.context != self.layout.context:
            # Only set the heading counters when exporting this chapter
            # directly. Otherwise it is not the first content.
            return ''

        counters = (
            'chapter',
            'section',
            'subsection',
            'subsubsection',
            'paragraph',
            'subparagraph',
        )

        helper = BookHelper()
        heading_numbers = helper.get_chapter_level(self.context)
        latex = []

        if heading_numbers[-1] == 1:
            del heading_numbers[-1]
        else:
            heading_numbers[-1] -= 1

        for level, num in enumerate(heading_numbers):
            latex.append(r'\setcounter{%s}{%s}' % (counters[level], num))

        if latex:
            latex.append('')

        return '\n'.join(latex)
Exemple #3
0
def get_latex_chapter_author(context):
    helper = BookHelper()
    if not helper.is_first_level_chapter(context): return ""

    latex = ""
    author = context.Schema().getField("author")
    if author:
        author = author.get(context)
        latex += "\\setarticleauthor{%s}\n" % author
        if author:
            latex += "\\makearticleauthor\n"
    return latex
    def getFields(self):
        # Only chapters in the first level need to be extended
        if self.context.isTemporary():
            return []
        helper = BookHelper()
        if not helper.is_first_level_chapter(self.context):
            return []

        #Check Book's Layout
        par = aq_parent(aq_inner(self.context))
        layout_layer_name = getattr(par, 'latex_layout', None)
        if layout_layer_name:
            layout_layer = resolve(layout_layer_name)
            if layout_layer == IProceedingsLayoutSelectionLayer:
                return self.fields
        return []
 def export_table(self):
     context = aq_inner(self.context)
     file_ = StringIO()
     writer = csv.DictWriter(file_, self.active_columns,
                             dialect='excel', delimiter=';')
     first_column = self.active_columns[0]
     first_row = dict([(first_column, self.context.absolute_url())])
     writer.writerow(first_row)
     for row in context.data:
         writer.writerow(dict([
                     (col, row[col])
                     for col
                     in self.active_columns]))
     file_.seek(0)
     self.request.RESPONSE.setHeader(
         'Content-Type', 'text/csv; charset=utf-8')
     # we use all parent-ids (seperated by "_") as filename.
     # the user should be able to find the table later..
     # since we do not have enough charactors on windows, we need to use
     # a special numbering for generating the filename:
     # bookid.1.3.2.tableid.csv
     # for the chapters instead of the IDs the position-in-parent is used
     filename = [
         '%s.csv' % self.context.id,
         ]
     obj = aq_parent(aq_inner(self.context))
     filename.insert(0, BookHelper().get_chapter_level_string(obj))
     filename = '.'.join(filename)
     # set the request and send the file
     self.request.RESPONSE.setHeader('Content-disposition',
                                     'attachment; filename=%s' % filename)
     return file_.read()
    def test_generated_title_result_is_consistent(self):
        self.mock_inject_settings(self.textblock1, hideFromTOC=True)
        self.replay()

        self.assertTrue(self.textblock1.Schema().getField('hideFromTOC').get(
            self.textblock1))

        self.assertTrue(self.textblock1.Schema().getField('hideFromTOC').get(
            self.textblock1))

        self.assertTrue(self.textblock1.Schema().getField('hideFromTOC').get(
            self.textblock1))

        self.assertEqual(BookHelper()(self.textblock1), '<h4>Textblock1</h4>')

        self.assertEqual(BookHelper()(self.textblock1), '<h4>Textblock1</h4>')
Exemple #7
0
class SimpleLayoutListingViewlet(viewlets.SimpleLayoutListingViewlet):

    render = ViewPageTemplateFile('listing.pt')
    helper = BookHelper()

    def get_valid_parent_h_tags(self):
        return self.helper.generate_valid_hierarchy_h_tags(self.context)
Exemple #8
0
 def get_dynamic_title(self):
     return BookHelper()(self.context)
Exemple #9
0
 def get_dynamic_title(self):
     return BookHelper()(self.context, linked=True)
Exemple #10
0
class TestUnitBookHelper(MockTestCase):
    """ Unittests for helper methods in BookHelper Class
    """

    def setUp(self):
        """ Setup test environment

        book
        |
        --- chapter1
        |
        --- chapter2
            |
            ---chapter3
            |  |
            |  ---chapter5
            |  |
            |  ---image
            |  |
            |  ---paragraph1
            |  |
            |  ---paragraph2
            |
            ---chapter4
        """

        # Helper object
        self.helper = BookHelper()

        #  Siteroot
        self.root = self.create_dummy()
        directlyProvides(self.root, IPloneSiteRoot)

        #  Book
        self.book = self.create_dummy()
        directlyProvides(self.book, [IBook, IFolderish])
        self.book = self.mocker.proxy(self.book, spec=False, count=False)
        self.expect(self.book.__parent__).result(self.root)
        self.expect(self.book.Title()).result('Book')

        # Childs of book
        self.chapter1 = self.create_dummy()
        directlyProvides(self.chapter1, IFolderish)
        self.chapter1 = self.mocker.proxy(
            self.chapter1, spec=False, count=False)
        self.expect(self.chapter1.__parent__).result(self.book)
        self.expect(self.chapter1.Title()).result('Chapter1')
        self.expect(self.chapter1.portal_type).result('Chapter')
        self.chapter2 = self.create_dummy()
        directlyProvides(self.chapter2, IFolderish)
        self.chapter2 = self.mocker.proxy(
            self.chapter2, spec=False, count=False)
        self.expect(self.chapter2.__parent__).result(self.book)
        self.expect(self.chapter2.Title()).result('Chapter2')
        self.expect(self.chapter2.portal_type).result('Chapter')

        #  Childs of chapter 2
        self.chapter3 = self.create_dummy()
        directlyProvides(self.chapter3, IFolderish)
        self.chapter3 = self.mocker.proxy(
            self.chapter3, spec=False, count=False)
        self.expect(self.chapter3.__parent__).result(self.chapter2)
        self.expect(self.chapter3.Title()).result('Chapter3')
        self.expect(self.chapter3.portal_type).result('Chapter')
        self.chapter4 = self.create_dummy()
        directlyProvides(self.chapter4, IFolderish)
        self.chapter4 = self.mocker.proxy(
            self.chapter4, spec=False, count=False)
        self.expect(self.chapter4.__parent__).result(self.chapter2)
        self.expect(self.chapter4.Title()).result('Chapter4')
        self.expect(self.chapter4.portal_type).result('Chapter')

        #  Childs of chapter 3
        self.chapter5 = self.create_dummy()
        directlyProvides(self.chapter5, IFolderish)
        self.chapter5 = self.mocker.proxy(
            self.chapter5, spec=False, count=False)
        self.expect(self.chapter5.__parent__).result(self.chapter3)
        self.expect(self.chapter5.Title()).result('Chapter5')
        self.expect(self.chapter5.portal_type).result('Chapter')
        self.image = self.mocker.mock(count=False)
        self.expect(self.image.__parent__).result(self.chapter3)
        self.expect(self.image.Title()).result('Image')
        self.expect(self.image.portal_type).result('Image')
        self.paragraph1 = self.mocker.mock(count=False)
        self.expect(self.paragraph1.__parent__).result(self.chapter3)
        self.expect(self.paragraph1.showTitle).result(True)
        self.expect(self.paragraph1.Title()).result('Paragraph1')
        self.expect(self.paragraph1.portal_type).result('Paragraph')
        self.paragraph2 = self.mocker.mock(count=False)
        self.expect(self.paragraph2.__parent__).result(self.chapter3)
        self.expect(self.paragraph2.__inner__).result(self.paragraph2)
        self.expect(self.paragraph2.showTitle).result(True)
        self.expect(self.paragraph2.Title()).result('Paragraph2')
        self.expect(self.paragraph2.title_or_id()).result('Paragraph2')
        self.expect(self.paragraph2.portal_type).result('Paragraph')

        # Folderish content
        self.expect(self.book.contentValues()).result(
            [self.chapter1, self.chapter2])
        self.expect(self.chapter1.contentValues()).result([])
        self.expect(self.chapter2.contentValues()).result(
            [self.chapter3, self.chapter4])
        self.expect(self.chapter3.contentValues()).result(
            [self.chapter5, self.image, self.paragraph1, self.paragraph2])
        self.expect(self.chapter4.contentValues()).result([])
        self.expect(self.chapter5.contentValues()).result([])

    def test_generate_title(self):
        """ Test generate title
        """
        self.replay()
        title = self.helper(self.paragraph2)
        self.assertTrue(title == '<h4>2.1.3 Paragraph2</h4>')

    def test_generate_valid_hierarchy_h_tags(self):
        """ Test generate valid hierarchy h tags
        """
        self.replay()
        tags = self.helper.generate_valid_hierarchy_h_tags(self.chapter3)
        self.assertTrue(
            tags == '<h1>Book</h1>' +
                    '<h2>Chapter2</h2>' +
                    '<h3>Chapter3</h3>')

    def test_get_folder_position(self):
        """ Test get folder position
        """
        self.replay()
        position = self.helper.get_folder_position(self.paragraph2)
        self.assertTrue(position == 3)

    def test_get_hierarchy_position(self):
        """ Test hierarchy position
        """
        self.replay()
        position = self.helper.get_hierarchy_position(self.paragraph2)
        self.assertTrue(position == 4)
Exemple #11
0
    def setUp(self):
        """ Setup test environment

        book
        |
        --- chapter1
        |
        --- chapter2
            |
            ---chapter3
            |  |
            |  ---chapter5
            |  |
            |  ---image
            |  |
            |  ---paragraph1
            |  |
            |  ---paragraph2
            |
            ---chapter4
        """

        # Helper object
        self.helper = BookHelper()

        #  Siteroot
        self.root = self.create_dummy()
        directlyProvides(self.root, IPloneSiteRoot)

        #  Book
        self.book = self.create_dummy()
        directlyProvides(self.book, [IBook, IFolderish])
        self.book = self.mocker.proxy(self.book, spec=False, count=False)
        self.expect(self.book.__parent__).result(self.root)
        self.expect(self.book.Title()).result('Book')

        # Childs of book
        self.chapter1 = self.create_dummy()
        directlyProvides(self.chapter1, IFolderish)
        self.chapter1 = self.mocker.proxy(
            self.chapter1, spec=False, count=False)
        self.expect(self.chapter1.__parent__).result(self.book)
        self.expect(self.chapter1.Title()).result('Chapter1')
        self.expect(self.chapter1.portal_type).result('Chapter')
        self.chapter2 = self.create_dummy()
        directlyProvides(self.chapter2, IFolderish)
        self.chapter2 = self.mocker.proxy(
            self.chapter2, spec=False, count=False)
        self.expect(self.chapter2.__parent__).result(self.book)
        self.expect(self.chapter2.Title()).result('Chapter2')
        self.expect(self.chapter2.portal_type).result('Chapter')

        #  Childs of chapter 2
        self.chapter3 = self.create_dummy()
        directlyProvides(self.chapter3, IFolderish)
        self.chapter3 = self.mocker.proxy(
            self.chapter3, spec=False, count=False)
        self.expect(self.chapter3.__parent__).result(self.chapter2)
        self.expect(self.chapter3.Title()).result('Chapter3')
        self.expect(self.chapter3.portal_type).result('Chapter')
        self.chapter4 = self.create_dummy()
        directlyProvides(self.chapter4, IFolderish)
        self.chapter4 = self.mocker.proxy(
            self.chapter4, spec=False, count=False)
        self.expect(self.chapter4.__parent__).result(self.chapter2)
        self.expect(self.chapter4.Title()).result('Chapter4')
        self.expect(self.chapter4.portal_type).result('Chapter')

        #  Childs of chapter 3
        self.chapter5 = self.create_dummy()
        directlyProvides(self.chapter5, IFolderish)
        self.chapter5 = self.mocker.proxy(
            self.chapter5, spec=False, count=False)
        self.expect(self.chapter5.__parent__).result(self.chapter3)
        self.expect(self.chapter5.Title()).result('Chapter5')
        self.expect(self.chapter5.portal_type).result('Chapter')
        self.image = self.mocker.mock(count=False)
        self.expect(self.image.__parent__).result(self.chapter3)
        self.expect(self.image.Title()).result('Image')
        self.expect(self.image.portal_type).result('Image')
        self.paragraph1 = self.mocker.mock(count=False)
        self.expect(self.paragraph1.__parent__).result(self.chapter3)
        self.expect(self.paragraph1.showTitle).result(True)
        self.expect(self.paragraph1.Title()).result('Paragraph1')
        self.expect(self.paragraph1.portal_type).result('Paragraph')
        self.paragraph2 = self.mocker.mock(count=False)
        self.expect(self.paragraph2.__parent__).result(self.chapter3)
        self.expect(self.paragraph2.__inner__).result(self.paragraph2)
        self.expect(self.paragraph2.showTitle).result(True)
        self.expect(self.paragraph2.Title()).result('Paragraph2')
        self.expect(self.paragraph2.title_or_id()).result('Paragraph2')
        self.expect(self.paragraph2.portal_type).result('Paragraph')

        # Folderish content
        self.expect(self.book.contentValues()).result(
            [self.chapter1, self.chapter2])
        self.expect(self.chapter1.contentValues()).result([])
        self.expect(self.chapter2.contentValues()).result(
            [self.chapter3, self.chapter4])
        self.expect(self.chapter3.contentValues()).result(
            [self.chapter5, self.image, self.paragraph1, self.paragraph2])
        self.expect(self.chapter4.contentValues()).result([])
        self.expect(self.chapter5.contentValues()).result([])
class TestUnitBookHelper(MockTestCase):
    """ Unittests for helper methods in BookHelper Class
    """

    def setUp(self):
        """ Setup test environment

        book
        |
        --- chapter1
        |
        --- chapter2
            |
            ---chapter3
            |  |
            |  ---chapter5
            |  |
            |  ---image
            |  |
            |  ---textblock1
            |  |
            |  ---textblock2
            |
            ---chapter4
        """
        super(TestUnitBookHelper, self).setUp()

        self._injection = {}

        # Helper object
        self.helper = BookHelper()

        #  Siteroot
        self.root = self.create_dummy()
        directlyProvides(self.root, IPloneSiteRoot)

        #  Book
        self.book = self.create_dummy()
        directlyProvides(self.book, [IBook, IFolderish])
        self.book = self.mocker.proxy(self.book, spec=False, count=False)
        self.expect(self.book.__parent__).result(self.root)
        self.expect(self.book.Title()).result('Book')
        self.mock_inject_settings(self.book)

        # Children of book
        self.chapter1 = self.create_dummy()
        directlyProvides(self.chapter1, IFolderish)
        self.chapter1 = self.mocker.proxy(
            self.chapter1, spec=False, count=False)
        self.expect(self.chapter1.__parent__).result(self.book)
        self.expect(self.chapter1.Title()).result('Chapter1')
        self.expect(self.chapter1.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter1)

        self.chapter2 = self.create_dummy()
        directlyProvides(self.chapter2, IFolderish)
        self.chapter2 = self.mocker.proxy(
            self.chapter2, spec=False, count=False)
        self.expect(self.chapter2.__parent__).result(self.book)
        self.expect(self.chapter2.Title()).result('Chapter2')
        self.expect(self.chapter2.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter2)

        #  Children of chapter 2
        self.chapter3 = self.create_dummy()
        directlyProvides(self.chapter3, IFolderish)
        self.chapter3 = self.mocker.proxy(
            self.chapter3, spec=False, count=False)
        self.expect(self.chapter3.__parent__).result(self.chapter2)
        self.expect(self.chapter3.Title()).result('Chapter3')
        self.expect(self.chapter3.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter3)

        self.chapter4 = self.create_dummy()
        directlyProvides(self.chapter4, IFolderish)
        self.chapter4 = self.mocker.proxy(
            self.chapter4, spec=False, count=False)
        self.expect(self.chapter4.__parent__).result(self.chapter2)
        self.expect(self.chapter4.Title()).result('Chapter4')
        self.expect(self.chapter4.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter4)

        #  Children of chapter 3
        self.chapter5 = self.create_dummy()
        directlyProvides(self.chapter5, IFolderish)
        self.chapter5 = self.mocker.proxy(
            self.chapter5, spec=False, count=False)
        self.expect(self.chapter5.__parent__).result(self.chapter3)
        self.expect(self.chapter5.Title()).result('Chapter5')
        self.expect(self.chapter5.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter5)

        self.image = self.mocker.mock(count=False)
        self.expect(self.image.__parent__).result(self.chapter3)
        self.expect(self.image.Title()).result('Image')
        self.expect(self.image.portal_type).result('Image')
        self.mock_inject_settings(self.image)

        self.textblock1 = self.mocker.mock(count=False)
        self.expect(self.textblock1.__parent__).result(self.chapter3)
        self.expect(self.textblock1.showTitle).result(True)
        self.expect(self.textblock1.Title()).result('Textblock1')
        self.expect(self.textblock1.title_or_id()).result('Textblock1')
        self.expect(self.textblock1.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock1, hideFromTOC=False)

        self.textblock2 = self.mocker.mock(count=False)
        self.expect(self.textblock2.__parent__).result(self.chapter3)
        self.expect(self.textblock2.__inner__).result(self.textblock2)
        self.expect(self.textblock2.showTitle).result(True)
        self.expect(self.textblock2.Title()).result('Textblock2')
        self.expect(self.textblock2.title_or_id()).result('Textblock2')
        self.expect(self.textblock2.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock2, hideFromTOC=False)

        # Folderish content
        self.expect(self.book.contentValues()).result(
            [self.chapter1, self.chapter2])
        self.expect(self.chapter1.contentValues()).result([])
        self.expect(self.chapter2.contentValues()).result(
            [self.chapter3, self.chapter4])
        self.expect(self.chapter3.contentValues()).result(
            [self.chapter5, self.image, self.textblock1, self.textblock2])
        self.expect(self.chapter4.contentValues()).result([])
        self.expect(self.chapter5.contentValues()).result([])

    def mock_inject_settings(self, mock, **settings):
        if mock not in self._injection:
            cache = self._injection[mock] = {'schema': self.stub()}
            schema = cache['schema']
            self.expect(mock.Schema()).result(schema).count(0, None)

            self.expect(schema.getField(ANY)).call(
                lambda name: cache.get(name, None))

        else:
            cache = self._injection[mock]

        for name, value in settings.items():
            field = self.stub()
            cache[name] = field
            self.expect(field.get(mock)).result(value)

    def test_generate_title(self):
        """ Test generate title
        """
        self.replay()

        self.assertEqual(self.helper(self.textblock1),
                         '<h4>2.1.2 Textblock1</h4>')
        self.assertEqual(self.helper(self.textblock2),
                         '<h4>2.1.3 Textblock2</h4>')

    def test_generate_title_respects_hideFromTOC(self):
        """ Test generate title
        """
        self.mock_inject_settings(self.textblock1, hideFromTOC=True)
        self.replay()

        # hideFromTOC is True
        self.assertEqual(self.helper(self.textblock1),
                         '<h4>Textblock1</h4>')
        self.assertEqual(self.helper(self.textblock2),
                         '<h4>2.1.2 Textblock2</h4>')

    def test_generated_title_result_is_consistent(self):
        self.mock_inject_settings(self.textblock1, hideFromTOC=True)
        self.replay()

        self.assertTrue(self.textblock1.Schema().getField(
                'hideFromTOC').get(self.textblock1))

        self.assertTrue(self.textblock1.Schema().getField(
                'hideFromTOC').get(self.textblock1))

        self.assertTrue(self.textblock1.Schema().getField(
                'hideFromTOC').get(self.textblock1))

        self.assertEqual(BookHelper()(self.textblock1),
                         '<h4>Textblock1</h4>')

        self.assertEqual(BookHelper()(self.textblock1),
                         '<h4>Textblock1</h4>')


    def test_generate_valid_hierarchy_h_tags(self):
        """ Test generate valid hierarchy h tags
        """
        self.replay()
        tags = self.helper.generate_valid_hierarchy_h_tags(self.chapter3)
        self.assertTrue(
            tags == '<h1>Book</h1>' +
                    '<h2>Chapter2</h2>' +
                    '<h3>Chapter3</h3>')

    def test_get_folder_position(self):
        """ Test get folder position
        """
        self.replay()
        position = self.helper.get_folder_position(self.textblock2)
        self.assertTrue(position == 3)

    def test_get_hierarchy_position(self):
        """ Test hierarchy position
        """
        self.replay()
        position = self.helper.get_hierarchy_position(self.textblock2)
        self.assertTrue(position == 4)
    def setUp(self):
        """ Setup test environment

        book
        |
        --- chapter1
        |
        --- chapter2
            |
            ---chapter3
            |  |
            |  ---chapter5
            |  |
            |  ---image
            |  |
            |  ---textblock1
            |  |
            |  ---textblock2
            |
            ---chapter4
        """
        super(TestUnitBookHelper, self).setUp()

        self._injection = {}

        # Helper object
        self.helper = BookHelper()

        #  Siteroot
        self.root = self.create_dummy()
        directlyProvides(self.root, IPloneSiteRoot)

        #  Book
        self.book = self.create_dummy()
        directlyProvides(self.book, [IBook, IFolderish])
        self.book = self.mocker.proxy(self.book, spec=False, count=False)
        self.expect(self.book.__parent__).result(self.root)
        self.expect(self.book.Title()).result('Book')
        self.mock_inject_settings(self.book)

        # Children of book
        self.chapter1 = self.create_dummy()
        directlyProvides(self.chapter1, IFolderish)
        self.chapter1 = self.mocker.proxy(
            self.chapter1, spec=False, count=False)
        self.expect(self.chapter1.__parent__).result(self.book)
        self.expect(self.chapter1.Title()).result('Chapter1')
        self.expect(self.chapter1.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter1)

        self.chapter2 = self.create_dummy()
        directlyProvides(self.chapter2, IFolderish)
        self.chapter2 = self.mocker.proxy(
            self.chapter2, spec=False, count=False)
        self.expect(self.chapter2.__parent__).result(self.book)
        self.expect(self.chapter2.Title()).result('Chapter2')
        self.expect(self.chapter2.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter2)

        #  Children of chapter 2
        self.chapter3 = self.create_dummy()
        directlyProvides(self.chapter3, IFolderish)
        self.chapter3 = self.mocker.proxy(
            self.chapter3, spec=False, count=False)
        self.expect(self.chapter3.__parent__).result(self.chapter2)
        self.expect(self.chapter3.Title()).result('Chapter3')
        self.expect(self.chapter3.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter3)

        self.chapter4 = self.create_dummy()
        directlyProvides(self.chapter4, IFolderish)
        self.chapter4 = self.mocker.proxy(
            self.chapter4, spec=False, count=False)
        self.expect(self.chapter4.__parent__).result(self.chapter2)
        self.expect(self.chapter4.Title()).result('Chapter4')
        self.expect(self.chapter4.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter4)

        #  Children of chapter 3
        self.chapter5 = self.create_dummy()
        directlyProvides(self.chapter5, IFolderish)
        self.chapter5 = self.mocker.proxy(
            self.chapter5, spec=False, count=False)
        self.expect(self.chapter5.__parent__).result(self.chapter3)
        self.expect(self.chapter5.Title()).result('Chapter5')
        self.expect(self.chapter5.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter5)

        self.image = self.mocker.mock(count=False)
        self.expect(self.image.__parent__).result(self.chapter3)
        self.expect(self.image.Title()).result('Image')
        self.expect(self.image.portal_type).result('Image')
        self.mock_inject_settings(self.image)

        self.textblock1 = self.mocker.mock(count=False)
        self.expect(self.textblock1.__parent__).result(self.chapter3)
        self.expect(self.textblock1.showTitle).result(True)
        self.expect(self.textblock1.Title()).result('Textblock1')
        self.expect(self.textblock1.title_or_id()).result('Textblock1')
        self.expect(self.textblock1.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock1, hideFromTOC=False)

        self.textblock2 = self.mocker.mock(count=False)
        self.expect(self.textblock2.__parent__).result(self.chapter3)
        self.expect(self.textblock2.__inner__).result(self.textblock2)
        self.expect(self.textblock2.showTitle).result(True)
        self.expect(self.textblock2.Title()).result('Textblock2')
        self.expect(self.textblock2.title_or_id()).result('Textblock2')
        self.expect(self.textblock2.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock2, hideFromTOC=False)

        # Folderish content
        self.expect(self.book.contentValues()).result(
            [self.chapter1, self.chapter2])
        self.expect(self.chapter1.contentValues()).result([])
        self.expect(self.chapter2.contentValues()).result(
            [self.chapter3, self.chapter4])
        self.expect(self.chapter3.contentValues()).result(
            [self.chapter5, self.image, self.textblock1, self.textblock2])
        self.expect(self.chapter4.contentValues()).result([])
        self.expect(self.chapter5.contentValues()).result([])
class TestUnitBookHelper(MockTestCase):
    """ Unittests for helper methods in BookHelper Class
    """
    def setUp(self):
        """ Setup test environment

        book
        |
        --- chapter1
        |
        --- chapter2
            |
            ---chapter3
            |  |
            |  ---chapter5
            |  |
            |  ---image
            |  |
            |  ---textblock1
            |  |
            |  ---textblock2
            |
            ---chapter4
        """
        super(TestUnitBookHelper, self).setUp()

        self._injection = {}

        # Helper object
        self.helper = BookHelper()

        #  Siteroot
        self.root = self.create_dummy()
        directlyProvides(self.root, IPloneSiteRoot)

        #  Book
        self.book = self.create_dummy()
        directlyProvides(self.book, [IBook, IFolderish])
        self.book = self.mocker.proxy(self.book, spec=False, count=False)
        self.expect(self.book.__parent__).result(self.root)
        self.expect(self.book.Title()).result('Book')
        self.mock_inject_settings(self.book)

        # Children of book
        self.chapter1 = self.create_dummy()
        directlyProvides(self.chapter1, IFolderish)
        self.chapter1 = self.mocker.proxy(self.chapter1,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter1.__parent__).result(self.book)
        self.expect(self.chapter1.Title()).result('Chapter1')
        self.expect(self.chapter1.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter1)

        self.chapter2 = self.create_dummy()
        directlyProvides(self.chapter2, IFolderish)
        self.chapter2 = self.mocker.proxy(self.chapter2,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter2.__parent__).result(self.book)
        self.expect(self.chapter2.Title()).result('Chapter2')
        self.expect(self.chapter2.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter2)

        #  Children of chapter 2
        self.chapter3 = self.create_dummy()
        directlyProvides(self.chapter3, IFolderish)
        self.chapter3 = self.mocker.proxy(self.chapter3,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter3.__parent__).result(self.chapter2)
        self.expect(self.chapter3.Title()).result('Chapter3')
        self.expect(self.chapter3.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter3)

        self.chapter4 = self.create_dummy()
        directlyProvides(self.chapter4, IFolderish)
        self.chapter4 = self.mocker.proxy(self.chapter4,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter4.__parent__).result(self.chapter2)
        self.expect(self.chapter4.Title()).result('Chapter4')
        self.expect(self.chapter4.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter4)

        #  Children of chapter 3
        self.chapter5 = self.create_dummy()
        directlyProvides(self.chapter5, IFolderish)
        self.chapter5 = self.mocker.proxy(self.chapter5,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter5.__parent__).result(self.chapter3)
        self.expect(self.chapter5.Title()).result('Chapter5')
        self.expect(self.chapter5.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter5)

        self.image = self.mocker.mock(count=False)
        self.expect(self.image.__parent__).result(self.chapter3)
        self.expect(self.image.Title()).result('Image')
        self.expect(self.image.portal_type).result('Image')
        self.mock_inject_settings(self.image)

        self.textblock1 = self.mocker.mock(count=False)
        self.expect(self.textblock1.__parent__).result(self.chapter3)
        self.expect(self.textblock1.showTitle).result(True)
        self.expect(self.textblock1.Title()).result('Textblock1')
        self.expect(self.textblock1.title_or_id()).result('Textblock1')
        self.expect(self.textblock1.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock1, hideFromTOC=False)

        self.textblock2 = self.mocker.mock(count=False)
        self.expect(self.textblock2.__parent__).result(self.chapter3)
        self.expect(self.textblock2.__inner__).result(self.textblock2)
        self.expect(self.textblock2.showTitle).result(True)
        self.expect(self.textblock2.Title()).result('Textblock2')
        self.expect(self.textblock2.title_or_id()).result('Textblock2')
        self.expect(self.textblock2.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock2, hideFromTOC=False)

        # Folderish content
        self.expect(self.book.contentValues()).result(
            [self.chapter1, self.chapter2])
        self.expect(self.chapter1.contentValues()).result([])
        self.expect(self.chapter2.contentValues()).result(
            [self.chapter3, self.chapter4])
        self.expect(self.chapter3.contentValues()).result(
            [self.chapter5, self.image, self.textblock1, self.textblock2])
        self.expect(self.chapter4.contentValues()).result([])
        self.expect(self.chapter5.contentValues()).result([])

    def mock_inject_settings(self, mock, **settings):
        if mock not in self._injection:
            cache = self._injection[mock] = {'schema': self.stub()}
            schema = cache['schema']
            self.expect(mock.Schema()).result(schema).count(0, None)

            self.expect(
                schema.getField(ANY)).call(lambda name: cache.get(name, None))

        else:
            cache = self._injection[mock]

        for name, value in settings.items():
            field = self.stub()
            cache[name] = field
            self.expect(field.get(mock)).result(value)

    def test_generate_title(self):
        """ Test generate title
        """
        self.replay()

        self.assertEqual(self.helper(self.textblock1),
                         '<h4>2.1.2 Textblock1</h4>')
        self.assertEqual(self.helper(self.textblock2),
                         '<h4>2.1.3 Textblock2</h4>')

    def test_generate_title_respects_hideFromTOC(self):
        """ Test generate title
        """
        self.mock_inject_settings(self.textblock1, hideFromTOC=True)
        self.replay()

        # hideFromTOC is True
        self.assertEqual(self.helper(self.textblock1), '<h4>Textblock1</h4>')
        self.assertEqual(self.helper(self.textblock2),
                         '<h4>2.1.2 Textblock2</h4>')

    def test_generated_title_result_is_consistent(self):
        self.mock_inject_settings(self.textblock1, hideFromTOC=True)
        self.replay()

        self.assertTrue(self.textblock1.Schema().getField('hideFromTOC').get(
            self.textblock1))

        self.assertTrue(self.textblock1.Schema().getField('hideFromTOC').get(
            self.textblock1))

        self.assertTrue(self.textblock1.Schema().getField('hideFromTOC').get(
            self.textblock1))

        self.assertEqual(BookHelper()(self.textblock1), '<h4>Textblock1</h4>')

        self.assertEqual(BookHelper()(self.textblock1), '<h4>Textblock1</h4>')

    def test_generate_valid_hierarchy_h_tags(self):
        """ Test generate valid hierarchy h tags
        """
        self.replay()
        tags = self.helper.generate_valid_hierarchy_h_tags(self.chapter3)
        self.assertTrue(tags == '<h1>Book</h1>' + '<h2>Chapter2</h2>' +
                        '<h3>Chapter3</h3>')

    def test_get_folder_position(self):
        """ Test get folder position
        """
        self.replay()
        position = self.helper.get_folder_position(self.textblock2)
        self.assertTrue(position == 3)

    def test_get_hierarchy_position(self):
        """ Test hierarchy position
        """
        self.replay()
        position = self.helper.get_hierarchy_position(self.textblock2)
        self.assertTrue(position == 4)
    def setUp(self):
        """ Setup test environment

        book
        |
        --- chapter1
        |
        --- chapter2
            |
            ---chapter3
            |  |
            |  ---chapter5
            |  |
            |  ---image
            |  |
            |  ---textblock1
            |  |
            |  ---textblock2
            |
            ---chapter4
        """
        super(TestUnitBookHelper, self).setUp()

        self._injection = {}

        # Helper object
        self.helper = BookHelper()

        #  Siteroot
        self.root = self.create_dummy()
        directlyProvides(self.root, IPloneSiteRoot)

        #  Book
        self.book = self.create_dummy()
        directlyProvides(self.book, [IBook, IFolderish])
        self.book = self.mocker.proxy(self.book, spec=False, count=False)
        self.expect(self.book.__parent__).result(self.root)
        self.expect(self.book.Title()).result('Book')
        self.mock_inject_settings(self.book)

        # Children of book
        self.chapter1 = self.create_dummy()
        directlyProvides(self.chapter1, IFolderish)
        self.chapter1 = self.mocker.proxy(self.chapter1,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter1.__parent__).result(self.book)
        self.expect(self.chapter1.Title()).result('Chapter1')
        self.expect(self.chapter1.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter1)

        self.chapter2 = self.create_dummy()
        directlyProvides(self.chapter2, IFolderish)
        self.chapter2 = self.mocker.proxy(self.chapter2,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter2.__parent__).result(self.book)
        self.expect(self.chapter2.Title()).result('Chapter2')
        self.expect(self.chapter2.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter2)

        #  Children of chapter 2
        self.chapter3 = self.create_dummy()
        directlyProvides(self.chapter3, IFolderish)
        self.chapter3 = self.mocker.proxy(self.chapter3,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter3.__parent__).result(self.chapter2)
        self.expect(self.chapter3.Title()).result('Chapter3')
        self.expect(self.chapter3.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter3)

        self.chapter4 = self.create_dummy()
        directlyProvides(self.chapter4, IFolderish)
        self.chapter4 = self.mocker.proxy(self.chapter4,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter4.__parent__).result(self.chapter2)
        self.expect(self.chapter4.Title()).result('Chapter4')
        self.expect(self.chapter4.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter4)

        #  Children of chapter 3
        self.chapter5 = self.create_dummy()
        directlyProvides(self.chapter5, IFolderish)
        self.chapter5 = self.mocker.proxy(self.chapter5,
                                          spec=False,
                                          count=False)
        self.expect(self.chapter5.__parent__).result(self.chapter3)
        self.expect(self.chapter5.Title()).result('Chapter5')
        self.expect(self.chapter5.portal_type).result('Chapter')
        self.mock_inject_settings(self.chapter5)

        self.image = self.mocker.mock(count=False)
        self.expect(self.image.__parent__).result(self.chapter3)
        self.expect(self.image.Title()).result('Image')
        self.expect(self.image.portal_type).result('Image')
        self.mock_inject_settings(self.image)

        self.textblock1 = self.mocker.mock(count=False)
        self.expect(self.textblock1.__parent__).result(self.chapter3)
        self.expect(self.textblock1.showTitle).result(True)
        self.expect(self.textblock1.Title()).result('Textblock1')
        self.expect(self.textblock1.title_or_id()).result('Textblock1')
        self.expect(self.textblock1.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock1, hideFromTOC=False)

        self.textblock2 = self.mocker.mock(count=False)
        self.expect(self.textblock2.__parent__).result(self.chapter3)
        self.expect(self.textblock2.__inner__).result(self.textblock2)
        self.expect(self.textblock2.showTitle).result(True)
        self.expect(self.textblock2.Title()).result('Textblock2')
        self.expect(self.textblock2.title_or_id()).result('Textblock2')
        self.expect(self.textblock2.portal_type).result('Textblock')
        self.mock_inject_settings(self.textblock2, hideFromTOC=False)

        # Folderish content
        self.expect(self.book.contentValues()).result(
            [self.chapter1, self.chapter2])
        self.expect(self.chapter1.contentValues()).result([])
        self.expect(self.chapter2.contentValues()).result(
            [self.chapter3, self.chapter4])
        self.expect(self.chapter3.contentValues()).result(
            [self.chapter5, self.image, self.textblock1, self.textblock2])
        self.expect(self.chapter4.contentValues()).result([])
        self.expect(self.chapter5.contentValues()).result([])