Bug 862250 - Make 'equals' searches on tel only match the value entered by the user. r=gwagner

--HG--
extra : rebase_source : 2652c484e01b9b6bb8c27212fc76cb5d9ddfba2d
This commit is contained in:
Reuben Morais 2013-04-25 08:40:18 -07:00
parent b95e9bd66e
commit 1cfc5531f6
2 changed files with 73 additions and 8 deletions

View File

@ -19,7 +19,7 @@ Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
Cu.import("resource://gre/modules/Timer.jsm");
const DB_NAME = "contacts";
const DB_VERSION = 8;
const DB_VERSION = 9;
const STORE_NAME = "contacts";
const SAVED_GETALL_STORE_NAME = "getallcache";
const CHUNK_SIZE = 20;
@ -315,6 +315,28 @@ ContactDB.prototype = {
} else if (currVersion == 7) {
if (DEBUG) debug("Adding object store for cached searches");
db.createObjectStore(SAVED_GETALL_STORE_NAME);
} else if (currVersion == 8) {
if (DEBUG) debug("Make exactTel only contain the value entered by the user");
if (!objectStore) {
objectStore = aTransaction.objectStore(STORE_NAME);
}
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
if (cursor.value.properties.tel) {
cursor.value.search.exactTel = [];
cursor.value.properties.tel.forEach(
function(tel) {
let normalized = PhoneNumberUtils.normalize(tel.value.toString());
cursor.value.search.exactTel.push(normalized);
}
);
cursor.update(cursor.value);
}
cursor.continue();
}
};
}
}
@ -417,9 +439,10 @@ ContactDB.prototype = {
// Chop off the first characters
let number = aContact.properties[field][i].value;
contact.search.exactTel.push(number);
let search = {};
if (number) {
number = number.toString();
contact.search.exactTel.push(PhoneNumberUtils.normalize(number));
for (let i = 0; i < number.length; i++) {
search[number.substring(i, number.length)] = 1;
}
@ -442,7 +465,6 @@ ContactDB.prototype = {
}
if (parsedNumber.internationalNumber &&
number.toString() !== parsedNumber.internationalNumber) {
contact.search.exactTel.push(parsedNumber.internationalNumber);
let digits = parsedNumber.internationalNumber.match(/\d/g);
if (digits) {
digits = digits.join('');
@ -775,7 +797,11 @@ ContactDB.prototype = {
if (DEBUG) debug("Getting index: " + key);
// case sensitive
let index = store.index(key);
request = index.mozGetAll(options.filterValue, limit);
let filterValue = options.filterValue;
if (key == "tel") {
filterValue = PhoneNumberUtils.normalize(filterValue);
}
request = index.mozGetAll(filterValue, limit);
} else {
// not case sensitive
let tmp = typeof options.filterValue == "string"

View File

@ -87,7 +87,7 @@ var properties1 = {
familyName: ["TestFamilyName","Wagner"],
givenName: ["Test1","Test2"],
nickname: "nicktest",
tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+9-876-5432"}],
tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+55 (31) 9876-3456"}],
adr: adr1,
email: [{type: ["work"], value: "x@y.com"}]
};
@ -333,12 +333,12 @@ var steps = [
checkContacts(createResult1, properties1);
dump("findResult: " + JSON.stringify(findResult1) + "\n");
// Some manual testing. Testint the testfunctions
// tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+9-876-5432"}],
// tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+55 (31) 9876-3456"}],
is(findResult1.tel[0].carrier, "testCarrier", "Same Carrier");
is(findResult1.tel[0].type, "work", "Same type");
is(findResult1.tel[0].value, "123456", "Same Value");
is(findResult1.tel[1].type[1], "fax", "Same type");
is(findResult1.tel[1].value, "+9-876-5432", "Same Value");
is(findResult1.tel[1].value, "+55 (31) 9876-3456", "Same Value");
is(findResult1.adr[0].countryName, "country 1", "Same country");
@ -503,6 +503,45 @@ var steps = [
};
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving by tel exact");
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: "+55 319 8 7 6 3456"};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
checkContacts(createResult1, properties1);
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving by tel exact with substring");
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: "3456"};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 0, "Found no contacts.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving by tel exact with substring");
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: "+55 (31)"};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 0, "Found no contacts.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Retrieving by substring tel2");
var options = {filterBy: ["tel"],
@ -522,7 +561,7 @@ var steps = [
ok(true, "Retrieving by substring tel3");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: "98765432"};
filterValue: "98763456"};
req = mozContacts.find(options);
req.onsuccess = function () {
is(req.result.length, 1, "Found exactly 1 contact.");