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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#49081] (Re-)Add tests for pdf content
as-opf59f8f933b75
diff --git a/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rb b/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rbnew file mode 100644index 00000000000..8c4c5e5275f--- /dev/null+++ b/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rb@@ -0,0 +1,227 @@+#-- 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'++RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf do+ include Redmine::I18n+ include PDFExportSpecUtils+ let(:type_standard) { create(:type_standard) }+ let(:type_bug) { create(:type_bug) }+ let(:project) { create(:project, name: 'Foo Bla. Report No. 4/2021 with/for Case 42', types: [type_standard, type_bug]) }+ 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) }+ let(:export_time_formatted) { format_time(export_time, true) }+ let(:work_package_parent) do+ create(:work_package,+ project:,+ type: type_standard,+ subject: 'Work package 1',+ story_points: 1,+ description: 'This is a description')+ end+ let(:work_package_child) do+ create(:work_package,+ project:,+ parent: work_package_parent,+ type: type_bug,+ subject: 'Work package 2',+ story_points: 2,+ description: 'This is work package 2')+ end+ let(:work_packages) do+ [work_package_parent, work_package_child]+ 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| column_title(name) } }+ 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+ let(:column_names) { %i[id subject status story_points] }++ def work_packages_sum+ work_package_parent.story_points + work_package_child.story_points+ end++ def work_package_columns(work_package)+ [work_package.id.to_s, work_package.subject, work_package.status.name, work_package.story_points.to_s]+ end++ def work_package_details(work_package, index)+ ["#{index}.", work_package.subject,+ column_title(:id), work_package.id.to_s,+ column_title(:status), work_package.status.name,+ column_title(:story_points), work_package.story_points.to_s,+ label_title(:description), work_package.description]+ 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_package_parent),+ *work_package_columns(work_package_child),+ '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,+ work_package_parent.type.name,+ *column_titles,+ *work_package_columns(work_package_parent),+ work_package_child.type.name,+ *column_titles,+ *work_package_columns(work_package_child),+ '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,+ work_package_parent.type.name,+ *column_titles,+ *work_package_columns(work_package_parent),+ I18n.t('js.label_sum'), work_package_parent.story_points.to_s,+ work_package_child.type.name,+ *column_titles,+ *work_package_columns(work_package_child),+ I18n.t('js.label_sum'), work_package_child.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_package_parent.subject,+ '2.', '2', work_package_child.subject,+ '1/2', export_time_formatted, query.name,+ *work_package_details(work_package_parent, "1"),+ *work_package_details(work_package_child, "2"),+ '2/2', export_time_formatted, query.name+ ])+ end+ end++ describe 'with a request for a PDF Report with hierarchies' do+ let(:options) { { show_report: true } }+ let(:query_attributes) { { show_hierarchies: true } }++ it 'contains correct data' do+ expect(pdf.strings).to eq([+ query.name,+ '1.', '2', work_package_parent.subject,+ '1.1.', '2', work_package_child.subject,+ '1/2', export_time_formatted, query.name,+ *work_package_details(work_package_parent, '1'),+ *work_package_details(work_package_child, '1.1'),+ '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_package_parent.subject,+ '2.', '2', work_package_child.subject,+ '1/2', export_time_formatted, query.name,+ I18n.t('js.work_packages.tabs.overview'),+ column_title(:story_points),+ I18n.t('js.label_sum'), work_packages_sum.to_s,+ *work_package_details(work_package_parent, "1"),+ *work_package_details(work_package_child, "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_package_parent.subject,+ '2.', '2', work_package_child.subject,+ '1/2', export_time_formatted, query.name,+ I18n.t('js.work_packages.tabs.overview'),+ column_title(:type), column_title(:story_points),+ work_package_parent.type.name, work_package_parent.story_points.to_s,+ work_package_child.type.name, work_package_child.story_points.to_s,+ I18n.t('js.label_sum'), work_packages_sum.to_s,+ *work_package_details(work_package_parent, "1"),+ *work_package_details(work_package_child, "2"),+ '2/2', export_time_formatted, query.name+ ])+ end+ end+enddiff --git a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rbnew file mode 100644index 00000000000..15541fb6439--- /dev/null+++ b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb@@ -0,0 +1,112 @@+#-- 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'++RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do+ include Redmine::I18n+ include PDFExportSpecUtils+ 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) }+ let(:export_time_formatted) { format_time(export_time, true) }+ let(:image_path) { Rails.root.join("spec/fixtures/files/image.png") }+ let(:image_attachment) { Attachment.new author: user, file: File.open(image_path) }+ let(:attachments) { [image_attachment] }+ let(:work_package) do+ description = <<~DESCRIPTION+ **Lorem** _ipsum_ ~~dolor~~ `sit` [amet](https://example.com/), consetetur sadipscing elitr.+ <mention data-text="@OpenProject Admin">@OpenProject Admin</mention>+ + <p class="op-uc-p">+ <figure class="op-uc-figure">+ <div class="op-uc-figure--content">+ <img class="op-uc-image" src="/api/v3/attachments/#{image_attachment.id}/content" alt='"foobar"'>+ </div>+ <figcaption>Image Caption</figcaption>+ </figure>+ </p>+ <p><unknown-tag>Foo</unknown-tag></p>+ DESCRIPTION+ create(:work_package,+ project:,+ type:,+ subject: 'Work package 1',+ story_points: 1,+ description:).tap do |wp|+ allow(wp)+ .to receive(:attachments)+ .and_return attachments+ end+ end+ let(:options) { {} }+ let(:export) do+ login_as(user)+ described_class.new(work_package, options)+ end+ let(:export_pdf) do+ Timecop.freeze(export_time) do+ export.export!+ end+ end++ subject(:pdf) do+ content = export_pdf.content+ # File.binwrite('WorkPackageToPdf-test-preview.pdf', content)+ { strings: PDF::Inspector::Text.analyze(content).strings,+ images: PDF::Inspector::XObject.analyze(content).page_xobjects.flat_map do |o|+ o.values.select { |v| v.hash[:Subtype] == :Image }+ end }+ end++ describe 'with a request for a PDF' do+ it 'contains correct data' do+ expect(pdf[:strings]).to eq([+ "#{type.name} ##{work_package.id} - #{work_package.subject}",+ column_title(:id), work_package.id.to_s,+ column_title(:updated_at), export_time_formatted,+ column_title(:type), type.name,+ column_title(:created_at), export_time_formatted,+ column_title(:status), work_package.status.name,+ column_title(:priority), work_package.priority.name,+ label_title(:description),+ 'Lorem', ' ', 'ipsum', ' ', 'dolor', ' ', 'sit', ' ',+ 'amet', ', consetetur sadipscing elitr.', ' ', '@OpenProject Admin',+ 'Image Caption',+ 'Foo',+ '1', export_time_formatted, project.name+ ])+ expect(pdf[:images].length).to eq(2)+ end+ end+enddiff --git a/spec/support/work_packages/pdf_export.rb b/spec/support/work_packages/pdf_export.rbnew file mode 100644index 00000000000..2d7c266098b--- /dev/null+++ b/spec/support/work_packages/pdf_export.rb@@ -0,0 +1,12 @@+require 'spec_helper'+require 'pdf/inspector'++module PDFExportSpecUtils+ def column_title(column_name)+ label_title(column_name).upcase+ end++ def label_title(column_name)+ WorkPackage.human_attribute_name(column_name)+ end+end