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

[#49081] Add tests for pdf content

as-op4cf30166afae
diff --git a/Gemfile b/Gemfile
index 904dbb8c240..e1709008789 100644
--- a/Gemfile
+++ b/Gemfile
@@ -237,6 +237,9 @@ group :test do
# XML comparison tests
gem 'compare-xml', '~> 0.66', require: false
+ # PDF Export tests
+ gem 'pdf-inspector', '~> 1.2'
+
# brings back testing for 'assigns' and 'assert_template' extracted in rails 5
gem 'rails-controller-testing', '~> 1.0.2'
diff --git a/Gemfile.lock b/Gemfile.lock
index 563f6a50e3f..4d6f7846854 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -205,6 +205,7 @@ PATH
GEM
remote: https://rubygems.org/
specs:
+ Ascii85 (1.1.0)
actioncable (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
@@ -294,6 +295,7 @@ GEM
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
aes_key_wrap (1.1.0)
+ afm (0.2.2)
airbrake (13.0.3)
airbrake-ruby (~> 6.0)
airbrake-ruby (6.2.1)
@@ -518,6 +520,7 @@ GEM
gravatar_image_tag (1.2.0)
hana (1.3.7)
hashdiff (1.0.1)
+ hashery (2.1.2)
hashie (3.6.0)
html-pipeline (2.14.3)
activesupport (>= 2)
@@ -661,6 +664,14 @@ GEM
ast (~> 2.4.1)
racc
pdf-core (0.9.0)
+ pdf-inspector (1.3.0)
+ pdf-reader (>= 1.0, < 3.0.a)
+ pdf-reader (2.11.0)
+ Ascii85 (~> 1.0)
+ afm (~> 0.2.1)
+ hashery (~> 2.0)
+ ruby-rc4
+ ttfunk
pg (1.5.3)
plaintext (0.3.4)
activesupport (> 2.2.1)
@@ -848,6 +859,7 @@ GEM
ruby-ole (1.2.12.2)
ruby-prof (1.6.3)
ruby-progressbar (1.13.0)
+ ruby-rc4 (0.1.5)
ruby-saml (1.15.0)
nokogiri (>= 1.13.10)
rexml
@@ -1084,6 +1096,7 @@ DEPENDENCIES
ox
paper_trail (~> 12.3)
parallel_tests (~> 4.0)
+ pdf-inspector (~> 1.2)
pg (~> 1.5.0)
plaintext (~> 0.3.2)
posix-spawn (~> 0.3.13)
diff --git a/spec/workers/work_packages/pdf_export/work_package_list_to_pdf_spec.rb b/spec/workers/work_packages/pdf_export/work_package_list_to_pdf_spec.rb
new file mode 100644
index 00000000000..4f1389d8269
--- /dev/null
+++ b/spec/workers/work_packages/pdf_export/work_package_list_to_pdf_spec.rb
@@ -0,0 +1,220 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 WorkPackage::PDFExport::WorkPackageListToPdf do
+ include Redmine::I18n
+ let(:type1) { create(:type_standard) }
+ let(:type2) { create(:type_bug) }
+ let(:project) { create(:project, name: 'Foo Bla. Report No. 4/2021 with/for Case 42', types: [type1, type2]) }
+ let(:user) do
+ create(:user,
+ member_in_project: project,
+ member_with_permissions: %w[view_work_packages export_work_packages])
+ end
+ let(:column_names) { [:id, :subject, :status, :story_points] }
+
+ def work_package_columns(wp)
+ [wp.id.to_s, wp.subject, wp.status.name, wp.story_points.to_s]
+ end
+
+ def work_package_details(wp, nr)
+ ["#{nr}.", wp.subject,
+ 'ID', wp.id.to_s,
+ 'STATUS', wp.status.name,
+ 'STORY POINTS', wp.story_points.to_s,
+ 'Description',
+ wp.description]
+ end
+
+ let(:export_time) {
+ DateTime.new(2023, 6, 30, 23, 59, 00, '+00:00')
+ }
+ let(:export_time_formatted) {
+ format_time(export_time, true)
+ }
+
+ let(:work_package1) do
+ create(:work_package,
+ project:,
+ type: type1,
+ subject: 'Work package 1',
+ story_points: 1,
+ description: 'This is a description'
+ )
+ end
+ let(:work_package2) do
+ create(:work_package, project:,
+ type: type2,
+ subject: 'Work package 2',
+ story_points: 2,
+ description: 'This is work package 2'
+ )
+ end
+ let(:work_packages) do
+ [work_package1, work_package2]
+ end
+ let(:query_attributes) { {} }
+ let!(:query) do
+ build(:query, user:, project:, **query_attributes).tap do |q|
+ q.column_names = column_names
+ q.sort_criteria = [%w[id asc]]
+ end
+ end
+ let(:column_titles) { column_names.map { |name| WorkPackage.human_attribute_name(name).upcase } }
+ let(:options) { {} }
+
+ let(:export) do
+ login_as(user)
+ work_packages
+ described_class.new(query, options)
+ end
+
+ let(:export_pdf) do
+ Timecop.freeze(export_time) do
+ export.export!
+ end
+ end
+
+ def open_temp_pdf
+ cmd = "open -a Preview #{export_pdf.content.path}"
+ puts cmd
+ `#{cmd}`
+ end
+
+ subject(:pdf) do
+ PDF::Inspector::Text.analyze(File.read(export_pdf.content.path))
+ end
+
+ describe 'with a request for a PDF table' do
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ query.name,
+ *column_titles,
+ *work_package_columns(work_package1),
+ *work_package_columns(work_package2),
+ '1/1', export_time_formatted, query.name
+ ])
+ end
+ end
+
+ describe 'with a request for a PDF table grouped' do
+ let(:query_attributes) { { group_by: 'type' } }
+
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ query.name,
+ type1.name,
+ *column_titles,
+ *work_package_columns(work_package1),
+ type2.name,
+ *column_titles,
+ *work_package_columns(work_package2),
+ '1/1', export_time_formatted, query.name
+ ])
+ end
+ end
+
+ describe 'with a request for a PDF table grouped with sums' do
+ let(:query_attributes) { { group_by: 'type', display_sums: true } }
+
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ query.name,
+ type1.name,
+ *column_titles,
+ *work_package_columns(work_package1),
+ "Sum", work_package1.story_points.to_s,
+ type2.name,
+ *column_titles,
+ *work_package_columns(work_package2),
+ "Sum", work_package2.story_points.to_s,
+ '1/1', export_time_formatted, query.name
+ ])
+ end
+ end
+
+ describe 'with a request for a PDF Report' do
+ let(:options) { { show_report: true } }
+
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ query.name,
+ '1.', '2', work_package1.subject,
+ '2.', '2', work_package2.subject,
+ '1/2', export_time_formatted, query.name,
+ *work_package_details(work_package1, 1),
+ *work_package_details(work_package2, 2),
+ '2/2', export_time_formatted, query.name
+ ])
+ end
+ end
+
+ describe 'with a request for a PDF Report with sums' do
+ let(:options) { { show_report: true } }
+ let(:query_attributes) { { display_sums: true } }
+
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ query.name,
+ '1.', '2', work_package1.subject,
+ '2.', '2', work_package2.subject,
+ '1/2', export_time_formatted, query.name,
+ "Overview",
+ "STORY POINTS", "Sum", (work_package1.story_points + work_package2.story_points).to_s,
+ *work_package_details(work_package1, 1),
+ *work_package_details(work_package2, 2),
+ '2/2', export_time_formatted, query.name
+ ])
+ end
+ end
+
+ describe 'with a request for a PDF Report grouped with sums' do
+ let(:options) { { show_report: true } }
+ let(:query_attributes) { { display_sums: true, group_by: 'type' } }
+
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ query.name,
+ '1.', '2', work_package1.subject,
+ '2.', '2', work_package2.subject,
+ '1/2', export_time_formatted, query.name,
+ "Overview",
+ "TYPE", "STORY POINTS",
+ type1.name, work_package1.story_points.to_s,
+ type2.name, work_package2.story_points.to_s,
+ "Sum", (work_package1.story_points + work_package2.story_points).to_s,
+ *work_package_details(work_package1, 1),
+ *work_package_details(work_package2, 2),
+ '2/2', export_time_formatted, query.name
+ ])
+ end
+ end
+end
diff --git a/spec/workers/work_packages/pdf_export/work_package_to_pdf_spec.rb b/spec/workers/work_packages/pdf_export/work_package_to_pdf_spec.rb
new file mode 100644
index 00000000000..9806a54f02a
--- /dev/null
+++ b/spec/workers/work_packages/pdf_export/work_package_to_pdf_spec.rb
@@ -0,0 +1,96 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 WorkPackage::PDFExport::WorkPackageToPdf do
+ include Redmine::I18n
+ let(:type) { create(:type_bug) }
+ let(:project) { create(:project, name: 'Foo Bla. Report No. 4/2021 with/for Case 42', types: [type]) }
+ let(:user) do
+ create(:user,
+ member_in_project: project,
+ member_with_permissions: %w[view_work_packages export_work_packages])
+ end
+ let(:export_time) {
+ DateTime.new(2023, 6, 30, 23, 59, 00, '+00:00')
+ }
+ let(:export_time_formatted) {
+ format_time(export_time, true)
+ }
+
+ let(:wp) do
+ create(:work_package,
+ project:,
+ type:,
+ subject: 'Work package 1',
+ story_points: 1,
+ description: 'This is a description'
+ )
+ end
+ let(:options) { {} }
+
+ let(:export) do
+ login_as(user)
+ described_class.new(wp, options)
+ end
+
+ let(:export_pdf) do
+ Timecop.freeze(export_time) do
+ export.export!
+ end
+ end
+
+ subject(:pdf) do
+ PDF::Inspector::Text.analyze(export_pdf.content)
+ end
+
+ describe 'with a request for a PDF' do
+ it 'contains correct data' do
+ expect(pdf.strings).to eq([
+ "#{type.name} ##{wp.id} - #{wp.subject}",
+ 'ID', wp.id.to_s,
+ "UPDATED ON", export_time_formatted,
+ "TYPE", type.name,
+ "CREATED ON", export_time_formatted,
+ 'STATUS', wp.status.name,
+ "FINISH DATE",
+ "VERSION",
+ "PRIORITY", wp.priority.name,
+ "DURATION",
+ "WORK",
+ "CATEGORY",
+ "ASSIGNEE",
+ 'Description',
+ wp.description,
+ '1', export_time_formatted, project.name
+ ])
+ end
+ end
+end