From 03d5c15ca9c4ce80d805dd34cfe642999a014a93 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 11 Mar 2015 17:00:40 -0300 Subject: [PATCH] Fix the use of theme images block store --- app/models/theme.rb | 13 ++++++++++++- test/unit/box_organizer_helper_test.rb | 312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 test/unit/box_organizer_helper_test.rb diff --git a/app/models/theme.rb b/app/models/theme.rb index 2a6a958..8c22de1 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -13,8 +13,14 @@ class Theme Rails.root.join('public', 'user_themes') end + #FIXME make this test def system_themes_dir - Rails.root.join('public', 'designs', 'themes') + Rails.root.join('public', relative_themes_dir) + end + + #FIXME make this test + def relative_themes_dir + File.join('designs', 'themes') end def create(id, attributes = {}) @@ -94,6 +100,11 @@ class Theme end #FIXME make this test + def public_path + File.join('/', self.class.relative_themes_dir, self.id) + end + + #FIXME make this test def filesystem_path File.join(self.class.system_themes_dir, self.id) end diff --git a/test/unit/box_organizer_helper_test.rb b/test/unit/box_organizer_helper_test.rb new file mode 100644 index 0000000..d8dc65d --- /dev/null +++ b/test/unit/box_organizer_helper_test.rb @@ -0,0 +1,312 @@ +# encoding: UTF-8 +require File.dirname(__FILE__) + '/../test_helper' + +class BoxOrganizerHelperTest < ActionView::TestCase + + + def setup + @environment = Environment.default + end + + attr_reader :environment + + should 'display the default icon for block without icon' do + class SomeBlock < Block; end + block = SomeBlock + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + assert_match '/images/icon_block.png', display_icon(block) + end + + should 'display the icon block' do + class SomeBlock < Block; end + block = SomeBlock + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'images', '/blocks/some_block/icon.png')).returns(true) + assert_match 'blocks/some_block/icon.png', display_icon(block) + end + + should 'display the plugin icon block' do + class SomeBlock < Block; end + block = SomeBlock + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'plugins/some/images/blocks/some_block/icon.png')).returns(true) + assert_match 'plugins/some/images/blocks/some_block/icon.png', display_icon(block) + end + + should 'display the theme icon block' do + class SomeBlock < Block; end + block = SomeBlock + + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'designs/themes/some_theme/images/blocks/some_block/icon.png')).returns(true) + assert_match 'designs/themes/some_theme/images/blocks/some_block/icon.png', display_icon(block) + end + + should 'display the theme icon block instead of block icon' do + class SomeBlock < Block; end + block = SomeBlock + + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'designs/themes/some_theme/images/blocks/some_block/icon.png')).returns(true) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'images', '/blocks/some_block/icon.png')).returns(true) + assert_match 'designs/themes/some_theme/images/blocks/some_block/icon.png', display_icon(block) + end + + should 'display the theme icon block instead of plugin block icon' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'designs/themes/some_theme/images/blocks/some_block/icon.png')).returns(true) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'plugins/some/images/blocks/some_block/icon.png')).returns(true) + assert_match 'designs/themes/some_theme/images/blocks/some_block/icon.png', display_icon(block) + end + + should 'display the theme icon block instead of block icon and plugin icon' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'designs/themes/some_theme/images/blocks/some_block/icon.png')).returns(true) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'plugins/some/images/blocks/some_block/icon.png')).returns(true) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'images', '/blocks/some_block/icon.png')).returns(true) + assert_match 'designs/themes/some_theme/images/blocks/some_block/icon.png', display_icon(block) + end + + should 'display the plugin icon block instead of block icon' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + + File.stubs(:exists?).returns(false) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'plugins/some/images/blocks/some_block/icon.png')).returns(true) + File.stubs(:exists?).with(File.join(Rails.root, 'public', 'images', '/blocks/some_block/icon.png')).returns(true) + assert_match 'plugins/some/images/blocks/some_block/icon.png', display_icon(block) + end + + should 'display the default preview for block without previews images' do + class SomeBlock < Block; end + block = SomeBlock + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[0].to_s + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[1].to_s + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[2].to_s + end + end + + should 'display the previews of block' do + class SomeBlock < Block; end + block = SomeBlock + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + end + + should 'display the plugin preview images of block' do + class SomeBlock < Block; end + block = SomeBlock + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the theme previews of block' do + class SomeBlock < Block; end + block = SomeBlock + + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the theme preview images of block instead of block preview images' do + class SomeBlock < Block; end + block = SomeBlock + + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + end + + should 'display the theme preview images of block instead of plugin preview images' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the theme preview images of block instead of block previews and plugin previews' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the plugin preview images of block instead of block previews' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + +end -- libgit2 0.21.2