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