diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb index 674e0f6..1f66c67 100644 --- a/app/controllers/guidances_controller.rb +++ b/app/controllers/guidances_controller.rb @@ -13,7 +13,7 @@ ## # GET /guidances/1 def admin_show - @guidance = Guidance.includes(:guidance_group, :question, :themes).find(params[:id]) + @guidance = Guidance.includes(:guidance_group, :themes).find(params[:id]) authorize @guidance end @@ -26,7 +26,7 @@ @templates = (Org.funders.collect{|o| o.templates } + current_user.org.templates).flatten @phases = nil - @templates.includes(:phases).each do |template| + @templates.each do |template| if @phases.nil? then @phases = template.phases.all.order('number') else @@ -53,7 +53,9 @@ @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') end +# TODO: These no longer appear to be in use #setup variables for use in the dynamic updating +=begin def update_phases authorize Guidance # updates phases, versions, sections and questions based on template selected @@ -90,7 +92,8 @@ section = Section.find(params[:section_id]) @questions = section.questions.map{|s| [s.text, s.id]}.insert(0, _('Select a question')) end - +=end + ## # GET /guidances/1/edit def admin_edit @@ -103,12 +106,12 @@ ## # POST /guidances def admin_create - @guidance = Guidance.new(params[:guidance]) + @guidance = Guidance.new(guidance_params) authorize @guidance @guidance.text = params["guidance-text"] @guidance.question_id = params["question_id"] if @guidance.published == true then - @gg = GuidanceGroup.find(@guidance.guidance_group_ids).first + @gg = GuidanceGroup.find(@guidance.guidance_group_id) if @gg.published == false || @gg.published.nil? then @gg.published = true @gg.save @@ -118,7 +121,7 @@ if @guidance.save redirect_to admin_show_guidance_path(@guidance), notice: _('Guidance was successfully created.') else - render action: "new" + redirect_to admin_new_guidance_path(current_user.org.id), notice: generate_error_notice(@guidance) end end @@ -130,10 +133,10 @@ @guidance.text = params["guidance-text"] @guidance.question_id = params["question_id"] - if @guidance.update_attributes(params[:guidance]) + if @guidance.save(guidance_params) redirect_to admin_show_guidance_path(params[:guidance]), notice: _('Guidance was successfully updated.') else - render action: "edit" + redirect_to admin_edit_guidance_path(@guidance), notice: generate_error_notice(@guidance) end end @@ -144,7 +147,12 @@ authorize @guidance @guidance.destroy - redirect_to admin_index_guidance_path + redirect_to admin_index_guidance_path, notice: _('Guidance was successfully deleted.') end + + private + def guidance_params + params.require(:guidance).permit(:text, :published, :guidance_group_id, :question_id) + end end \ No newline at end of file diff --git a/app/models/guidance.rb b/app/models/guidance.rb index 26a83ff..a45c2f2 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -13,10 +13,10 @@ ## # Associations belongs_to :guidance_group -# belongs_to :question + belongs_to :question has_and_belongs_to_many :themes, join_table: "themes_in_guidance" # depricated, but required for migration "single_group_for_guidance" - has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" + #has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index e1e5fcc..ce4ecdb 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -41,6 +41,9 @@ test "a user's org language specification gets picked up and used if the user has no language setting" do if LANGUAGES.count > 1 @user.language = nil + +puts "LANGUAGES: #{LANGUAGES.inspect}" + @user.org[:language_id] = LANGUAGES.last.id @user.save! diff --git a/test/functional/guidances_controller_test.rb b/test/functional/guidances_controller_test.rb new file mode 100644 index 0000000..24979f9 --- /dev/null +++ b/test/functional/guidances_controller_test.rb @@ -0,0 +1,170 @@ +require 'test_helper' + +class GuidancesControllerTest < ActionDispatch::IntegrationTest + + include Devise::Test::IntegrationHelpers + + # TODO: The following methods SHOULD replace the old 'admin_' prefixed methods. The routes file already has + # these defined. They are defined multiple times though and we need to clean this up: + # + # SHOULD BE: + # -------------------------------------------------- + # guidances GET /guidances guidances#index + # POST /guidances guidances#create + # guidance GET /guidance/:id guidances#show + # PATCH /guidance/:id guidances#update + # PUT /guidance/:id guidances#update + # DELETE /guidance/:id guidances#destroy + # + # CURRENT RESULTS OF `rake routes` + # -------------------------------------------------- + # admin_show_guidance GET /org/admin/guidance/:id/admin_show guidances#admin_show + # admin_index_guidance GET /org/admin/guidance/:id/admin_index guidances#admin_index + # admin_edit_guidance GET /org/admin/guidance/:id/admin_edit guidances#admin_edit + # admin_new_guidance GET /org/admin/guidance/:id/admin_new guidances#admin_new + # admin_destroy_guidance DELETE /org/admin/guidance/:id/admin_destroy guidances#admin_destroy + # admin_create_guidance POST /org/admin/guidance/:id/admin_create guidances#admin_create + # admin_update_guidance PUT /org/admin/guidance/:id/admin_update guidances#admin_update + # update_phases_guidance GET /org/admin/guidance/:id/update_phases guidances#update_phases + # update_versions_guidance GET /org/admin/guidance/:id/update_versions guidances#update_versions + # update_sections_guidance GET /org/admin/guidance/:id/update_sections guidances#update_sections + # update_questions_guidance GET /org/admin/guidance/:id/update_questions guidances#update_questions + + setup do + scaffold_org_admin(GuidanceGroup.first.org) + @guidance_group = GuidanceGroup.first + end + + # GET /org/admin/guidance/:id/admin_index (admin_index_guidance_path) + # ---------------------------------------------------------- + test 'load the list of guidances page' do + # Should redirect user to the root path if they are not logged in! + get admin_index_guidance_path(@guidance_group) + assert_unauthorized_redirect_to_root_path + + sign_in @user + + get admin_index_guidance_path(@guidance_group) + assert_response :success + assert assigns(:guidances) + assert assigns(:guidance_groups) + end + + # GET /org/admin/guidance/:id/admin_show (admin_show_guidance_path) + # ---------------------------------------------------------- + test 'load the guidance page' do + # Should redirect user to the root path if they are not logged in! + # TODO: Why is there an id here!? its a new guidance_group! + get admin_show_guidance_path(guidance_group_id: @guidance_group.id, id: @guidance_group.guidances.first.id) + assert_unauthorized_redirect_to_root_path + + sign_in @user + + get admin_show_guidance_path(guidance_group_id: @guidance_group.id, id: @guidance_group.guidances.first.id) + assert_response :success + assert assigns(:guidance) + end + + # /org/admin/guidance/:id/admin_new (admin_new_guidance_path) + # ---------------------------------------------------------- + test 'load the new guidance page' do + # Should redirect user to the root path if they are not logged in! + get admin_new_guidance_path(@guidance_group) + assert_unauthorized_redirect_to_root_path + + sign_in @user + + get admin_new_guidance_path(@guidance_group) + assert_response :success + assert assigns(:templates) + assert assigns(:phases) + assert assigns(:sections) + assert assigns(:questions) + assert assigns(:guidance) + assert assigns(:guidance_groups) + assert assigns(:themes) + end + + # /org/admin/guidance/:id/admin_edit (admin_edit_guidance_path) + # ---------------------------------------------------------- + test 'load the edit guidance page' do + # Should redirect user to the root path if they are not logged in! + get admin_edit_guidance_path(@guidance_group) + assert_unauthorized_redirect_to_root_path + + sign_in @user + + get admin_edit_guidance_path(@guidance_group) + assert_response :success + assert assigns(:guidance) + assert assigns(:guidance_groups) + assert assigns(:themes) + end + + # POST /org/admin/guidance/:id/admin_create (admin_create_guidance_path) + # ---------------------------------------------------------- + test 'create a new guidance' do + params = {'guidance-text': 'Testing UPDATE', guidance: {guidance_group_id: GuidanceGroup.first.id, published: true}} + + # Should redirect user to the root path if they are not logged in! + post admin_create_guidance_path(@user.org), params + assert_unauthorized_redirect_to_root_path + + sign_in @user + + post admin_create_guidance_path(@user.org), params + assert_response :redirect + assert_redirected_to admin_show_guidance_path(Guidance.last) + assert_equal _('Guidance was successfully created.'), flash[:notice] + assert assigns(:guidance) + + # Invalid object + post admin_create_guidance_path(@user.org), {'guidance-text': nil, guidance: {published: false}} + assert_response :redirect + assert_redirected_to admin_new_guidance_path(@user.org) + assert assigns(:guidance) + assert flash[:notice].starts_with?(_('Unable to save your changes.')) + end + + # PUT /org/admin/guidance/:id/admin_update (admin_update_guidance_path) + # ---------------------------------------------------------- + test 'update the guidance' do + params = {'guidance-text': 'Testing UPDATE', guidance: {guidance_group_id: GuidanceGroup.first.id}} + + # Should redirect user to the root path if they are not logged in! + put admin_update_guidance_path(Guidance.first), params + assert_unauthorized_redirect_to_root_path + + sign_in @user + + put admin_update_guidance_path(Guidance.first), params + assert_response :redirect + assert_equal _('Guidance was successfully updated.'), flash[:notice] + assert_redirected_to "#{admin_show_guidance_path(Guidance.first)}?guidance_group_id=#{GuidanceGroup.first.id}" + assert assigns(:guidance) + + # Invalid object + put admin_update_guidance_path(Guidance.first), {'guidance-text': nil, guidance: {guidance_group_id: GuidanceGroup.first.id}} + assert_response :redirect + assert_redirected_to admin_edit_guidance_path(Guidance.first) + assert assigns(:guidance) + assert flash[:notice].starts_with?(_('Unable to save your changes.')) + end + + # DELETE /org/admin/guidance/:id/admin_destroy (admin_destroy_guidance_path) + # ---------------------------------------------------------- + test 'delete the guidance' do + # Should redirect user to the root path if they are not logged in! + delete admin_destroy_guidance_path(Guidance.first) + assert_unauthorized_redirect_to_root_path + + sign_in @user + + delete admin_destroy_guidance_path(Guidance.first) + assert_response :redirect + assert_redirected_to admin_index_guidance_path + assert_equal _('Guidance was successfully deleted.'), flash[:notice] + assert assigns(:guidance) + end + +end \ No newline at end of file diff --git a/test/functional/orgs_controller_test.rb b/test/functional/orgs_controller_test.rb new file mode 100644 index 0000000..40b5086 --- /dev/null +++ b/test/functional/orgs_controller_test.rb @@ -0,0 +1,51 @@ +require 'test_helper' + +class OrgsControllerTest < ActionController::TestCase +=begin + setup do + @organisation = organisations(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:organisations) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create organisation" do + assert_difference('Org.count') do + post :create, organisation: { abbreviation: @organisation.abbreviation, banner_file_id: @organisation.banner_file_id, description: @organisation.description, domain: @organisation.domain, logo_file_id: @organisation.logo_file_id, name: @organisation.name, stylesheet_file_id: @organisation.stylesheet_file_id, target_url: @organisation.target_url, type_id: @organisation.type_id, wayfless_entite: @organisation.wayfless_entite } + end + + assert_redirected_to organisation_path(assigns(:organisation)) + end + + test "should show organisation" do + get :show, id: @organisation + assert_response :success + end + + test "should get edit" do + get :edit, id: @organisation + assert_response :success + end + + test "should update organisation" do + put :update, id: @organisation, organisation: { abbreviation: @organisation.abbreviation, banner_file_id: @organisation.banner_file_id, description: @organisation.description, domain: @organisation.domain, logo_file_id: @organisation.logo_file_id, name: @organisation.name, stylesheet_file_id: @organisation.stylesheet_file_id, target_url: @organisation.target_url, type_id: @organisation.type_id, wayfless_entite: @organisation.wayfless_entite } + assert_redirected_to organisation_path(assigns(:organisation)) + end + + test "should destroy organisation" do + assert_difference('Org.count', -1) do + delete :destroy, id: @organisation + end + + assert_redirected_to organisations_path + end +=end +end