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

Always look into en/ folder for seeding attachments

Christophe Bliard26154538aefc
diff --git a/app/seeders/demo_data/create_attachments.rb b/app/seeders/demo_data/create_attachments.rb
new file mode 100644
index 00000000000..98c70294dcb
--- /dev/null
+++ b/app/seeders/demo_data/create_attachments.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+module DemoData::CreateAttachments
+ def create_attachments!(container, attributes)
+ Array(attributes['attachments']).each do |file_name|
+ attachment = container.attachments.build
+ attachment.author = user
+ attachment.file = File.new(attachment_path(file_name))
+
+ attachment.save!
+ end
+ end
+
+ def attachment_path(file_name)
+ Rails.root.join(
+ "config/locales/media/en/#{file_name}"
+ )
+ end
+end
diff --git a/app/seeders/demo_data/overview_seeder.rb b/app/seeders/demo_data/overview_seeder.rb
index 53923772320..3b8be1ed1f9 100644
--- a/app/seeders/demo_data/overview_seeder.rb
+++ b/app/seeders/demo_data/overview_seeder.rb
@@ -1,6 +1,7 @@
module DemoData
class OverviewSeeder < Seeder
- include ::DemoData::References
+ include CreateAttachments
+ include References
def seed_data!
print_status "*** Seeding Overview"
@@ -49,22 +50,6 @@ module DemoData
overview.widgets.build(widget_config.except('attachments'))
end
- def create_attachments!(overview, attributes)
- Array(attributes['attachments']).each do |file_name|
- attachment = overview.attachments.build
- attachment.author = user
- attachment.file = File.new(attachment_path(file_name))
-
- attachment.save!
- end
- end
-
- def attachment_path(file_name)
- Rails.root.join(
- "config/locales/media/#{I18n.locale}/#{file_name}"
- )
- end
-
def find_project(project_data)
Project.find_by!(identifier: project_data.lookup('identifier'))
end
diff --git a/app/seeders/demo_data/work_package_seeder.rb b/app/seeders/demo_data/work_package_seeder.rb
index a68f6bc8f3e..2c560a9b3c8 100644
--- a/app/seeders/demo_data/work_package_seeder.rb
+++ b/app/seeders/demo_data/work_package_seeder.rb
@@ -26,11 +26,12 @@
# See COPYRIGHT and LICENSE files for more details.
module DemoData
class WorkPackageSeeder < Seeder
+ include CreateAttachments
+ include References
+
attr_reader :project, :statuses, :repository, :types
alias_method :project_data, :seed_data
- include ::DemoData::References
-
def initialize(project, project_data)
super(project_data)
@project = project
@@ -153,16 +154,6 @@ module DemoData
wp_attr[:story_points] = attributes['story_points'].presence&.to_i
end
- def create_attachments!(work_package, attributes)
- Array(attributes['attachments']).each do |file_name|
- attachment = work_package.attachments.build
- attachment.author = work_package.author
- attachment.file = File.new("config/locales/media/en/#{file_name}")
-
- attachment.save!
- end
- end
-
def set_work_package_relations
project_data.each('work_packages') do |attributes|
create_relations attributes
diff --git a/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb b/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb
index fe5f449997e..1e21a31e829 100644
--- a/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb
+++ b/modules/bim/spec/seeders/root_seeder_bim_edition_spec.rb
@@ -33,8 +33,9 @@ RSpec::Matchers.define_negated_matcher :not_start_with, :start_with
describe RootSeeder,
'BIM edition',
- with_config: { edition: 'bim' },
- with_settings: { journal_aggregation_time_minutes: 0 } do
+ with_config: { edition: 'bim' } do
+ include RootSeederTestHelpers
+
shared_examples 'creates BIM demo data' do
it 'creates an admin user' do
expect(User.not_builtin.where(admin: true).count).to eq 1
@@ -81,12 +82,7 @@ describe RootSeeder,
describe 'demo data' do
before_all do
- RSpec::Mocks.with_temporary_scope do
- # the mocking of settings and configuration is duplicated here because
- # it's executed outside of an example context
- with_config(edition: 'bim')
- with_settings(journal_aggregation_time_minutes: 0)
-
+ with_edition('bim') do
described_class.new.seed_data!
end
end
@@ -96,14 +92,9 @@ describe RootSeeder,
include_examples 'no email deliveries'
end
- describe 'demo data translated in another language' do
+ describe 'demo data mock-translated in another language' do
before_all do
- RSpec::Mocks.with_temporary_scope do
- # the mocking of settings and configuration is duplicated here because
- # it's executed outside of an example context
- with_config(edition: 'bim')
- with_settings(journal_aggregation_time_minutes: 0)
-
+ with_edition('bim') do
# simulate a translation by changing the returned string on `I18n#t` calls
allow(I18n).to receive(:t).and_wrap_original do |m, *args, **kw|
original_translation = m.call(*args, **kw)
@@ -142,4 +133,19 @@ describe RootSeeder,
end
end
end
+
+ describe 'demo data with a non-English language' do
+ before_all do
+ with_edition('bim') do
+ stub_language('de')
+ described_class.new.seed_data!
+ end
+ end
+
+ before do
+ stub_language('de')
+ end
+
+ include_examples 'creates BIM demo data'
+ end
end
diff --git a/spec/seeders/root_seeder_standard_edition_spec.rb b/spec/seeders/root_seeder_standard_edition_spec.rb
index c423ba85bc1..fe212918ce2 100644
--- a/spec/seeders/root_seeder_standard_edition_spec.rb
+++ b/spec/seeders/root_seeder_standard_edition_spec.rb
@@ -31,8 +31,9 @@ require_relative './root_seeder_shared_examples'
describe RootSeeder,
'standard edition',
- with_config: { edition: 'standard' },
- with_settings: { journal_aggregation_time_minutes: 0 } do
+ with_config: { edition: 'standard' } do
+ include RootSeederTestHelpers
+
shared_examples 'creates standard demo data' do
it 'creates the system user' do
expect(SystemUser.where(admin: true).count).to eq 1
@@ -81,7 +82,9 @@ describe RootSeeder,
describe 'demo data' do
before_all do
- described_class.new.seed_data!
+ with_edition('standard') do
+ described_class.new.seed_data!
+ end
end
include_examples 'creates standard demo data'
@@ -112,9 +115,9 @@ describe RootSeeder,
end
end
- describe 'demo data translated in another language' do
+ describe 'demo data mock-translated in another language' do
before_all do
- RSpec::Mocks.with_temporary_scope do
+ with_edition('standard') do
# simulate a translation by changing the returned string on `I18n#t` calls
allow(I18n).to receive(:t).and_wrap_original do |m, *args, **kw|
original_translation = m.call(*args, **kw)
@@ -131,6 +134,21 @@ describe RootSeeder,
end
end
+ describe 'demo data with a non-English language' do
+ before_all do
+ with_edition('standard') do
+ stub_language('de')
+ described_class.new.seed_data!
+ end
+ end
+
+ before do
+ stub_language('de')
+ end
+
+ include_examples 'creates standard demo data'
+ end
+
describe 'demo data with development data' do
before_all do
described_class.new(seed_development_data: true).seed_data!
diff --git a/spec/support/root_seeder_test_helpers.rb b/spec/support/root_seeder_test_helpers.rb
new file mode 100644
index 00000000000..ddb96af7948
--- /dev/null
+++ b/spec/support/root_seeder_test_helpers.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+module RootSeederTestHelpers
+ def stub_language(locale)
+ locale = locale.to_s
+ with_settings(default_language: locale)
+ stub_const('ENV', { 'OPENPROJECT_SEED_LOCALE' => locale })
+ end
+
+ def with_edition(edition)
+ RSpec::Mocks.with_temporary_scope do
+ with_config(edition:)
+ yield
+ end
+ end
+end