/* This file is part of the KDE project SPDX-FileCopyrightText: 2022 Dag Andersen SPDX-License-Identifier: LGPL-2.0-or-later */ // clazy:excludeall=qstring-arg #include "SharedResourcesTester.h" #include #include #include #include #include #include #include #include #include #include #include using namespace KPlato; void SharedResources::init() { part = nullptr; } void SharedResources::cleanup() { delete part; part = nullptr; } QString resourceData_1() { return QStringLiteral( "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; ">&nbsp;</p></body></html>\" priority=\"0\" start-time=\"2021-10-06T00:00:00+02:00\">" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ); } QString projectData_1() { return QStringLiteral( "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; ">&nbsp;</p></body></html>\" priority=\"0\" start-time=\"2021-10-06T00:00:00+02:00\">" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>\" priority=\"0\" wbs=\"1\">" "" "" "" "" "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>\" priority=\"0\" wbs=\"1.1\">" "" "" "" "" "" "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>\" priority=\"0\" wbs=\"1.2\">" "" "" "" "" "" "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>\" priority=\"0\" wbs=\"1.3\">" "" "" "" "" "" "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>\" priority=\"0\" wbs=\"1.4\">" "" "" "" "" "" "" "" "" " <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>\" priority=\"0\" wbs=\"1.5\">" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ); } MainDocument *loadXml(const QString &xml) { Part *part = new Part(nullptr); auto doc = part->createDocument(part); KoXmlDocument xmlDoc; xmlDoc.setContent(xml); doc->loadXML(xmlDoc, nullptr); return qobject_cast(doc); } void checkSharedResources(const Project *sharedProject) { QCOMPARE(sharedProject->calendarCount(), 1); QCOMPARE(sharedProject->resourceGroupCount(), 2); QCOMPARE(sharedProject->resourceCount(), 6); const auto G1 = sharedProject->resourceGroups().value(0); const auto G2 = sharedProject->resourceGroups().value(1); QCOMPARE(G1->resources().count(), 4); QCOMPARE(G1->resources().value(0), sharedProject->resourceAt(0)); QCOMPARE(G1->resources().value(1), sharedProject->resourceAt(1)); QCOMPARE(G1->resources().value(2), sharedProject->resourceAt(2)); QCOMPARE(G1->resources().value(3), sharedProject->resourceAt(3)); QCOMPARE(G2->resources().value(0), sharedProject->resourceAt(4)); QCOMPARE(G2->resources().value(1), sharedProject->resourceAt(5)); } void SharedResources::testEmpty() { auto sharedDoc = loadXml(resourceData_1()); QVERIFY(sharedDoc); auto sharedProject = sharedDoc->project(); checkSharedResources(sharedProject); QCOMPARE(sharedProject->calendarCount(), 1); QCOMPARE(sharedProject->resourceGroupCount(), 2); QCOMPARE(sharedProject->resourceCount(), 6); Part part(nullptr); auto doc = qobject_cast(part.createDocument(&part)); part.setDocument(doc); doc->setProperty(NOUI, true); // doc->setProperty(DEFAULTSHAREDRESOURCESRESULT, 2 /*keep*/); QVERIFY(static_cast(doc)->mergeResources(*sharedProject)); Debug::print(doc->project(), "", true); QCOMPARE(doc->project()->calendarCount(), 1); QCOMPARE(doc->project()->resourceGroupCount(), 2); QCOMPARE(doc->project()->resourceCount(), 6); delete sharedDoc; } void SharedResources::testRemoveResource() { auto sharedDoc = loadXml(resourceData_1()); QVERIFY(sharedDoc); auto sharedProject = sharedDoc->project(); checkSharedResources(sharedProject); Part part(nullptr); auto doc = qobject_cast(part.createDocument(&part)); part.setDocument(doc); QVERIFY(doc->mergeResources(*sharedProject)); QCOMPARE(doc->project()->calendarCount(), 1); QCOMPARE(doc->project()->resourceGroupCount(), 2); QCOMPARE(doc->project()->resourceCount(), 6); delete sharedDoc; sharedDoc = loadXml(resourceData_1()); sharedProject = sharedDoc->project(); RemoveResourceCmd cmd(sharedProject->resourceList().value(0)); cmd.redo(); QCOMPARE(sharedProject->resourceCount(), 5); doc->setProperty(NOUI, true); doc->setProperty(SHAREDRESOURCESACTION, SHAREDRESOURCESREMOVE); auto project = doc->project(); doc->mergeResources(*sharedProject); QCOMPARE(project->calendarCount(), 1); QCOMPARE(project->resourceGroupCount(), 2); QCOMPARE(project->resourceCount(), 5); const auto G1 = project->resourceGroups().value(0); const auto G2 = project->resourceGroups().value(1); QCOMPARE(G1->resources().count(), 3); QStringList names = {QStringLiteral("R2"),QStringLiteral("R3"),QStringLiteral("Team 1")}; for (const auto r : G1->resources()) { QVERIFY(names.contains(r->name())); } QStringList names2 = {QStringLiteral("M1"), QStringLiteral("M2")}; for (const auto r : G2->resources()) { QVERIFY(names2.contains(r->name())); } delete sharedDoc; } void SharedResources::testConvertResource() { auto sharedDoc = loadXml(resourceData_1()); QVERIFY(sharedDoc); auto sharedProject = sharedDoc->project(); checkSharedResources(sharedProject); Part part(nullptr); auto doc = qobject_cast(part.createDocument(&part)); part.setDocument(doc); QVERIFY(doc->mergeResources(*sharedProject)); QCOMPARE(doc->project()->calendarCount(), 1); QCOMPARE(doc->project()->resourceGroupCount(), 2); QCOMPARE(doc->project()->resourceCount(), 6); delete sharedDoc; sharedDoc = loadXml(resourceData_1()); sharedProject = sharedDoc->project(); RemoveResourceCmd cmd(sharedProject->resourceList().value(0)); cmd.redo(); QCOMPARE(sharedProject->resourceCount(), 5); doc->setProperty(NOUI, true); doc->setProperty(SHAREDRESOURCESACTION, SHAREDRESOURCESCONVERT); auto project = doc->project(); doc->mergeResources(*sharedProject); QCOMPARE(project->calendarCount(), 1); QCOMPARE(project->resourceGroupCount(), 2); QCOMPARE(project->resourceCount(), 6); //Debug::print(project, "", true); QVERIFY(!project->resourceList().value(0)->isShared()); const auto G1 = project->resourceGroups().value(0); const auto G2 = project->resourceGroups().value(1); QCOMPARE(G1->resources().count(), 4); QStringList names = {QStringLiteral("R1"), QStringLiteral("R2"),QStringLiteral("R3"),QStringLiteral("Team 1")}; for (const auto r : G1->resources()) { QVERIFY(names.contains(r->name())); } QStringList names2 = {QStringLiteral("M1"), QStringLiteral("M2")}; for (const auto r : G2->resources()) { QVERIFY(names2.contains(r->name())); } delete sharedDoc; } QTEST_MAIN(SharedResources)