From 1cc34ff84b13b611ea88b9d7fb2b037b245f112c Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Sat, 4 Jul 2015 20:31:23 -0300 Subject: [PATCH] rails4: fix all unit tests --- app/controllers/my_profile/cms_controller.rb | 2 +- app/models/approve_comment.rb | 2 +- app/models/create_enterprise.rb | 2 +- app/models/organization.rb | 2 +- app/models/profile_suggestion.rb | 16 +++++++--------- app/models/tag.rb | 34 ++++++++++++++++++++++++++++++++++ lib/acts_as_having_settings.rb | 27 +++++++++++++++------------ lib/extended_tag.rb | 36 ------------------------------------ test/test_helper.rb | 1 + test/unit/article_test.rb | 4 ++-- test/unit/block_test.rb | 2 +- test/unit/blog_helper_test.rb | 2 +- test/unit/catalog_helper_test.rb | 9 ++------- test/unit/display_helper_test.rb | 4 ++-- test/unit/environment_test.rb | 9 +++++---- test/unit/extended_tag_test.rb | 37 ------------------------------------- test/unit/language_helper_test.rb | 2 +- test/unit/person_notifier_test.rb | 27 ++++++++++++++++++--------- test/unit/product_category_test.rb | 1 - test/unit/profile_suggestions_job_test.rb | 2 +- test/unit/profile_test.rb | 4 ++-- test/unit/tag_test.rb | 37 +++++++++++++++++++++++++++++++++++++ test/unit/task_mailer_test.rb | 103 ++++++++++++++++++++++++++++++------------------------------------------------------------------------- test/unit/task_test.rb | 2 +- vendor/plugins/action_tracker/lib/action_tracker_model.rb | 3 +-- 25 files changed, 165 insertions(+), 205 deletions(-) create mode 100644 app/models/tag.rb delete mode 100644 lib/extended_tag.rb delete mode 100644 test/unit/extended_tag_test.rb create mode 100644 test/unit/tag_test.rb diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 7288e07..fb2e0e1 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -6,7 +6,7 @@ class CmsController < MyProfileController def search_tags arg = params[:term].downcase - result = ActsAsTaggableOn::Tag.where('name ILIKE ?', "%#{arg}%").limit(10) + result = Tag.where('name ILIKE ?', "%#{arg}%").limit(10) render :text => prepare_to_token_input_by_label(result).to_json, :content_type => 'application/json' end diff --git a/app/models/approve_comment.rb b/app/models/approve_comment.rb index 035f2dc..cadf357 100644 --- a/app/models/approve_comment.rb +++ b/app/models/approve_comment.rb @@ -8,7 +8,7 @@ class ApproveComment < Task def comment unless @comment || self.comment_attributes.nil? @comment = Comment.new - @comment.assign_attributes(ActiveSupport::JSON.decode(self.comment_attributes), :without_protection => true) + @comment.assign_attributes(ActiveSupport::JSON.decode(self.comment_attributes.to_s), :without_protection => true) end @comment end diff --git a/app/models/create_enterprise.rb b/app/models/create_enterprise.rb index 27b5ae4..18a2f1f 100644 --- a/app/models/create_enterprise.rb +++ b/app/models/create_enterprise.rb @@ -22,7 +22,7 @@ class CreateEnterprise < Task #checks if the validation method is region to validates validates_presence_of :region_id, :if => lambda { |obj| obj.environment.organization_approval_method == :region } - validates_numericality_of :foundation_year, only_integer: true + validates_numericality_of :foundation_year, only_integer: true, allow_nil: true # checks for actual attributes validates_presence_of :requestor_id, :target_id diff --git a/app/models/organization.rb b/app/models/organization.rb index 2b24b14..fe6eac1 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -112,7 +112,7 @@ class Organization < Profile settings_items :zip_code, :city, :state, :country - validates_format_of :foundation_year, :with => Noosfero::Constants::INTEGER_FORMAT + validates_numericality_of :foundation_year, only_integer: true, allow_nil: true validates_format_of :contact_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |org| !org.contact_email.blank? }) validates_as_cnpj :cnpj diff --git a/app/models/profile_suggestion.rb b/app/models/profile_suggestion.rb index e60a608..54e7b41 100644 --- a/app/models/profile_suggestion.rb +++ b/app/models/profile_suggestion.rb @@ -55,13 +55,13 @@ class ProfileSuggestion < ActiveRecord::Base :threshold => 2, :weight => 1, :connection => 'Profile' }, :people_with_common_tags => { - :threshold => 2, :weight => 1, :connection => 'ActsAsTaggableOn::Tag' + :threshold => 2, :weight => 1, :connection => 'Tag' }, :communities_with_common_friends => { :threshold => 2, :weight => 1, :connection => 'Profile' }, :communities_with_common_tags => { - :threshold => 2, :weight => 1, :connection => 'ActsAsTaggableOn::Tag' + :threshold => 2, :weight => 1, :connection => 'Tag' } } @@ -127,17 +127,15 @@ class ProfileSuggestion < ActiveRecord::Base rescue NoMethodError next end - connections = suggested_profile.send("#{rule}_connections") - if connections.present? - connections = connections[1..-2].split(',') - else - connections = [] - end - suggestion.send("#{rule}=", value) + + connections = suggested_profile.send("#{rule}_connections") || [] + connections = connections[1..-2] if connections.present? connections.each do |connection_id| next if SuggestionConnection.where(:suggestion_id => suggestion.id, :connection_id => connection_id, :connection_type => options[:connection]).present? SuggestionConnection.create!(:suggestion => suggestion, :connection_id => connection_id, :connection_type => options[:connection]) end + + suggestion.send("#{rule}=", value) suggestion.score += value * options[:weight] end suggestion.save! diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..53e53fd --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,34 @@ +Tag = ActsAsTaggableOn::Tag +class Tag + + attr_accessible :name, :parent_id, :pending + + has_many :children, class_name: 'Tag', foreign_key: 'parent_id', dependent: :destroy + + @@original_find = self.method(:find) + # Rename the find method to find_with_pendings that includes all tags in the search regardless if its pending or not + def self.find_with_pendings(*args) + @@original_find.call(*args) + end + + # Redefine the find method to exclude the pending tags from the search not allowing to tag something with an unapproved tag + def self.find(*args) + self.where(pending: false).find_with_pendings(*args) + end + + # Return all the tags that were suggested but not yet approved + def self.find_pendings + self.where(pending: true) + end + + # All the tags that can be a new parent for this tag, that is all but itself and its descendents to avoid loops + def parent_candidates + ActsAsTaggableOn::Tag.all - descendents - [self] + end + + # All tags that have this tag as its one of its ancestors + def descendents + children.to_a.sum([], &:descendents) + children + end + +end diff --git a/lib/acts_as_having_settings.rb b/lib/acts_as_having_settings.rb index 20fc92e..54434fe 100644 --- a/lib/acts_as_having_settings.rb +++ b/lib/acts_as_having_settings.rb @@ -21,6 +21,12 @@ end module ActsAsHavingSettings + def self.type_cast value, type + # do not cast nil + return value if value.nil? + type.send :cast_value, value + end + module ClassMethods def acts_as_having_settings(*args) @@ -50,13 +56,12 @@ module ActsAsHavingSettings settings_items *args end - def settings_items(*names) + def settings_items *names - options = names.last.is_a?(Hash) ? names.pop : {} - default = if !options[:default].nil? then options[:default] else nil end - data_type = options[:type] - data_type = if data_type.present? then data_type.to_s.camelize.to_sym else :String end - data_type = ActiveRecord::Type.const_get(data_type).new + options = names.extract_options! + default = options[:default] + type = options[:type] + type = if type.present? then ActiveRecord::Type.const_get(type.to_s.camelize.to_sym).new else nil end names.each do |setting| # symbolize key @@ -65,19 +70,17 @@ module ActsAsHavingSettings define_method setting do h = send self.class.settings_field val = h[setting] - if val.nil? then (if default.is_a? String then gettext default else default end) else val end + # translate default value if it is used + if not val.nil? then val elsif default.is_a? String then gettext default else default end end + define_method "#{setting}=" do |value| h = send self.class.settings_field - h[setting] = self.class.acts_as_having_settings_type_cast value, data_type + h[setting] = if type then ActsAsHavingSettings.type_cast value, type else value end end end end - def acts_as_having_settings_type_cast value, type - type.send :cast_value, value - end - end end diff --git a/lib/extended_tag.rb b/lib/extended_tag.rb deleted file mode 100644 index dc9c316..0000000 --- a/lib/extended_tag.rb +++ /dev/null @@ -1,36 +0,0 @@ -class ActsAsTaggableOn::Tag - - attr_accessible :name, :parent_id, :pending - - has_many :children, :class_name => 'Tag', :foreign_key => 'parent_id', :dependent => :destroy - - - @@original_find = self.method(:find) - # Rename the find method to find_with_pendings that includes all tags in the search regardless if its pending or not - def self.find_with_pendings(*args) - @@original_find.call(*args) - end - - # Redefine the find method to exclude the pending tags from the search not allowing to tag something with an unapproved tag - def self.find(*args) - self.with_scope(:find => { :conditions => ['pending = ?', false] }) do - self.find_with_pendings(*args) - end - end - - # Return all the tags that were suggested but not yet approved - def self.find_pendings - self.find_with_pendings.where('pending = ?', true) - end - - # All the tags that can be a new parent for this tag, that is all but itself and its descendents to avoid loops - def parent_candidates - ActsAsTaggableOn::Tag.all - descendents - [self] - end - - # All tags that have this tag as its one of its ancestors - def descendents - children.to_a.sum([], &:descendents) + children - end - -end diff --git a/test/test_helper.rb b/test/test_helper.rb index 7f00b13..ca7724b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -139,6 +139,7 @@ class ActiveSupport::TestCase assert !text.index('<'), "Text '#{text}' expected to be sanitized" end + # TODO: HTML::Document is deprecated, port to Nokogiri::HTML def assert_tag_in_string(text, options) doc = HTML::Document.new(text, false, false) tag = doc.find(options) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 87ae564..fb54c8a 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -886,7 +886,7 @@ class ArticleTest < ActiveSupport::TestCase should 'sanitize tags after save article' do article = fast_create(Article, :slug => 'article-with-tags', :profile_id => profile.id) - tag = build(ActsAsTaggableOn::Tag, :name => "TV Web w") + tag = build(Tag, name: "TV Web w") assert_match /[<>]/, tag.name article.tag_list.add(tag.name) article.save! @@ -895,7 +895,7 @@ class ArticleTest < ActiveSupport::TestCase should 'strip HTML from tag names after save article' do article = fast_create(Article, :slug => 'article-with-tags', :profile_id => profile.id) - tag = build(ActsAsTaggableOn::Tag, :name => "TV Web w