Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

attachment_format_test.rb 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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. require 'redmine/field_format'
  20. class Redmine::AttachmentFieldFormatTest < ActionView::TestCase
  21. include Redmine::I18n
  22. fixtures :users
  23. def setup
  24. User.current = nil
  25. set_language_if_valid 'en'
  26. set_tmp_attachments_directory
  27. end
  28. def test_should_accept_a_hash_with_upload_on_create
  29. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  30. group = Group.new(:name => 'Group')
  31. attachment = nil
  32. custom_value = new_record(CustomValue) do
  33. attachment = new_record(Attachment) do
  34. group.custom_field_values = {field.id => {:file => mock_file}}
  35. assert group.save
  36. end
  37. end
  38. assert_equal 'a_file.png', attachment.filename
  39. assert_equal custom_value, attachment.container
  40. assert_equal field, attachment.container.custom_field
  41. assert_equal group, attachment.container.customized
  42. end
  43. def test_should_accept_a_hash_with_no_upload_on_create
  44. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  45. group = Group.new(:name => 'Group')
  46. attachment = nil
  47. custom_value = new_record(CustomValue) do
  48. assert_no_difference 'Attachment.count' do
  49. group.custom_field_values = {field.id => {}}
  50. assert group.save
  51. end
  52. end
  53. assert_equal '', custom_value.value
  54. end
  55. def test_should_not_validate_with_invalid_upload_on_create
  56. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  57. group = Group.new(:name => 'Group')
  58. with_settings :attachment_max_size => 0 do
  59. assert_no_difference 'CustomValue.count' do
  60. assert_no_difference 'Attachment.count' do
  61. group.custom_field_values = {field.id => {:file => mock_file}}
  62. assert_equal false, group.save
  63. end
  64. end
  65. end
  66. end
  67. def test_should_accept_a_hash_with_token_on_create
  68. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  69. group = Group.new(:name => 'Group')
  70. attachment = Attachment.create!(:file => mock_file, :author => User.find(2))
  71. assert_nil attachment.container
  72. custom_value = new_record(CustomValue) do
  73. assert_no_difference 'Attachment.count' do
  74. group.custom_field_values = {field.id => {:token => attachment.token}}
  75. assert group.save
  76. end
  77. end
  78. attachment.reload
  79. assert_equal custom_value, attachment.container
  80. assert_equal field, attachment.container.custom_field
  81. assert_equal group, attachment.container.customized
  82. end
  83. def test_should_not_validate_with_invalid_token_on_create
  84. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  85. group = Group.new(:name => 'Group')
  86. assert_no_difference 'CustomValue.count' do
  87. assert_no_difference 'Attachment.count' do
  88. group.custom_field_values = {field.id => {:token => "123.0123456789abcdef"}}
  89. assert_equal false, group.save
  90. end
  91. end
  92. end
  93. def test_should_replace_attachment_on_update
  94. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  95. group = Group.new(:name => 'Group')
  96. attachment = nil
  97. custom_value = new_record(CustomValue) do
  98. attachment = new_record(Attachment) do
  99. group.custom_field_values = {field.id => {:file => mock_file}}
  100. assert group.save
  101. end
  102. end
  103. group.reload
  104. assert_no_difference 'Attachment.count' do
  105. assert_no_difference 'CustomValue.count' do
  106. group.custom_field_values = {field.id => {:file => mock_file}}
  107. assert group.save
  108. end
  109. end
  110. assert !Attachment.exists?(attachment.id)
  111. assert CustomValue.exists?(custom_value.id)
  112. new_attachment = Attachment.order(:id => :desc).first
  113. custom_value.reload
  114. assert_equal custom_value, new_attachment.container
  115. end
  116. def test_should_delete_attachment_on_update
  117. field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
  118. group = Group.new(:name => 'Group')
  119. attachment = nil
  120. custom_value = new_record(CustomValue) do
  121. attachment = new_record(Attachment) do
  122. group.custom_field_values = {field.id => {:file => mock_file}}
  123. assert group.save
  124. end
  125. end
  126. group.reload
  127. assert_difference 'Attachment.count', -1 do
  128. assert_no_difference 'CustomValue.count' do
  129. group.custom_field_values = {field.id => {}}
  130. assert group.save
  131. end
  132. end
  133. assert !Attachment.exists?(attachment.id)
  134. assert CustomValue.exists?(custom_value.id)
  135. custom_value.reload
  136. assert_equal '', custom_value.value
  137. end
  138. end