Adds file custom field format (#6719).

git-svn-id: http://svn.redmine.org/redmine/trunk@15917 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2016-10-23 09:16:14 +00:00
parent c91a4391d3
commit ef45304817
17 changed files with 607 additions and 77 deletions

View File

@@ -0,0 +1,156 @@
# Redmine - project management software
# Copyright (C) 2006-2016 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 File.expand_path('../../../../../test_helper', __FILE__)
class AttachmentFieldFormatTest < Redmine::IntegrationTest
fixtures :projects,
:users, :email_addresses,
:roles,
:members,
:member_roles,
:trackers,
:projects_trackers,
:enabled_modules,
:issue_statuses,
:issues,
:enumerations,
:custom_fields,
:custom_values,
:custom_fields_trackers,
:attachments
def setup
set_tmp_attachments_directory
@field = IssueCustomField.generate!(:name => "File", :field_format => "attachment")
log_user "jsmith", "jsmith"
end
def test_new_should_include_inputs
get '/projects/ecookbook/issues/new'
assert_response :success
assert_select '[name^=?]', "issue[custom_field_values][#{@field.id}]", 2
assert_select 'input[name=?][type=hidden][value=""]', "issue[custom_field_values][#{@field.id}][blank]"
end
def test_create_with_attachment
issue = new_record(Issue) do
assert_difference 'Attachment.count' do
post '/projects/ecookbook/issues', {
:issue => {
:subject => "Subject",
:custom_field_values => {
@field.id => {
'blank' => '',
'1' => {:file => uploaded_test_file("testfile.txt", "text/plain")}
}
}
}
}
assert_response 302
end
end
custom_value = issue.custom_value_for(@field)
assert custom_value
assert custom_value.value.present?
attachment = Attachment.find_by_id(custom_value.value)
assert attachment
assert_equal custom_value, attachment.container
follow_redirect!
assert_response :success
# link to the attachment
link = css_select(".cf_#{@field.id} .value a")
assert_equal 1, link.size
assert_equal "testfile.txt", link.text
# download the attachment
get link.attr('href')
assert_response :success
assert_equal "text/plain", response.content_type
end
def test_create_without_attachment
issue = new_record(Issue) do
assert_no_difference 'Attachment.count' do
post '/projects/ecookbook/issues', {
:issue => {
:subject => "Subject",
:custom_field_values => {
@field.id => {:blank => ''}
}
}
}
assert_response 302
end
end
custom_value = issue.custom_value_for(@field)
assert custom_value
assert custom_value.value.blank?
follow_redirect!
assert_response :success
# no links to the attachment
assert_select ".cf_#{@field.id} .value a", 0
end
def test_failure_on_create_should_preserve_attachment
attachment = new_record(Attachment) do
assert_no_difference 'Issue.count' do
post '/projects/ecookbook/issues', {
:issue => {
:subject => "",
:custom_field_values => {
@field.id => {:file => uploaded_test_file("testfile.txt", "text/plain")}
}
}
}
assert_response :success
assert_select_error /Subject cannot be blank/
end
end
assert_nil attachment.container_id
assert_select 'input[name=?][value=?][type=hidden]', "issue[custom_field_values][#{@field.id}][p0][token]", attachment.token
assert_select 'input[name=?][value=?]', "issue[custom_field_values][#{@field.id}][p0][filename]", 'testfile.txt'
issue = new_record(Issue) do
assert_no_difference 'Attachment.count' do
post '/projects/ecookbook/issues', {
:issue => {
:subject => "Subject",
:custom_field_values => {
@field.id => {:token => attachment.token}
}
}
}
assert_response 302
end
end
custom_value = issue.custom_value_for(@field)
assert custom_value
assert_equal attachment.id.to_s, custom_value.value
assert_equal custom_value, attachment.reload.container
end
end

View File

@@ -0,0 +1,163 @@
# Redmine - project management software
# Copyright (C) 2006-2016 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 File.expand_path('../../../../../test_helper', __FILE__)
require 'redmine/field_format'
class Redmine::AttachmentFieldFormatTest < ActionView::TestCase
include ApplicationHelper
include Redmine::I18n
fixtures :users
def setup
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