diff --git a/lib/redmine/helpers/time_report.rb b/lib/redmine/helpers/time_report.rb index 2ae5fd8eb..c935e82e3 100644 --- a/lib/redmine/helpers/time_report.rb +++ b/lib/redmine/helpers/time_report.rb @@ -50,6 +50,7 @@ def run reorder(nil). group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns). joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact). + order(@criteria.collect{|criteria| @available_criteria[criteria][:order]}.compact). sum(:hours).each do |hash, hours| h = {'hours' => hours} (@criteria + time_columns).each_with_index do |name, i| @@ -106,7 +107,10 @@ def load_available_criteria @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", :klass => Project, - :label => :label_project}, + :label => :label_project, + :joins => :project, + :order => "#{Project.table_name}.name" + }, 'status' => {:sql => "#{Issue.table_name}.status_id", :klass => IssueStatus, :label => :field_status}, diff --git a/test/unit/time_report_test.rb b/test/unit/time_report_test.rb new file mode 100644 index 000000000..396ec4a0b --- /dev/null +++ b/test/unit/time_report_test.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006-2020 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 TimeReportTest < ActiveSupport::TestCase + fixtures :trackers, :users + + def setup + User.current = nil + TimeEntry.destroy_all + end + + def report_by(criteria) + project = nil + issue = nil # Unused argument. TODO: Remove + query = TimeEntryQuery.new + Redmine::Helpers::TimeReport.new(project, issue, criteria, 'month', query.results_scope) + end + + def test_project_sorted_by_name + project_b = Project.generate!(name: 'Project B') + project_a = Project.generate!(name: 'Project A') + TimeEntry.generate!(project: project_b) + TimeEntry.generate!(project: project_a) + report = report_by(['project']) + + result_projects = report.hours.map { |row| row['project'] } + + assert_equal [project_a.id, project_b.id], result_projects + end + + # TODO: This test is passing. Why??? + # def test_status_sorted_by_position + # status_b = IssueStatus.generate!(position: 2) + # status_a = IssueStatus.generate!(position: 1) + + # issue_b = Issue.generate! + # issue_b.update(status: status_b) + # issue_a = Issue.generate! + # issue_a.update(status: status_a) + + # TimeEntry.generate!(issue: issue_a) + # TimeEntry.generate!(issue: issue_b) + # report = report_by(['status']) + + # result_statuses = report.hours.map { |row| row['status'] } + + # assert_equal [status_a.id, status_b.id], result_statuses + # end +end