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
replace legacy spec
ulferts7ea6a13ad1fd
diff --git a/lib/redmine/menu_manager/menu_helper.rb b/lib/redmine/menu_manager/menu_helper.rbindex 145cee57d2e..177cf6e71e8 100644--- a/lib/redmine/menu_manager/menu_helper.rb+++ b/lib/redmine/menu_manager/menu_helper.rb@@ -173,7 +173,7 @@ module Redmine::MenuManager::MenuHelperlink_text << (' '.html_safe + op_icon(item.icon_after)) if item.icon_after.present?html_options = item.html_options(selected:)html_options[:title] ||= selected ? t(:description_current_position) + caption : caption- html_options[:class] = "#{html_options[:class]} #{menu_class}--item-action"+ html_options[:class] = "#{html_options[:class]} #{menu_class}--item-action"html_options['data-qa-selector'] = "#{menu_class}--item-action"link_to link_text, url, html_optionsdiff --git a/spec/lib/redmine/menu_manager/menu_helper_spec.rb b/spec/lib/redmine/menu_manager/menu_helper_spec.rbnew file mode 100644index 00000000000..49dd6c7b515--- /dev/null+++ b/spec/lib/redmine/menu_manager/menu_helper_spec.rb@@ -0,0 +1,451 @@+# --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::MenuHelper, type: :helper do+ before do+ # Stub the current menu item in the controller+ def @controller.current_menu_item+ :index+ end+ end++ let(:allowed_urls) { [] }+ let(:allowed_projects) { [] }++ current_user do+ build_stubbed(:user).tap do |u|+ allow(u)+ .to receive(:allowed_to?) do |queried_url, queried_project|+ allowed_urls.include?(queried_url) &&+ allowed_projects.include?(queried_project)+ end+ end+ end++ describe '#render_single_menu_node' do+ let(:item) { Redmine::MenuManager::MenuItem.new(:testing, '/test', caption: 'This is a test') }+ let(:expected) do+ <<~HTML.squish+ <a class="testing-menu-item op-menu--item-action" title="This is a test" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ This is a test+ </span>+ </a>+ HTML+ end++ it 'renders' do+ expect(render_single_menu_node(item))+ .to be_html_eql(expected)+ end+ end++ describe '#render_menu_node' do+ context 'for a single item' do+ let(:item) { Redmine::MenuManager::MenuItem.new(:single_node, '/test', {}) }++ let(:expected) do+ <<~HTML.squish+ <li data-name="single_node">+ <a class="single-node-menu-item op-menu--item-action" title="Single node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Single node+ </span>+ </a>+ </li>+ HTML+ end++ it 'renders' do+ expect(render_menu_node(item, nil))+ .to be_html_eql(expected)+ end+ end++ context 'for a node with nested items' do+ let(:item) do+ node = Redmine::MenuManager::MenuItem.new(:parent_node, '/test', {})+ node << Redmine::MenuManager::MenuItem.new(:child_one_node, '/test', {})+ node << Redmine::MenuManager::MenuItem.new(:child_two_node, '/test', {})+ node << Redmine::MenuManager::MenuItem.new(:child_three_node, '/test', {})+ node << Redmine::MenuManager::MenuItem.new(:child_three_inner_node, '/test', {})++ node+ end++ let(:expected) do+ <<~HTML.squish+ <li data-name="parent_node">+ <a class="parent-node-menu-item op-menu--item-action" title="Parent node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Parent node+ </span>+ </a>+ <ul class="main-menu--children">+ <li data-name="child_one_node">+ <a class="child-one-node-menu-item op-menu--item-action" title="Child one node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Child one node+ </span>+ </a>+ </li>+ <li data-name="child_two_node">+ <a class="child-two-node-menu-item op-menu--item-action" title="Child two node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Child two node+ </span>+ </a>+ </li>+ <li data-name="child_three_node">+ <a class="child-three-node-menu-item op-menu--item-action" title="Child three node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Child three node+ </span>+ </a>+ </li>+ <li data-name="child_three_inner_node">+ <a class="child-three-inner-node-menu-item op-menu--item-action" title="Child three inner node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Child three inner node+ </span>+ </a>+ </li>+ </ul>+ </li>+ HTML+ end++ it 'renders' do+ expect(render_menu_node(item, nil))+ .to be_html_eql(expected)+ end+ end++ context 'for a node with children' do+ let(:project) { build_stubbed(:project) }++ let(:allowed_projects) { [project] }+ let(:allowed_urls) { ['/test'] }++ let(:item) do+ Redmine::MenuManager::MenuItem+ .new(:parent_node,+ allowed_urls[0],+ children: Proc.new do |_p|+ children = []+ 3.times do |time|+ children << Redmine::MenuManager::MenuItem+ .new("test_child_#{time}",+ allowed_urls[0],+ {})+ end+ children+ end)+ end++ let(:expected) do+ <<~HTML.squish+ <li data-name="parent_node">+ <a class="parent-node-menu-item op-menu--item-action" title="Parent node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Parent node+ </span>+ </a>++ <ul class="main-menu--children unattached">+ <li>+ <a class="test-child-0-menu-item" href="/test">+ Test child 0+ </a>+ </li>+ <li>+ <a class="test-child-1-menu-item" href="/test">+ Test child 1+ </a>+ </li>+ <li>+ <a class="test-child-2-menu-item" href="/test">+ Test child 2+ </a>+ </li>+ </ul>+ </li>+ HTML+ end++ it 'renders' do+ expect(render_menu_node(item, project))+ .to be_html_eql(expected)+ end+ end++ context 'for a node with nested items and children' do+ let(:project) { build_stubbed(:project) }++ let(:allowed_projects) { [project] }+ let(:allowed_urls) { ['/test'] }++ let(:item) do+ parent_node = Redmine::MenuManager::MenuItem+ .new(:parent_node,+ allowed_urls[0],+ children: Proc.new do |_p|+ children = []+ 3.times do |time|+ children << Redmine::MenuManager::MenuItem+ .new("test_child_#{time}",+ allowed_urls[0],+ {})+ end+ children+ end)++ parent_node << Redmine::MenuManager::MenuItem+ .new(:child_node,+ allowed_urls[0],+ children: Proc.new do |_p|+ children = []+ 6.times do |time|+ children << Redmine::MenuManager::MenuItem+ .new("test_dynamic_child_#{time}",+ allowed_urls[0],+ {})+ end+ children+ end)+ parent_node+ end++ let(:expected) do+ <<~HTML.squish+ <li data-name="parent_node">+ <a class="parent-node-menu-item op-menu--item-action" title="Parent node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Parent node+ </span>+ </a>++ <ul class="main-menu--children">+ <li data-name="child_node">+ <a class="child-node-menu-item op-menu--item-action" title="Child node" data-qa-selector="op-menu--item-action" href="/test">+ <span class="op-menu--item-title ">+ Child node+ </span>+ </a>+ <ul class="main-menu--children unattached">+ <li>+ <a class="test-dynamic-child-0-menu-item" href="/test">+ Test dynamic child 0+ </a>+ </li>+ <li>+ <a class="test-dynamic-child-1-menu-item" href="/test">+ Test dynamic child 1+ </a>+ </li>+ <li>+ <a class="test-dynamic-child-2-menu-item" href="/test">+ Test dynamic child 2+ </a>+ </li>+ <li>+ <a class="test-dynamic-child-3-menu-item" href="/test">+ Test dynamic child 3+ </a>+ </li>+ <li>+ <a class="test-dynamic-child-4-menu-item" href="/test">+ Test dynamic child 4+ </a>+ </li>+ <li>+ <a class="test-dynamic-child-5-menu-item" href="/test">+ Test dynamic child 5+ </a>+ </li>+ </ul>+ </li>+ </ul>++ <ul class="main-menu--children unattached">+ <li>+ <a class="test-child-0-menu-item" href="/test">+ Test child 0+ </a>+ </li>+ <li>+ <a class="test-child-1-menu-item" href="/test">+ Test child 1+ </a>+ </li>+ <li>+ <a class="test-child-2-menu-item" href="/test">+ Test child 2+ </a>+ </li>+ </ul>+ </li>+ HTML+ end++ it 'renders' do+ expect(render_menu_node(item, project))+ .to be_html_eql(expected)+ end+ end++ context 'for a node with children that is not an array' do+ let(:project) { build_stubbed(:project) }++ let(:allowed_projects) { [project] }+ let(:allowed_urls) { ['/test'] }++ let(:item) do+ Redmine::MenuManager::MenuItem+ .new(:parent_node,+ allowed_urls[0],+ children: Proc.new do |_p|+ Redmine::MenuManager::MenuItem.new('test_child',+ allowed_urls[0],+ {})+ end)+ end++ it 'causes an error' do+ expect { render_menu_node(item, project) }+ .to raise_error Redmine::MenuManager::MenuError+ end+ end+ end++ describe '#first_level_menu_items_for' do+ let(:project) { build_stubbed(:project) }+ let(:allowed_projects) { [project] }+ let(:allowed_urls) { ['/test'] }++ let(:root_node) do+ instance_double(Redmine::MenuManager::TreeNode).tap do |root_node|+ allow(root_node)+ .to receive(:root)+ .and_return(root_node)++ allow(root_node)+ .to receive(:children)+ .and_return(children)+ end+ end++ let(:children) do+ (0..2).map do |i|+ Redmine::MenuManager::MenuItem.new("test_child#{i}",+ allowed_urls[0],+ {})+ end+ end++ before do+ allow(Redmine::MenuManager)+ .to receive(:items)+ .with(:test_menu)+ .and_return(root_node)+ end++ context 'when passed a block' do+ it 'yields three times' do+ expect { |b| first_level_menu_items_for(:test_menu, project, &b) }+ .to yield_successive_args(*children)+ end+ end++ context 'without a block' do+ it 'returns the child items' do+ expect(first_level_menu_items_for(:test_menu, project))+ .to eq children+ end+ end++ context 'with a child not being allowed' do+ let(:children) do+ [Redmine::MenuManager::MenuItem.new("test_child1",+ allowed_urls[0],+ {}),+ Redmine::MenuManager::MenuItem.new("test_child2",+ "/not_allowed",+ {}),+ Redmine::MenuManager::MenuItem.new("test_child3",+ allowed_urls[0],+ {})]+ end++ it 'returns only the allowed child items' do+ expect(first_level_menu_items_for(:test_menu, project))+ .to eq(children.reject { |child| child == children[1] })+ end+ end++ context 'with a child having an if proc returning true' do+ let(:children) do+ [Redmine::MenuManager::MenuItem.new("test_child1",+ allowed_urls[0],+ {}),+ Redmine::MenuManager::MenuItem.new("test_child2",+ allowed_urls[0],+ if: Proc.new { true }),+ Redmine::MenuManager::MenuItem.new("test_child3",+ allowed_urls[0],+ {})]+ end++ it 'returns only the allowed child items' do+ expect(first_level_menu_items_for(:test_menu, project))+ .to eq children+ end+ end++ context 'with a child having an if proc returning false' do+ let(:children) do+ [Redmine::MenuManager::MenuItem.new("test_child1",+ allowed_urls[0],+ {}),+ Redmine::MenuManager::MenuItem.new("test_child2",+ allowed_urls[0],+ if: Proc.new { false }),+ Redmine::MenuManager::MenuItem.new("test_child3",+ allowed_urls[0],+ {})]+ end++ it 'returns only the allowed child items' do+ expect(first_level_menu_items_for(:test_menu, project))+ .to eq(children.reject { |child| child == children[1] })+ end+ end+ end+enddiff --git a/spec_legacy/unit/lib/redmine/menu_manager/menu_helper_spec.rb b/spec_legacy/unit/lib/redmine/menu_manager/menu_helper_spec.rbdeleted file mode 100644index 8efa9302db3..00000000000--- a/spec_legacy/unit/lib/redmine/menu_manager/menu_helper_spec.rb+++ /dev/null@@ -1,250 +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::MenuHelper, type: :helper do- include Redmine::MenuManager::MenuHelper- include ::Rails::Dom::Testing::Assertions::SelectorAssertions- fixtures :all-- # Used by assert_select- def html_document- Nokogiri::HTML(@response.body)- end-- before do- @response = ActionDispatch::TestResponse.new- # Stub the current menu item in the controller- def @controller.current_menu_item- :index- end- end-- it 'renders single menu node' do- node = Redmine::MenuManager::MenuItem.new(:testing, '/test', caption: 'This is a test')- @response.body = render_single_menu_node(node)-- html_node = Nokogiri::HTML(@response.body)- assert_select(html_node.root, 'a.testing-menu-item', 'This is a test')- end-- it 'renders menu node' do- single_node = Redmine::MenuManager::MenuItem.new(:single_node, '/test', {})- @response.body = render_menu_node(single_node, nil)-- html_node = Nokogiri::HTML(@response.body)- assert_select(html_node.root, 'li') do- assert_select('a.single-node-menu-item', 'Single node')- end- end-- it 'renders menu node with nested items' do- parent_node = Redmine::MenuManager::MenuItem.new(:parent_node, '/test', {})- parent_node << Redmine::MenuManager::MenuItem.new(:child_one_node, '/test', {})- parent_node << Redmine::MenuManager::MenuItem.new(:child_two_node, '/test', {})- parent_node <<- Redmine::MenuManager::MenuItem.new(:child_three_node, '/test', {}) <<- Redmine::MenuManager::MenuItem.new(:child_three_inner_node, '/test', {})-- @response.body = render_menu_node(parent_node, nil)-- html_node = Nokogiri::HTML(@response.body)- assert_select(html_node.root, 'li') do- assert_select('a.parent-node-menu-item', 'Parent node')- assert_select('ul') do- assert_select('li a.child-one-node-menu-item', 'Child one node')- assert_select('li a.child-two-node-menu-item', 'Child two node')- assert_select('li') do- assert_select('a.child-three-node-menu-item', 'Child three node')- assert_select('ul') do- assert_select('li a.child-three-inner-node-menu-item', 'Child three inner node')- end- end- end- end- end-- it 'renders menu node with children' do- User.current = User.find(1)-- parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,- { controller: 'work_packages', action: 'index' },- children: Proc.new do |_p|- children = []- 3.times do |time|- children << Redmine::MenuManager::MenuItem.new("test_child_#{time}",- {- controller: 'work_packages', action: 'index'- },- {})- end- children- end)- @response.body = render_menu_node(parent_node, Project.find(1))-- html_node = Nokogiri::HTML(@response.body)- assert_select(html_node.root, 'li') do- assert_select('a.parent-node-menu-item', 'Parent node')- assert_select('ul') do- assert_select('li a.test-child-0-menu-item', 'Test child 0')- assert_select('li a.test-child-1-menu-item', 'Test child 1')- assert_select('li a.test-child-2-menu-item', 'Test child 2')- end- end- end-- it 'renders menu node with nested items and children' do- User.current = User.find(1)-- parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,- { controller: 'work_packages', action: 'index' },- children: Proc.new do |_p|- children = []- 3.times do |time|- children << Redmine::MenuManager::MenuItem.new("test_child_#{time}",- { controller: 'work_packages', action: 'index' }, {})- end- children- end)-- parent_node << Redmine::MenuManager::MenuItem.new(:child_node,- { controller: 'work_packages', action: 'index' },- children: Proc.new do |_p|- children = []- 6.times do |time|- children << Redmine::MenuManager::MenuItem.new(- "test_dynamic_child_#{time}", { controller: 'work_packages',- action: 'index' }, {}- )- end- children- end)-- @response.body = render_menu_node(parent_node, Project.find(1))-- html_node = Nokogiri::HTML(@response.body)- assert_select(html_node.root, 'li') do- assert_select('a.parent-node-menu-item', 'Parent node')- assert_select('ul') do- assert_select('li a.child-node-menu-item', 'Child node')- assert_select('ul') do- assert_select('li a.test-dynamic-child-0-menu-item', 'Test dynamic child 0')- assert_select('li a.test-dynamic-child-1-menu-item', 'Test dynamic child 1')- assert_select('li a.test-dynamic-child-2-menu-item', 'Test dynamic child 2')- assert_select('li a.test-dynamic-child-3-menu-item', 'Test dynamic child 3')- assert_select('li a.test-dynamic-child-4-menu-item', 'Test dynamic child 4')- assert_select('li a.test-dynamic-child-5-menu-item', 'Test dynamic child 5')- end- assert_select('li a.test-child-0-menu-item', 'Test child 0')- assert_select('li a.test-child-1-menu-item', 'Test child 1')- assert_select('li a.test-child-2-menu-item', 'Test child 2')- end- end- end-- it 'renders menu node with children without an array' do- parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,- { controller: 'work_packages', action: 'index' },- children: Proc.new do |_p|- Redmine::MenuManager::MenuItem.new('test_child',- { controller: 'work_packages', action: 'index' }, {})- end)-- assert_raises Redmine::MenuManager::MenuError, ':children must be an array of MenuItems' do- @response.body = render_menu_node(parent_node, Project.find(1))- end- end-- it 'renders menu node with incorrect children' do- parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,- { controller: 'work_packages', action: 'index' },- children: Proc.new { |_p| ['a string'] })-- assert_raises Redmine::MenuManager::MenuError, ':children must be an array of MenuItems' do- @response.body = render_menu_node(parent_node, Project.find(1))- end- end-- it 'firsts level menu items for should yield all items if passed a block' do- menu_name = :test_first_level_menu_items_for_should_yield_all_items_if_passed_a_block- Redmine::MenuManager.map menu_name do |menu|- menu.push(:a_menu, '/', {})- menu.push(:a_menu_2, '/', {})- menu.push(:a_menu_3, '/', {})- end-- items_yielded = []- first_level_menu_items_for(menu_name) do |item|- items_yielded << item- end-- assert_equal 3, items_yielded.size- end-- it 'firsts level menu items for should return all items' do- menu_name = :test_first_level_menu_items_for_should_return_all_items- Redmine::MenuManager.map menu_name do |menu|- menu.push(:a_menu, '/', {})- menu.push(:a_menu_2, '/', {})- menu.push(:a_menu_3, '/', {})- end-- items = first_level_menu_items_for(menu_name)- assert_equal 3, items.size- end-- it 'firsts level menu items for should skip unallowed items on a project' do- menu_name = :test_first_level_menu_items_for_should_skip_unallowed_items_on_a_project- Redmine::MenuManager.map menu_name do |menu|- menu.push(:a_menu, { controller: 'work_packages', action: 'index' }, {})- menu.push(:a_menu_2, { controller: 'work_packages', action: 'index' }, {})- menu.push(:unallowed, { controller: 'work_packages', action: 'unallowed' }, {})- end-- User.current = User.find(1)-- items = first_level_menu_items_for(menu_name, Project.find(1))- assert_equal 2, items.size- end-- it 'firsts level menu items for should skip items that fail the conditions' do- menu_name = :test_first_level_menu_items_for_should_skip_items_that_fail_the_conditions- Redmine::MenuManager.map menu_name do |menu|- menu.push(:a_menu, { controller: 'work_packages', action: 'index' }, {})- menu.push(:unallowed,- { controller: 'work_packages', action: 'index' },- if: Proc.new { false })- end-- User.current = User.find(1)-- items = first_level_menu_items_for(menu_name, Project.find(1))- assert_equal 1, items.size- end-end