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

replace legacy spec

ulfertse71b9284900e
diff --git a/spec/lib/redmine/menu_manager/mapper_spec.rb b/spec/lib/redmine/menu_manager/mapper_spec.rb
new file mode 100644
index 00000000000..abbfe2c4b6e
--- /dev/null
+++ b/spec/lib/redmine/menu_manager/mapper_spec.rb
@@ -0,0 +1,198 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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'
+
+describe Redmine::MenuManager::Mapper do
+ let(:mapper) { described_class.new(:test_menu, {}) }
+
+ shared_context 'with a single node pushed to root' do
+ before do
+ mapper.push :test_item, { controller: 'foo', action: 'bar' }, {}
+ end
+ end
+
+ shared_context 'with a node with a child' do
+ before do
+ mapper.push :test_parent_item, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_child_item, { controller: 'foo', action: 'bar' }, parent: :test_parent_item
+ end
+ end
+
+ describe '#exists?' do
+ include_context 'with a single node pushed to root' do
+ it 'the pushed node does exist' do
+ expect(mapper)
+ .to exist(:test_item)
+ end
+
+ it 'a different node does not exist' do
+ expect(mapper)
+ .not_to exist(:not_pushed)
+ end
+ end
+
+ include_context 'with a node with a child' do
+ it 'the pushed node does exist' do
+ expect(mapper)
+ .to exist(:test_child_item)
+ end
+ end
+ end
+
+ describe '#find' do
+ include_context 'with a single node pushed to root' do
+ it 'finds the pushed node' do
+ expect(mapper.find(:test_item))
+ .to be_a(Redmine::MenuManager::MenuItem)
+ end
+
+ it 'does not find a non pushed node' do
+ expect(mapper.find(:not_pushed))
+ .to be_nil
+ end
+
+ it 'does not find a deleted node' do
+ mapper.delete(:test_item)
+
+ expect(mapper.find(:test_item))
+ .to be_nil
+ end
+ end
+
+ include_context 'with a node with a child' do
+ it 'finds the pushed child node' do
+ expect(mapper.find(:test_child_item))
+ .to be_a(Redmine::MenuManager::MenuItem)
+ end
+ end
+ end
+
+ describe '#delete' do
+ include_context 'with a single node pushed to root' do
+ it 'returns the deleted node' do
+ expect(mapper.delete(:test_item))
+ .to be_a(Redmine::MenuManager::MenuItem)
+ end
+
+ it 'does not bail on a non existing node' do
+ expect { mapper.delete(:not_pushed) }
+ .not_to raise_error
+ end
+
+ it 'returns nil for a non existing node' do
+ expect(mapper.delete(:not_pushed))
+ .to be_nil
+ end
+ end
+ end
+
+ describe '#children order of the pushed elements' do
+ shared_examples_for 'items in the expected order' do
+ it 'has the items in the expected order' do
+ expect(mapper.find(:root).children.map(&:name))
+ .to eq %i[
+ test_first
+ test_second
+ test_third
+ test_fourth
+ test_fifth
+ ]
+ end
+ end
+
+ context 'for items without any special order options' do
+ before do
+ mapper.push :test_first, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_second, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_third, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fourth, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fifth, { controller: 'foo', action: 'bar' }, {}
+ end
+
+ it_behaves_like 'items in the expected order'
+ end
+
+ context 'for items with a first options' do
+ before do
+ mapper.push :test_second, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_third, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fourth, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fifth, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_first, { controller: 'foo', action: 'bar' }, { first: true }
+ end
+
+ it_behaves_like 'items in the expected order'
+ end
+
+ context 'for items with a last options' do
+ before do
+ mapper.push :test_first, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fifth, { controller: 'foo', action: 'bar' }, { last: true }
+ mapper.push :test_second, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_third, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fourth, { controller: 'foo', action: 'bar' }, {}
+ end
+
+ it_behaves_like 'items in the expected order'
+ end
+
+ context 'for items with a before options' do
+ before do
+ mapper.push :test_first, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_second, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fourth, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fifth, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_third, { controller: 'foo', action: 'bar' }, { before: :test_fourth }
+ end
+
+ it_behaves_like 'items in the expected order'
+ end
+
+ context 'for items with a after options' do
+ before do
+ mapper.push :test_first, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_second, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_third, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fifth, { controller: 'foo', action: 'bar' }, {}
+ mapper.push :test_fourth, { controller: 'foo', action: 'bar' }, { after: :test_third }
+ end
+
+ it_behaves_like 'items in the expected order'
+ end
+ end
+
+ describe '#url of the pushed element' do
+ include_context 'with a single node pushed to root' do
+ it 'is the pushed controller/action pair' do
+ expect(mapper.find(:test_item).url)
+ .to eq(controller: 'foo', action: 'bar')
+ end
+ end
+ end
+end
diff --git a/spec_legacy/unit/lib/redmine/menu_manager/mapper_spec.rb b/spec_legacy/unit/lib/redmine/menu_manager/mapper_spec.rb
deleted file mode 100644
index eeef73fb932..00000000000
--- a/spec_legacy/unit/lib/redmine/menu_manager/mapper_spec.rb
+++ /dev/null
@@ -1,184 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 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_relative '../../../../legacy_spec_helper'
-
-describe Redmine::MenuManager::Mapper do
- it 'pushes onto root' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
-
- menu_mapper.exists?(:test_overview)
- end
-
- it 'pushes onto parent' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_child, { controller: 'projects', action: 'show' }, parent: :test_overview
-
- assert menu_mapper.exists?(:test_child)
- assert_equal :test_child, menu_mapper.find(:test_child).name
- end
-
- it 'pushes onto grandparent' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_child, { controller: 'projects', action: 'show' }, parent: :test_overview
- menu_mapper.push :test_grandchild, { controller: 'projects', action: 'show' }, parent: :test_child
-
- assert menu_mapper.exists?(:test_grandchild)
- grandchild = menu_mapper.find(:test_grandchild)
- assert_equal :test_grandchild, grandchild.name
- assert_equal :test_child, grandchild.parent.name
- end
-
- it 'pushes first' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_second, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_third, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fourth, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fifth, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_first, { controller: 'projects', action: 'show' }, first: true
-
- root = menu_mapper.find(:root)
- assert_equal 5, root.children.size
- { 0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth }.each do |position, name|
- refute_nil root.children[position]
- assert_equal name, root.children[position].name
- end
- end
-
- it 'pushes before' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_first, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_second, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fourth, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fifth, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_third, { controller: 'projects', action: 'show' }, before: :test_fourth
-
- root = menu_mapper.find(:root)
- assert_equal 5, root.children.size
- { 0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth }.each do |position, name|
- refute_nil root.children[position]
- assert_equal name, root.children[position].name
- end
- end
-
- it 'pushes after' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_first, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_second, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_third, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fifth, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fourth, { controller: 'projects', action: 'show' }, after: :test_third
-
- root = menu_mapper.find(:root)
- assert_equal 5, root.children.size
- { 0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth }.each do |position, name|
- refute_nil root.children[position]
- assert_equal name, root.children[position].name
- end
- end
-
- it 'pushes last' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_first, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_second, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_third, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_fifth, { controller: 'projects', action: 'show' }, last: true
- menu_mapper.push :test_fourth, { controller: 'projects', action: 'show' }, {}
-
- root = menu_mapper.find(:root)
- assert_equal 5, root.children.size
- { 0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth }.each do |position, name|
- refute_nil root.children[position]
- assert_equal name, root.children[position].name
- end
- end
-
- it 'existses for child node' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
- menu_mapper.push :test_child, { controller: 'projects', action: 'show' }, parent: :test_overview
-
- assert menu_mapper.exists?(:test_child)
- end
-
- it 'existses for invalid node' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
-
- assert !menu_mapper.exists?(:nothing)
- end
-
- it 'finds' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
-
- item = menu_mapper.find(:test_overview)
- assert_equal :test_overview, item.name
- assert_equal({ controller: 'projects', action: 'show' }, item.url)
- end
-
- it 'finds missing' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
-
- item = menu_mapper.find(:nothing)
- assert_equal nil, item
- end
-
- it 'deletes' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- menu_mapper.push :test_overview, { controller: 'projects', action: 'show' }, {}
- refute_nil menu_mapper.delete(:test_overview)
-
- assert_nil menu_mapper.find(:test_overview)
- end
-
- it 'deletes missing' do
- menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {})
- assert_nil menu_mapper.delete(:test_missing)
- end
-
- specify 'deleting all items' do
- # Exposed by deleting :last items
- Redmine::MenuManager.map :test_menu do |menu|
- menu.push :not_last, OpenProject::Static::Links.help_link
- menu.push :administration, { controller: 'projects', action: 'show' }, last: true
- menu.push :help, OpenProject::Static::Links.help_link, last: true
- end
-
- expect do
- Redmine::MenuManager.map :test_menu do |menu|
- menu.delete(:administration)
- menu.delete(:help)
- menu.push :test_overview, { controller: 'projects', action: 'show' }, {}
- end
- end.not_to raise_error
- end
-end