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