Commit 764f9947114df53833189e66531cb7612bb1371a

Authored by Daniela Feitosa
1 parent e520c0fb

Fixed change of imgs/links url on profiles with domain

Links and images are saved with relative path but it wasn't considering
the profile domain. Because of this, the links were broken on article
visualization.
This commit also fix the url on edition of tinymce articles.
app/helpers/application_helper.rb
... ... @@ -903,7 +903,7 @@ module ApplicationHelper
903 903 end
904 904  
905 905 def base_url
906   - environment.top_url(request.scheme)
  906 + profile ? profile.top_url(request.scheme) : environment.top_url(request.scheme)
907 907 end
908 908 alias :top_url :base_url
909 909  
... ...
app/models/profile.rb
... ... @@ -572,6 +572,14 @@ class Profile < ActiveRecord::Base
572 572 options.merge(Noosfero.url_options)
573 573 end
574 574  
  575 + def top_url(scheme = 'http')
  576 + url = scheme + '://'
  577 + url << url_options[:host]
  578 + url << ':' << url_options[:port].to_s if url_options.key?(:port)
  579 + url << Noosfero.root('')
  580 + url
  581 + end
  582 +
575 583 private :generate_url, :url_options
576 584  
577 585 def default_hostname
... ...
app/models/text_article.rb
... ... @@ -33,11 +33,11 @@ class TextArticle &lt; Article
33 33 end
34 34  
35 35 def change_element_path(el, attribute)
36   - fullpath = /(https?):\/\/(#{environment.default_hostname})(:\d+)?(\/.*)/.match(el[attribute])
  36 + fullpath = /(https?):\/\/(#{profile.default_hostname})(:\d+)?(\/.*)/.match(el[attribute])
37 37 if fullpath
38 38 domain = fullpath[2]
39 39 path = fullpath[4]
40   - el[attribute] = path if domain == environment.default_hostname
  40 + el[attribute] = path if domain == profile.default_hostname
41 41 end
42 42 end
43 43  
... ...
test/unit/application_helper_test.rb
... ... @@ -1022,6 +1022,27 @@ class ApplicationHelperTest &lt; ActionView::TestCase
1022 1022 assert_equal "Clone Article", label_for_clone_article(TinyMceArticle.new)
1023 1023 end
1024 1024  
  1025 + should "return top url of environment" do
  1026 + env = Environment.default
  1027 + request = mock()
  1028 + request.expects(:scheme).returns('http')
  1029 + stubs(:request).returns(request)
  1030 + stubs(:environment).returns(env)
  1031 + stubs(:profile).returns(nil)
  1032 + assert_equal env.top_url('http'), top_url
  1033 + end
  1034 +
  1035 + should "return top url considering profile" do
  1036 + env = Environment.default
  1037 + c = fast_create(Community)
  1038 + request = mock()
  1039 + request.stubs(:scheme).returns('http')
  1040 + stubs(:request).returns(request)
  1041 + stubs(:environment).returns(env)
  1042 + stubs(:profile).returns(c)
  1043 + assert_equal c.top_url, top_url
  1044 + end
  1045 +
1025 1046 protected
1026 1047 include NoosferoTestHelper
1027 1048  
... ...
test/unit/profile_test.rb
... ... @@ -256,6 +256,20 @@ class ProfileTest &lt; ActiveSupport::TestCase
256 256 assert_equal({:host => 'micojones.net', :profile => nil, :controller => 'content_viewer', :action => 'view_page', :page => []}, profile.url)
257 257 end
258 258  
  259 + should 'provide environment top URL when profile has not a domain' do
  260 + env = Environment.default
  261 + profile = fast_create(Profile, :environment_id => env.id)
  262 + assert_equal env.top_url, profile.top_url
  263 + end
  264 +
  265 + should 'provide top URL to profile with domain' do
  266 + env = Environment.default
  267 + profile = fast_create(Profile, :environment_id => env.id)
  268 + domain = fast_create(Domain, :name => 'example.net')
  269 + profile.domains << domain
  270 + assert_equal 'http://example.net', profile.top_url
  271 + end
  272 +
259 273 should 'help developers by adding a suitable port to url' do
260 274 profile = build(Profile)
261 275  
... ...
test/unit/text_article_test.rb
... ... @@ -85,6 +85,17 @@ class TextArticleTest &lt; ActiveSupport::TestCase
85 85 assert_equal "<img src=\"/test.png\">", article.body
86 86 end
87 87  
  88 + should 'change image path to relative for profile with own domain' do
  89 + person = create_user('testuser').person
  90 + person.domains << build(Domain)
  91 +
  92 + article = TextArticle.new(:profile => person, :name => 'test')
  93 + env = Environment.default
  94 + article.body = "<img src=\"http://#{person.default_hostname}:3000/link-profile.png\">"
  95 + article.save!
  96 + assert_equal "<img src=\"/link-profile.png\">", article.body
  97 + end
  98 +
88 99 should 'not be translatable if there is no language available on environment' do
89 100 environment = fast_create(Environment)
90 101 environment.languages = nil
... ...