Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12611 from jmcanterafonseca/fix_matcher_info
Browse files Browse the repository at this point in the history
Bug 923029 - [Contacts] Contacts matching library returning wrong results (r=gtorodelvalle)
  • Loading branch information
gtorodelvalle committed Oct 2, 2013
2 parents 73a64d3 + 541c5f6 commit d798ff8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
41 changes: 22 additions & 19 deletions apps/communications/contacts/js/contacts_matcher.js
Expand Up @@ -60,6 +60,18 @@ contacts.Matcher = (function() {
if (matchingOptions.selfContactId === aMatching.id) {
return;
}
var matchings, matchingObj;
if (!finalMatchings[aMatching.id]) {
matchingObj = {
matchings: {},
matchingContact: aMatching
};
finalMatchings[aMatching.id] = matchingObj;
matchingObj.matchings[filterBy[0]] = [];
}
else {
matchingObj = finalMatchings[aMatching.id];
}

var field = options.filterBy[0];
var values = aMatching[field];
Expand All @@ -70,33 +82,24 @@ contacts.Matcher = (function() {
var sanitizedValue = value;
var sanitizedTarget = target;

sanitizedValue = sanitize(value, field);
sanitizedTarget = sanitize(target, field);
sanitizedValue = sanitize(field, value);
sanitizedTarget = sanitize(field, target);

var valueMatched = false;
if (sanitizedValue === sanitizedTarget ||
sanitizedValue.indexOf(sanitizedTarget) !== -1 ||
sanitizedTarget.indexOf(sanitizedValue) !== -1) {
matchedValue = value;
valueMatched = true;
}

var matchings, matchingObj;
if (!finalMatchings[aMatching.id]) {
matchingObj = {
matchings: {},
matchingContact: aMatching
};
finalMatchings[aMatching.id] = matchingObj;
matchingObj.matchings[filterBy[0]] = [];
if (valueMatched) {
matchings = matchingObj.matchings[filterBy[0]];
matchings.push({
'target': target,
'matchedValue': matchedValue
});
}
else {
matchingObj = finalMatchings[aMatching.id];
}

matchings = matchingObj.matchings[filterBy[0]];
matchings.push({
'target': target,
'matchedValue': matchedValue
});
});
}); // matchings.forEach

Expand Down
56 changes: 51 additions & 5 deletions apps/communications/contacts/test/unit/contacts_matcher_test.js
Expand Up @@ -32,7 +32,14 @@ suite('Test Contacts Matcher', function() {

if (matchingFields) {
matchingFields.forEach(function(matchingField) {
assert.isDefined(results['1B'].matchings[matchingField]);
if (typeof matchingField === 'string') {
assert.isDefined(results['1B'].matchings[matchingField]);
}
else if (matchingField && typeof matchingField === 'object') {
var matchArr = results['1B'].matchings[matchingField.field];
assert.isDefined(matchArr);
assert.lengthOf(matchArr, matchingField.times);
}
});
}
}
Expand Down Expand Up @@ -367,16 +374,33 @@ suite('Test Contacts Matcher', function() {
test('Matching by phone', function(done) {
var myObj = Object.create(contact);
myObj.id = '1A';
myObj.email = [{
type: ['work'],
value: 'work@work.it'
}];

myObj.givenName = [];
myObj.familyName = null;

testMatch(myObj, 'active', ['tel'], done);
});

test('Matching by multiple phones', function(done) {
var myObj = Object.create(contact);
myObj.id = '1A';
var savedTel = contact.tel;
contact.tel = [
savedTel[0],
{
type: ['home'],
value: '67890123'
}
];
myObj.givenName = [];
myObj.familyName = null;

testMatch(myObj, 'active', [{field: 'tel', times: 2}], function() {
contact.tel = savedTel;
done();
});
});

test('Matching by email', function(done) {
var myObj = Object.create(contact);
myObj.id = '1A';
Expand All @@ -390,6 +414,28 @@ suite('Test Contacts Matcher', function() {
testMatch(myObj, 'active', ['email'], done);
});

test('Matching by multiple emails', function(done) {
var myObj = Object.create(contact);
myObj.id = '1A';

var savedEmail = contact.email;

contact.email = [
savedEmail[0],
{
type: ['work'],
value: 'k1zq@example.com'
}];

myObj.givenName = [];
myObj.familyName = null;

testMatch(myObj, 'active', [{field: 'email', times: 2}], function() {
contact.email = savedEmail;
done();
});
});

test('Matching by phone and email', function(done) {
var myObj = Object.create(contact);
myObj.id = '1A';
Expand Down
6 changes: 4 additions & 2 deletions apps/communications/contacts/test/unit/mock_find_matcher.js
Expand Up @@ -5,13 +5,15 @@ var MockFindMatcher = {
if (options.filterBy.indexOf('tel') !== -1) {
var value = options.filterValue;

if (this.data.tel && this.data.tel[0].value === value) {
if (this.data.tel && this.data.tel[0].value === value ||
(this.data.tel[1] && this.data.tel[1].value === value)) {
this.result = [this.data];
}
}
else if (options.filterBy.indexOf('email') !== -1) {
var value = options.filterValue;
if (this.data.email && this.data.email[0].value === value) {
if (this.data.email && this.data.email[0].value === value ||
this.data.email[1] && this.data.email[1].value === value) {
this.result = [this.data];
}
}
Expand Down

0 comments on commit d798ff8

Please sign in to comment.