tdf#161417 DOC export: handle endnotes at section ends

Similar to commit 566c7017a84e3d573de85a6d986b81d3f59de0fa (tdf#160984
sw continuous endnotes: DOCX: export of <w:endnotePr> pos == sectEnd,
2024-05-29), but this is for DOC, not DOCX.

Change-Id: I4301d97115ba48f7ed1dacfdd5b29336bdaf9e63
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168401
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
diff --git a/sw/qa/extras/ww8export/ww8export4.cxx b/sw/qa/extras/ww8export/ww8export4.cxx
index b52e2b8..187e182 100644
--- a/sw/qa/extras/ww8export/ww8export4.cxx
+++ b/sw/qa/extras/ww8export/ww8export4.cxx
@@ -36,6 +36,7 @@
#include <frmmgr.hxx>
#include <formatflysplit.hxx>
#include <fmtwrapinfluenceonobjpos.hxx>
#include <fmtftntx.hxx>

namespace
{
@@ -122,6 +123,36 @@ DECLARE_WW8EXPORT_TEST(testTdf141649_conditionalText, "tdf141649_conditionalText
    getParagraph(1, "trueResult");
}

CPPUNIT_TEST_FIXTURE(Test, testEndnotesAtSectEndDOC)
{
    // Given a document, endnotes at collected at section end:
    createSwDoc();
    {
        SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
        pWrtShell->SplitNode();
        pWrtShell->Up(/*bSelect=*/false);
        pWrtShell->Insert("x");
        pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
        SwSectionData aSection(SectionType::Content, pWrtShell->GetUniqueSectionName());
        pWrtShell->StartAction();
        SfxItemSetFixed<RES_FTN_AT_TXTEND, RES_FRAMEDIR> aSet(pWrtShell->GetAttrPool());
        aSet.Put(SwFormatEndAtTextEnd(FTNEND_ATTXTEND));
        pWrtShell->InsertSection(aSection, &aSet);
        pWrtShell->EndAction();
        pWrtShell->InsertFootnote(OUString(), /*bEndNote=*/true);
    }

    // When saving to DOC:
    saveAndReload("MS Word 97");

    // Then make sure the endnote position is section end:
    SwDoc* pDoc = getSwDoc();
    SwSectionFormats& rSections = pDoc->GetSections();
    SwSectionFormat* pFormat = rSections[0];
    // Without the accompanying fix in place, this test would have failed, endnotes were at doc end.
    CPPUNIT_ASSERT(pFormat->GetEndAtTextEnd().IsAtEnd());
}

DECLARE_WW8EXPORT_TEST(testTdf90408, "tdf90408.doc")
{
    uno::Reference<beans::XPropertySet> xRun(getRun(getParagraph(1), 1), uno::UNO_QUERY_THROW);
diff --git a/sw/source/filter/ww8/wrtww8.cxx b/sw/source/filter/ww8/wrtww8.cxx
index a144124..467a7c1 100644
--- a/sw/source/filter/ww8/wrtww8.cxx
+++ b/sw/source/filter/ww8/wrtww8.cxx
@@ -87,6 +87,7 @@
#include <fmtline.hxx>
#include <fmtfsize.hxx>
#include <formatflysplit.hxx>
#include <fmtftntx.hxx>
#include "sprmids.hxx"

#include <comphelper/sequenceashashmap.hxx>
@@ -493,6 +494,18 @@ static void WriteDop( WW8Export& rWrt )
    rDop.cParas = rDStat.nPara;
    rDop.cLines = rDStat.nPara;

    SwSectionFormats& rSections = rWrt.m_rDoc.GetSections();
    if (!rSections.empty())
    {
        SwSectionFormat* pFormat = rSections[0];
        bool bEndnAtEnd = pFormat->GetEndAtTextEnd().IsAtEnd();
        if (bEndnAtEnd)
        {
            // DopBase.epc: this is normally 3 (end of document), change this to end of section.
            rDop.epc = 0;
        }
    }

    SwDocShell *pDocShell(rWrt.m_rDoc.GetDocShell());
    OSL_ENSURE(pDocShell, "no SwDocShell");
    uno::Reference<document::XDocumentProperties> xDocProps;