require File.dirname(__FILE__) + '/../test_helper' require 'networks_controller' # Re-raise errors caught by the controller. class NetworksController; def rescue_action(e) raise e end; end class NetworksControllerTest < Test::Unit::TestCase fixtures :networks, :panels, :furnitures fixtures :objects_in_world, :objects_off_world fixtures :objects_in_world_panels, :objects_off_world_panels def setup @controller = NetworksController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @first_id = networks(:network_1).id end def test_index get :index assert_response :success assert_template 'list' end def test_list get :list assert_response :success assert_template 'list' assert_not_nil assigns(:networks) end def test_show get :show, :id => @first_id assert_response :success assert_template 'show' assert_not_nil assigns(:network) assert assigns(:network).valid? assert_not_nil assigns(:network_parameters) end def test_show_parameters_form get :show_parameters_form, :id => @first_id assert_response :success assert_template 'show_parameters_form' assert_not_nil assigns(:network) assert assigns(:network).valid? assert_not_nil assigns(:network_parameters) assert_not_nil assigns(:textures) assert_not_nil assigns(:landmarks) assert_not_nil assigns(:notecards) assert_not_nil assigns(:urls) assert_not_nil assigns(:mediaurls) network_parameters = assigns(:network_parameters) assert_equal 1, network_parameters.texture_id assert_equal 3, network_parameters.notecard_id assert_equal 5, network_parameters.landmark_id assert_equal 3, network_parameters.url_id assert_equal 5, network_parameters.mediaurl_id end def test_new get :new assert_response :success assert_template 'new' assert_not_nil assigns(:network) end def test_create num_networks = Network.count post :create, :network => { :name => "glop" } assert_response :redirect assert_redirected_to :action => 'show' assert_equal num_networks + 1, Network.count end def test_edit get :edit, :id => @first_id assert_response :success assert_template 'edit' assert_not_nil assigns(:network) assert assigns(:network).valid? end def test_update post :update, :id => @first_id assert_response :redirect assert_redirected_to :action => 'show', :id => @first_id end def test_destroy assert_nothing_raised { @network = Network.find(@first_id) } post :destroy, :id => @first_id assert_response :redirect assert_redirected_to :action => 'list' assert_equal "Network '#{@network.name}' deleted.", flash[:notice] assert_raise(ActiveRecord::RecordNotFound) { Network.find(@first_id) } end def test_set_parameters post :set_parameters, :id => 1, :network_parameters => { :texture_id => "2", :notecard_id => "", :landmark_id => "7", :url_id => "4", :mediaurl_id => "" } assert_response :redirect assert_redirected_to :action => 'show' assert_not_nil flash[:notice] panel1 = panels(:panel1_furniture1) assert_equal 2, panel1.objects_in_world.find_by_otype("texture").id assert_nil panel1.objects_in_world.find_by_otype("notecard") assert_equal 7, panel1.objects_in_world.find_by_otype("landmark").id assert_equal 4, panel1.objects_off_world.find_by_otype("url").id assert_nil panel1.objects_off_world.find_by_otype("mediaurl") end def test_remove_panel network = networks(:network_1) panel_id = panels(:panel1_furniture2) assert_equal 3, network.panels.count assert_nothing_raised { network.panels.find(panel_id) } post :remove_panel, :id => network.id, :panel_id => panel_id assert_response :redirect assert_redirected_to :action => 'show', :id => network.id assert_equal "Panel 'B1 : CC - 4x3 Casino' removed from network.", flash[:notice] assert_equal 2, network.panels.count assert_raise(ActiveRecord::RecordNotFound) { network.panels.find(panel_id) } end def test_remove_panel_invalid_panel_id network = networks(:network_1) panel_id = 42 assert_equal 3, network.panels.count assert_raise(ActiveRecord::RecordNotFound) { network.panels.find(panel_id) } post :remove_panel, :id => network.id, :panel_id => panel_id assert_response :redirect assert_redirected_to :action => 'list' assert_equal "Remove panel: Invalid network id ('#{network.id}') or panel id ('42').", flash[:notice] assert_equal 3, network.panels.count end def test_remove_panel_invalid_network_id network_id = 42 panel = panels(:panel1_furniture2) assert_raise(ActiveRecord::RecordNotFound) { Network.find(network_id) } post :remove_panel, :id => network_id, :panel_id => panel.id assert_response :redirect assert_redirected_to :action => 'list' assert_equal "Remove panel: Invalid network id ('#{network_id}') or panel id ('#{panel.id}').", flash[:notice] end def test_add_panel network = networks(:network_1) # Récupère un panneau existant du réseau first_panel = network.panels.first # Panneau à ajouter au réseau new_panel = panels(:panel2_furniture3) assert_equal 3, network.panels.count assert_nil new_panel.network assert_raise(ActiveRecord::RecordNotFound) { network.panels.find(new_panel.id) } post :add_panel, :id => network.id, :panel_id => new_panel.id assert_response :redirect assert_redirected_to :action => 'show', :id => network.id assert_equal "Panel 'B2 : CC - 4x3 Palace' added to network.", flash[:notice] new_panel.reload assert_equal 4, network.panels.count assert_nothing_raised { network.panels.find(new_panel.id) } assert_equal network, new_panel.network # Le panneau ajouté a-t-il bien pris les caractéristiques du réseau ? assert_equal first_panel.texture, new_panel.texture assert_equal first_panel.landmark, new_panel.landmark assert_equal first_panel.notecard, new_panel.notecard assert_equal first_panel.url, new_panel.url assert_equal first_panel.mediaurl, new_panel.mediaurl end def test_add_panel_invalid_panel_id network = networks(:network_1) panel_id = 42 assert_equal 3, network.panels.count assert_raise(ActiveRecord::RecordNotFound) { Panel.find(panel_id) } post :add_panel, :id => network.id, :panel_id => panel_id assert_response :redirect assert_redirected_to :action => 'list' assert_equal "Add panel: Invalid network id ('#{network.id}') or panel id ('#{panel_id}').", flash[:notice] assert_equal 3, network.panels.count end def test_add_panel_invalid_network_id network_id = 42 panel = panels(:panel1_furniture2) assert_raise(ActiveRecord::RecordNotFound) { Network.find(network_id) } post :add_panel, :id => network_id, :panel_id => panel.id assert_response :redirect assert_redirected_to :action => 'list' assert_equal "Add panel: Invalid network id ('#{network_id}') or panel id ('#{panel.id}').", flash[:notice] end def test_add_panel_already_used_elsewhere network1 = networks(:network_1) panel = panels(:panel2_furniture1) assert_equal 3, network1.panels.count assert_not_nil panel.network network2 = panel.network assert_raise(ActiveRecord::RecordNotFound) { network1.panels.find(panel.id) } post :add_panel, :id => network1.id, :panel_id => panel.id assert_response :redirect assert_redirected_to :action => 'show', :id => network1.id assert_equal "Panel 'B2 : Furniture 1' already belongs to a network ('#{network2.name}').", flash[:notice] assert_equal 3, network1.panels.count assert_raise(ActiveRecord::RecordNotFound) { network1.panels.find(panel.id) } panel.reload assert_equal network2, panel.network end end