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 specs for workPackageValue, workPackageLabel, projectLabel, projectValue macros

as-op5f9b6c57139e
diff --git a/spec/models/work_package/pdf_export/common/macro_spec.rb b/spec/models/work_package/pdf_export/common/macro_spec.rb
index 9d32edcad91..2f78102c248 100644
--- a/spec/models/work_package/pdf_export/common/macro_spec.rb
+++ b/spec/models/work_package/pdf_export/common/macro_spec.rb
@@ -31,14 +31,52 @@
require "spec_helper"
RSpec.describe WorkPackage::PDFExport::Common::Macro do
- let(:work_package) do
- create(:work_package, id: 185, subject: "Work package 1")
+ shared_let(:type_task) { create(:type_task) }
+ shared_let(:custom_field) do
+ create(
+ :work_package_custom_field,
+ name: "Custom Field 1",
+ field_format: "string",
+ types: [type_task]
+ )
+ end
+ shared_let(:project_custom_field_section) { create(:project_custom_field_section) }
+ shared_let(:project_custom_field) do
+ create(:string_project_custom_field, name: "Project Custom Field 1", project_custom_field_section:)
+ end
+ shared_let(:project) do
+ create(
+ :project,
+ status_code: "on_track",
+ work_package_custom_fields: [custom_field],
+ project_custom_fields: [project_custom_field],
+ custom_field_values: { project_custom_field.id => "Project custom value 1" },
+ &:save!
+ )
+ end
+ shared_let(:work_package) do
+ create(
+ :work_package,
+ id: 185,
+ subject: "Work package 1",
+ type: type_task,
+ status: create(:status, name: "In Progress"), project: project,
+ custom_field_values: { custom_field.id => "Custom value 1" },
+ &:save!
+ )
+ end
+ shared_let(:formatter) { Class.new { extend WorkPackage::PDFExport::Common::Macro } }
+ shared_let(:user) do
+ create(:user, member_with_permissions: { project => %i[view_work_packages view_project_attributes view_project] })
+ end
+ let!(:markdown) { "" }
+
+ before do
+ User.current = user
end
- let(:markdown) { "" }
- let(:formatter) { Class.new { extend WorkPackage::PDFExport::Common::Macro } }
subject(:formatted) do
- formatter.apply_markdown_field_macros(markdown, { work_package: work_package, user: User.current })
+ formatter.apply_markdown_field_macros(markdown, { work_package: work_package, project: project, user: })
end
describe "empty text" do
@@ -47,48 +85,362 @@ RSpec.describe WorkPackage::PDFExport::Common::Macro do
end
end
- describe "wp mention tag" do
- let(:markdown) { '<mention class="mention" data-id="185" data-type="work_package" data-text="#185">#185</mention>' }
+ describe "wp mention macro" do
+ describe "with tag" do
+ let(:markdown) { '<mention class="mention" data-id="185" data-type="work_package" data-text="#185">#185</mention>' }
+
+ it "ignores the tag" do
+ expect(formatted).to eq("<mention class=\"mention\" data-id=\"185\" " +
+ "data-type=\"work_package\" data-text=\"#185\">\\#185</mention>\n")
+ end
+ end
+
+ describe "with plain" do
+ let(:markdown) { "#185" }
+
+ it "contains correct data" do
+ expect(formatted).to eq("<mention class=\"mention\" data-id=\"185\" " +
+ "data-type=\"work_package\" data-text=\"#185\">#185</mention>\n")
+ end
+ end
+
+ describe "with markdown formating bold" do
+ let(:markdown) { "\n**#185**\n" }
- it "ignores the tag" do
- expect(formatted).to eq("<mention class=\"mention\" data-id=\"185\" " +
- "data-type=\"work_package\" data-text=\"#185\">\\#185</mention>\n")
+ it "contains correct data" do
+ expect(formatted).to eq("**<mention class=\"mention\" data-id=\"185\" " +
+ "data-type=\"work_package\" data-text=\"#185\">#185</mention>**\n")
+ end
+ end
+
+ describe "with markdown formating strikethrough" do
+ let(:markdown) { "~~#185~~" }
+
+ it "contains correct data" do
+ expect(formatted).to eq("~~<mention class=\"mention\" data-id=\"185\" " +
+ "data-type=\"work_package\" data-text=\"#185\">#185</mention>~~\n")
+ end
+ end
+
+ describe "with strikethrough in table" do
+ let(:markdown) { "<table><tr><td><p><s>##185</s></p></td></tr></table>" }
+
+ it "contains correct data" do
+ expect(formatted).to eq("<table><tr><td><p><s><mention class=\"mention\" data-id=\"185\" " +
+ "data-type=\"work_package\" data-text=\"##185\">##185</mention></s></p></td></tr></table>\n")
+ end
end
end
- describe "wp mention plain" do
- let(:markdown) { "#185" }
+ describe "workPackageValue macro" do
+ describe "with current work package attribute" do
+ let(:markdown) { "workPackageValue:subject" }
- it "contains correct data" do
- expect(formatted).to eq("<mention class=\"mention\" data-id=\"185\" " +
- "data-type=\"work_package\" data-text=\"#185\">#185</mention>\n")
+ it "outputs the attribute value" do
+ expect(formatted).to eq("Work package 1\n")
+ end
+ end
+
+ describe "with specific work package ID and attribute" do
+ let(:markdown) { "workPackageValue:185:subject" }
+
+ it "outputs the attribute value for the specified work package" do
+ expect(formatted).to eq("Work package 1\n")
+ end
+ end
+
+ describe "with non-existent work package ID" do
+ let(:markdown) { "workPackageValue:999:subject" }
+
+ it "outputs an error message" do
+ expect(formatted).to include("Macro error, resource not found")
+ end
+ end
+
+ describe "with status attribute" do
+ let(:markdown) { "workPackageValue:status" }
+
+ it "outputs the status name" do
+ expect(formatted).to eq("In Progress\n")
+ end
+ end
+
+ describe "with custom field by name" do
+ let(:markdown) { "workPackageValue:\"Custom Field 1\"" }
+
+ it "outputs the custom field value" do
+ expect(formatted).to eq("Custom value 1\n")
+ end
+ end
+
+ describe "with specific work package ID and custom field" do
+ let(:markdown) { "workPackageValue:185:\"Custom Field 1\"" }
+
+ it "outputs the custom field value for the specified work package" do
+ expect(formatted).to eq("Custom value 1\n")
+ end
+ end
+
+ describe "with non-existent attribute" do
+ let(:markdown) { "workPackageValue:nonexistent_attribute" }
+
+ it "outputs an empty value" do
+ expect(formatted).to eq(" \n")
+ end
+ end
+
+ describe "with markdown formatting" do
+ let(:markdown) { "**workPackageValue:subject**" }
+
+ it "preserves the markdown formatting" do
+ expect(formatted).to eq("**Work package 1**\n")
+ end
+ end
+
+ describe "in a table" do
+ let(:markdown) { "<table><tr><td>workPackageValue:subject</td></tr></table>" }
+
+ it "processes the macro inside HTML" do
+ expect(formatted).to eq("<table><tr><td>Work package 1</td></tr></table>\n")
+ end
end
end
- describe "wp mention with markdown formating bold" do
- let(:markdown) { "\n**#185**\n" }
+ describe "workPackageLabel macro" do
+ describe "with current work package attribute" do
+ let(:markdown) { "workPackageLabel:subject" }
- it "contains correct data" do
- expect(formatted).to eq("**<mention class=\"mention\" data-id=\"185\" " +
- "data-type=\"work_package\" data-text=\"#185\">#185</mention>**\n")
+ it "outputs the attribute label" do
+ expect(formatted).to eq("Subject\n")
+ end
+ end
+
+ describe "with specific work package ID and attribute" do
+ let(:markdown) { "workPackageLabel:185:subject" }
+
+ it "outputs the attribute label for the specified work package" do
+ expect(formatted).to eq("Subject\n")
+ end
+ end
+
+ describe "with non-existent work package ID" do
+ let(:markdown) { "workPackageLabel:999:subject" }
+
+ it "outputs a humanized form" do
+ expect(formatted).to include("Subject")
+ end
+ end
+
+ describe "with status attribute" do
+ let(:markdown) { "workPackageLabel:status" }
+
+ it "outputs the status label" do
+ expect(formatted).to eq("Status\n")
+ end
+ end
+
+ describe "with custom field by name" do
+ let(:markdown) { "workPackageLabel:\"Custom Field 1\"" }
+
+ it "outputs the custom field name" do
+ expect(formatted).to include("Custom Field 1")
+ end
+ end
+
+ describe "with specific work package ID and custom field" do
+ let(:markdown) { "workPackageLabel:185:\"Custom Field 1\"" }
+
+ it "outputs the custom field name for the specified work package" do
+ expect(formatted).to include("Custom Field 1")
+ end
+ end
+
+ describe "with non-existent attribute" do
+ let(:markdown) { "workPackageLabel:nonexistent_attribute" }
+
+ it "outputs the humanized attribute name" do
+ expect(formatted).to include("nonexistent_attribute")
+ end
+ end
+
+ describe "with markdown formatting" do
+ let(:markdown) { "**workPackageLabel:subject**" }
+
+ it "preserves the markdown formatting" do
+ expect(formatted).to include("**Subject**")
+ end
+ end
+
+ describe "in a table" do
+ let(:markdown) { "<table><tr><td>workPackageLabel:subject</td></tr></table>" }
+
+ it "processes the macro inside HTML" do
+ expect(formatted).to eq("<table><tr><td>Subject</td></tr></table>\n")
+ end
end
end
- describe "wp mention with markdown formating strikethrough" do
- let(:markdown) { "~~#185~~" }
+ describe "projectValue macro" do
+ describe "with current project attribute" do
+ let(:markdown) { "projectValue:name" }
- it "contains correct data" do
- expect(formatted).to eq("~~<mention class=\"mention\" data-id=\"185\" " +
- "data-type=\"work_package\" data-text=\"#185\">#185</mention>~~\n")
+ it "outputs the attribute value" do
+ expect(formatted).to eq("#{project.name}\n")
+ end
+ end
+
+ describe "with specific project ID and attribute" do
+ let(:markdown) { "projectValue:#{project.id}:name" }
+
+ it "outputs the attribute value for the specified project" do
+ expect(formatted).to eq("#{project.name}\n")
+ end
+ end
+
+ describe "with specific project identifier and attribute" do
+ let(:markdown) { "projectValue:\"#{project.identifier}\":name" }
+
+ it "outputs the attribute value for the specified project" do
+ expect(formatted).to eq("#{project.name}\n")
+ end
+ end
+
+ describe "with non-existent project ID" do
+ let(:markdown) { "projectValue:999:name" }
+
+ it "outputs an error message" do
+ expect(formatted).to include("Macro error, resource not found")
+ end
+ end
+
+ describe "with status attribute" do
+ let(:markdown) { "projectValue:status_code" }
+
+ it "outputs the status code" do
+ expect(formatted).to include(project.status_code)
+ end
+ end
+
+ describe "with custom field by name" do
+ let(:markdown) { "projectValue:\"Project Custom Field 1\"" }
+
+ it "outputs the custom field value" do
+ expect(formatted).to eq("Project custom value 1\n")
+ end
+ end
+
+ describe "with specific project ID and custom field" do
+ let(:markdown) { "projectValue:#{project.id}:\"Project Custom Field 1\"" }
+
+ it "outputs the custom field value for the specified project" do
+ expect(formatted).to eq("Project custom value 1\n")
+ end
+ end
+
+ describe "with non-existent attribute" do
+ let(:markdown) { "projectValue:nonexistent_attribute" }
+
+ it "outputs an empty value" do
+ expect(formatted).to eq(" \n")
+ end
+ end
+
+ describe "with markdown formatting" do
+ let(:markdown) { "**projectValue:name**" }
+
+ it "preserves the markdown formatting" do
+ expect(formatted).to eq("**#{project.name}**\n")
+ end
+ end
+
+ describe "in a table" do
+ let(:markdown) { "<table><tr><td>projectValue:name</td></tr></table>" }
+
+ it "processes the macro inside HTML" do
+ expect(formatted).to eq("<table><tr><td>#{project.name}</td></tr></table>\n")
+ end
end
end
- describe "wp mention with strikethrough in table" do
- let(:markdown) { "<table><tr><td><p><s>##185</s></p></td></tr></table>" }
+ describe "projectLabel macro" do
+ describe "with current project attribute" do
+ let(:markdown) { "projectLabel:name" }
- it "contains correct data" do
- expect(formatted).to eq("<table><tr><td><p><s><mention class=\"mention\" data-id=\"185\" " +
- "data-type=\"work_package\" data-text=\"##185\">##185</mention></s></p></td></tr></table>\n")
+ it "outputs the attribute label" do
+ expect(formatted).to eq("Name\n")
+ end
+ end
+
+ describe "with specific project ID and attribute" do
+ let(:markdown) { "projectLabel:#{project.id}:name" }
+
+ it "outputs the attribute label for the specified project" do
+ expect(formatted).to eq("Name\n")
+ end
+ end
+
+ describe "with specific project identifier and attribute" do
+ let(:markdown) { "projectLabel:\"#{project.identifier}\":name" }
+
+ it "outputs the attribute label for the specified project" do
+ expect(formatted).to eq("Name\n")
+ end
+ end
+
+ describe "with non-existent project ID" do
+ let(:markdown) { "projectLabel:999:name" }
+
+ it "outputs the attribute label" do
+ expect(formatted).to eq("Name\n")
+ end
+ end
+
+ describe "with status attribute" do
+ let(:markdown) { "projectLabel:status_code" }
+
+ it "outputs the status label" do
+ expect(formatted).to eq("Project status\n")
+ end
+ end
+
+ describe "with custom field by name" do
+ let(:markdown) { "projectLabel:\"Project Custom Field 1\"" }
+
+ it "outputs the custom field name" do
+ expect(formatted).to eq("Project Custom Field 1\n")
+ end
+ end
+
+ describe "with specific project ID and custom field" do
+ let(:markdown) { "projectLabel:#{project.id}:\"Project Custom Field 1\"" }
+
+ it "outputs the custom field name for the specified project" do
+ expect(formatted).to include("Project Custom Field 1")
+ end
+ end
+
+ describe "with non-existent attribute" do
+ let(:markdown) { "projectLabel:nonexistent_attribute" }
+
+ it "outputs the humanized attribute name" do
+ expect(formatted).to include("nonexistent_attribute")
+ end
+ end
+
+ describe "with markdown formatting" do
+ let(:markdown) { "**projectLabel:name**" }
+
+ it "preserves the markdown formatting" do
+ expect(formatted).to eq("**Name**\n")
+ end
+ end
+
+ describe "in a table" do
+ let(:markdown) { "<table><tr><td>projectLabel:name</td></tr></table>" }
+
+ it "processes the macro inside HTML" do
+ expect(formatted).to eq("<table><tr><td>Name</td></tr></table>\n")
+ end
end
end
end