Commit 4a969303 authored by Robby Stephenson's avatar Robby Stephenson
Browse files

Fix the CSV export to properly escape values with delimiter

An old commit  accidently removed the escapeText() call from one
of the text export sections. As a result, values that include the
delimiter (usually a comma) were not enclosed in double quotation marks.

Add a unit test, as well.

BUG: 317473
FIXED-IN: 2.3.8
parent 420153d5
2013-03-27 Robby Stephenson <robby@periapsis.org>
* Fixed bug with not properly escaping text in CSV exporter (Bug 317473).
2013-03-24 Robby Stephenson <robby@periapsis.org>
* Updated GiantBombFetcher for XML changes in responses.
......
......@@ -19,9 +19,6 @@ TARGET_LINK_LIBRARIES(lccntest utils ${QT_AND_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} $
KDE4_ADD_UNIT_TEST(lcctest NOGUI lcctest.cpp ../field.cpp ../fieldformat.cpp ../tellico_utils.cpp)
TARGET_LINK_LIBRARIES(lcctest utils core ${QT_AND_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY} )
KDE4_ADD_UNIT_TEST(csvtest NOGUI csvtest.cpp ../translators/csvparser.cpp)
TARGET_LINK_LIBRARIES(csvtest csv ${QT_AND_KDECORE_LIBS} ${QT_QTCORE_LIBRARY} ${QT_QTTEST_LIBRARY} )
KDE4_ADD_UNIT_TEST(formattest NOGUI formattest.cpp ../fieldformat.cpp)
TARGET_LINK_LIBRARIES(formattest core ${QT_AND_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY} )
......@@ -96,6 +93,9 @@ TARGET_LINK_LIBRARIES(bibtexmltest ${TELLICO_TEST_LIBS})
KDE4_ADD_UNIT_TEST(ciwtest NOGUI ciwtest.cpp)
TARGET_LINK_LIBRARIES(ciwtest ${TELLICO_TEST_LIBS})
KDE4_ADD_UNIT_TEST(csvtest NOGUI csvtest.cpp)
TARGET_LINK_LIBRARIES(csvtest csv ${TELLICO_TEST_LIBS})
KDE4_ADD_UNIT_TEST(delicioustest NOGUI delicioustest.cpp)
TARGET_LINK_LIBRARIES(delicioustest ${TELLICO_TEST_LIBS})
......
......@@ -27,6 +27,7 @@
#include "qtest_kde.h"
#include "../translators/csvparser.h"
#include "../translators/csvexporter.h"
QTEST_MAIN( CsvTest )
......@@ -40,17 +41,14 @@ void CsvTest::cleanupTestCase() {
delete p;
}
void CsvTest::init() {
QFETCH(QString, delim);
p->setDelimiter(delim);
}
void CsvTest::testAll() {
QFETCH(QString, line);
QFETCH(int, pos1);
QFETCH(QString, token1);
QFETCH(int, pos2);
QFETCH(QString, token2);
QFETCH(QString, delim);
p->setDelimiter(delim);
p->reset(line);
QStringList tokens = p->nextTokens();
......@@ -76,3 +74,21 @@ void CsvTest::testAll_data() {
QTest::newRow("quotes") << "robby,\"stephenson,is,cool\"" << "," << 2 << 0 << "robby" << 1 << "stephenson,is,cool";
QTest::newRow("newline") << "robby,\"stephenson\n,is,cool\"" << "," << 2 << 0 << "robby" << 1 << "stephenson\n,is,cool";
}
void CsvTest::testEntry() {
Tellico::Data::CollPtr coll(new Tellico::Data::Collection(true));
Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll));
entry->setField(QLatin1String("title"), QLatin1String("title, with comma"));
coll->addEntries(entry);
Tellico::Export::CSVExporter exporter(coll);
exporter.setEntries(coll->entries());
exporter.setFields(Tellico::Data::FieldList() << coll->fieldByName(QLatin1String("title")));
QString output = exporter.text();
// the header line has the field titles, skip that
output = output.section(QLatin1Char('\n'), 1);
output.chop(1);
QCOMPARE(output, QLatin1String("\"title, with comma\""));
}
......@@ -38,9 +38,9 @@ private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void init();
void testAll();
void testAll_data();
void testEntry();
private:
Tellico::CSVParser* p;
......
......@@ -59,7 +59,7 @@ QString CSVExporter::fileFilter() const {
return i18n("*.csv|CSV Files (*.csv)") + QLatin1Char('\n') + i18n("*|All Files");
}
QString& CSVExporter::escapeText(QString& text_) {
QString& CSVExporter::escapeText(QString& text_) const {
bool quotes = false;
if(text_.contains(QLatin1Char('"'))) {
quotes = true;
......@@ -79,6 +79,10 @@ bool CSVExporter::exec() {
return false;
}
return FileHandler::writeTextURL(url(), text(), options() & ExportUTF8, options() & Export::ExportForce);
}
QString CSVExporter::text() const {
QString text;
if(m_includeTitles) {
......@@ -112,12 +116,11 @@ bool CSVExporter::exec() {
if(replaceRowDelimiter) {
value.replace(FieldFormat::rowDelimiterString(), m_rowDelimiter);
}
values += value;
values += escapeText(value);
}
text += values.join(m_delimiter) + QLatin1Char('\n');
}
return FileHandler::writeTextURL(url(), text, options() & ExportUTF8, options() & Export::ExportForce);
return text;
}
QWidget* CSVExporter::widget(QWidget* parent_) {
......
......@@ -48,13 +48,14 @@ public:
virtual bool exec();
virtual QString formatString() const;
virtual QString fileFilter() const;
QString text() const;
virtual QWidget* widget(QWidget* parent);
virtual void readOptions(KSharedConfigPtr config);
virtual void saveOptions(KSharedConfigPtr config);
private:
QString& escapeText(QString& text);
QString& escapeText(QString& text) const;
bool m_includeTitles;
QString m_delimiter;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment