From b2c228b4297e709afaa0f224a4dbaa3066ea2f85 Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Mon, 28 Mar 2016 12:08:17 -0300 Subject: [PATCH] fixes visibles_method and unit tests --- plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb | 7 +++++-- plugins/environment_notification/test/helpers/environment_notification_test_helper.rb | 10 ++++++++++ plugins/environment_notification/test/unit/environment_notification_test.rb | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 plugins/environment_notification/test/helpers/environment_notification_test_helper.rb diff --git a/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb index 73f0716..94eccef 100644 --- a/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb +++ b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb @@ -9,7 +9,7 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas "EnvironmentNotificationPlugin::DangerNotification" ] - attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title + attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title, :target has_many :environment_notifications_users has_many :users, :through => :environment_notifications_users @@ -41,7 +41,10 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas end if controller_path != "home" - notifications = notifications.where(display_only_in_homepage: false) + notifications = notifications.where.not("display_only_in_homepage = ? AND target_type = ?",true,"Environment") + if controller_path != "profile" + notifications = notifications.where.not("display_only_in_homepage = ? AND target_type = ?",true,"Profile") + end end notifications diff --git a/plugins/environment_notification/test/helpers/environment_notification_test_helper.rb b/plugins/environment_notification/test/helpers/environment_notification_test_helper.rb new file mode 100644 index 0000000..ef4d5ad --- /dev/null +++ b/plugins/environment_notification/test/helpers/environment_notification_test_helper.rb @@ -0,0 +1,10 @@ +module EnvironmentNotificationTestHelper + def create_notification target, display_only_in_homepage=false, message="any_message", active=true + EnvironmentNotificationPlugin::WarningNotification.create!( + :target => target, + :message => message, + :active => active, + :display_only_in_homepage => display_only_in_homepage + ) + end +end diff --git a/plugins/environment_notification/test/unit/environment_notification_test.rb b/plugins/environment_notification/test/unit/environment_notification_test.rb index 73759fc..66e9ad4 100644 --- a/plugins/environment_notification/test/unit/environment_notification_test.rb +++ b/plugins/environment_notification/test/unit/environment_notification_test.rb @@ -1,26 +1,29 @@ require_relative '../../../../test/test_helper' +require_relative '../helpers/environment_notification_test_helper' class EnvironmentNotificationTest < ActiveSupport::TestCase + include EnvironmentNotificationTestHelper + def setup @env = Environment.default @env.enable_plugin('EnvironmentNotificationPlugin') @user = User.create!(:environment_id => @env.id, :email => "user@domain.com", :login => "new_user", :password => "test", :password_confirmation => "test") @danger_notification = EnvironmentNotificationPlugin::DangerNotification.create!( - :target_id => @env.id, + :target => @env, :message => "Danger Message", :active => true, ) @warning_notification = EnvironmentNotificationPlugin::WarningNotification.create!( - :target_id => @env.id, + :target => @env, :message => "Warning Message", :active => true, ) @information_notification = EnvironmentNotificationPlugin::InformationNotification.create!( - :target_id => @env.id, + :target => @env, :message => "Information Message", :active => true, ) @@ -83,6 +86,38 @@ class EnvironmentNotificationTest < ActiveSupport::TestCase assert !notifications.include?(@information_notification) end + should 'get notifications configured to be displayed on profile' do + community = fast_create(Community) + + EnvironmentNotificationPlugin::EnvironmentNotification.destroy_all + env_home_notification = create_notification(@env, true) + env_not_home_notification = create_notification(@env, false) + profile_not_home_notification = create_notification(community, false) + profile_home_notification = create_notification(community, true) + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(community, @user, "profile") + assert_equivalent notifications.to_a, [env_not_home_notification, profile_not_home_notification, profile_home_notification] + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(community, @user, "profile_but_bot_homepage") + assert_equivalent notifications.to_a, [env_not_home_notification, profile_not_home_notification] + end + + should 'get notifications configured to be displayed on environment' do + community = fast_create(Community) + + EnvironmentNotificationPlugin::EnvironmentNotification.destroy_all + env_home_notification = create_notification(@env, true) + env_not_home_notification = create_notification(@env, false) + profile_not_home_notification = create_notification(community, false) + profile_home_notification = create_notification(community, true) + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, "home") + assert_equivalent notifications.to_a, [env_home_notification, env_not_home_notification] + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, "not_home_not_profile") + assert_equivalent notifications.to_a, [env_not_home_notification] + end + should 'get only notifications configured to be displayed to all users and in all pages and not closed by an user' do @information_notification.display_to_all_users = true @information_notification.save! -- libgit2 0.21.2