Merge pull request #1 from Logout22/implement-resolving-for-references-placeholders

Implement resolving for references placeholders
This commit is contained in:
Serhii Moroz 2017-11-12 21:25:03 +02:00 committed by frostasm
commit 5c7d34677d
4 changed files with 86 additions and 1 deletions

View File

@ -838,6 +838,8 @@ QString Entry::referenceFieldValue(EntryReferenceType referenceType) const
return url();
case EntryReferenceType::Notes:
return notes();
case EntryReferenceType::Uuid:
return uuid().toHex();
default:
break;
}

View File

@ -34,7 +34,7 @@ const QString EntryAttributes::RememberCmdExecAttr = "_EXEC_CMD";
EntryAttributes::EntryAttributes(QObject* parent)
: QObject(parent)
, m_referenceRegExp("\\{REF:(?<WantedField>[TUPAN])@(?<SearchIn>[TUPANIO]):(?<SearchText>[^}]+)\\}",
, m_referenceRegExp("\\{REF:(?<WantedField>[TUPANI])@(?<SearchIn>[TUPANIO]):(?<SearchText>[^}]+)\\}",
QRegularExpression::CaseInsensitiveOption)
{
clear();

View File

@ -344,3 +344,85 @@ void TestEntry::testResolveReferencePlaceholders()
QCOMPARE(tstEntry->resolveMultiplePlaceholders(QString("{REF:A@I:%1}").arg(entry3->uuid().toHex())), QString("UrlValue"));
QCOMPARE(tstEntry->resolveMultiplePlaceholders(QString("{REF:N@I:%1}").arg(entry3->uuid().toHex())), QString("NotesValue"));
}
static const char placeholders[] = {
'T',
'U',
'P',
'A',
'N',
};
void TestEntry::testResolveNonIdPlaceholdersToUuid()
{
Database db;
auto* root = db.rootGroup();
Entry referencedEntryTitle;
referencedEntryTitle.setGroup(root);
referencedEntryTitle.setTitle("myTitle");
referencedEntryTitle.setUuid(Uuid::random());
Entry referencedEntryUsername;
referencedEntryUsername.setGroup(root);
referencedEntryUsername.setUsername("myUser");
referencedEntryUsername.setUuid(Uuid::random());
Entry referencedEntryPassword;
referencedEntryPassword.setGroup(root);
referencedEntryPassword.setPassword("myPassword");
referencedEntryPassword.setUuid(Uuid::random());
Entry referencedEntryUrl;
referencedEntryUrl.setGroup(root);
referencedEntryUrl.setUrl("myUrl");
referencedEntryUrl.setUuid(Uuid::random());
Entry referencedEntryNotes;
referencedEntryNotes.setGroup(root);
referencedEntryNotes.setNotes("myNotes");
referencedEntryNotes.setUuid(Uuid::random());
for (const auto searchIn : placeholders) {
const Entry* referencedEntry = nullptr;
QString newEntryNotesRaw("{REF:I@%1:%2}");
switch(searchIn) {
case 'T':
referencedEntry = &referencedEntryTitle;
newEntryNotesRaw = newEntryNotesRaw.arg(
QString(searchIn), referencedEntry->title());
break;
case 'U':
referencedEntry = &referencedEntryUsername;
newEntryNotesRaw = newEntryNotesRaw.arg(
QString(searchIn), referencedEntry->username());
break;
case 'P':
referencedEntry = &referencedEntryPassword;
newEntryNotesRaw = newEntryNotesRaw.arg(
QString(searchIn), referencedEntry->password());
break;
case 'A':
referencedEntry = &referencedEntryUrl;
newEntryNotesRaw = newEntryNotesRaw.arg(
QString(searchIn), referencedEntry->url());
break;
case 'N':
referencedEntry = &referencedEntryNotes;
newEntryNotesRaw = newEntryNotesRaw.arg(
QString(searchIn), referencedEntry->notes());
break;
default:
break;
}
Entry newEntry;
newEntry.setGroup(root);
newEntry.setNotes(newEntryNotesRaw);
const auto newEntryNotesResolved =
newEntry.resolveMultiplePlaceholders(newEntry.notes());
QCOMPARE(newEntryNotesResolved, QString(referencedEntry->uuid().toHex()));
}
}

View File

@ -35,6 +35,7 @@ private slots:
void testResolveUrlPlaceholders();
void testResolveRecursivePlaceholders();
void testResolveReferencePlaceholders();
void testResolveNonIdPlaceholdersToUuid();
};
#endif // KEEPASSX_TESTENTRY_H