From cbb2b612df56fc6567d1e09a2030363b1d2f6956 Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Sat, 26 Oct 2019 20:17:03 +0300 Subject: [PATCH] Fix CSV export --- app/helpers/projects_queries_helper.rb | 11 +++-- app/views/projects/_list.html.erb | 16 +++++++ app/views/projects/index.html.erb | 3 ++ test/functional/projects_controller_test.rb | 49 ++++++++++++++++++++ test/helpers/projects_queries_helper_test.rb | 35 ++++++++++++++ 5 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 test/helpers/projects_queries_helper_test.rb diff --git a/app/helpers/projects_queries_helper.rb b/app/helpers/projects_queries_helper.rb index b5de291c0..9a97cc861 100644 --- a/app/helpers/projects_queries_helper.rb +++ b/app/helpers/projects_queries_helper.rb @@ -38,16 +38,17 @@ module ProjectsQueriesHelper end end - def csv_content(column, item) - if item.is_a?(Project) + def csv_value(column, object, value) + if object.is_a?(Project) case column.name when :status - get_project_status_label[column.value_object(item)] + get_project_status_label[column.value_object(object)] when :parent_id - return item.parent.name unless item.parent.nil? + object.parent.name unless object.parent.nil? + else + super end end - super end private diff --git a/app/views/projects/_list.html.erb b/app/views/projects/_list.html.erb index 298ec8ca7..8f1f6d974 100644 --- a/app/views/projects/_list.html.erb +++ b/app/views/projects/_list.html.erb @@ -34,3 +34,19 @@ <%= pagination_links_full @entry_pages, @entry_count %> + + diff --git a/app/views/projects/index.html.erb b/app/views/projects/index.html.erb index 59fc4e6e2..4afb5de19 100644 --- a/app/views/projects/index.html.erb +++ b/app/views/projects/index.html.erb @@ -29,6 +29,9 @@ <% end %> <% other_formats_links do |f| %> + <% if @query.display_type == 'list' %> + <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '350px'); return false;" %> + <% end %> <%= f.link_to 'Atom', :url => {:key => User.current.rss_key} %> <% end %> diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb index 460752148..34c25d930 100644 --- a/test/functional/projects_controller_test.rb +++ b/test/functional/projects_controller_test.rb @@ -160,6 +160,55 @@ class ProjectsControllerTest < Redmine::ControllerTest assert_equal ['Name', 'Description', 'Status'], columns_in_list end + def test_index_as_board_should_not_include_csv_export + @request.session[:user_id] = 1 + + get :index + + assert_response :success + assert_select 'p.other-formats a.csv', 0 + assert_select '#csv-export-options', 0 + end + + def test_index_as_list_should_include_csv_export + @request.session[:user_id] = 1 + + get :index, :params => { + :display_type => 'list', + :f => ['parent_id'], + :op => {'parent_id' => '='}, + :v => {'parent_id' => ['1']} + } + assert_response :success + + # Assert CSV export link + assert_select 'p.other-formats a.csv' + + # Assert export modal + assert_select '#csv-export-options' do + assert_select 'form[action=?][method=get]', '/projects.csv' do + # filter + assert_select 'input[name=?][value=?]', 'f[]', 'parent_id' + assert_select 'input[name=?][value=?]', 'op[parent_id]', '=' + assert_select 'input[name=?][value=?]', 'v[parent_id][]', '1' + # columns + assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'name' + assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'identifier' + assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'short_description' + assert_select 'input[name=?][type=hidden]', 'c[]', 3 + assert_select 'input[name=?][value=?]', 'c[]', 'all_inline' + end + end + end + + def test_index_csv + with_settings :date_format => '%m/%d/%Y' do + get :index, :params => {:format => 'csv'} + assert_response :success + assert_equal 'text/csv', response.content_type + end + end + def test_autocomplete_js get :autocomplete, :params => { :format => 'js', diff --git a/test/helpers/projects_queries_helper_test.rb b/test/helpers/projects_queries_helper_test.rb new file mode 100644 index 000000000..e70471fe2 --- /dev/null +++ b/test/helpers/projects_queries_helper_test.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006-2019 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 ProjectsQueriesHelperTest < Redmine::HelperTest + include ProjectsQueriesHelper + + fixtures :projects, :enabled_modules, + :custom_fields, :custom_values + + def test_csv_value + c_status = QueryColumn.new(:status) + c_parent_id = QueryColumn.new(:parent_id) + + assert_equal "active", csv_value(c_status, Project.find(1), 1) + assert_equal "eCookbook", csv_value(c_parent_id, Project.find(4), 1) + end +end -- 2.22.0