diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb index 4c95a72..badf707 100644 --- a/app/controllers/my_profile/profile_editor_controller.rb +++ b/app/controllers/my_profile/profile_editor_controller.rb @@ -11,6 +11,7 @@ class ProfileEditorController < MyProfileController # edits the profile info (posts back) def edit @profile_data = profile + @possible_domains = profile.possible_domains if request.post? if profile.update_attributes(params[:profile_data]) redirect_to :action => 'index' diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f6f5efd..6874deb 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -130,21 +130,6 @@ module ApplicationHelper link_to text, p.url, options end - def link_to_myprofile(text, url = {}, profile = nil, options = {}) - p = if profile - Profile[profile] - else - user - end - link_to text, p.admin_url.merge(url), options - end - - def link_to_document(doc, text = nil, html_options = {}, url_options = {}) - text ||= doc.title - path = doc.path.split(/\//) - link_to text, homepage_path({:profile => doc.profile.identifier , :page => path, :view => true}.merge(url_options)), html_options - end - def link_if_permitted(link, permission = nil, target = nil) if permission.nil? || current_user.person.has_permission?(permission, target) link @@ -736,9 +721,7 @@ module ApplicationHelper end def login_url - options = { :controller => 'account', :action => 'login' } - options.merge!(:protocol => 'https://', :host => request.host) unless ENV['RAILS_ENV'] == 'development' || environment.disable_ssl - url_for(options) + url_for(Noosfero.url_options.merge({ :controller => 'account', :action => 'login', :host => request.host })) end def base_url diff --git a/app/helpers/profile_editor_helper.rb b/app/helpers/profile_editor_helper.rb index a2c0109..bffa40c 100644 --- a/app/helpers/profile_editor_helper.rb +++ b/app/helpers/profile_editor_helper.rb @@ -117,4 +117,20 @@ module ProfileEditorHelper select(object, method, [[_('[Select ...]'), nil]] + ProfileEditorHelper::SCHOOLING_STATUS.map{|s| [gettext(s), s]}, {}, options) end + def select_preferred_domain(object) + profile = instance_variable_get("@#{object}") + domains = [] + if profile + if profile.preferred_domain + # FIXME should be able to change + return '' + else + domains = profile.possible_domains + end + else + domains = environment.domains + end + labelled_form_field(__('Preferred domain name:'), select(object, :preferred_domain_id, domains.map {|item| [item.name, item.id]}, :prompt => '<' + _('Select domain') + '>')) + end + end diff --git a/app/models/domain.rb b/app/models/domain.rb index 93d0615..4902fdf 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -63,4 +63,25 @@ class Domain < ActiveRecord::Base end end + @hosting = {} + + # Tells whether the given domain name is hosting a profile or not. + # + # Since this is going to be called a lot, The queries are cached so the + # database is hit only for the first query for a given domain name. This way, + # transfering a domain from a profile to an environment of vice-versa + # requires restarting the application. + def self.hosting_profile_at(domainname) + @hosting[domainname] ||= + begin + domain = Domain.find_by_name(domainname) + !domain.nil? && (domain.owner_type == 'Profile') + end + end + + # clears the cache of hosted domains. Used for testing. + def self.clear_cache + @hosting = {} + end + end diff --git a/app/models/environment.rb b/app/models/environment.rb index d4becb6..e6972a1 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -45,6 +45,7 @@ class Environment < ActiveRecord::Base 'warn_obsolete_browser' => _('Enable warning of obsolete browser'), 'wysiwyg_editor_for_environment_home' => _('Use WYSIWYG editor to edit environment home page'), 'media_panel' => _('Media panel in WYSIWYG editor'), + 'select_preferred_domain' => _('Select preferred domains per profile'), } end diff --git a/app/models/person.rb b/app/models/person.rb index 23dead7..ac9e617 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -42,6 +42,7 @@ class Person < Profile end FIELDS = %w[ + preferred_domain nickname sex address diff --git a/app/models/profile.rb b/app/models/profile.rb index 2705e93..ee3127a 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -94,6 +94,7 @@ class Profile < ActiveRecord::Base belongs_to :user has_many :domains, :as => :owner + belongs_to :preferred_domain, :class_name => 'Domain', :foreign_key => 'preferred_domain_id' belongs_to :environment has_many :articles, :dependent => :destroy @@ -327,19 +328,15 @@ class Profile < ActiveRecord::Base end def url - @url ||= if self.domains.empty? - generate_url(:controller => 'content_viewer', :action => 'view_page', :page => []) - else - Noosfero.url_options.merge({ :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []}) - end + @url ||= generate_url(:controller => 'content_viewer', :action => 'view_page', :page => []) end def admin_url - generate_url(:controller => 'profile_editor', :action => 'index') + { :profile => identifier, :controller => 'profile_editor', :action => 'index' } end def public_profile_url - generate_url(:controller => 'profile', :action => 'index') + generate_url(:profile => identifier, :controller => 'profile', :action => 'index') end def generate_url(options) @@ -347,7 +344,25 @@ class Profile < ActiveRecord::Base end def url_options - Noosfero.url_options.merge({ :host => self.environment.default_hostname, :profile => self.identifier}) + options = { :host => default_hostname, :profile => (hostname ? nil : self.identifier) } + Noosfero.url_options.merge(options) + end + + def default_hostname + @default_hostname ||= (hostname || environment.default_hostname) + end + + def hostname + if preferred_domain + return preferred_domain.name + else + domain = self.domains.first + domain ? domain.name : nil + end + end + + def possible_domains + environment.domains + domains end # FIXME this can be SLOW diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index e01f8e3..f8879e0 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -64,12 +64,10 @@ class ProfileListBlock < Block title = self.title nl = "\n" lambda do - #list = profiles.map {|item| content_tag( 'li', profile_image_link(item) ) }.join("\n ") count=0 list = profiles.map {|item| count+=1 profile_image_link( item ) #+ - #( ( count%2 == 0 && count < profiles.size ) ? nl+'' : '' ) }.join("\n ") if list.empty? list = '
'+ _('None') +'
' diff --git a/app/views/account/index.rhtml b/app/views/account/index.rhtml index a019a01..e8ed9d2 100644 --- a/app/views/account/index.rhtml +++ b/app/views/account/index.rhtml @@ -6,7 +6,7 @@

-<%= link_to_myprofile _('Edit Personal details.'), :controller => 'profile_editor' %> +<%= link_to _('Edit Personal details.'), :controller => 'profile_editor', :profile => user.identifier, :action => 'index' %> <%= _('You can change your personal details.') %>

@@ -16,7 +16,7 @@

-<%= link_to_myprofile(_('Manage content.'), :controller => 'cms') %> +<%= link_to(_('Manage content.'), :controller => 'cms', :profile => user.identifier, :action => 'index') %> <%= _('Manage your content.') %>

diff --git a/app/views/blocks/profile_info.rhtml b/app/views/blocks/profile_info.rhtml index d2114d6..63d95c1 100644 --- a/app/views/blocks/profile_info.rhtml +++ b/app/views/blocks/profile_info.rhtml @@ -19,7 +19,7 @@
  • <%= link_to _('View profile'), block.owner.public_profile_url %>
  • <%= link_to(_('Products/Services'), :controller => 'catalog', :profile => block.owner.identifier) if block.owner.enterprise? && !block.owner.environment.enabled?('disable_products_for_enterprises') %>
  • <% if !user.nil? and user.has_permission?('edit_profile', profile) %> -
  • <%= link_to _('Control panel'), :controller => 'profile_editor' %>
  • +
  • <%= link_to _('Control panel'), block.owner.admin_url %>
  • <% end %> <% if profile.person? %>
  • <%= _('Since %{year}/%{month}') % { :year => block.owner.created_at.year, :month => block.owner.created_at.month } %>
  • diff --git a/app/views/cms/view.rhtml b/app/views/cms/view.rhtml index 6c49c78..da67109 100644 --- a/app/views/cms/view.rhtml +++ b/app/views/cms/view.rhtml @@ -7,7 +7,7 @@

    <%= icon('cms') %> <%= link_to profile.identifier, :action => 'index' %> - <%= @article.hierarchy.map {|item| " / " + ((item == @article) ? item.name : link_to_document(item)) } %> + <%= @article.hierarchy.map {|item| " / " + ((item == @article) ? item.name : link_to(item.slug, :id => item.id)) } %>

    <% end %> @@ -42,7 +42,7 @@ <%= link_to _('Properties'), :action => 'edit', :id => folder.id %> - <%= link_to_document(folder, _('Public view')) %> + <%= link_to _('Public view'), folder.url %> <%= link_to _('Use as homepage'), { :action => 'set_home_page', :id => folder.id }, :method => :post %> <%= link_to _('Delete'), { :action => 'destroy', :id => folder.id }, :method => :post, :confirm => _('Are you sure that you want to remove this folder? Note that all the items inside it will also be removed!') %> @@ -68,7 +68,7 @@ <%= link_to _('Edit'), :action => 'edit', :id => item.id %> - <%= link_to_document(item, _('Public view')) %> + <%= link_to _('Public view'), item.url %> <%= link_to _('Spread this'), :action => 'publish', :id => item.id %> <%= link_to _('Use as homepage'), { :action => 'set_home_page', :id => item.id }, :method => :post %> <%= link_to _('Delete'), { :action => 'destroy', :id => item.id }, :method => :post, :confirm => _('Are you sure that you want to remove this item?') %> diff --git a/app/views/content_viewer/_article.rhtml b/app/views/content_viewer/_article.rhtml index 6550dfb..44da196 100644 --- a/app/views/content_viewer/_article.rhtml +++ b/app/views/content_viewer/_article.rhtml @@ -1,2 +1,2 @@ -<%= link_to_document(article, '', :class => article.css_class_name) %> -<%= link_to_document(article) %> +<%= link_to '', article.url, '', :class => article.css_class_name) %> +<%= link_to article.title, article.view_url %> diff --git a/app/views/content_viewer/_uploaded_file.rhtml b/app/views/content_viewer/_uploaded_file.rhtml index 410d39d..cd1cdb4 100644 --- a/app/views/content_viewer/_uploaded_file.rhtml +++ b/app/views/content_viewer/_uploaded_file.rhtml @@ -1,5 +1,5 @@ <% if uploaded_file.image? %> -
    <%= link_to_document(uploaded_file, image_tag(uploaded_file.public_filename(:thumb))) %>
    +
    <%= link_to image_tag(uploaded_file.public_filename(:thumb)), uploaded_file.view_url %>
    <% else %> <%= render :partial => 'article', :object => uploaded_file %> <% end %> diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index 9b52cc8..ff1290c 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -34,24 +34,24 @@ @@ -73,7 +73,7 @@ <% if @page.parent && !@page.parent.path.blank? %>
    - <%= link_to_document(@page.parent, _('Go back')) %> + <%= link_to _('Go back'), @page.parent.url %>
    <% end %> diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml index 4a185ea..184af5a 100644 --- a/app/views/layouts/application.rhtml +++ b/app/views/layouts/application.rhtml @@ -49,7 +49,6 @@ <%= icon_theme_stylesheet_tag %> <%= stylesheet_link_tag 'iepngfix/iepngfix.css' %> - 'new_consumption' %>

    -<%= link_to_myprofile( ''+ _('Back') +'', {}, @profile.identifier, :class => 'button with-text icon-back' ) %> +<%= link_to( ''+ _('Back') +'', { :controller => 'profile_editor', :profile => @profile.identifier, :action => 'index' }, :class => 'button with-text icon-back' ) %>

    diff --git a/app/views/profile_editor/_person_form.rhtml b/app/views/profile_editor/_person_form.rhtml index 412dce7..364bbc5 100644 --- a/app/views/profile_editor/_person_form.rhtml +++ b/app/views/profile_editor/_person_form.rhtml @@ -9,6 +9,7 @@ <% end %> +<%= optional_field(@person, 'preferred_domain', select_preferred_domain(:profile_data)) %> <%= optional_field(@person, 'contact_information', f.text_field(:contact_information)) %> <%= optional_field(@person, 'contact_phone', labelled_form_field(_('Home phone'), text_field(:profile_data, :contact_phone))) %> <%= optional_field(@person, 'cell_phone', f.text_field(:cell_phone)) %> diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml index 7db58a1..e9f350c 100644 --- a/app/views/profile_editor/edit.rhtml +++ b/app/views/profile_editor/edit.rhtml @@ -13,6 +13,7 @@ <% end %> +

    <%= _('Privacy options') %>

    <%= _('This profile is:') %> diff --git a/app/views/shared/user_menu.rhtml b/app/views/shared/user_menu.rhtml index f32f724..74183bf 100644 --- a/app/views/shared/user_menu.rhtml +++ b/app/views/shared/user_menu.rhtml @@ -27,10 +27,9 @@ <% end %> -

  • <%= link_to_myprofile( ''+ _('Control panel'), - {}, nil, :id => 'link_edit_profile', - :help => _('Control panel: change your picture, edit your personal information, create content or change the way your home page looks.') - ) %>
  • +
  • + <%= link_to(''+ _('Control panel'), { :controller => 'profile_editor', :action => 'index', :profile => user.identifier }, :id => 'link_edit_profile', :help => _('Control panel: change your picture, edit your personal information, create content or change the way your home page looks.')) %> +
  • <% if user.is_admin?(environment) %>
  • <%= link_to( ''+ _('Admin'), diff --git a/config/routes.rb b/config/routes.rb index ce1ab7b..8bbe3a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,18 +15,11 @@ ActionController::Routing::Routes.draw do |map| ## Public controllers ###################################################### - # You can have the root of your site routed by hooking up '' - hosted_domain_matcher = lambda do |env| - domain = Domain.find_by_name(env[:host]) - domain && (domain.owner_type == 'Profile') - end - map.connect 'profile/:profile/:action/:id', :controller => 'profile', :id => /.*/, :profile => /#{Noosfero.identifier_format}/, :conditions => { :if => hosted_domain_matcher } - map.connect '*page', :controller => 'content_viewer', :action => 'view_page', :conditions => { :if => hosted_domain_matcher } - map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/ # -- just remember to delete public/index.html. - map.connect '', :controller => "home" + # You can have the root of your site routed by hooking up '' + map.connect '', :controller => "home", :conditions => { :if => lambda { |env| !Domain.hosting_profile_at(env[:host]) } } # user account controller map.connect 'account/new_password/:code', :controller => 'account', :action => 'new_password' @@ -83,7 +76,9 @@ ActionController::Routing::Routes.draw do |map| # cache stuff - hack map.cache 'public/:action/:id', :controller => 'public' - # *content viewing* + # match requests for content in domains hosted for profiles + map.connect '*page', :controller => 'content_viewer', :action => 'view_page', :conditions => { :if => lambda { |env| Domain.hosting_profile_at(env[:host])} } + # XXX this route must come last so other routes have priority over it. map.homepage ':profile/*page', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format}/ diff --git a/db/schema.rb b/db/schema.rb index 77fd5ae..ba371e3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 65) do +ActiveRecord::Schema.define(:version => 66) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -87,8 +87,8 @@ ActiveRecord::Schema.define(:version => 65) do t.boolean "virtual", :default => false end - add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id" + add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" create_table "blocks", :force => true do |t| t.string "title" @@ -128,8 +128,8 @@ ActiveRecord::Schema.define(:version => 65) do t.boolean "virtual", :default => false end - add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" + add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" create_table "comments", :force => true do |t| t.string "title" @@ -207,8 +207,8 @@ ActiveRecord::Schema.define(:version => 65) do t.datetime "updated_at" end - add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" + add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" create_table "products", :force => true do |t| t.integer "enterprise_id" @@ -230,7 +230,7 @@ ActiveRecord::Schema.define(:version => 65) do t.string "type" t.string "identifier" t.integer "environment_id" - t.boolean "active", :default => true + t.boolean "active", :default => true t.string "address" t.string "contact_phone" t.integer "home_page_id" @@ -241,13 +241,14 @@ ActiveRecord::Schema.define(:version => 65) do t.float "lat" t.float "lng" t.integer "geocode_precision" - t.boolean "enabled", :default => true - t.string "nickname", :limit => 16 + t.boolean "enabled", :default => true + t.string "nickname", :limit => 16 t.text "custom_header" t.text "custom_footer" t.string "theme" - t.boolean "public_profile", :default => true + t.boolean "public_profile", :default => true t.date "birth_date" + t.integer "preferred_domain_id" end add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" @@ -285,8 +286,8 @@ ActiveRecord::Schema.define(:version => 65) do t.datetime "created_at" end - add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type" + add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" create_table "tags", :force => true do |t| t.string "name" diff --git a/lib/zen3_terminology.rb b/lib/zen3_terminology.rb index df9940e..edb67cf 100644 --- a/lib/zen3_terminology.rb +++ b/lib/zen3_terminology.rb @@ -65,6 +65,7 @@ class Zen3Terminology < Noosfero::Terminology::Custom 'Disable activation of enterprises' => N_('Disable activation of organizations'), 'Manage community fields' => N_('Manage group fields'), 'Create a new community' => N_('Create a new group'), + 'Preferred domain name:' => N_('Choose your host community:'), }) end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 9a54904..c4feb9c 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -600,6 +600,7 @@ class AccountControllerTest < Test::Unit::TestCase end should 'not point to SSL URL in login popup when in development mode' do + @request.stubs(:ssl?).returns(false) ENV.expects(:[]).with('RAILS_ENV').returns('development').at_least_once get :login_popup assert_no_tag :tag => 'form', :attributes => { :action => /^https:\/\// } diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 0303e00..bfc1fe1 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -234,32 +234,6 @@ class ApplicationControllerTest < Test::Unit::TestCase assert_no_tag :tag => 'div', :attributes => { :id => 'user_box' }, :descendant => { :tag => 'a', :attributes => { :href => 'http://web.mail/' } } end - should 'use environment top_url as base' do - Environment.any_instance.expects(:top_url).returns('http://www.lala.net') - get :index - assert_tag :tag => 'base', :attributes => { :href => 'http://www.lala.net' } - end - - should 'request environment top_url with ssl when under ssl for base' do - Environment.any_instance.expects(:top_url).with(true).returns('https://www.lala.net/') - @request.expects(:ssl?).returns(true).at_least_once - get :index - assert_tag :tag => 'base', :attributes => { :href => 'https://www.lala.net/' } - end - - should 'use correct environment for base tag' do - default = Environment.default - Environment.stubs(:default).returns(default) - default.stubs(:top_url).returns('http://default.com/') - - current = Environment.create!(:name => 'test environment') - current.domains.create!(:name => 'example.com') - - @request.expects(:host).returns('example.com').at_least_once - get :index - assert_tag :tag => 'base', :attributes => { :href => 'http://example.com' } - end - should 'display theme test panel when testing theme' do @request.session[:theme] = 'my-test-theme' theme = mock diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 7b402fe..d739069 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -632,4 +632,39 @@ class ProfileEditorControllerTest < Test::Unit::TestCase assert_tag :tag => 'a', :attributes => { :href => "/myprofile/default_user/cms/edit/#{profile.blog.id}" } end + should 'not show select preferred domain if not enabled in environment' do + profile.environment.custom_person_fields = {}; profile.environment.save! + + get :edit, :profile => profile.identifier + assert_no_tag :tag => 'select', :attributes => { :name => 'profile_data[preferred_domain_id]' } + end + + should 'be able to choose preferred domain' do + profile.environment.custom_person_fields = {'preferred_domain' => {'required' => 'true', 'active' => 'true'} }; profile.environment.save! + + profile.domains << Domain.new(:name => 'myowndomain.net') + profile.environment.domains << Domain.new(:name => 'myenv.net') + + get :edit, :profile => profile.identifier + assert_tag :tag => 'select', :attributes => { :name => 'profile_data[preferred_domain_id]' }, :descendant => { :tag => "option", :content => 'myowndomain.net' } + assert_tag :tag => 'select', :attributes => { :name => 'profile_data[preferred_domain_id]' }, :descendant => { :tag => "option", :content => 'myenv.net' } + + post :edit, :profile => profile.identifier, :profile_data => { :preferred_domain_id => profile.domains.first.id.to_s } + + assert_equal 'myowndomain.net', Profile.find(profile.id).preferred_domain.name + end + + should 'be able to set no preferred domain at all' do + profile.environment.custom_person_fields = {'preferred_domain' => {'required' => 'true', 'active' => 'true'} }; profile.environment.save! + + profile.domains << Domain.new(:name => 'myowndomain.net') + profile.environment.domains << Domain.new(:name => 'myenv.net') + + get :edit, :profile => profile.identifier + assert_tag :tag => "select", :attributes => { :name => 'profile_data[preferred_domain_id]'}, :descendant => { :tag => 'option', :content => '<Select domain>', :attributes => { :value => '' } } + + post :edit, :profile => profile.identifier, :profile_data => { :preferred_domain_id => '' } + assert_nil Profile.find(profile.id).preferred_domain + end + end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 035639c..e2261d7 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -2,6 +2,10 @@ require "#{File.dirname(__FILE__)}/../test_helper" class RoutingTest < ActionController::IntegrationTest + def setup + Domain.clear_cache + end + # home page ################################################################ def test_homepage @@ -165,7 +169,6 @@ class RoutingTest < ActionController::IntegrationTest end def test_hosted_domain_routing - user = create_user('testuser').person domain = Domain.create!(:name => 'example.com', :owner => user) @@ -174,6 +177,15 @@ class RoutingTest < ActionController::IntegrationTest assert_routing('/work/free-software', :controller => 'content_viewer', :action => 'view_page', :page => [ 'work', 'free-software'] ) end + def test_root_of_hosted_domain + user = create_user('testuser').person + domain = Domain.create!(:name => 'example.com', :owner => user) + + ActionController::TestRequest.any_instance.expects(:host).returns('www.example.com') + + assert_routing('', :controller => 'content_viewer', :action => 'view_page', :page => []) + end + def test_profile_under_hosted_domain community = Community.create!(:identifier => 'testcomm', :name => "test community") domain = Domain.create!(:name => 'example.com', :owner => community) diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index abceb36..9c093b3 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -202,6 +202,9 @@ class ApplicationHelperTest < Test::Unit::TestCase environment = mock environment.expects(:disable_ssl).returns(true).at_least_once stubs(:environment).returns(environment) + request = mock + request.stubs(:host).returns('localhost') + stubs(:request).returns(request) expects(:url_for).with(has_entries(:protocol => 'https://')).never expects(:url_for).with(has_key(:controller)).returns("LALALA") diff --git a/test/unit/domain_test.rb b/test/unit/domain_test.rb index 68681db..c54662f 100644 --- a/test/unit/domain_test.rb +++ b/test/unit/domain_test.rb @@ -3,6 +3,10 @@ require File.dirname(__FILE__) + '/../test_helper' class DomainTest < Test::Unit::TestCase fixtures :domains, :environments, :profiles, :users + def setup + Domain.clear_cache + end + # Replace this with your real tests. def test_domain_name_format c = Domain.new @@ -83,4 +87,17 @@ class DomainTest < Test::Unit::TestCase assert_nil Domain.find_by_name('colivre.net').profile end + def test_hosted_domain + assert_equal false, Domain.hosting_profile_at('example.com') + + profile = create_user('hosted_user').person + Domain.create!(:name => 'example.com', :owner => profile) + assert_equal true, Domain.hosting_profile_at('example.com') + end + + def test_not_report_profile_hosted_for_environment_domains + Domain.create!(:name => 'example.com', :owner => Environment.default) + assert_equal false, Domain.hosting_profile_at('example.com') + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 31e78b9..a1d56bc 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1,7 +1,7 @@ require File.dirname(__FILE__) + '/../test_helper' class ProfileTest < Test::Unit::TestCase - fixtures :profiles, :environments, :users, :roles + fixtures :profiles, :environments, :users, :roles, :domains def test_identifier_validation p = Profile.new @@ -284,7 +284,7 @@ class ProfileTest < Test::Unit::TestCase should "use own domain name instead of environment's for home page url" do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) profile.domains << Domain.new(:name => 'micojones.net') - assert_equal({:host => 'micojones.net', :controller => 'content_viewer', :action => 'view_page', :page => []}, profile.url) + assert_equal({:host => 'micojones.net', :profile => nil, :controller => 'content_viewer', :action => 'view_page', :page => []}, profile.url) end should 'help developers by adding a suitable port to url options' do @@ -1258,6 +1258,44 @@ class ProfileTest < Test::Unit::TestCase assert_nil person.find_in_all_tasks(task2.id) end + should 'use environment hostname by default' do + profile = Profile.new + env = mock + env.stubs(:default_hostname).returns('myenvironment.net') + profile.stubs(:environment).returns(env) + assert_equal 'myenvironment.net', profile.default_hostname + end + + should 'use its first domain hostname name if available' do + profile = create_user('testuser').person + profile.domains << Domain.new(:name => 'myowndomain.net') + assert_equal 'myowndomain.net', profile.default_hostname + end + + should 'have a preferred domain name' do + person = create_user('testuser').person + domain = Domain.create!(:name => 'myowndomain.net', :owner => person) + person.preferred_domain = domain + person.save! + + assert_equal domain, Person.find(person.id).preferred_domain(true) + end + + should 'use preferred domain for hostname' do + profile = Profile.new + profile.stubs(:preferred_domain).returns(Domain.new(:name => 'preferred.net')) + assert_equal 'preferred.net', profile.url[:host] + end + + should 'provide a list of possible preferred domain names' do + profile = create_user('testuser').person + domain1 = Domain.create!(:name => 'envdomain.net', :owner => profile.environment) + domain2 = Domain.create!(:name => 'profiledomain.net', :owner => profile) + + assert_includes profile.possible_domains, domain1 + assert_includes profile.possible_domains, domain2 + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2