Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require File.expand_path('../../../../../test_helper', __FILE__) Chris@1494: Chris@1494: class Redmine::MenuManager::MapperTest < ActiveSupport::TestCase Chris@1494: test "Mapper#initialize should define a root MenuNode if menu is not present in items" do Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: node = menu_mapper.menu_items Chris@1494: assert_not_nil node Chris@1494: assert_equal :root, node.name Chris@1494: end Chris@1494: Chris@1494: test "Mapper#initialize should use existing MenuNode if present" do Chris@1494: node = "foo" # just an arbitrary reference Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {:test_menu => node}) Chris@1494: assert_equal node, menu_mapper.menu_items Chris@1494: end Chris@1494: Chris@1494: def test_push_onto_root Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: Chris@1494: menu_mapper.exists?(:test_overview) Chris@1494: end Chris@1494: Chris@1494: def test_push_onto_parent Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview} Chris@1494: Chris@1494: assert menu_mapper.exists?(:test_child) Chris@1494: assert_equal :test_child, menu_mapper.find(:test_child).name Chris@1494: end Chris@1494: Chris@1494: def test_push_onto_grandparent Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview} Chris@1494: menu_mapper.push :test_grandchild, { :controller => 'projects', :action => 'show'}, {:parent => :test_child} Chris@1494: Chris@1494: assert menu_mapper.exists?(:test_grandchild) Chris@1494: grandchild = menu_mapper.find(:test_grandchild) Chris@1494: assert_equal :test_grandchild, grandchild.name Chris@1494: assert_equal :test_child, grandchild.parent.name Chris@1494: end Chris@1494: Chris@1494: def test_push_first Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {:first => true} Chris@1494: Chris@1494: root = menu_mapper.find(:root) Chris@1494: assert_equal 5, root.children.size Chris@1494: {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| Chris@1494: assert_not_nil root.children[position] Chris@1494: assert_equal name, root.children[position].name Chris@1494: end Chris@1494: Chris@1494: end Chris@1494: Chris@1494: def test_push_before Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {:before => :test_fourth} Chris@1494: Chris@1494: root = menu_mapper.find(:root) Chris@1494: assert_equal 5, root.children.size Chris@1494: {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| Chris@1494: assert_not_nil root.children[position] Chris@1494: assert_equal name, root.children[position].name Chris@1494: end Chris@1494: Chris@1494: end Chris@1494: Chris@1494: def test_push_after Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {:after => :test_third} Chris@1494: Chris@1494: root = menu_mapper.find(:root) Chris@1494: assert_equal 5, root.children.size Chris@1494: {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| Chris@1494: assert_not_nil root.children[position] Chris@1494: assert_equal name, root.children[position].name Chris@1494: end Chris@1494: Chris@1494: end Chris@1494: Chris@1494: def test_push_last Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_first, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_second, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_third, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_fifth, { :controller => 'projects', :action => 'show'}, {:last => true} Chris@1494: menu_mapper.push :test_fourth, { :controller => 'projects', :action => 'show'}, {} Chris@1494: Chris@1494: root = menu_mapper.find(:root) Chris@1494: assert_equal 5, root.children.size Chris@1494: {0 => :test_first, 1 => :test_second, 2 => :test_third, 3 => :test_fourth, 4 => :test_fifth}.each do |position, name| Chris@1494: assert_not_nil root.children[position] Chris@1494: assert_equal name, root.children[position].name Chris@1494: end Chris@1494: Chris@1494: end Chris@1494: Chris@1494: def test_exists_for_child_node Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: menu_mapper.push :test_child, { :controller => 'projects', :action => 'show'}, {:parent => :test_overview } Chris@1494: Chris@1494: assert menu_mapper.exists?(:test_child) Chris@1494: end Chris@1494: Chris@1494: def test_exists_for_invalid_node Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: Chris@1494: assert !menu_mapper.exists?(:nothing) Chris@1494: end Chris@1494: Chris@1494: def test_find Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: Chris@1494: item = menu_mapper.find(:test_overview) Chris@1494: assert_equal :test_overview, item.name Chris@1494: assert_equal({:controller => 'projects', :action => 'show'}, item.url) Chris@1494: end Chris@1494: Chris@1494: def test_find_missing Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: Chris@1494: item = menu_mapper.find(:nothing) Chris@1494: assert_equal nil, item Chris@1494: end Chris@1494: Chris@1494: def test_delete Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: menu_mapper.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: assert_not_nil menu_mapper.delete(:test_overview) Chris@1494: Chris@1494: assert_nil menu_mapper.find(:test_overview) Chris@1494: end Chris@1494: Chris@1494: def test_delete_missing Chris@1494: menu_mapper = Redmine::MenuManager::Mapper.new(:test_menu, {}) Chris@1494: assert_nil menu_mapper.delete(:test_missing) Chris@1494: end Chris@1494: Chris@1494: test 'deleting all items' do Chris@1494: # Exposed by deleting :last items Chris@1494: Redmine::MenuManager.map :test_menu do |menu| Chris@1494: menu.push :not_last, Redmine::Info.help_url Chris@1494: menu.push :administration, { :controller => 'projects', :action => 'show'}, {:last => true} Chris@1494: menu.push :help, Redmine::Info.help_url, :last => true Chris@1494: end Chris@1494: Chris@1494: assert_nothing_raised do Chris@1494: Redmine::MenuManager.map :test_menu do |menu| Chris@1494: menu.delete(:administration) Chris@1494: menu.delete(:help) Chris@1494: menu.push :test_overview, { :controller => 'projects', :action => 'show'}, {} Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end