Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

add pdf inspector spec for timesheet export

as-op5dfe2b378fbc
diff --git a/modules/reporting/spec/features/export_timesheet_spec.rb b/modules/reporting/spec/features/export_timesheet_spec.rb
index df3db35ad34..35f552fc961 100644
--- a/modules/reporting/spec/features/export_timesheet_spec.rb
+++ b/modules/reporting/spec/features/export_timesheet_spec.rb
@@ -28,7 +28,6 @@
require_relative "../spec_helper"
require_relative "support/pages/cost_report_page"
-require "pdf/inspector"
RSpec.describe "Timesheet PDF export", :js do
shared_let(:project) { create(:project) }
diff --git a/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb b/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb
new file mode 100644
index 00000000000..7d7cf08f230
--- /dev/null
+++ b/modules/reporting/spec/workers/cost_query/pdf/timesheet_generator_spec.rb
@@ -0,0 +1,167 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require "pdf/inspector"
+
+RSpec.describe CostQuery::PDF::TimesheetGenerator do
+ include Redmine::I18n
+ let(:query) { create(:cost_query) }
+ let(:user) { create(:user, firstname: "Export", lastname: "User") }
+ let(:time_entry_user) { create(:user, firstname: "TimeEntry", lastname: "User") }
+ let(:project) { create(:project) }
+ let(:generator) { described_class.new(query, project) }
+ let(:export_time) { DateTime.new(2024, 12, 04, 23, 59) }
+ let(:export_time_formatted) { format_time(export_time, include_date: true) }
+ let(:user_time_entry) {
+ create(:time_entry,
+ project:,
+ user: user,
+ spent_on: Date.new(2024, 12, 01),
+ start_time: 8 * 60,
+ time_zone: "UTC"
+ ) }
+ let(:time_entry) {
+ create(:time_entry,
+ project:,
+ user: time_entry_user,
+ spent_on: Date.new(2024, 12, 01),
+ start_time: 9 * 60,
+ time_zone: "UTC"
+ ) }
+ let(:other_time_entry) {
+ create(:time_entry,
+ project:,
+ user: time_entry_user,
+ spent_on: Date.new(2024, 12, 01),
+ start_time: 10 * 60,
+ time_zone: "UTC"
+ ) }
+ let(:time_entry_with_comment) {
+ create(:time_entry,
+ project:,
+ user: time_entry_user,
+ comments: "This is a comment",
+ spent_on: Date.new(2024, 12, 02)
+ ) }
+ let(:time_entry_without_time) {
+ create(:time_entry,
+ project:,
+ user: time_entry_user,
+ spent_on: Date.new(2024, 12, 03)
+ ) }
+ let(:time_entries) { [user_time_entry, time_entry, other_time_entry, time_entry_with_comment, time_entry_without_time] }
+
+ before do
+ User.current = user
+ allow(generator).to receive(:all_entries).and_return(time_entries)
+ end
+
+ subject(:pdf) do
+ content = Timecop.freeze(export_time) do
+ generator.generate!
+ end
+ # If you want to actually see the PDF for debugging, uncomment the following line
+ # File.binwrite("TimesheetGenerator-test-preview.pdf", content)
+ PDF::Inspector::Text.analyze(content).strings.join(" ")
+ end
+
+ def expected_cover_page
+ ["OpenProject", query.name,
+ time_entries.empty? ? nil : "#{format_date(time_entries.first.spent_on)} - #{format_date(time_entries.last.spent_on)}",
+ user.name, export_time_formatted].compact
+ end
+
+ def expected_first_page_content
+ [query.name]
+ end
+
+ def expected_table_header(with_times_column)
+ [
+ I18n.t(:"activerecord.attributes.time_entry.spent_on"),
+ I18n.t(:"activerecord.models.work_package"),
+ with_times_column ? I18n.t(:"export.timesheet.time") : nil,
+ I18n.t(:"activerecord.attributes.time_entry.hours"),
+ I18n.t(:"activerecord.attributes.time_entry.activity")
+ ].compact
+ end
+
+ def expected_page_footer(page_number)
+ [page_number, export_time_formatted, query.name]
+ end
+
+ def expected_entry_row(t_entry, with_times_column)
+ [format_date(t_entry.spent_on)].concat(expected_entry_columns(t_entry, with_times_column))
+ end
+
+ def expected_entry_columns(t_entry, with_times_column)
+ time_column = generator.format_spent_on_time(t_entry)
+ [
+ t_entry.work_package.subject,
+ with_times_column && time_column.present? ? time_column : nil,
+ generator.format_duration(t_entry.hours),
+ t_entry.activity.name,
+ t_entry.comments
+ ].compact
+ end
+
+ def expected_document(with_times_column)
+ [
+ *expected_cover_page,
+ *expected_first_page_content,
+
+ user.name,
+ *expected_table_header(with_times_column),
+ *expected_entry_row(user_time_entry, with_times_column),
+
+ time_entry.user.name,
+ *expected_table_header(with_times_column),
+ format_date(time_entry.spent_on), # merged date rows
+ *expected_entry_columns(time_entry, with_times_column),
+ *expected_entry_columns(other_time_entry, with_times_column),
+ *expected_entry_row(time_entry_with_comment, with_times_column),
+ *expected_entry_row(time_entry_without_time, with_times_column),
+
+ *expected_page_footer("1")
+ ].join(" ")
+ end
+
+ context "with allow_tracking_start_and_end_times", with_settings: { allow_tracking_start_and_end_times: true } do
+ it "renders the expected document" do
+ expect(subject).to eq expected_document(true)
+ end
+ end
+
+ context "without allow_tracking_start_and_end_times", with_settings: { allow_tracking_start_and_end_times: false } do
+ it "renders the expected document" do
+ expect(subject).to eq expected_document(false)
+ end
+ end
+end