Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#49081] PDF-Export: Add tests for pdf content #13238

Merged
merged 19 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ PATH
GEM
remote: https://rubygems.org/
specs:
Ascii85 (1.1.0)
actioncable (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
Expand Down Expand Up @@ -305,6 +306,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)
Expand Down Expand Up @@ -530,6 +532,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)
Expand Down Expand Up @@ -675,6 +678,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)
Expand Down Expand Up @@ -862,6 +873,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
Expand Down Expand Up @@ -1098,6 +1110,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)
Expand Down
235 changes: 235 additions & 0 deletions spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
#-- 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(: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 column_title(column_name)
label_title(column_name).upcase
end

def label_title(column_name)
WorkPackage.human_attribute_name(column_name)
end

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
end
Loading
Loading