From 005f3f4db55e8fd055b9350b29ce62ae5d48457c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 18 Jul 2013 19:05:27 -0700 Subject: [PATCH] CRM-12943 - Add support for "delete", incl test-case for "create+read+delete+read" ---------------------------------------- * CRM-12943: Make HTML prototype of job UI functional http://issues.civicrm.org/jira/browse/CRM-12943 --- js/crm.backbone.js | 7 ++-- tests/qunit/crm-backbone/test.js | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/js/crm.backbone.js b/js/crm.backbone.js index a4e4f71688..96b70c0124 100644 --- a/js/crm.backbone.js +++ b/js/crm.backbone.js @@ -43,7 +43,7 @@ success: function(data) { // unwrap data var values = _.toArray(data['values']); - if (values.length == 1) { + if (data.count == 1) { options.success(values[0]); } else { data.is_error = 1; @@ -65,14 +65,15 @@ CRM.api(model.crmEntityName, 'create', model.toJSON(), apiOptions); break; case 'read': + case 'delete': + var apiAction = (method == 'delete') ? 'delete' : 'get'; var params = model.toCrmCriteria(); if (!params.id) { apiOptions.error({is_error: 1, error_message: 'Missing ID for ' + model.crmEntityName}); return; } - CRM.api(model.crmEntityName, 'get', params, apiOptions); + CRM.api(model.crmEntityName, apiAction, params, apiOptions); break; - case 'delete': default: apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for models"}); } diff --git a/tests/qunit/crm-backbone/test.js b/tests/qunit/crm-backbone/test.js index 9285cf31c3..659d7ceaeb 100644 --- a/tests/qunit/crm-backbone/test.js +++ b/tests/qunit/crm-backbone/test.js @@ -57,6 +57,70 @@ asyncTest("fetch (error)", function() { }); }); + +module('model - create'); + +asyncTest("create/read/delete/read (ok)", function() { + var TOKEN = new Date().getTime(); + var c1 = new ContactModel({ + contact_type: "Individual", + first_name: "George" + TOKEN, + last_name: "Anon" + TOKEN + }); + + // Create the new contact + c1.save({}, { + error: onUnexpectedError, + success: function() { + equal(c1.get("first_name"), "George"+TOKEN, "save() should return new first name"); + + // Fetch the newly created contact + var c2 = new ContactModel({id: c1.get('id')}); + c2.fetch({ + error: onUnexpectedError, + success: function() { + equal(c2.get("first_name"), c1.get("first_name"), "fetch() should return first name"); + + // Destroy the newly created contact + c2.destroy({ + error: onUnexpectedError, + success: function() { + + // Attempt (but fail) to fetch the deleted contact + var c3 = new ContactModel({id: c1.get('id')}); + c3.fetch({ + success: onUnexpectedSuccess, + error: function(model, error) { + assertApiError(error); + start(); + } + }); // fetch + } + }); // destroy + } + }); // fetch + } + }); // save +}); + +asyncTest("create (error)", function() { + var TOKEN = new Date().getTime(); + var c1 = new ContactModel({ + // MISSING: contact_type: "Individual", + first_name: "George" + TOKEN, + last_name: "Anon" + TOKEN + }); + + // Create the new contact + c1.save({}, { + success: onUnexpectedSuccess, + error: function(model, error) { + assertApiError(error); + start(); + } + }); +}); + module('model - update'); asyncTest("update (ok)", function() { -- 2.25.1