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