@@ -1,55 +1,62 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class FilesController < ApplicationController |
|
19 | 19 | menu_item :files |
|
20 | 20 | |
|
21 | 21 | before_filter :find_project_by_project_id |
|
22 | 22 | before_filter :authorize |
|
23 | 23 | |
|
24 | 24 | helper :sort |
|
25 | 25 | include SortHelper |
|
26 | 26 | |
|
27 | 27 | def index |
|
28 | 28 | sort_init 'filename', 'asc' |
|
29 | 29 | sort_update 'filename' => "#{Attachment.table_name}.filename", |
|
30 | 30 | 'created_on' => "#{Attachment.table_name}.created_on", |
|
31 | 31 | 'size' => "#{Attachment.table_name}.filesize", |
|
32 | 32 | 'downloads' => "#{Attachment.table_name}.downloads" |
|
33 | 33 | |
|
34 | 34 | @containers = [Project.includes(:attachments). |
|
35 | 35 | references(:attachments).reorder(sort_clause).find(@project.id)] |
|
36 | 36 | @containers += @project.versions.includes(:attachments). |
|
37 | 37 | references(:attachments).reorder(sort_clause).to_a.sort.reverse |
|
38 | 38 | render :layout => !request.xhr? |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def new |
|
42 | 42 | @versions = @project.versions.sort |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def create |
|
46 | 46 | container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) |
|
47 | 47 | attachments = Attachment.attach_files(container, params[:attachments]) |
|
48 | 48 | render_attachment_warning_if_needed(container) |
|
49 | 49 | |
|
50 | if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added') | |
|
51 | Mailer.attachments_added(attachments[:files]).deliver | |
|
50 | if attachments[:files].present? | |
|
51 | if Setting.notified_events.include?('file_added') | |
|
52 | Mailer.attachments_added(attachments[:files]).deliver | |
|
53 | end | |
|
54 | flash[:notice] = l(:label_file_added) | |
|
55 | redirect_to project_files_path(@project) | |
|
56 | else | |
|
57 | flash.now[:error] = l(:label_attachment) + " " + l('activerecord.errors.messages.invalid') | |
|
58 | new | |
|
59 | render :action => 'new' | |
|
52 | 60 | end |
|
53 | redirect_to project_files_path(@project) | |
|
54 | 61 | end |
|
55 | 62 | end |
@@ -1,109 +1,120 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class FilesControllerTest < ActionController::TestCase |
|
21 | 21 | fixtures :projects, :trackers, :issue_statuses, :issues, |
|
22 | 22 | :enumerations, :users, |
|
23 | 23 | :email_addresses, |
|
24 | 24 | :issue_categories, |
|
25 | 25 | :projects_trackers, |
|
26 | 26 | :roles, |
|
27 | 27 | :member_roles, |
|
28 | 28 | :members, |
|
29 | 29 | :enabled_modules, |
|
30 | 30 | :journals, :journal_details, |
|
31 | 31 | :attachments, |
|
32 | 32 | :versions |
|
33 | 33 | |
|
34 | 34 | def setup |
|
35 | 35 | @request.session[:user_id] = nil |
|
36 | 36 | Setting.default_language = 'en' |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def test_index |
|
40 | 40 | get :index, :project_id => 1 |
|
41 | 41 | assert_response :success |
|
42 | 42 | assert_template 'index' |
|
43 | 43 | assert_not_nil assigns(:containers) |
|
44 | 44 | |
|
45 | 45 | # file attached to the project |
|
46 | 46 | assert_select 'a[href=?]', '/attachments/download/8/project_file.zip', :text => 'project_file.zip' |
|
47 | 47 | |
|
48 | 48 | # file attached to a project's version |
|
49 | 49 | assert_select 'a[href=?]', '/attachments/download/9/version_file.zip', :text => 'version_file.zip' |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def test_new |
|
53 | 53 | @request.session[:user_id] = 2 |
|
54 | 54 | get :new, :project_id => 1 |
|
55 | 55 | assert_response :success |
|
56 | 56 | assert_template 'new' |
|
57 | 57 | |
|
58 | 58 | assert_select 'select[name=?]', 'version_id' |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def test_new_without_versions |
|
62 | 62 | Version.delete_all |
|
63 | 63 | @request.session[:user_id] = 2 |
|
64 | 64 | get :new, :project_id => 1 |
|
65 | 65 | assert_response :success |
|
66 | 66 | assert_template 'new' |
|
67 | 67 | |
|
68 | 68 | assert_select 'select[name=?]', 'version_id', 0 |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def test_create_file |
|
72 | 72 | set_tmp_attachments_directory |
|
73 | 73 | @request.session[:user_id] = 2 |
|
74 | 74 | ActionMailer::Base.deliveries.clear |
|
75 | 75 | |
|
76 | 76 | with_settings :notified_events => %w(file_added) do |
|
77 | 77 | assert_difference 'Attachment.count' do |
|
78 | 78 | post :create, :project_id => 1, :version_id => '', |
|
79 | 79 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} |
|
80 | 80 | assert_response :redirect |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | assert_redirected_to '/projects/ecookbook/files' |
|
84 | 84 | a = Attachment.order('created_on DESC').first |
|
85 | 85 | assert_equal 'testfile.txt', a.filename |
|
86 | 86 | assert_equal Project.find(1), a.container |
|
87 | 87 | |
|
88 | 88 | mail = ActionMailer::Base.deliveries.last |
|
89 | 89 | assert_not_nil mail |
|
90 | 90 | assert_equal "[eCookbook] New file", mail.subject |
|
91 | 91 | assert_mail_body_match 'testfile.txt', mail |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def test_create_version_file |
|
95 | 95 | set_tmp_attachments_directory |
|
96 | 96 | @request.session[:user_id] = 2 |
|
97 | 97 | |
|
98 | 98 | assert_difference 'Attachment.count' do |
|
99 | 99 | post :create, :project_id => 1, :version_id => '2', |
|
100 | 100 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} |
|
101 | 101 | assert_response :redirect |
|
102 | 102 | end |
|
103 | 103 | assert_redirected_to '/projects/ecookbook/files' |
|
104 | 104 | a = Attachment.order('created_on DESC').first |
|
105 | 105 | assert_equal 'testfile.txt', a.filename |
|
106 | 106 | assert_equal Version.find(2), a.container |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | def test_create_without_file | |
|
110 | set_tmp_attachments_directory | |
|
111 | @request.session[:user_id] = 2 | |
|
112 | ||
|
113 | assert_no_difference 'Attachment.count' do | |
|
114 | post :create, :project_id => 1, :version_id => '' | |
|
115 | assert_response 200 | |
|
116 | assert_template 'new' | |
|
117 | end | |
|
118 | assert_select 'div.error', 'File is invalid' | |
|
119 | end | |
|
109 | 120 | end |
General Comments 0
You need to be logged in to leave comments.
Login now