summaryrefslogtreecommitdiffstats
path: root/test/unit/lib/redmine/field_format/attachment_format_test.rb
blob: b0acf6d69dc0b36713b92f6ea3b0383f90fbc1f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require_relative '../../../../test_helper'
require 'redmine/field_format'

class Redmine::AttachmentFieldFormatTest < ActionView::TestCase
  include Redmine::I18n

  def setup
    User.current = nil
    set_language_if_valid 'en'
    set_tmp_attachments_directory
  end

  def test_should_accept_a_hash_with_upload_on_create
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')
    attachment = nil

    custom_value = new_record(CustomValue) do
      attachment = new_record(Attachment) do
        group.custom_field_values = {field.id => {:file => mock_file}}
        assert group.save
      end
    end

    assert_equal 'a_file.png', attachment.filename
    assert_equal custom_value, attachment.container
    assert_equal field, attachment.container.custom_field
    assert_equal group, attachment.container.customized
  end

  def test_should_accept_a_hash_with_no_upload_on_create
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')
    attachment = nil

    custom_value = new_record(CustomValue) do
      assert_no_difference 'Attachment.count' do
        group.custom_field_values = {field.id => {}}
        assert group.save
      end
    end

    assert_equal '', custom_value.value
  end

  def test_should_not_validate_with_invalid_upload_on_create
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')

    with_settings :attachment_max_size => 0 do
      assert_no_difference 'CustomValue.count' do
        assert_no_difference 'Attachment.count' do
          group.custom_field_values = {field.id => {:file => mock_file}}
          assert_equal false, group.save
        end
      end
    end
  end

  def test_should_accept_a_hash_with_token_on_create
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')

    attachment = Attachment.create!(:file => mock_file, :author => User.find(2))
    assert_nil attachment.container

    custom_value = new_record(CustomValue) do
      assert_no_difference 'Attachment.count' do
        group.custom_field_values = {field.id => {:token => attachment.token}}
        assert group.save
      end
    end

    attachment.reload
    assert_equal custom_value, attachment.container
    assert_equal field, attachment.container.custom_field
    assert_equal group, attachment.container.customized
  end

  def test_should_not_validate_with_invalid_token_on_create
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')

    assert_no_difference 'CustomValue.count' do
      assert_no_difference 'Attachment.count' do
        group.custom_field_values = {field.id => {:token => "123.0123456789abcdef"}}
        assert_equal false, group.save
      end
    end
  end

  def test_should_replace_attachment_on_update
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')
    attachment = nil
    custom_value = new_record(CustomValue) do
      attachment = new_record(Attachment) do
        group.custom_field_values = {field.id => {:file => mock_file}}
        assert group.save
      end
    end
    group.reload

    assert_no_difference 'Attachment.count' do
      assert_no_difference 'CustomValue.count' do
        group.custom_field_values = {field.id => {:file => mock_file}}
        assert group.save
      end
    end

    assert !Attachment.exists?(attachment.id)
    assert CustomValue.exists?(custom_value.id)

    new_attachment = Attachment.order(:id => :desc).first
    custom_value.reload
    assert_equal custom_value, new_attachment.container
  end

  def test_should_delete_attachment_on_update
    field = GroupCustomField.generate!(:name => "File", :field_format => 'attachment')
    group = Group.new(:name => 'Group')
    attachment = nil
    custom_value = new_record(CustomValue) do
      attachment = new_record(Attachment) do
        group.custom_field_values = {field.id => {:file => mock_file}}
        assert group.save
      end
    end
    group.reload

    assert_difference 'Attachment.count', -1 do
      assert_no_difference 'CustomValue.count' do
        group.custom_field_values = {field.id => {}}
        assert group.save
      end
    end

    assert !Attachment.exists?(attachment.id)
    assert CustomValue.exists?(custom_value.id)

    custom_value.reload
    assert_equal '', custom_value.value
  end
end