CRM-12943 - Add support for "delete", incl test-case for "create+read+delete+read"
authorTim Otten <totten@civicrm.org>
Fri, 19 Jul 2013 02:05:27 +0000 (19:05 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 19 Jul 2013 02:06:22 +0000 (19:06 -0700)
----------------------------------------
* CRM-12943: Make HTML prototype of job UI functional
  http://issues.civicrm.org/jira/browse/CRM-12943

js/crm.backbone.js
tests/qunit/crm-backbone/test.js

index a4e4f716883d7254d88ee432d121b47cf5ebb85e..96b70c01242909b87f2a0bda64193c5522b9148f 100644 (file)
@@ -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;
           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"});
       }
index 9285cf31c3e08067d5e855e04a90895720c1ed93..659d7ceaeb86bf938e6ba27b604be744a2e232e8 100644 (file)
@@ -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() {