You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

avatars_helper_test.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class AvatarsHelperTest < Redmine::HelperTest
  20. include ERB::Util
  21. include Rails.application.routes.url_helpers
  22. include AvatarsHelper
  23. fixtures :users, :email_addresses
  24. def setup
  25. Setting.gravatar_enabled = '1'
  26. end
  27. def test_avatar_with_user
  28. assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar(User.find_by_mail('jsmith@somenet.foo'))
  29. end
  30. def test_avatar_with_email_string
  31. assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar('jsmith <jsmith@somenet.foo>')
  32. end
  33. def test_avatar_with_anonymous_user
  34. assert_match %r{src="/assets/anonymous(-\w+)?.png"}, avatar(User.anonymous)
  35. end
  36. def test_avatar_with_group
  37. assert_match %r{src="/assets/group(-\w+)?.png"}, avatar(Group.first)
  38. end
  39. def test_avatar_with_invalid_arg_should_return_nil
  40. assert_nil avatar('jsmith')
  41. assert_nil avatar(nil)
  42. end
  43. def test_avatar_default_size_should_be_24
  44. assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>')
  45. end
  46. def test_avatar_with_size_option
  47. assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>', :size => 24)
  48. assert_include 'width="24" height="24"', avatar(User.anonymous, :size => 24)
  49. end
  50. def test_avatar_with_html_option
  51. # Non-avatar options should be considered html options
  52. assert_include 'title="John Smith"', avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith')
  53. end
  54. def test_avatar_css_class
  55. # The default class of the img tag should be gravatar
  56. assert_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>')
  57. assert_include 'class="gravatar picture"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
  58. end
  59. def test_avatar_disabled
  60. with_settings :gravatar_enabled => '0' do
  61. assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
  62. end
  63. end
  64. def test_avatar_server_url
  65. to_test = {
  66. 'https://www.gravatar.com' => %r|https://www.gravatar.com/avatar/\h{32}|,
  67. 'https://seccdn.libravatar.org' => %r|https://seccdn.libravatar.org/avatar/\h{32}|,
  68. 'http://localhost:8080' => %r|http://localhost:8080/avatar/\h{32}|,
  69. }
  70. to_test.each do |url, expected|
  71. Redmine::Configuration.with 'avatar_server_url' => url do
  72. assert_match expected, avatar('<jsmith@somenet.foo>')
  73. end
  74. end
  75. end
  76. end