Commit c729594dda684843f4fea01a4e588c31c2efa49d

Authored by Daniela Feitosa
2 parents 9258c291 95080411

Merge remote branch 'remotes/diguliu/language-selection' into language-selection

Conflicts:
	app/sweepers/article_sweeper.rb
	db/schema.rb
	test/unit/environment_test.rb
app/controllers/admin/admin_panel_controller.rb
@@ -8,6 +8,9 @@ class AdminPanelController < AdminController @@ -8,6 +8,9 @@ class AdminPanelController < AdminController
8 8
9 def site_info 9 def site_info
10 if request.post? 10 if request.post?
  11 + if params[:environment][:languages]
  12 + params[:environment][:languages] = params[:environment][:languages].map {|lang, value| lang if value=='true'}.compact
  13 + end
11 if @environment.update_attributes(params[:environment]) 14 if @environment.update_attributes(params[:environment])
12 session[:notice] = _('Environment settings updated') 15 session[:notice] = _('Environment settings updated')
13 redirect_to :action => 'index' 16 redirect_to :action => 'index'
app/controllers/application_controller.rb
@@ -42,9 +42,9 @@ class ApplicationController < ActionController::Base @@ -42,9 +42,9 @@ class ApplicationController < ActionController::Base
42 42
43 before_filter :set_locale 43 before_filter :set_locale
44 def set_locale 44 def set_locale
45 - FastGettext.available_locales = Noosfero.available_locales  
46 - FastGettext.default_locale = Noosfero.default_locale  
47 - FastGettext.locale = (params[:lang] || session[:lang] || Noosfero.default_locale || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en') 45 + FastGettext.available_locales = environment.available_locales
  46 + FastGettext.default_locale = environment.default_locale
  47 + FastGettext.locale = (params[:lang] || session[:lang] || environment.default_locale || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
48 I18n.locale = FastGettext.locale 48 I18n.locale = FastGettext.locale
49 if params[:lang] 49 if params[:lang]
50 session[:lang] = params[:lang] 50 session[:lang] = params[:lang]
app/controllers/my_profile/cms_controller.rb
@@ -116,11 +116,11 @@ class CmsController < MyProfileController @@ -116,11 +116,11 @@ class CmsController < MyProfileController
116 @parent_id = parent.id 116 @parent_id = parent.id
117 end 117 end
118 118
119 - translations if @article.translatable?  
120 -  
121 @article.profile = profile 119 @article.profile = profile
122 @article.last_changed_by = user 120 @article.last_changed_by = user
123 121
  122 + translations if @article.translatable?
  123 +
124 continue = params[:continue] 124 continue = params[:continue]
125 if request.post? 125 if request.post?
126 if @article.save 126 if @article.save
@@ -344,7 +344,7 @@ class CmsController < MyProfileController @@ -344,7 +344,7 @@ class CmsController < MyProfileController
344 end 344 end
345 345
346 def translations 346 def translations
347 - @locales = Noosfero.locales.invert.reject { |name, lang| !@article.possible_translations.include?(lang) } 347 + @locales = environment.locales.invert.reject { |name, lang| !@article.possible_translations.include?(lang) }
348 @selected_locale = @article.language || FastGettext.locale 348 @selected_locale = @article.language || FastGettext.locale
349 end 349 end
350 350
app/helpers/content_viewer_helper.rb
@@ -42,7 +42,7 @@ module ContentViewerHelper @@ -42,7 +42,7 @@ module ContentViewerHelper
42 def article_translations(article) 42 def article_translations(article)
43 unless article.native_translation.translations.empty? 43 unless article.native_translation.translations.empty?
44 links = (article.native_translation.translations + [article.native_translation]).map do |translation| 44 links = (article.native_translation.translations + [article.native_translation]).map do |translation|
45 - { Noosfero.locales[translation.language] => { :href => url_for(translation.url) } } 45 + { article.environment.locales[translation.language] => { :href => url_for(translation.url) } }
46 end 46 end
47 content_tag(:div, link_to(_('Translations'), '#', 47 content_tag(:div, link_to(_('Translations'), '#',
48 :onclick => "toggleSubmenu(this, '#{_('Translations')}', #{links.to_json}); return false", 48 :onclick => "toggleSubmenu(this, '#{_('Translations')}', #{links.to_json}); return false",
app/helpers/forms_helper.rb
@@ -123,6 +123,25 @@ module FormsHelper @@ -123,6 +123,25 @@ module FormsHelper
123 options_for_select.join("\n") 123 options_for_select.join("\n")
124 end 124 end
125 125
  126 + def balanced_table(items, per_row=3)
  127 + items = items.map {|item| content_tag('td', item, :style => 'border: none; background: transparent;')}
  128 + rows = []
  129 + row = []
  130 + counter = 0
  131 + items.each do |item|
  132 + counter += 1
  133 + row << item
  134 + if counter % per_row == 0
  135 + rows << content_tag('tr', row.join("\n"))
  136 + counter = 0
  137 + row = []
  138 + end
  139 + end
  140 + rows << content_tag('tr', row.join("\n"))
  141 +
  142 + content_tag('table',rows.join("\n"))
  143 + end
  144 +
126 def date_field(name, value, format = '%Y-%m-%d', datepicker_options = {}, html_options = {}) 145 def date_field(name, value, format = '%Y-%m-%d', datepicker_options = {}, html_options = {})
127 datepicker_options[:disabled] ||= false 146 datepicker_options[:disabled] ||= false
128 datepicker_options[:alt_field] ||= '' 147 datepicker_options[:alt_field] ||= ''
app/helpers/language_helper.rb
@@ -13,18 +13,19 @@ module LanguageHelper @@ -13,18 +13,19 @@ module LanguageHelper
13 13
14 alias :calendar_date_select_language :tinymce_language 14 alias :calendar_date_select_language :tinymce_language
15 15
16 - def language_chooser(options = {}) 16 + def language_chooser(environment, options = {})
  17 + return if environment.locales.size < 2
17 current = language 18 current = language
18 separator = options[:separator] || ' &mdash; ' 19 separator = options[:separator] || ' &mdash; '
19 20
20 if options[:element] == 'dropdown' 21 if options[:element] == 'dropdown'
21 select_tag('lang', 22 select_tag('lang',
22 - options_for_select(Noosfero.locales.map{|code,name| [name, code]}, current), 23 + options_for_select(environment.locales.map{|code,name| [name, code]}, current),
23 :onchange => "document.location.href= #{url_for(params.merge(:lang => 'LANGUAGE')).inspect}.replace(/LANGUAGE/, this.value) ;", 24 :onchange => "document.location.href= #{url_for(params.merge(:lang => 'LANGUAGE')).inspect}.replace(/LANGUAGE/, this.value) ;",
24 :help => _('The language you choose here is the language used for options, buttons, etc. It does not affect the language of the content created by other users.') 25 :help => _('The language you choose here is the language used for options, buttons, etc. It does not affect the language of the content created by other users.')
25 ) 26 )
26 else 27 else
27 - languages = Noosfero.locales.map do |code,name| 28 + languages = environment.locales.map do |code,name|
28 if code == current 29 if code == current
29 content_tag('strong', name) 30 content_tag('strong', name)
30 else 31 else
app/models/article.rb
@@ -336,14 +336,14 @@ class Article &lt; ActiveRecord::Base @@ -336,14 +336,14 @@ class Article &lt; ActiveRecord::Base
336 end 336 end
337 337
338 def possible_translations 338 def possible_translations
339 - possibilities = Noosfero.locales.keys - self.native_translation.translations(:select => :language).map(&:language) - [self.native_translation.language] 339 + possibilities = environment.locales.keys - self.native_translation.translations(:select => :language).map(&:language) - [self.native_translation.language]
340 possibilities << self.language unless self.language_changed? 340 possibilities << self.language unless self.language_changed?
341 possibilities 341 possibilities
342 end 342 end
343 343
344 def known_language 344 def known_language
345 unless self.language.blank? 345 unless self.language.blank?
346 - errors.add(:language, N_('Language not supported by Noosfero')) unless Noosfero.locales.key?(self.language) 346 + errors.add(:language, N_('Language not supported by the environment.')) unless environment.locales.key?(self.language)
347 end 347 end
348 end 348 end
349 349
@@ -676,7 +676,7 @@ class Article &lt; ActiveRecord::Base @@ -676,7 +676,7 @@ class Article &lt; ActiveRecord::Base
676 self.categories.collect(&:name) 676 self.categories.collect(&:name)
677 end 677 end
678 678
679 - delegate :region, :region_id, :environment, :environment_id, :to => :profile 679 + delegate :region, :region_id, :environment, :environment_id, :to => :profile, :allow_nil => true
680 def name_sortable # give a different name for solr 680 def name_sortable # give a different name for solr
681 name 681 name
682 end 682 end
app/models/block.rb
@@ -7,6 +7,8 @@ class Block &lt; ActiveRecord::Base @@ -7,6 +7,8 @@ class Block &lt; ActiveRecord::Base
7 # Block-specific stuff 7 # Block-specific stuff
8 include BlockHelper 8 include BlockHelper
9 9
  10 + delegate :environment, :to => :box, :allow_nil => true
  11 +
10 acts_as_list :scope => :box 12 acts_as_list :scope => :box
11 belongs_to :box 13 belongs_to :box
12 14
app/models/box.rb
@@ -3,6 +3,10 @@ class Box &lt; ActiveRecord::Base @@ -3,6 +3,10 @@ class Box &lt; ActiveRecord::Base
3 acts_as_list :scope => 'owner_id = #{owner_id} and owner_type = \'#{owner_type}\'' 3 acts_as_list :scope => 'owner_id = #{owner_id} and owner_type = \'#{owner_type}\''
4 has_many :blocks, :dependent => :destroy, :order => 'position' 4 has_many :blocks, :dependent => :destroy, :order => 'position'
5 5
  6 + def environment
  7 + owner ? (owner.kind_of?(Environment) ? owner : owner.environment) : nil
  8 + end
  9 +
6 def acceptable_blocks 10 def acceptable_blocks
7 to_css_class_name central? ? Box.acceptable_center_blocks : Box.acceptable_side_blocks 11 to_css_class_name central? ? Box.acceptable_center_blocks : Box.acceptable_side_blocks
8 end 12 end
app/models/environment.rb
@@ -804,4 +804,53 @@ class Environment &lt; ActiveRecord::Base @@ -804,4 +804,53 @@ class Environment &lt; ActiveRecord::Base
804 def image_galleries 804 def image_galleries
805 portal_community ? portal_community.image_galleries : [] 805 portal_community ? portal_community.image_galleries : []
806 end 806 end
  807 +
  808 + serialize :languages
  809 +
  810 + before_validation do |environment|
  811 + environment.default_language = nil if environment.default_language.blank?
  812 + end
  813 +
  814 + validate :default_language_available
  815 + validate :languages_available
  816 +
  817 + def locales
  818 + if languages.present?
  819 + languages.inject({}) {|r, l| r.merge({l => Noosfero.locales[l]})}
  820 + else
  821 + Noosfero.locales
  822 + end
  823 + end
  824 +
  825 + def default_locale
  826 + default_language || Noosfero.default_locale
  827 + end
  828 +
  829 + def available_locales
  830 + locales_list = locales.keys
  831 + # move English to the beginning
  832 + if locales_list.include?('en')
  833 + locales_list = ['en'] + (locales_list - ['en']).sort
  834 + end
  835 + locales_list
  836 + end
  837 +
  838 + private
  839 +
  840 + def default_language_available
  841 + if default_language.present? && !available_locales.include?(default_language)
  842 + errors.add(:default_language, _('is not available.'))
  843 + end
  844 + end
  845 +
  846 + def languages_available
  847 + if languages.present?
  848 + languages.each do |language|
  849 + if !Noosfero.available_locales.include?(language)
  850 + errors.add(:languages, _('have unsupported languages.'))
  851 + break
  852 + end
  853 + end
  854 + end
  855 + end
807 end 856 end
app/sweepers/article_sweeper.rb
@@ -19,6 +19,7 @@ class ArticleSweeper &lt; ActiveRecord::Observer @@ -19,6 +19,7 @@ class ArticleSweeper &lt; ActiveRecord::Observer
19 protected 19 protected
20 20
21 def expire_caches(article) 21 def expire_caches(article)
  22 + return if !article.environment
22 article.hierarchy(true).each { |a| a.touch if a != article } 23 article.hierarchy(true).each { |a| a.touch if a != article }
23 blocks = article.profile.blocks 24 blocks = article.profile.blocks
24 blocks += article.profile.environment.blocks if article.profile.environment 25 blocks += article.profile.environment.blocks if article.profile.environment
@@ -26,7 +27,7 @@ protected @@ -26,7 +27,7 @@ protected
26 BlockSweeper.expire_blocks(blocks) 27 BlockSweeper.expire_blocks(blocks)
27 env = article.profile.environment 28 env = article.profile.environment
28 if env && (env.portal_community == article.profile) 29 if env && (env.portal_community == article.profile)
29 - Noosfero.locales.keys.each do |locale| 30 + article.environment.locales.keys.each do |locale|
30 expire_fragment(env.portal_news_cache_key(locale)) 31 expire_fragment(env.portal_news_cache_key(locale))
31 end 32 end
32 end 33 end
app/sweepers/block_sweeper.rb
@@ -7,10 +7,11 @@ class BlockSweeper &lt; ActiveRecord::Observer @@ -7,10 +7,11 @@ class BlockSweeper &lt; ActiveRecord::Observer
7 7
8 # Expire block's all languages cache 8 # Expire block's all languages cache
9 def expire_block(block) 9 def expire_block(block)
  10 + return if !block.environment
10 regex = '-[a-z]*$' 11 regex = '-[a-z]*$'
11 clean_ck = block.cache_key.gsub(/#{regex}/,'') 12 clean_ck = block.cache_key.gsub(/#{regex}/,'')
12 13
13 - Noosfero.locales.keys.each do |locale| 14 + block.environment.locales.keys.each do |locale|
14 expire_timeout_fragment("#{clean_ck}-#{locale}") 15 expire_timeout_fragment("#{clean_ck}-#{locale}")
15 end 16 end
16 end 17 end
app/views/admin_panel/_site_info.rhtml
1 <%= required labelled_form_field(_('Site name'), text_field(:environment, :name)) %> 1 <%= required labelled_form_field(_('Site name'), text_field(:environment, :name)) %>
  2 +<%= labelled_form_field(_('Contact email'), text_field(:environment, :contact_email)) %>
  3 +<% themes_options = Theme.system_themes.map {|theme| [theme.name, theme.id] }.sort %>
  4 +<%= labelled_form_field(_('Theme'), select(:environment, :theme, options_for_select(themes_options, environment.theme))) %>
2 <%= required f.text_field(:reports_lower_bound, :size => 3) %> 5 <%= required f.text_field(:reports_lower_bound, :size => 3) %>
  6 +<%= labelled_form_field(_('Default language'), select(:environment, :default_language, environment.locales.invert, { :selected => environment.default_locale, :include_blank => true })) %>
  7 +<%= label_tag :languages, _('Available languages') %>
  8 +<br />
  9 +
  10 +<%
  11 + fields = Noosfero.locales.map do |value, name|
  12 + labelled_check_box(name, "environment[languages][#{value}]", true, environment.available_locales.include?(value))
  13 + end
  14 +%>
  15 +<%= balanced_table(fields)%>
  16 +
  17 +<br />
3 <%= labelled_form_field _('Homepage content'), text_area(:environment, :description, :cols => 40, :style => 'width: 90%', :class => 'mceEditor') %> 18 <%= labelled_form_field _('Homepage content'), text_area(:environment, :description, :cols => 40, :style => 'width: 90%', :class => 'mceEditor') %>
app/views/box_organizer/edit.rhtml
@@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
22 <%= label_tag('block_display_never', _("Don't display")) %> 22 <%= label_tag('block_display_never', _("Don't display")) %>
23 </div> 23 </div>
24 24
25 - <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + Noosfero.locales.map {|key, value| [value, key]} )) %> 25 + <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + environment.locales.map {|key, value| [value, key]} )) %>
26 26
27 <% button_bar do %> 27 <% button_bar do %>
28 <%= submit_button(:save, _('Save')) %> 28 <%= submit_button(:save, _('Save')) %>
app/views/cms/_blog.rhtml
@@ -62,7 +62,7 @@ @@ -62,7 +62,7 @@
62 62
63 <% f.fields_for 'feed', @article.feed do |feed| %> 63 <% f.fields_for 'feed', @article.feed do |feed| %>
64 <%= labelled_form_field(_('Limit of posts in RSS Feed'), feed.select(:limit, [5, 10, 20, 50])) %> 64 <%= labelled_form_field(_('Limit of posts in RSS Feed'), feed.select(:limit, [5, 10, 20, 50])) %>
65 - <%= labelled_form_field(_('Include in RSS Feed only posts from language:'), feed.select(:language, [[_('All'), nil ]] + Noosfero.locales.map { |k,v| [v, k]})) %> 65 + <%= labelled_form_field(_('Include in RSS Feed only posts from language:'), feed.select(:language, [[_('All'), nil ]] + environment.locales.map { |k,v| [v, k]})) %>
66 <% end %> 66 <% end %>
67 67
68 <% f.fields_for 'external_feed_builder', @article.external_feed do |efeed| %> 68 <% f.fields_for 'external_feed_builder', @article.external_feed do |efeed| %>
app/views/cms/_rss_feed.rhtml
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 6
7 <%= required labelled_form_field(_('Limit of articles'), text_field(:article, :limit)) %> 7 <%= required labelled_form_field(_('Limit of articles'), text_field(:article, :limit)) %>
8 8
9 -<%= labelled_form_field(_('Include in RSS Feed only posts from language:'), f.select(:language, [[_('All'), nil ]] + Noosfero.locales.map { |k,v| [v, k]})) %> 9 +<%= labelled_form_field(_('Include in RSS Feed only posts from language:'), f.select(:language, [[_('All'), nil ]] + environment.locales.map { |k,v| [v, k]})) %>
10 10
11 <%= labelled_form_field(_('Use as item description:'), select(:article, :feed_item_description, [ [ _('Article abstract'), 'abstract'], [ _('Article body'), 'body']])) %> 11 <%= labelled_form_field(_('Use as item description:'), select(:article, :feed_item_description, [ [ _('Article abstract'), 'abstract'], [ _('Article body'), 'body']])) %>
12 12
app/views/layouts/application.rhtml
@@ -80,7 +80,7 @@ @@ -80,7 +80,7 @@
80 </div><!-- id='navigation_bar' --> 80 </div><!-- id='navigation_bar' -->
81 81
82 <div id="language-selector"> 82 <div id="language-selector">
83 - <%= language_chooser(:element => 'dropdown') %> 83 + <%= language_chooser(environment, :element => 'dropdown') %>
84 </div> 84 </div>
85 85
86 <div id="user_box"> 86 <div id="user_box">
db/migrate/20120823215007_add_languages_and_default_language_to_environment.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class AddLanguagesAndDefaultLanguageToEnvironment < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :environments, :languages, :string
  4 + add_column :environments, :default_language, :string
  5 + end
  6 +
  7 + def self.down
  8 + remove_column :environments, :languages
  9 + remove_column :environments, :default_language
  10 + end
  11 +end
@@ -263,6 +263,8 @@ ActiveRecord::Schema.define(:version =&gt; 20121008185303) do @@ -263,6 +263,8 @@ ActiveRecord::Schema.define(:version =&gt; 20121008185303) do
263 t.integer "reports_lower_bound", :default => 0, :null => false 263 t.integer "reports_lower_bound", :default => 0, :null => false
264 t.string "redirection_after_login", :default => "keep_on_same_page" 264 t.string "redirection_after_login", :default => "keep_on_same_page"
265 t.text "signup_welcome_text" 265 t.text "signup_welcome_text"
  266 + t.string "languages"
  267 + t.string "default_language"
266 end 268 end
267 269
268 create_table "external_feeds", :force => true do |t| 270 create_table "external_feeds", :force => true do |t|
public/designs/themes/base/footer.rhtml
@@ -4,4 +4,4 @@ @@ -4,4 +4,4 @@
4 <div id="copyright"> 4 <div id="copyright">
5 <p><%= _('This social network uses <a href="http://noosfero.org/">Noosfero</a>, developed by %s and licensed under the <a href="http://www.gnu.org/licenses/agpl.html">GNU Affero General Public License</a> version 3 or any later version.') % link_to('Colivre', 'http://colivre.coop.br/') %></p> 5 <p><%= _('This social network uses <a href="http://noosfero.org/">Noosfero</a>, developed by %s and licensed under the <a href="http://www.gnu.org/licenses/agpl.html">GNU Affero General Public License</a> version 3 or any later version.') % link_to('Colivre', 'http://colivre.coop.br/') %></p>
6 </div><!-- end id="copyright" --> 6 </div><!-- end id="copyright" -->
7 -<%= language_chooser %> 7 +<%= language_chooser(environment) %>
test/functional/admin_panel_controller_test.rb
@@ -372,4 +372,13 @@ class AdminPanelControllerTest &lt; ActionController::TestCase @@ -372,4 +372,13 @@ class AdminPanelControllerTest &lt; ActionController::TestCase
372 assert_tag :tag => 'a', :content => /Plugin2 link/, :attributes => {:href => /plugin2.com/} 372 assert_tag :tag => 'a', :content => /Plugin2 link/, :attributes => {:href => /plugin2.com/}
373 end 373 end
374 374
  375 + should 'save available languages and default language properly' do
  376 + post :site_info, :environment => {:default_language => 'pt', :languages => {'pt' => 'true', 'en' => 'false'}}
  377 + environment = Environment.default
  378 +
  379 + assert_equal 'pt', environment.default_language
  380 + assert_includes environment.languages, 'pt'
  381 + assert_not_includes environment.languages, 'en'
  382 + end
  383 +
375 end 384 end
test/unit/article_test.rb
@@ -1210,8 +1210,8 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1210,8 +1210,8 @@ class ArticleTest &lt; ActiveSupport::TestCase
1210 assert_nothing_raised { a.language = 'en' } 1210 assert_nothing_raised { a.language = 'en' }
1211 end 1211 end
1212 1212
1213 - should 'validade inclusion of language' do  
1214 - a = build(Article) 1213 + should 'validate inclusion of language' do
  1214 + a = build(Article, :profile_id => fast_create(Profile).id)
1215 a.language = '12' 1215 a.language = '12'
1216 a.valid? 1216 a.valid?
1217 assert a.errors.invalid?(:language) 1217 assert a.errors.invalid?(:language)
@@ -1243,7 +1243,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1243,7 +1243,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1243 end 1243 end
1244 1244
1245 should 'list possible translations' do 1245 should 'list possible translations' do
1246 - native_article = fast_create(Article, :language => 'pt') 1246 + native_article = fast_create(Article, :language => 'pt', :profile_id => fast_create(Profile).id )
1247 article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id) 1247 article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id)
1248 possible_translations = native_article.possible_translations 1248 possible_translations = native_article.possible_translations
1249 assert !possible_translations.include?('en') 1249 assert !possible_translations.include?('en')
@@ -1253,7 +1253,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1253,7 +1253,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1253 should 'verify if translation is already in use' do 1253 should 'verify if translation is already in use' do
1254 native_article = fast_create(Article, :language => 'pt') 1254 native_article = fast_create(Article, :language => 'pt')
1255 article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id) 1255 article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id)
1256 - a = build(Article) 1256 + a = build(Article, :profile => fast_create(Profile))
1257 a.language = 'en' 1257 a.language = 'en'
1258 a.translation_of = native_article 1258 a.translation_of = native_article
1259 a.valid? 1259 a.valid?
@@ -1265,7 +1265,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1265,7 +1265,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1265 1265
1266 should 'verify if native translation is already in use' do 1266 should 'verify if native translation is already in use' do
1267 native_article = fast_create(Article, :language => 'pt') 1267 native_article = fast_create(Article, :language => 'pt')
1268 - a = build(Article) 1268 + a = build(Article, :profile => fast_create(Profile))
1269 a.language = 'pt' 1269 a.language = 'pt'
1270 a.translation_of = native_article 1270 a.translation_of = native_article
1271 a.valid? 1271 a.valid?
@@ -1277,7 +1277,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1277,7 +1277,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1277 1277
1278 should 'translation have a language' do 1278 should 'translation have a language' do
1279 native_article = fast_create(Article, :language => 'pt') 1279 native_article = fast_create(Article, :language => 'pt')
1280 - a = build(Article) 1280 + a = build(Article, :profile_id => fast_create(Profile).id)
1281 a.translation_of = native_article 1281 a.translation_of = native_article
1282 a.valid? 1282 a.valid?
1283 assert a.errors.invalid?(:language) 1283 assert a.errors.invalid?(:language)
@@ -1287,8 +1287,8 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1287,8 +1287,8 @@ class ArticleTest &lt; ActiveSupport::TestCase
1287 end 1287 end
1288 1288
1289 should 'native translation have a language' do 1289 should 'native translation have a language' do
1290 - native_article = fast_create(Article)  
1291 - a = build(Article) 1290 + native_article = fast_create(Article, :profile_id => fast_create(Profile).id )
  1291 + a = build(Article, :profile_id => fast_create(Profile).id)
1292 a.language = 'en' 1292 a.language = 'en'
1293 a.translation_of = native_article 1293 a.translation_of = native_article
1294 a.valid? 1294 a.valid?
@@ -1356,15 +1356,15 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -1356,15 +1356,15 @@ class ArticleTest &lt; ActiveSupport::TestCase
1356 end 1356 end
1357 1357
1358 should 'not list own language as a possible translation if language has changed' do 1358 should 'not list own language as a possible translation if language has changed' do
1359 - a = build(Article, :language => 'pt') 1359 + a = build(Article, :language => 'pt', :profile_id => fast_create(Profile).id)
1360 assert !a.possible_translations.include?('pt') 1360 assert !a.possible_translations.include?('pt')
1361 - a = fast_create(Article, :language => 'pt') 1361 + a = fast_create(Article, :language => 'pt', :profile_id => fast_create(Profile).id )
1362 a.language = 'en' 1362 a.language = 'en'
1363 assert !a.possible_translations.include?('en') 1363 assert !a.possible_translations.include?('en')
1364 end 1364 end
1365 1365
1366 should 'list own language as a possible translation if language has not changed' do 1366 should 'list own language as a possible translation if language has not changed' do
1367 - a = fast_create(Article, :language => 'pt') 1367 + a = fast_create(Article, :language => 'pt', :profile_id => fast_create(Profile).id)
1368 assert a.possible_translations.include?('pt') 1368 assert a.possible_translations.include?('pt')
1369 end 1369 end
1370 1370
test/unit/block_test.rb
@@ -107,7 +107,7 @@ class BlockTest &lt; ActiveSupport::TestCase @@ -107,7 +107,7 @@ class BlockTest &lt; ActiveSupport::TestCase
107 107
108 should 'be able to save display setting' do 108 should 'be able to save display setting' do
109 user = create_user('testinguser').person 109 user = create_user('testinguser').person
110 - box = fast_create(Box, :owner_id => user.id) 110 + box = fast_create(Box, :owner_id => user.id, :owner_type => 'Profile')
111 block = create(Block, :display => 'never', :box_id => box.id) 111 block = create(Block, :display => 'never', :box_id => box.id)
112 block.reload 112 block.reload
113 assert_equal 'never', block.display 113 assert_equal 'never', block.display
@@ -115,7 +115,7 @@ class BlockTest &lt; ActiveSupport::TestCase @@ -115,7 +115,7 @@ class BlockTest &lt; ActiveSupport::TestCase
115 115
116 should 'be able to update display setting' do 116 should 'be able to update display setting' do
117 user = create_user('testinguser').person 117 user = create_user('testinguser').person
118 - box = fast_create(Box, :owner_id => user.id) 118 + box = fast_create(Box, :owner_id => user.id, :owner_type => 'Profile')
119 block = create(Block, :display => 'never', :box_id => box.id) 119 block = create(Block, :display => 'never', :box_id => box.id)
120 assert block.update_attributes!(:display => 'always') 120 assert block.update_attributes!(:display => 'always')
121 block.reload 121 block.reload
@@ -148,4 +148,12 @@ class BlockTest &lt; ActiveSupport::TestCase @@ -148,4 +148,12 @@ class BlockTest &lt; ActiveSupport::TestCase
148 assert_equal false, block.visible?(:locale => 'en') 148 assert_equal false, block.visible?(:locale => 'en')
149 end 149 end
150 150
  151 + should 'delegate environment to box' do
  152 + box = fast_create(Box, :owner_id => fast_create(Profile).id)
  153 + block = Block.new(:box => box)
  154 + box.stubs(:environment).returns(Environment.default)
  155 +
  156 + assert_equal box.environment, block.environment
  157 + end
  158 +
151 end 159 end
test/unit/box_test.rb
1 require File.dirname(__FILE__) + '/../test_helper' 1 require File.dirname(__FILE__) + '/../test_helper'
2 2
3 class BoxTest < ActiveSupport::TestCase 3 class BoxTest < ActiveSupport::TestCase
  4 + should 'retrieve environment based on owner' do
  5 + profile = fast_create(Profile)
  6 + box = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile')
  7 + assert_equal profile.environment, box.environment
  8 +
  9 + box = fast_create(Box, :owner_id => Environment.default.id, :owner_type => 'Environment')
  10 + assert_equal Environment.default, box.environment
  11 + end
4 12
5 should 'list allowed blocks for center box' do 13 should 'list allowed blocks for center box' do
6 blocks = Box.new(:position => 1).acceptable_blocks 14 blocks = Box.new(:position => 1).acceptable_blocks
test/unit/environment_test.rb
@@ -1320,4 +1320,61 @@ class EnvironmentTest &lt; ActiveSupport::TestCase @@ -1320,4 +1320,61 @@ class EnvironmentTest &lt; ActiveSupport::TestCase
1320 assert_equal environment.signup_welcome_text[:body], environment.signup_welcome_text_body 1320 assert_equal environment.signup_welcome_text[:body], environment.signup_welcome_text_body
1321 end 1321 end
1322 1322
  1323 + should 'allow only default languages there are defined in available locales' do
  1324 + environment = Environment.default
  1325 + environment.stubs(:available_locales).returns(['en'])
  1326 + environment.default_language = 'pt'
  1327 + environment.valid?
  1328 + assert environment.errors.invalid?(:default_language)
  1329 +
  1330 + environment.default_language = 'en'
  1331 + environment.valid?
  1332 + assert !environment.errors.invalid?(:default_language)
  1333 + end
  1334 +
  1335 + should 'define default locale or use the config default locale' do
  1336 + environment = Environment.default
  1337 + environment.default_language = nil
  1338 + environment.save!
  1339 + assert_equal Noosfero.default_locale, environment.default_locale
  1340 +
  1341 + environment.default_language = 'en'
  1342 + environment.save!
  1343 + assert_equal environment.default_language, environment.default_locale
  1344 + end
  1345 +
  1346 + should 'allow only languages there are defined in locales' do
  1347 + environment = Environment.default
  1348 +
  1349 + environment.languages = ['zz']
  1350 + environment.valid?
  1351 + assert environment.errors.invalid?(:languages)
  1352 +
  1353 + environment.languages = ['en']
  1354 + environment.valid?
  1355 + assert !environment.errors.invalid?(:languages)
  1356 + end
  1357 +
  1358 + should 'define locales or use the config locales' do
  1359 + environment = Environment.default
  1360 + environment.languages = nil
  1361 + environment.save!
  1362 + assert_equal Noosfero.locales, environment.locales
  1363 +
  1364 + environment.languages = ['en']
  1365 + environment.save!
  1366 + hash = {'en' => 'English'}
  1367 + assert_equal hash, environment.locales
  1368 + end
  1369 +
  1370 + should 'define available_locales or use the config available_locales' do
  1371 + environment = Environment.default
  1372 + environment.languages = nil
  1373 + environment.save!
  1374 + assert_equal Noosfero.available_locales, environment.available_locales
  1375 +
  1376 + environment.languages = ['pt', 'en']
  1377 + environment.save!
  1378 + assert_equal ['en', 'pt'], environment.available_locales
  1379 + end
1323 end 1380 end
test/unit/language_helper_test.rb
@@ -20,29 +20,30 @@ class LanguageHelperTest &lt; ActiveSupport::TestCase @@ -20,29 +20,30 @@ class LanguageHelperTest &lt; ActiveSupport::TestCase
20 end 20 end
21 21
22 should 'generate language chooser correcly' do 22 should 'generate language chooser correcly' do
23 - Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once 23 + environment = Environment.default
  24 + environment.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once
24 25
25 self.expects(:language).returns('pt_BR') 26 self.expects(:language).returns('pt_BR')
26 - result = self.language_chooser 27 + result = self.language_chooser(environment)
27 assert_match /<strong>Português Brasileiro<\/strong>/, result 28 assert_match /<strong>Português Brasileiro<\/strong>/, result
28 assert_no_match /<strong>English<\/strong>/, result 29 assert_no_match /<strong>English<\/strong>/, result
29 assert_no_match /<strong>Français<\/strong>/, result 30 assert_no_match /<strong>Français<\/strong>/, result
30 assert_no_match /<strong>Italiano<\/strong>/, result 31 assert_no_match /<strong>Italiano<\/strong>/, result
31 32
32 self.expects(:language).returns('fr') 33 self.expects(:language).returns('fr')
33 - result = self.language_chooser 34 + result = self.language_chooser(environment)
34 assert_no_match /<strong>Português Brasileiro<\/strong>/, result 35 assert_no_match /<strong>Português Brasileiro<\/strong>/, result
35 assert_no_match /<strong>English<\/strong>/, result 36 assert_no_match /<strong>English<\/strong>/, result
36 assert_match /<strong>Français<\/strong>/, result 37 assert_match /<strong>Français<\/strong>/, result
37 assert_no_match /<strong>Italiano<\/strong>/, result 38 assert_no_match /<strong>Italiano<\/strong>/, result
38 -  
39 end 39 end
40 40
41 should 'generate drodown language chooser correcly' do 41 should 'generate drodown language chooser correcly' do
42 - Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once 42 + environment = Environment.default
  43 + environment.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once
43 44
44 self.expects(:language).returns('en') 45 self.expects(:language).returns('en')
45 - result = self.language_chooser(:element => 'dropdown') 46 + result = self.language_chooser(environment, :element => 'dropdown')
46 assert_match /<option value="en" selected="selected">English<\/option>/, result 47 assert_match /<option value="en" selected="selected">English<\/option>/, result
47 assert_match /<option value="pt_BR">Português Brasileiro<\/option>/, result 48 assert_match /<option value="pt_BR">Português Brasileiro<\/option>/, result
48 assert_match /<option value="fr">Français<\/option>/, result 49 assert_match /<option value="fr">Français<\/option>/, result
@@ -52,6 +53,18 @@ class LanguageHelperTest &lt; ActiveSupport::TestCase @@ -52,6 +53,18 @@ class LanguageHelperTest &lt; ActiveSupport::TestCase
52 assert_no_match /<option value="it" selected="selected">Italiano<\/option>/, result 53 assert_no_match /<option value="it" selected="selected">Italiano<\/option>/, result
53 end 54 end
54 55
  56 + should 'not list languages if there is less than 2 languages available' do
  57 + environment = Environment.default
  58 +
  59 + environment.expects(:locales).returns({ 'en' => 'English'}).at_least_once
  60 + result = self.language_chooser(environment)
  61 + assert result.blank?
  62 +
  63 + environment.expects(:locales).returns({}).at_least_once
  64 + result = self.language_chooser(environment)
  65 + assert result.blank?
  66 + end
  67 +
55 protected 68 protected
56 include NoosferoTestHelper 69 include NoosferoTestHelper
57 include ActionView::Helpers::FormOptionsHelper 70 include ActionView::Helpers::FormOptionsHelper