Project

General

Profile

Patch #37994 ยป 37994.patch

Dmitry Makurin , 2022-12-05 17:12

View differences:

app/helpers/application_helper.rb
49 49
    end
50 50
  end
51 51

  
52
  # Displays a link to user's account page if active
53
  def link_to_user(user, options={})
54
    user.is_a?(User) ? link_to_principal(user, options) : h(user.to_s)
55
  end
56

  
57 52
  # Displays a link to user's account page or group page
58 53
  def link_to_principal(principal, options={})
59 54
    only_path = options[:only_path].nil? ? true : options[:only_path]
......
77 72
    css_classes += " #{options[:class]}" if css_classes && options[:class].present?
78 73
    url ? link_to(name, url, :class => css_classes) : name
79 74
  end
80

  
81
  # Displays a link to edit group page if current user is admin
82
  # Otherwise display only the group name
83
  def link_to_group(group, options={})
84
    if group.is_a?(Group)
85
      name = h(group.name)
86
      if User.current.admin?
87
        only_path = options[:only_path].nil? ? true : options[:only_path]
88
        link_to name, edit_group_path(group, :only_path => only_path)
89
      else
90
        name
91
      end
92
    end
93
  end
75
  alias link_to_user link_to_principal
76
  alias link_to_group link_to_principal
94 77

  
95 78
  # Displays a link to +issue+ with its subject.
96 79
  # Examples:
test/functional/groups_controller_test.rb
289 289
    assert_response :success
290 290
    assert_include 'John Smith', response.body
291 291
  end
292

  
293
  def test_show_should_display_edit_link_for_admin
294
    @request.session[:user_id] = 1
295
    get :show, :params => {:id => 10}
296
    assert_response :success
297

  
298
    assert_select '.contextual > .icon-edit'
299
  end
292 300
end
test/functional/users_controller_test.rb
301 301
    assert_select 'div#groups', 1 do
302 302
      assert_select 'h3', :text => 'Groups'
303 303
      assert_select 'li', 2
304
      assert_select 'a[href=?]', '/groups/10/edit', :text => 'A Team'
305
      assert_select 'a[href=?]', '/groups/11/edit', :text => 'B Team'
304
      assert_select 'a[href=?]', '/groups/10', :text => 'A Team'
305
      assert_select 'a[href=?]', '/groups/11', :text => 'B Team'
306 306
    end
307 307
  end
308 308

  
test/helpers/application_helper_test.rb
1851 1851
    assert_include "&lt;&gt;&#39;&amp;", link_to_principal("<>'&")
1852 1852
  end
1853 1853

  
1854
  def test_link_to_group_should_return_only_group_name_for_non_admin_users
1855
    User.current = nil
1856
    group = Group.find(10)
1857
    assert_equal "A Team", link_to_group(group)
1858
  end
1859

  
1860
  def test_link_to_group_should_link_to_group_edit_page_for_admin_users
1861
    User.current = User.find(1)
1862
    group = Group.find(10)
1863
    result = link_to("A Team", "/groups/10/edit")
1864
    assert_equal result, link_to_group(group)
1865
  end
1866

  
1867 1854
  def test_link_to_user_should_not_link_to_anonymous
1868 1855
    user = User.anonymous
1869 1856
    assert user.anonymous?
    (1-1/1)