Skip to content
Browse files

Changes to support proper handling of html entities in JSON

  • Loading branch information...
1 parent 783dfb6 commit 4f9a5936a5a614d185a83f32c575990cfe03d30c @chicks chicks committed Nov 26, 2011
Showing with 72 additions and 2 deletions.
  1. +7 −1 lib/sugarcrm/base.rb
  2. +15 −1 lib/sugarcrm/connection/request.rb
  3. +15 −0 test/connection/test_set_entry.rb
  4. +35 −0 test/test_request.rb
View
8 lib/sugarcrm/base.rb
@@ -36,8 +36,13 @@ def find(*args, &block)
sort_criteria = 'date_entered'
elsif self.method_defined? :date_created
sort_criteria = 'date_created'
+ # Added date_modified because TeamSets doesn't have a date_created or date_entered field.
+ # There's no test for this because it's Pro and above only.
+ # Hope this doesn't break anything!
+ elsif self.method_defined? :date_modified
+ sort_criteria = 'date_modified'
else
- raise InvalidAttribute, "Unable to determine record creation date for sorting criteria: expected date_entered or date_created attribute to be present"
+ raise InvalidAttribute, "Unable to determine record creation date for sorting criteria: expected date_entered, date_created, or date_modified attribute to be present"
end
options = {:order_by => sort_criteria}.merge(options)
validate_find_options(options)
@@ -193,6 +198,7 @@ def reload!
def blank?
@attributes.empty?
end
+ alias :empty? :blank?
# Returns true if +comparison_object+ is the same exact object, or +comparison_object+
# is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
View
16 lib/sugarcrm/connection/request.rb
@@ -8,7 +8,7 @@ module SugarCRM; class Request
def initialize(url, method, json, debug=false)
@url = url
@method = method
- @json = CGI.escape(json)
+ @json = escape(json)
@request = 'method=' << @method.to_s
@request << '&input_type=JSON'
@request << '&response_type=JSON'
@@ -21,6 +21,20 @@ def initialize(url, method, json, debug=false)
self
end
+ def escape(json)
+ # BUG: SugarCRM doesn't properly handle '&quot;' inside of JSON for some reason. Let's convert it back just in case.
+ j = CGI.unescapeHTML(json)
+ # Now we convert everything else.
+ j = CGI.escape(j)
+ j
+ end
+
+ # TODO: Fix this so that it parses.
+ def unescape
+ j = CGI.unescape(@json)
+ j.gsub!(/\n/, '')
+ end
+
def bytesize
self.to_s.bytesize
end
View
15 test/connection/test_set_entry.rb
@@ -0,0 +1,15 @@
+require 'helper'
+
+class TestSetEntry < ActiveSupport::TestCase
+ context "A SugarCRM.connection" do
+ should "set values on an Bean with #set_entry" do
+
+ name_value_list = {
+ "name" => { "name" => "name", "value" => "A Test Meeting" },
+ "date_start" => { "name" => "date_start", "value" => "2011-11-23 12:03:16" },
+ "description" => { "name" => "description", "value" => "RT @bumblebeenie &quot;It's Childish Gambino, home girl drop it like the Nasdaq&quot; Are you serious bro?" }
+ }
+ meeting = SugarCRM.connection.set_entry("Meetings", name_value_list)
+ end
+ end
+end
View
35 test/test_request.rb
@@ -0,0 +1,35 @@
+require 'helper'
+
+class TestRequest < ActiveSupport::TestCase
+ context "A SugarCRM::Request instance" do
+ setup do
+ @url = "http://localhost/sugarcrm"
+ @method = "get_entry"
+ @json = <<-EOF
+ {
+ "session": "an invalid session",
+ "module_name": "Accounts",
+ "name_value_list": {
+ "name": {
+ "name": "name",
+ "value": "A Test Meeting" },
+ "date_start": {
+ "name": "date_start",
+ "value": "2011-11-23 12:03:16" },
+ "description": {
+ "name": "description",
+ "value": "&quot;OMG HAI!&quot;" }
+ }
+ }
+ EOF
+ @json.gsub!(/^\s{6}/, '')
+ @request = SugarCRM::Request.new(@url, @method, @json, false)
+ end
+
+ should "properly escape JSON" do
+ assert_equal "%22OMG+HAI%21%22", @request.escape("&quot;OMG HAI!&quot;" )
+ end
+
+ end
+end
+

0 comments on commit 4f9a593

Please sign in to comment.
Something went wrong with that request. Please try again.