From 7bf3b7beb087ebf29de7888e5dd6240ec8ad0cbc Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 30 Sep 2015 04:12:16 +0000 Subject: [PATCH 1/4] CRM-17303 Fix to ensure you cannot schedule Mailings for in the past jshint fixes --- ang/crmUi.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ang/crmUi.js b/ang/crmUi.js index 448e8809195e..2c223886df2c 100644 --- a/ang/crmUi.js +++ b/ang/crmUi.js @@ -88,6 +88,20 @@ requiredLength = 8; } ngModel.$setValidity('incompleteDateTime', !($(this).val().length && $(this).val().length !== requiredLength)); + var d = new Date(), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + if (month.length < 2) month = '0' + month; + if (day.length < 2) day = '0' + day; + currentDate = [year, month, day].join('-'); + ngModel.$setValidity('dateTimeInThePast', !($(this).val().length && $(this).val() < currentDate)); + if ($(this).val().length && $(this).val() < currentDate){ + crmUiAlert({ + text: ts('The Scheulded date and time is in the past'), + title: ts('Error') + }); + } }); } }; From c81e99c78b9f932f1c21e77bd69c9c1039d99dbc Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 1 Oct 2015 00:49:49 +0000 Subject: [PATCH 2/4] Shift validation testing to radioDate from crmUI Minor fixes --- ang/crmMailing/RadioDate.js | 23 ++++++++++++++++++++++- ang/crmUi.js | 14 -------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ang/crmMailing/RadioDate.js b/ang/crmMailing/RadioDate.js index 8be382e3d87a..0f0bcdb93342 100644 --- a/ang/crmMailing/RadioDate.js +++ b/ang/crmMailing/RadioDate.js @@ -1,7 +1,7 @@ (function(angular, $, _) { // Represent a datetime field as if it were a radio ('schedule.mode') and a datetime ('schedule.datetime'). // example:
...
- angular.module('crmMailing').directive('crmMailingRadioDate', function() { + angular.module('crmMailing').directive('crmMailingRadioDate', function(crmUiAlert) { return { require: 'ngModel', link: function($scope, element, attrs, ngModel) { @@ -48,6 +48,27 @@ if (context === 'userInput' && $(this).val() === '' && $(this).siblings('.crm-form-date').val().length) { schedule.mode = 'at'; schedule.datetime = '?'; + } else { + var d = new Date() + var submittedDate = $(this).val(); + console.log(submittedDate); + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + if (month.length < 2) month = '0' + month; + if (day.length < 2) day = '0' + day; + date = [year, month, day].join('-'); + hours = '' + (d.getHours()), + minutes = '' + (d.getMinutes()); + time = [hours, minutes, "00"].join(':'); + currentDate = date + ' ' + time; + ngModel.$setValidity('dateTimeInThePast', !($(this).val().length && submittedDate < currentDate)); + if ($(this).val().length && submittedDate < currentDate) { + crmUiAlert({ + text: ts('The Scheulded date and time is in the past'), + title: ts('Error') + }); + } } }); diff --git a/ang/crmUi.js b/ang/crmUi.js index 2c223886df2c..448e8809195e 100644 --- a/ang/crmUi.js +++ b/ang/crmUi.js @@ -88,20 +88,6 @@ requiredLength = 8; } ngModel.$setValidity('incompleteDateTime', !($(this).val().length && $(this).val().length !== requiredLength)); - var d = new Date(), - month = '' + (d.getMonth() + 1), - day = '' + d.getDate(), - year = d.getFullYear(); - if (month.length < 2) month = '0' + month; - if (day.length < 2) day = '0' + day; - currentDate = [year, month, day].join('-'); - ngModel.$setValidity('dateTimeInThePast', !($(this).val().length && $(this).val() < currentDate)); - if ($(this).val().length && $(this).val() < currentDate){ - crmUiAlert({ - text: ts('The Scheulded date and time is in the past'), - title: ts('Error') - }); - } }); } }; From 6bbb41b25e9aae96fe8c84ae79aff270195d4b1f Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 1 Oct 2015 00:54:46 +0000 Subject: [PATCH 3/4] Remove debugging js hint fixes js hint fixes --- ang/crmMailing/RadioDate.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ang/crmMailing/RadioDate.js b/ang/crmMailing/RadioDate.js index 0f0bcdb93342..f509d62f1326 100644 --- a/ang/crmMailing/RadioDate.js +++ b/ang/crmMailing/RadioDate.js @@ -49,17 +49,16 @@ schedule.mode = 'at'; schedule.datetime = '?'; } else { - var d = new Date() - var submittedDate = $(this).val(); - console.log(submittedDate); + var d = new Date(), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), - year = d.getFullYear(); + year = d.getFullYear(), + hours = '' + d.getHours(), + minutes = '' + d.getMinutes(); + var submittedDate = $(this).val(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; date = [year, month, day].join('-'); - hours = '' + (d.getHours()), - minutes = '' + (d.getMinutes()); time = [hours, minutes, "00"].join(':'); currentDate = date + ' ' + time; ngModel.$setValidity('dateTimeInThePast', !($(this).val().length && submittedDate < currentDate)); From e749ee3b353269a0070a41ad399c125e3b777071 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 2 Oct 2015 22:25:52 +0000 Subject: [PATCH 4/4] Fix old tests to reflect new validation and add new test to verify that new validation function works Js Hint fixes Js Hint fixes Js Hint fixes Fix typo found by Dave --- ang/crmMailing/RadioDate.js | 2 +- tests/karma/unit/crmMailingRadioDateSpec.js | 24 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ang/crmMailing/RadioDate.js b/ang/crmMailing/RadioDate.js index f509d62f1326..153e9562b930 100644 --- a/ang/crmMailing/RadioDate.js +++ b/ang/crmMailing/RadioDate.js @@ -64,7 +64,7 @@ ngModel.$setValidity('dateTimeInThePast', !($(this).val().length && submittedDate < currentDate)); if ($(this).val().length && submittedDate < currentDate) { crmUiAlert({ - text: ts('The Scheulded date and time is in the past'), + text: ts('The scheduled date and time is in the past'), title: ts('Error') }); } diff --git a/tests/karma/unit/crmMailingRadioDateSpec.js b/tests/karma/unit/crmMailingRadioDateSpec.js index a90a820f6bb2..4ffee0cc6d7e 100644 --- a/tests/karma/unit/crmMailingRadioDateSpec.js +++ b/tests/karma/unit/crmMailingRadioDateSpec.js @@ -80,11 +80,31 @@ describe('crmMailingRadioDate', function() { model.the_date = '2014-01-02 02:03:00'; $rootScope.$digest(); - expect($rootScope.myForm.$valid).toBe(true); + expect($rootScope.myForm.$valid).toBe(false); expect(element.find('.radio-now').prop('checked')).toBe(false); expect(element.find('.radio-at').prop('checked')).toBe(true); expect(element.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual('Thu Jan 02 2014'); expect(element.find('.crm-form-time').timeEntry('getTime').getMinutes()).toBe(3); + + var now = new Date(); + now.setDate(now.getDate() + 1); + var month = '' + (now.getMonth() + 1); + var day = '' + now.getDate(); + var year = now.getFullYear(); + var hours = '' + now.getHours(); + var minutes = '' + now.getMinutes(); + if (month.length < 2) month = '0' + month; + if (day.length < 2) day = '0' + day; + var datenow = [year, month, day].join('-'); + var time = [hours, minutes, "00"].join(':'); + var currentDate = datenow + ' ' + time; + var n = now.toDateString(); + model.the_date = currentDate; + $rootScope.$digest(); + expect($rootScope.myForm.$valid).toBe(true); + expect(element.find('.radio-now').prop('checked')).toBe(false); + expect(element.find('.radio-at').prop('checked')).toBe(true); + expect(element.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual(n); }); it('should update the model after changing the date and time', function() { @@ -107,7 +127,7 @@ describe('crmMailingRadioDate', function() { element.find('.crm-form-time').timeEntry('setTime', '04:05').trigger('change'); $rootScope.$digest(); expect(model.the_date).toBe('2014-01-03 04:05:00'); - expect($rootScope.myForm.$valid).toBe(true); + expect($rootScope.myForm.$valid).toBe(false); expect(element.find('.radio-now').prop('checked')).toBe(false); expect(element.find('.radio-at').prop('checked')).toBe(true);