From c3453051d6579a42528513f353a50fa7e928d8dd Mon Sep 17 00:00:00 2001 From: Eduardo Tourinho Edington Date: Sun, 16 Mar 2014 20:39:35 -0300 Subject: [PATCH] Make a plugin to display statistics from Environment and Profiles --- app/controllers/admin/environment_design_controller.rb | 2 +- app/models/box.rb | 2 -- app/models/environment.rb | 1 - app/models/environment_statistics_block.rb | 30 ------------------------------ app/sweepers/profile_sweeper.rb | 6 ------ db/migrate/20140316171151_remove_environment_statistics_block.rb | 9 +++++++++ plugins/statistics/README | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/statistics/lib/statistics_block.rb | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/statistics/lib/statistics_plugin.rb | 19 +++++++++++++++++++ plugins/statistics/test/functional/statistics_plugin_environment_design_controller_test.rb | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/statistics/test/functional/statistics_plugin_home_controller_test.rb | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/statistics/test/functional/statistics_plugin_profile_design_controller_test.rb | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/statistics/test/test_helper.rb | 1 + plugins/statistics/test/unit/statistics_block_test.rb | 336 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/statistics/test/unit/statistics_plugin_test.rb | 9 +++++++++ plugins/statistics/views/box_organizer/_statistics_block.rhtml | 32 ++++++++++++++++++++++++++++++++ plugins/statistics/views/environment_design | 1 + plugins/statistics/views/profile_design | 1 + plugins/statistics/views/statistics_block.rhtml | 36 ++++++++++++++++++++++++++++++++++++ test/functional/environment_design_controller_test.rb | 12 +----------- test/unit/box_test.rb | 2 -- test/unit/environment_statistics_block_test.rb | 96 ------------------------------------------------------------------------------------------------ 22 files changed, 1039 insertions(+), 149 deletions(-) delete mode 100644 app/models/environment_statistics_block.rb create mode 100644 db/migrate/20140316171151_remove_environment_statistics_block.rb create mode 100644 plugins/statistics/README create mode 100644 plugins/statistics/lib/statistics_block.rb create mode 100644 plugins/statistics/lib/statistics_plugin.rb create mode 100644 plugins/statistics/test/functional/statistics_plugin_environment_design_controller_test.rb create mode 100644 plugins/statistics/test/functional/statistics_plugin_home_controller_test.rb create mode 100644 plugins/statistics/test/functional/statistics_plugin_profile_design_controller_test.rb create mode 100644 plugins/statistics/test/test_helper.rb create mode 100644 plugins/statistics/test/unit/statistics_block_test.rb create mode 100644 plugins/statistics/test/unit/statistics_plugin_test.rb create mode 100644 plugins/statistics/views/box_organizer/_statistics_block.rhtml create mode 120000 plugins/statistics/views/environment_design create mode 120000 plugins/statistics/views/profile_design create mode 100644 plugins/statistics/views/statistics_block.rhtml delete mode 100644 test/unit/environment_statistics_block_test.rb diff --git a/app/controllers/admin/environment_design_controller.rb b/app/controllers/admin/environment_design_controller.rb index c310a67..863fb64 100644 --- a/app/controllers/admin/environment_design_controller.rb +++ b/app/controllers/admin/environment_design_controller.rb @@ -3,7 +3,7 @@ class EnvironmentDesignController < BoxOrganizerController protect 'edit_environment_design', :environment def available_blocks - @available_blocks ||= [ ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] + @available_blocks ||= [ ArticleBlock, LoginBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] @available_blocks += plugins.dispatch(:extra_blocks, :type => Environment) end diff --git a/app/models/box.rb b/app/models/box.rb index 2576b8f..db104dc 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -26,7 +26,6 @@ class Box < ActiveRecord::Base CategoriesBlock, CommunitiesBlock, EnterprisesBlock, - EnvironmentStatisticsBlock, FansBlock, FavoriteEnterprisesBlock, FeedReaderBlock, @@ -52,7 +51,6 @@ class Box < ActiveRecord::Base CommunitiesBlock, DisabledEnterpriseMessageBlock, EnterprisesBlock, - EnvironmentStatisticsBlock, FansBlock, FavoriteEnterprisesBlock, FeaturedProductsBlock, diff --git a/app/models/environment.rb b/app/models/environment.rb index 6827d07..c86a308 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -161,7 +161,6 @@ class Environment < ActiveRecord::Base # "left" area env.boxes[1].blocks << LoginBlock.new - env.boxes[1].blocks << EnvironmentStatisticsBlock.new env.boxes[1].blocks << RecentDocumentsBlock.new # "right" area diff --git a/app/models/environment_statistics_block.rb b/app/models/environment_statistics_block.rb deleted file mode 100644 index 598edf7..0000000 --- a/app/models/environment_statistics_block.rb +++ /dev/null @@ -1,30 +0,0 @@ -class EnvironmentStatisticsBlock < Block - - def self.description - _('Environment stastistics') - end - - def default_title - _('Statistics for %s') % owner.name - end - - def help - _('This block presents some statistics about your environment.') - end - - def content(args={}) - users = owner.people.visible.count - enterprises = owner.enterprises.visible.count - communities = owner.communities.visible.count - - info = [] - info << (n_('One user', '%{num} users', users) % { :num => users }) - unless owner.enabled?('disable_asset_enterprises') - info << (n__('One enterprise', '%{num} enterprises', enterprises) % { :num => enterprises }) - end - info << (n__('One community', '%{num} communities', communities) % { :num => communities }) - - block_title(title) + content_tag('ul', info.map {|item| content_tag('li', item) }.join("\n")) - end - -end diff --git a/app/sweepers/profile_sweeper.rb b/app/sweepers/profile_sweeper.rb index 1d53bb9..454b593 100644 --- a/app/sweepers/profile_sweeper.rb +++ b/app/sweepers/profile_sweeper.rb @@ -8,7 +8,6 @@ class ProfileSweeper # < ActiveRecord::Observer end def after_create(profile) - expire_statistics_block_cache(profile) end protected @@ -29,11 +28,6 @@ protected expire_blogs(profile) if profile.organization? end - def expire_statistics_block_cache(profile) - blocks = profile.environment.blocks.select { |b| b.kind_of?(EnvironmentStatisticsBlock) } - BlockSweeper.expire_blocks(blocks) - end - def expire_blogs(profile) profile.blogs.select{|b| !b.empty?}.each do |blog| pages = blog.posts.count / blog.posts_per_page + 1 diff --git a/db/migrate/20140316171151_remove_environment_statistics_block.rb b/db/migrate/20140316171151_remove_environment_statistics_block.rb new file mode 100644 index 0000000..f402729 --- /dev/null +++ b/db/migrate/20140316171151_remove_environment_statistics_block.rb @@ -0,0 +1,9 @@ +class RemoveEnvironmentStatisticsBlock < ActiveRecord::Migration + def self.up + update("DELETE FROM blocks WHERE type = 'EnvironmentStatisticsBlock'") + end + + def self.down + say("Nothing to undo (cannot recover the data)") + end +end diff --git a/plugins/statistics/README b/plugins/statistics/README new file mode 100644 index 0000000..0d0ef3d --- /dev/null +++ b/plugins/statistics/README @@ -0,0 +1,54 @@ +README - Statistics (Statistics Plugin) +================================ + +Statistics is a plugin to allow the user adds a block where you could see statistics of it's context. + +The Statistics block will be available for all layout columns of communities, peole, enterprises and environments. + +INSTALL +======= + +Enable Plugin +------------- + +Also, you need to enable Statistics Plugin at you Noosfero: + +cd +./script/noosfero-plugins enable statistics + +Active Plugin +------------- + +As a Noosfero administrator user, go to administrator panel: + +- Click on "Enable/disable plugins" option +- Click on "Statistics Plugin" check-box + +Running Statistics tests +-------------------- + +$ rake test:noosfero_plugins:statistics + + +Get Involved +============ + +If you found any bug and/or want to collaborate, please send an e-mail to eduardo.edington@gmail.com + +LICENSE +======= + +Copyright (c) The Author developers. + +See Noosfero license. + + +AUTHORS +======= + + Eduardo Tourinho Edington (eduardo.edington at gmail.com) + +ACKNOWLEDGMENTS +=============== + +The author has been supported by Serpro diff --git a/plugins/statistics/lib/statistics_block.rb b/plugins/statistics/lib/statistics_block.rb new file mode 100644 index 0000000..8f5a96f --- /dev/null +++ b/plugins/statistics/lib/statistics_block.rb @@ -0,0 +1,146 @@ +class StatisticsBlock < Block + + settings_items :community_counter, :default => false + settings_items :user_counter, :default => true + settings_items :enterprise_counter, :default => false + settings_items :category_counter, :default => false + settings_items :tag_counter, :default => true + settings_items :comment_counter, :default => true + settings_items :hit_counter, :default => false + settings_items :templates_ids_counter, Hash, :default => {} + + USER_COUNTERS = [:community_counter, :user_counter, :enterprise_counter, :tag_counter, :comment_counter, :hit_counter] + COMMUNITY_COUNTERS = [:user_counter, :tag_counter, :comment_counter, :hit_counter] + ENTERPRISE_COUNTERS = [:user_counter, :tag_counter, :comment_counter, :hit_counter] + + def self.description + _('Statistics') + end + + def default_title + _('Statistics for %s') % owner.name + end + + def is_visible? counter + value = self.send(counter) + value == '1' || value == true + end + + def is_counter_available? counter + if owner.kind_of?(Environment) + true + elsif owner.kind_of?(Person) + USER_COUNTERS.include?(counter) + elsif owner.kind_of?(Community) + COMMUNITY_COUNTERS.include?(counter) + elsif owner.kind_of?(Enterprise) + ENTERPRISE_COUNTERS.include?(counter) + end + + end + + def help + _('This block presents some statistics about your context.') + end + + def timeout + 60.minutes + end + + def environment + if owner.kind_of?(Environment) + owner + elsif owner.kind_of?(Profile) + owner.environment + else + nil + end + end + + def templates + Community.templates(environment) + end + + def is_template_counter_active? template_id + self.templates_ids_counter[template_id.to_s].to_s == 'true' + end + + def template_counter_count(template_id) + owner.communities.visible.count(:conditions => {:template_id => template_id}) + end + + def users + if owner.kind_of?(Environment) + owner.people.visible.count + elsif owner.kind_of?(Organization) + owner.members.visible.count + elsif owner.kind_of?(Person) + owner.friends.visible.count + else + 0 + end + end + + def enterprises + if owner.kind_of?(Environment) || owner.kind_of?(Person) + owner.enterprises.visible.count + else + 0 + end + end + + def communities + if owner.kind_of?(Environment) || owner.kind_of?(Person) + owner.communities.visible.count + else + 0 + end + end + + def categories + if owner.kind_of?(Environment) then + owner.categories.count + else + 0 + end + end + + def tags + if owner.kind_of?(Environment) then + owner.tag_counts.count + elsif owner.kind_of?(Profile) then + owner.article_tags.count + else + 0 + end + end + + def comments + if owner.kind_of?(Environment) then + owner.profiles.joins(:articles).sum(:comments_count).to_i + elsif owner.kind_of?(Profile) then + owner.articles.sum(:comments_count) + else + 0 + end + end + + def hits + if owner.kind_of?(Environment) then + owner.profiles.joins(:articles).sum(:hits).to_i + elsif owner.kind_of?(Profile) then + owner.articles.sum(:hits) + else + 0 + end + end + + def content(args={}) + block = self + + lambda do + render :file => 'statistics_block', :locals => { :block => block } + end + end + +end diff --git a/plugins/statistics/lib/statistics_plugin.rb b/plugins/statistics/lib/statistics_plugin.rb new file mode 100644 index 0000000..f7965b5 --- /dev/null +++ b/plugins/statistics/lib/statistics_plugin.rb @@ -0,0 +1,19 @@ +require_dependency File.dirname(__FILE__) + '/statistics_block' + +class StatisticsPlugin < Noosfero::Plugin + + def self.plugin_name + "Statistics Plugin" + end + + def self.plugin_description + _("A plugin that adds a block where you can see statistics of it's context.") + end + + def self.extra_blocks + { + StatisticsBlock => {} + } + end + +end diff --git a/plugins/statistics/test/functional/statistics_plugin_environment_design_controller_test.rb b/plugins/statistics/test/functional/statistics_plugin_environment_design_controller_test.rb new file mode 100644 index 0000000..153f1e9 --- /dev/null +++ b/plugins/statistics/test/functional/statistics_plugin_environment_design_controller_test.rb @@ -0,0 +1,161 @@ +require File.dirname(__FILE__) + '/../test_helper' + +# Re-raise errors caught by the controller. +class EnvironmentDesignController; def rescue_action(e) raise e end; end + +class EnvironmentDesignControllerTest < ActionController::TestCase + + def setup + @controller = EnvironmentDesignController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + Environment.delete_all + + @environment = Environment.create(:name => 'testenv', :is_default => true) + @environment.enabled_plugins = ['StatisticsPlugin'] + @environment.save! + + user = create_user('testinguser') + @environment.add_admin(user.person) + + StatisticsBlock.delete_all + @box1 = Box.create!(:owner => @environment) + @environment.boxes = [@box1] + + @block = StatisticsBlock.new + @block.box = @box1 + @block.save! + + login_as(user.login) + end + + attr_accessor :block + + should 'be able to edit StatisticsBlock' do + get :edit, :id => @block.id + assert_tag :tag => 'input', :attributes => { :id => 'block_title' } + end + + should 'be able to save StatisticsBlock' do + get :edit, :id => @block.id + post :save, :id => @block.id, :block => {:title => 'Statistics' } + @block.reload + assert_equal 'Statistics', @block.title + end + + should 'be able to uncheck core counters' do + @block.user_counter = true + @block.community_counter = true + @block.enterprise_counter = true + @block.category_counter = true + @block.tag_counter = true + @block.comment_counter = true + @block.hit_counter = true + @block.save! + get :edit, :id => @block.id + post :save, :id => @block.id, :block => {:user_counter => '0', :community_counter => '0', :enterprise_counter => '0', + :category_counter => '0', :tag_counter => '0', :comment_counter => '0', :hit_counter => '0' } + @block.reload + any_checked = @block.is_visible?('user_counter') || + @block.is_visible?('community_counter') || + @block.is_visible?('enterprise_counter') || + @block.is_visible?('category_counter') || + @block.is_visible?('tag_counter') || + @block.is_visible?('comment_counter') || + @block.is_visible?('hit_counter') + assert_equal false, any_checked + + end + + should 'be able to check core counters' do + @block.user_counter = false + @block.community_counter = false + @block.enterprise_counter = false + @block.category_counter = false + @block.tag_counter = false + @block.comment_counter = false + @block.hit_counter = false + @block.save! + get :edit, :id => @block.id + post :save, :id => @block.id, :block => {:user_counter => '1', :community_counter => '1', :enterprise_counter => '1', + :category_counter => '1', :tag_counter => '1', :comment_counter => '1', :hit_counter => '1' } + @block.reload + all_checked = @block.is_visible?('user_counter') && + @block.is_visible?('community_counter') && + @block.is_visible?('enterprise_counter') && + @block.is_visible?('category_counter') && + @block.is_visible?('tag_counter') && + @block.is_visible?('comment_counter') && + @block.is_visible?('hit_counter') + assert all_checked + + end + + should 'be able to check template counters' do + template = fast_create(Community, :name => 'Councils', :is_template => true, :environment_id => @environment.id) + @block.templates_ids_counter = {template.id.to_s => 'false'} + @block.save! + get :edit, :id => @block.id + post :save, :id => @block.id, :block => {:templates_ids_counter => {template.id.to_s => 'true'}} + @block.reload + + assert @block.is_template_counter_active?(template.id) + end + + should 'be able to uncheck template counters' do + template = fast_create(Community, :name => 'Councils', :is_template => true, :environment_id => @environment.id) + @block.templates_ids_counter = {template.id.to_s => 'true'} + @block.save! + get :edit, :id => @block.id + post :save, :id => @block.id, :block => {:templates_ids_counter => {template.id.to_s => 'false'}} + @block.reload + + assert_equal false, @block.is_template_counter_active?(template.id) + end + + should 'input user counter be checked by default' do + get :edit, :id => @block.id + + assert_tag :input, :attributes => {:id => 'block_user_counter', :checked => 'checked'} + end + + should 'not input community counter be checked by default' do + get :edit, :id => @block.id + + assert_tag :input, :attributes => {:id => 'block_community_counter'} + assert_no_tag :input, :attributes => {:id => 'block_community_counter', :checked => 'checked'} + end + + should 'not input enterprise counter be checked by default' do + get :edit, :id => @block.id + + assert_tag :input, :attributes => {:id => 'block_enterprise_counter'} + assert_no_tag :input, :attributes => {:id => 'block_enterprise_counter', :checked => 'checked'} + end + + should 'not input category counter be checked by default' do + get :edit, :id => @block.id + + assert_tag :input, :attributes => {:id => 'block_category_counter'} + assert_no_tag :input, :attributes => {:id => 'block_category_counter', :checked => 'checked'} + end + + should 'input tag counter be checked by default' do + get :edit, :id => @block.id + + assert_tag :input, :attributes => {:id => 'block_tag_counter', :checked => 'checked'} + end + + should 'input comment counter be checked by default' do + get :edit, :id => @block.id + + assert_tag :input, :attributes => {:id => 'block_comment_counter', :checked => 'checked'} + end + + should 'input hit counter not be checked by default' do + get :edit, :id => @block.id + + assert_no_tag :input, :attributes => {:id => 'block_hit_counter', :checked => 'checked'} + end +end diff --git a/plugins/statistics/test/functional/statistics_plugin_home_controller_test.rb b/plugins/statistics/test/functional/statistics_plugin_home_controller_test.rb new file mode 100644 index 0000000..2a2e546 --- /dev/null +++ b/plugins/statistics/test/functional/statistics_plugin_home_controller_test.rb @@ -0,0 +1,147 @@ +require File.dirname(__FILE__) + '/../test_helper' + +# Re-raise errors caught by the controller. +class HomeController; def rescue_action(e) raise e end; end + +class HomeControllerTest < ActionController::TestCase + + def setup + @controller = HomeController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + Environment.delete_all + + @environment = Environment.create(:name => 'testenv', :is_default => true) + @environment.enabled_plugins = ['StatisticsPlugin'] + @environment.save! + + user = create_user('testinguser') + @environment.add_admin(user.person) + + StatisticsBlock.delete_all + @box1 = Box.create!(:owner => @environment) + @environment.boxes = [@box1] + + @block = StatisticsBlock.new + @block.box = @box1 + @block.save! + + login_as(user.login) + end + + attr_accessor :block + + should 'display statistics-block-data class in environment block edition' do + get :index + + assert_tag :div, :attributes => {:class => 'statistics-block-data'} + end + + should 'display users class in statistics-block-data block' do + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'users'} } + end + + should 'not display users class in statistics-block-data block' do + @block.user_counter = false + @block.save! + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'users'} } + end + + should 'display communities class in statistics-block-data block' do + @block.community_counter = true + @block.save! + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'communities'} } + end + + should 'not display communities class in statistics-block-data block' do + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'communities'} } + end + + should 'display enterprises class in statistics-block-data block' do + @block.enterprise_counter = true + @block.save! + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'enterprises'} } + end + + should 'not display enterprises class in statistics-block-data block' do + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'enterprises'} } + end + + should 'display categories class in statistics-block-data block' do + @block.category_counter = true + @block.save! + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'categories'} } + end + + should 'not display categories class in statistics-block-data block' do + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'categories'} } + end + + should 'display tags class in statistics-block-data block' do + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'tags'} } + end + + should 'not display tags class in statistics-block-data block' do + @block.tag_counter = false + @block.save! + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'tags'} } + end + + should 'display comments class in statistics-block-data block' do + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'comments'} } + end + + should 'not display comments class in statistics-block-data block' do + @block.comment_counter = false + @block.save! + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'comments'} } + end + + should 'display hits class in statistics-block-data block' do + @block.hit_counter = true + @block.save! + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'hits'} } + end + + should 'not display hits class in statistics-block-data block' do + get :index + + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'hits'} } + end + + should 'display template name in class in statistics-block-data block' do + template = fast_create(Community, :name => 'Councils', :is_template => true, :environment_id => @environment.id) + @block.templates_ids_counter = {template.id.to_s => 'true'} + @block.save! + get :index + + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'councils'} } + end +end diff --git a/plugins/statistics/test/functional/statistics_plugin_profile_design_controller_test.rb b/plugins/statistics/test/functional/statistics_plugin_profile_design_controller_test.rb new file mode 100644 index 0000000..ca42e64 --- /dev/null +++ b/plugins/statistics/test/functional/statistics_plugin_profile_design_controller_test.rb @@ -0,0 +1,85 @@ +require File.dirname(__FILE__) + '/../test_helper' + +# Re-raise errors caught by the controller. +class ProfileDesignController; def rescue_action(e) raise e end; end + +class ProfileDesignControllerTest < ActionController::TestCase + + def setup + @controller = ProfileDesignController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + Environment.delete_all + + @environment = Environment.create(:name => 'testenv', :is_default => true) + @environment.enabled_plugins = ['StatisticsPlugin'] + @environment.save! + + user = create_user('testinguser') + @person = user.person + @environment.add_admin(@person) + + StatisticsBlock.delete_all + @box1 = Box.create!(:owner => @person) + @environment.boxes = [@box1] + + @block = StatisticsBlock.new + @block.box = @box1 + @block.save! + + login_as(user.login) + end + + attr_accessor :block + + should 'be able to edit StatisticsBlock' do + get :edit, :id => @block.id, :profile => @person.identifier + assert_tag :tag => 'input', :attributes => { :id => 'block_title' } + end + + should 'be able to save StatisticsBlock' do + get :edit, :id => @block.id, :profile => @person.identifier + post :save, :id => @block.id, :block => {:title => 'Statistics' }, :profile => @person.identifier + @block.reload + assert_equal 'Statistics', @block.title + end + + should 'be able to uncheck core counters' do + @block.user_counter = true + @block.tag_counter = true + @block.comment_counter = true + @block.hit_counter = true + @block.save! + get :edit, :id => @block.id, :profile => @person.identifier + post :save, :id => @block.id, :block => {:user_counter => '0', :tag_counter => '0', :comment_counter => '0', :hit_counter => '0' }, :profile => @person.identifier + @block.reload + any_checked = @block.is_visible?('user_counter') || + @block.is_visible?('tag_counter') || + @block.is_visible?('comment_counter') || + @block.is_visible?('hit_counter') + assert_equal false, any_checked + end + + should 'be able to check core counters' do + @block.user_counter = false + @block.community_counter = false + @block.enterprise_counter = false + @block.category_counter = false + @block.tag_counter = false + @block.comment_counter = false + @block.hit_counter = false + @block.save! + get :edit, :id => @block.id, :profile => @person.identifier + post :save, :id => @block.id, :block => {:user_counter => '1', + :tag_counter => '1', :comment_counter => '1', :hit_counter => '1' }, :profile => @person.identifier + @block.reload + all_checked = @block.is_visible?('user_counter') && + @block.is_visible?('tag_counter') && + @block.is_visible?('comment_counter') && + @block.is_visible?('hit_counter') + assert all_checked + + end + +end diff --git a/plugins/statistics/test/test_helper.rb b/plugins/statistics/test/test_helper.rb new file mode 100644 index 0000000..cca1fd3 --- /dev/null +++ b/plugins/statistics/test/test_helper.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + '/../../../test/test_helper' diff --git a/plugins/statistics/test/unit/statistics_block_test.rb b/plugins/statistics/test/unit/statistics_block_test.rb new file mode 100644 index 0000000..df4b188 --- /dev/null +++ b/plugins/statistics/test/unit/statistics_block_test.rb @@ -0,0 +1,336 @@ +require File.dirname(__FILE__) + '/../test_helper' +class StatisticsBlockTest < ActiveSupport::TestCase + + ['user_counter', 'tag_counter', 'comment_counter'].map do |counter| + should "#{counter} be true by default" do + b = StatisticsBlock.new + assert b.is_visible?(counter) + end + end + + ['community_counter', 'enterprise_counter', 'category_counter', 'hit_counter'].map do |counter| + should "#{counter} be false by default" do + b = StatisticsBlock.new + assert !b.is_visible?(counter) + end + end + + should 'inherit from Block' do + assert_kind_of Block, StatisticsBlock.new + end + + should 'provide a default title' do + block = StatisticsBlock.new + + owner = mock + owner.expects(:name).returns('my environment') + block.expects(:owner).returns(owner) + assert_equal 'Statistics for my environment', block.title + end + + should 'describe itself' do + assert_not_equal StatisticsBlock.description, Block.description + end + + should 'is_visible? return true if setting is true' do + b = StatisticsBlock.new + b.community_counter = true + assert b.is_visible?('community_counter') + end + + should 'is_visible? return false if setting is false' do + b = StatisticsBlock.new + b.community_counter = false + assert !b.is_visible?('community_counter') + end + + should 'templates return the Community templates of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + t1 = fast_create(Community, :is_template => true, :environment_id => e.id) + t2 = fast_create(Community, :is_template => true, :environment_id => e.id) + fast_create(Community, :is_template => false) + + b.expects(:owner).at_least_once.returns(e) + + t = b.templates + assert_equal [], [t1,t2] - t + assert_equal [], t - [t1,t2] + end + + should 'users return the amount of users of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + fast_create(Person, :environment_id => e.id) + fast_create(Person, :environment_id => e.id) + fast_create(Person, :visible => false, :environment_id => e.id) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 2, b.users + end + + should 'users return the amount of members of the community' do + b = StatisticsBlock.new + + c1 = fast_create(Community) + c1.add_member(fast_create(Person)) + c1.add_member(fast_create(Person)) + c1.add_member(fast_create(Person)) + c1.add_member(fast_create(Person, :visible => false)) + c1.add_member(fast_create(Person, :visible => false)) + + b.expects(:owner).at_least_once.returns(c1) + assert_equal 3, b.users + end + + should 'users return the amount of friends of the person' do + b = StatisticsBlock.new + + p1 = fast_create(Person) + p1.add_friend(fast_create(Person)) + p1.add_friend(fast_create(Person)) + p1.add_friend(fast_create(Person)) + p1.add_friend(fast_create(Person, :visible => false)) + p1.add_friend(fast_create(Person, :visible => false)) + + b.expects(:owner).at_least_once.returns(p1) + assert_equal 3, b.users + end + + should 'communities return the amount of communities of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + fast_create(Community, :environment_id => e.id) + fast_create(Community, :environment_id => e.id) + fast_create(Community, :visible => false, :environment_id => e.id) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 2, b.communities + end + + should 'enterprises return the amount of enterprises of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + fast_create(Enterprise, :environment_id => e.id) + fast_create(Enterprise, :environment_id => e.id) + fast_create(Enterprise, :visible => false, :environment_id => e.id) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 2, b.enterprises + end + + should 'categories return the amount of categories of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + fast_create(Category, :environment_id => e.id) + fast_create(Category, :environment_id => e.id) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 2, b.categories + end + + should 'tags return the amount of tags of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + p1 = fast_create(Person, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => p1.id) + t1 = fast_create(Tag, :name => 'T1') + t2 = fast_create(Tag, :name => 'T2') + a1.tags << t1 + a1.tags << t2 + a2 = fast_create(Article, :profile_id => p1.id) + t3 = fast_create(Tag, :name => 'T3') + t4 = fast_create(Tag, :name => 'T4') + a2.tags << t3 + a2.tags << t4 + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 4, b.tags + end + + should 'tags return the amount of tags of the community' do + b = StatisticsBlock.new + e = fast_create(Environment) + + c1 = fast_create(Community, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => c1.id) + t1 = fast_create(Tag, :name => 'T1') + t2 = fast_create(Tag, :name => 'T2') + a1.tags << t1 + a1.tags << t2 + a2 = fast_create(Article, :profile_id => c1.id) + t3 = fast_create(Tag, :name => 'T3') + t4 = fast_create(Tag, :name => 'T4') + a2.tags << t3 + a2.tags << t4 + + b.expects(:owner).at_least_once.returns(c1) + + assert_equal 4, b.tags + end + + should 'tags return the amount of tags of the profile (person)' do + b = StatisticsBlock.new + e = fast_create(Environment) + + p1 = fast_create(Person, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => p1.id) + t1 = fast_create(Tag, :name => 'T1') + t2 = fast_create(Tag, :name => 'T2') + a1.tags << t1 + a1.tags << t2 + a2 = fast_create(Article, :profile_id => p1.id) + t3 = fast_create(Tag, :name => 'T3') + t4 = fast_create(Tag, :name => 'T4') + a2.tags << t3 + a2.tags << t4 + + b.expects(:owner).at_least_once.returns(p1) + + assert_equal 4, b.tags + end + + should 'comments return the amount of comments of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + p1 = fast_create(Person, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => p1.id) + + Comment.create!(:source => a1, :body => 'C1', :author_id => 1) + Comment.create!(:source => a1, :body => 'C2', :author_id => 1) + + a2 = fast_create(Article, :profile_id => p1.id) + Comment.create!(:source => a2, :body => 'C3', :author_id => 1) + Comment.create!(:source => a2, :body => 'C4', :author_id => 1) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 4, b.comments + end + + should 'comments return the amount of comments of the community' do + b = StatisticsBlock.new + e = Environment.default + + c1 = fast_create(Community, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => c1.id) + Comment.create!(:source => a1, :body => 'C1', :author_id => 1) + Comment.create!(:source => a1, :body => 'C2', :author_id => 1) + + a2 = fast_create(Article, :profile_id => c1.id) + Comment.create!(:source => a2, :body => 'C3', :author_id => 1) + Comment.create!(:source => a2, :body => 'C4', :author_id => 1) + + b.expects(:owner).at_least_once.returns(c1) + + assert_equal 4, b.comments + end + + should 'comments return the amount of comments of the profile (person)' do + b = StatisticsBlock.new + e = fast_create(Environment) + + p1 = fast_create(Person, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => p1.id) + Comment.create!(:source => a1, :body => 'C1', :author_id => 1) + Comment.create!(:source => a1, :body => 'C2', :author_id => 1) + + a2 = fast_create(Article, :profile_id => p1.id) + Comment.create!(:source => a1, :body => 'C3', :author_id => 1) + Comment.create!(:source => a1, :body => 'C4', :author_id => 1) + + b.expects(:owner).at_least_once.returns(p1) + + assert_equal 4, b.comments + end + + should 'hits return the amount of hits of the Environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + p1 = fast_create(Person, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => p1.id, :hits => 2) + a2 = fast_create(Article, :profile_id => p1.id, :hits => 5) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 7, b.hits + end + + should 'hits return the amount of hits of the community' do + b = StatisticsBlock.new + e = fast_create(Environment) + + c1 = fast_create(Community, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => c1.id, :hits => 2) + a2 = fast_create(Article, :profile_id => c1.id, :hits => 5) + + b.expects(:owner).at_least_once.returns(c1) + + assert_equal 7, b.hits + end + + should 'hits return the amount of hits of the profile (person)' do + b = StatisticsBlock.new + e = fast_create(Environment) + + p1 = fast_create(Person, :environment_id => e.id) + a1 = fast_create(Article, :profile_id => p1.id, :hits => 2) + a2 = fast_create(Article, :profile_id => p1.id, :hits => 5) + + b.expects(:owner).at_least_once.returns(p1) + + assert_equal 7, b.hits + end + + should 'is_counter_available? return true for all counters if owner is environment' do + b = StatisticsBlock.new + e = fast_create(Environment) + + b.expects(:owner).at_least_once.returns(e) + + assert b.is_counter_available?(:user_counter) + end + + should 'is_template_counter_active? return true if setting is true' do + b = StatisticsBlock.new + b.templates_ids_counter = {'1' => 'true'} + assert b.is_template_counter_active?(1) + end + + should 'is_template_counter_active? return false if setting is false' do + b = StatisticsBlock.new + b.templates_ids_counter = {'1' => 'false'} + assert !b.is_template_counter_active?(1) + end + + should 'template_counter_count return the amount of communities of the Environment using a template' do + b = StatisticsBlock.new + e = fast_create(Environment) + + t1 = fast_create(Community, :is_template => true, :environment_id => e.id) + t2 = fast_create(Community, :is_template => true, :environment_id => e.id) + fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t1.id, :visible => true) + fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t1.id, :visible => true) + fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t1.id, :visible => false) + + fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t2.id, :visible => true) + fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t2.id, :visible => false) + + b.expects(:owner).at_least_once.returns(e) + + assert_equal 2, b.template_counter_count(t1.id) + end +end diff --git a/plugins/statistics/test/unit/statistics_plugin_test.rb b/plugins/statistics/test/unit/statistics_plugin_test.rb new file mode 100644 index 0000000..da9c0e2 --- /dev/null +++ b/plugins/statistics/test/unit/statistics_plugin_test.rb @@ -0,0 +1,9 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class StatisticsPluginTest < ActiveSupport::TestCase + + should "return StatisticsBlock in extra_mlocks class method" do + assert StatisticsPlugin.extra_blocks.keys.include?(StatisticsBlock) + end + +end diff --git a/plugins/statistics/views/box_organizer/_statistics_block.rhtml b/plugins/statistics/views/box_organizer/_statistics_block.rhtml new file mode 100644 index 0000000..7bbe640 --- /dev/null +++ b/plugins/statistics/views/box_organizer/_statistics_block.rhtml @@ -0,0 +1,32 @@ +<%= labelled_form_field check_box(:block, :user_counter) + _('Show user counter'), '' %> + +<% if @block.is_counter_available?(:community_counter) %> +<%= labelled_form_field check_box(:block, :community_counter) + _('Show community counter'), '' %> +<% end %> + +<% if @block.is_counter_available?(:enterprise_counter) %> +<%= labelled_form_field check_box(:block, :enterprise_counter) + _('Show enterprise counter'), '' %> +<% end %> + +<% if @block.is_counter_available?(:category_counter) %> +<%= labelled_form_field check_box(:block, :category_counter) + _('Show category counter'), '' %> +<% end %> + +<% if @block.is_counter_available?(:tag_counter) %> +<%= labelled_form_field check_box(:block, :tag_counter) + _('Show tag counter'), '' %> +<% end %> + +<% if @block.is_counter_available?(:comment_counter) %> +<%= labelled_form_field check_box(:block, :comment_counter) + _('Show comment counter'), '' %> +<% end %> + +<% if @block.is_counter_available?(:hit_counter) %> +<%= labelled_form_field check_box(:block, :hit_counter) + _('Show hit counter'), '' %> +<% end %> + +<% if @block.is_counter_available?(:templates_ids_counter) %> +<% @block.templates.map do |item|%> + <%= hidden_field_tag("block[templates_ids_counter][#{item.id}]", false)%> + <%= labelled_form_field check_box_tag("block[templates_ids_counter][#{item.id}]", true, @block.is_template_counter_active?(item.id)) + _("Show counter for communities with template %s" % item.name), '' %> +<% end %> +<% end %> diff --git a/plugins/statistics/views/environment_design b/plugins/statistics/views/environment_design new file mode 120000 index 0000000..a75d184 --- /dev/null +++ b/plugins/statistics/views/environment_design @@ -0,0 +1 @@ +box_organizer \ No newline at end of file diff --git a/plugins/statistics/views/profile_design b/plugins/statistics/views/profile_design new file mode 120000 index 0000000..a75d184 --- /dev/null +++ b/plugins/statistics/views/profile_design @@ -0,0 +1 @@ +box_organizer \ No newline at end of file diff --git a/plugins/statistics/views/statistics_block.rhtml b/plugins/statistics/views/statistics_block.rhtml new file mode 100644 index 0000000..f9f1374 --- /dev/null +++ b/plugins/statistics/views/statistics_block.rhtml @@ -0,0 +1,36 @@ +

+ <%=block.title%> +

+
+
    + <% if block.is_visible?('user_counter') %> +
  • <%= block.users%> <%= _('users')%>
  • + <% end %> + <% if block.is_visible?('enterprise_counter') && !block.environment.enabled?('disable_asset_enterprises') %> +
  • <%= block.enterprises%> <%= _('enterprises')%>
  • + <% end %> + <% if block.is_visible?('community_counter') %> +
  • <%= block.communities%> <%= _('communities')%>
  • + <% end %> + <% if block.is_visible?('category_counter') %> +
  • <%= block.categories%> <%= _('categories')%>
  • + <% end %> + <% if block.is_visible?('tag_counter') %> +
  • <%= block.tags%> <%= _('tags')%>
  • + <% end %> + <% if block.is_visible?('comment_counter') %> +
  • <%= block.comments%> <%= _('comments')%>
  • + <% end %> + <% if block.is_visible?('hit_counter') %> +
  • <%= block.hits%> <%= _('hits')%>
  • + <% end %> + + <% if block.owner.kind_of?(Environment) then %> + <% block.templates.each do |item| %> + <% if block.is_template_counter_active? item.id %> +
  • <%= block.template_counter_count(item.id)%> <%= item.name%>
  • + <% end %> + <% end %> + <% end %> +
+
diff --git a/test/functional/environment_design_controller_test.rb b/test/functional/environment_design_controller_test.rb index 1c473fe..cc80649 100644 --- a/test/functional/environment_design_controller_test.rb +++ b/test/functional/environment_design_controller_test.rb @@ -6,7 +6,7 @@ class EnvironmentDesignController; def rescue_action(e) raise e end; end class EnvironmentDesignControllerTest < ActionController::TestCase - ALL_BLOCKS = [ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] + ALL_BLOCKS = [ArticleBlock, LoginBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] def setup @controller = EnvironmentDesignController.new @@ -83,16 +83,6 @@ class EnvironmentDesignControllerTest < ActionController::TestCase assert_tag :tag => 'p', :attributes => { :id => 'no_portal_community' } end - should 'be able to edit EnvironmentStatisticsBlock' do - login_as(create_admin_user(Environment.default)) - b = EnvironmentStatisticsBlock.create! - e = Environment.default - e.boxes.create! - e.boxes.first.blocks << b - get :edit, :id => b.id - assert_tag :tag => 'input', :attributes => { :id => 'block_title' } - end - should 'be able to edit EnterprisesBlock' do login_as(create_admin_user(Environment.default)) b = EnterprisesBlock.create! diff --git a/test/unit/box_test.rb b/test/unit/box_test.rb index 6978167..a1bf07f 100644 --- a/test/unit/box_test.rb +++ b/test/unit/box_test.rb @@ -33,7 +33,6 @@ class BoxTest < ActiveSupport::TestCase assert blocks.include?('categories-block') assert blocks.include?('communities-block') assert blocks.include?('enterprises-block') - assert blocks.include?('environment-statistics-block') assert blocks.include?('fans-block') assert blocks.include?('favorite-enterprises-block') assert blocks.include?('feed-reader-block') @@ -65,7 +64,6 @@ class BoxTest < ActiveSupport::TestCase assert blocks.include?('communities-block') assert blocks.include?('disabled-enterprise-message-block') assert blocks.include?('enterprises-block') - assert blocks.include?('environment-statistics-block') assert blocks.include?('fans-block') assert blocks.include?('favorite-enterprises-block') assert blocks.include?('featured-products-block') diff --git a/test/unit/environment_statistics_block_test.rb b/test/unit/environment_statistics_block_test.rb deleted file mode 100644 index 030691c..0000000 --- a/test/unit/environment_statistics_block_test.rb +++ /dev/null @@ -1,96 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class EnvironmentStatisticsBlockTest < ActiveSupport::TestCase - - should 'inherit from Block' do - assert_kind_of Block, EnvironmentStatisticsBlock.new - end - - should 'describe itself' do - assert_not_equal Block.description, EnvironmentStatisticsBlock.description - end - - should 'provide a default title' do - owner = mock - owner.expects(:name).returns('my environment') - - block = EnvironmentStatisticsBlock.new - block.expects(:owner).returns(owner) - assert_equal 'Statistics for my environment', block.title - end - - should 'generate statistics' do - env = create(Environment) - user1 = create_user('testuser1', :environment_id => env.id) - user2 = create_user('testuser2', :environment_id => env.id) - - fast_create(Enterprise, :environment_id => env.id) - fast_create(Community, :environment_id => env.id) - - block = EnvironmentStatisticsBlock.new - env.boxes.first.blocks << block - - content = block.content - - assert_match(/One enterprise/, content) - assert_match(/2 users/, content) - assert_match(/One community/, content) - end - - should 'generate statistics including private profiles' do - env = create(Environment) - user1 = create_user('testuser1', :environment_id => env.id) - user2 = create_user('testuser2', :environment_id => env.id) - user3 = create_user('testuser3', :environment_id => env.id) - p = user3.person; p.public_profile = false; p.save! - - fast_create(Enterprise, :environment_id => env.id) - fast_create(Enterprise, :environment_id => env.id, :public_profile => false) - - fast_create(Community, :environment_id => env.id) - fast_create(Community, :environment_id => env.id, :public_profile => false) - - block = EnvironmentStatisticsBlock.new - env.boxes.first.blocks << block - - content = block.content - - assert_match /2 enterprises/, content - assert_match /3 users/, content - assert_match /2 communities/, content - end - - should 'generate statistics but not for not visible profiles' do - env = create(Environment) - user1 = create_user('testuser1', :environment_id => env.id) - user2 = create_user('testuser2', :environment_id => env.id) - user3 = create_user('testuser3', :environment_id => env.id) - p = user3.person; p.visible = false; p.save! - - fast_create(Enterprise, :environment_id => env.id) - fast_create(Enterprise, :environment_id => env.id, :visible => false) - - fast_create(Community, :environment_id => env.id) - fast_create(Community, :environment_id => env.id, :visible => false) - - block = EnvironmentStatisticsBlock.new - env.boxes.first.blocks << block - - content = block.content - - assert_match /One enterprise/, content - assert_match /2 users/, content - assert_match /One community/, content - end - - should 'not display enterprises if disabled' do - env = fast_create(Environment) - env.enable('disable_asset_enterprises', false) - - block = EnvironmentStatisticsBlock.new - block.stubs(:owner).returns(env) - - assert_no_match /enterprises/i, block.content - end - -end -- libgit2 0.21.2