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.

attachment_format_test.rb 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../../../../test_helper', __FILE__)
  18. require 'redmine/field_format'
  19. class Redmine::AttachmentFieldFormatTest < ActionView::TestCase
  20. include ApplicationHelper
  21. include Redmine::I18n
  22. fixtures :users
  23. def setup
  24. set_language_if_valid 'en'
  25. set_tmp_attachments_directory
  26. end
  27. def test_should_accept_a_hash_with_upload_on_create
  28. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  29. group = Group.new(:name => 'Group')
  30. attachment = nil
  31. custom_value = new_record(CustomValue) do
  32. attachment = new_record(Attachment) do
  33. group.custom_field_values = {field.id => {:file => mock_file}}
  34. assert group.save
  35. end
  36. end
  37. assert_equal 'a_file.png', attachment.filename
  38. assert_equal custom_value, attachment.container
  39. assert_equal field, attachment.container.custom_field
  40. assert_equal group, attachment.container.customized
  41. end
  42. def test_should_accept_a_hash_with_no_upload_on_create
  43. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  44. group = Group.new(:name => 'Group')
  45. attachment = nil
  46. custom_value = new_record(CustomValue) do
  47. assert_no_difference 'Attachment.count' do
  48. group.custom_field_values = {field.id => {}}
  49. assert group.save
  50. end
  51. end
  52. assert_equal '', custom_value.value
  53. end
  54. def test_should_not_validate_with_invalid_upload_on_create
  55. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  56. group = Group.new(:name => 'Group')
  57. with_settings :attachment_max_size => 0 do
  58. assert_no_difference 'CustomValue.count' do
  59. assert_no_difference 'Attachment.count' do
  60. group.custom_field_values = {field.id => {:file => mock_file}}
  61. assert_equal false, group.save
  62. end
  63. end
  64. end
  65. end
  66. def test_should_accept_a_hash_with_token_on_create
  67. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  68. group = Group.new(:name => 'Group')
  69. attachment = Attachment.create!(:file => mock_file, :author => User.find(2))
  70. assert_nil attachment.container
  71. custom_value = new_record(CustomValue) do
  72. assert_no_difference 'Attachment.count' do
  73. group.custom_field_values = {field.id => {:token => attachment.token}}
  74. assert group.save
  75. end
  76. end
  77. attachment.reload
  78. assert_equal custom_value, attachment.container
  79. assert_equal field, attachment.container.custom_field
  80. assert_equal group, attachment.container.customized
  81. end
  82. def test_should_not_validate_with_invalid_token_on_create
  83. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  84. group = Group.new(:name => 'Group')
  85. assert_no_difference 'CustomValue.count' do
  86. assert_no_difference 'Attachment.count' do
  87. group.custom_field_values = {field.id => {:token => "123.0123456789abcdef"}}
  88. assert_equal false, group.save
  89. end
  90. end
  91. end
  92. def test_should_replace_attachment_on_update
  93. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  94. group = Group.new(:name => 'Group')
  95. attachment = nil
  96. custom_value = new_record(CustomValue) do
  97. attachment = new_record(Attachment) do
  98. group.custom_field_values = {field.id => {:file => mock_file}}
  99. assert group.save
  100. end
  101. end
  102. group.reload
  103. assert_no_difference 'Attachment.count' do
  104. assert_no_difference 'CustomValue.count' do
  105. group.custom_field_values = {field.id => {:file => mock_file}}
  106. assert group.save
  107. end
  108. end
  109. assert !Attachment.exists?(attachment.id)
  110. assert CustomValue.exists?(custom_value.id)
  111. new_attachment = Attachment.order(:id => :desc).first
  112. custom_value.reload
  113. assert_equal custom_value, new_attachment.container
  114. end
  115. def test_should_delete_attachment_on_update
  116. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  117. group = Group.new(:name => 'Group')
  118. attachment = nil
  119. custom_value = new_record(CustomValue) do
  120. attachment = new_record(Attachment) do
  121. group.custom_field_values = {field.id => {:file => mock_file}}
  122. assert group.save
  123. end
  124. end
  125. group.reload
  126. assert_difference 'Attachment.count', -1 do
  127. assert_no_difference 'CustomValue.count' do
  128. group.custom_field_values = {field.id => {}}
  129. assert group.save
  130. end
  131. end
  132. assert !Attachment.exists?(attachment.id)
  133. assert CustomValue.exists?(custom_value.id)
  134. custom_value.reload
  135. assert_equal '', custom_value.value
  136. end
  137. end