diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb
index 9d79400..391120b 100644
--- a/app/controllers/phases_controller.rb
+++ b/app/controllers/phases_controller.rb
@@ -4,8 +4,8 @@
after_action :verify_authorized
- # GET /plans/:plan_id/phases/:id/edit
- def edit
+ # GET /plans/:plan_id/phases/:id/edit
+ def edit
@plan = Plan.eager_load2(params[:plan_id])
authorize @plan
@@ -43,7 +43,7 @@
# create a map from theme to array of guidances
# where guidance is a hash with the text and the org name
- theme_guidance = {}
+ theme_guidance = {}
guidance_groups.each do |guidance_group|
guidance_group.guidances.each do |guidance|
@@ -83,14 +83,14 @@
if !user_signed_in? then
respond_to do |format|
- format.html { redirect_to edit_user_registration_path }
- end
- end
+ format.html { redirect_to edit_user_registration_path }
+ end
+ end
- end
+ end
- # GET /plans/PLANID/phases/PHASEID/status.json
+ # GET /plans/PLANID/phases/PHASEID/status.json
def status
@plan = Plan.eager_load(params[:plan_id])
authorize @plan
@@ -110,9 +110,10 @@
@phase = Phase.eager_load(:sections).find_by('phases.id = ?', params[:id])
authorize @phase
- @edit = (@phase.template.org == current_user.org)
+ @current = Template.current(@phase.template.dmptemplate_id)
+ @edit = (@phase.template.org == current_user.org) && (@phase.template == @current)
#@edit = params[:edit] == "true" ? true : false
-
+
#verify if there are any sections if not create one
@sections = @phase.sections
if !@sections.any?() || @sections.count == 0
@@ -158,13 +159,13 @@
def admin_create
@phase = Phase.new(params[:phase])
authorize @phase
-
+
@phase.description = params["phase-desc"]
@phase.modifiable = true
if @phase.save
@phase.template.dirty = true
@phase.template.save!
-
+
redirect_to admin_show_phase_path(id: @phase.id, edit: 'true'), notice: _('Information was successfully created.')
else
flash[:notice] = failed_create_error(@phase, _('phase'))
@@ -182,7 +183,7 @@
if @phase.update_attributes(params[:phase])
@phase.template.dirty = true
@phase.template.save!
-
+
redirect_to admin_show_phase_path(@phase), notice: _('Information was successfully updated.')
else
@sections = @phase.sections
@@ -190,7 +191,7 @@
# These params may not be available in this context so they may need
# to be set to true without the check
@edit = true
- @open = !params[:section_id].nil?
+ @open = !params[:section_id].nil?
@section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
@question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
flash[:notice] = failed_update_error(@phase, _('phase'))
@@ -206,15 +207,15 @@
if @phase.destroy
@template.dirty = true
@template.save!
-
+
redirect_to admin_template_template_path(@template), notice: _('Information was successfully deleted.')
else
@sections = @phase.sections
-
+
# These params may not be available in this context so they may need
# to be set to true without the check
@edit = true
- @open = !params[:section_id].nil?
+ @open = !params[:section_id].nil?
@section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
@question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
flash[:notice] = failed_destroy_error(@phase, _('phase'))
diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb
index 4f83340..6d7c775 100644
--- a/app/controllers/templates_controller.rb
+++ b/app/controllers/templates_controller.rb
@@ -140,7 +140,18 @@
@current = Template.current(@template.dmptemplate_id)
- unless @template == @current
+ if @template == @current
+ # If the template is published
+ if @template.published?
+ # We need to create a new, editable version
+ new_version = Template.deep_copy(@template)
+ new_version.version = (@template.version + 1)
+ new_version.published = false
+ new_version.save
+ @template = new_version
+# @current = Template.current(@template.dmptemplate_id)
+ end
+ else
flash[:notice] = _('You are viewing a historical version of this template. You will not be able to make changes.')
end
diff --git a/app/views/phases/_show_phase.html.erb b/app/views/phases/_show_phase.html.erb
index 0fb55d0..ecb5dd4 100644
--- a/app/views/phases/_show_phase.html.erb
+++ b/app/views/phases/_show_phase.html.erb
@@ -4,7 +4,7 @@
<%= _('Phase details')%>
- <% if @phase.modifiable %>
+ <% if @phase.modifiable && @edit %>
<%= link_to _('Edit phase details'), '#', class: "btn btn-primary", id: "edit_phase_button"%>
diff --git a/app/views/phases/admin_show.html.erb b/app/views/phases/admin_show.html.erb
index cd02a2d..e27f41e 100644
--- a/app/views/phases/admin_show.html.erb
+++ b/app/views/phases/admin_show.html.erb
@@ -17,7 +17,7 @@
|
<%= org_template.title%>
+ <% if org_template == @current && !org_template.published%>
+ <%=_('Draft')%>
+ <% end %>
|
<%= org_template.version %>
@@ -60,4 +63,4 @@
-<%end%>
\ No newline at end of file
+<%end%>
diff --git a/test/functional/templates_controller_test.rb b/test/functional/templates_controller_test.rb
index 30c57f9..70e6edd 100644
--- a/test/functional/templates_controller_test.rb
+++ b/test/functional/templates_controller_test.rb
@@ -1,12 +1,12 @@
require 'test_helper'
class TemplatesControllerTest < ActionDispatch::IntegrationTest
-
+
include Devise::Test::IntegrationHelpers
-
+
setup do
scaffold_template
-
+
# Get the first Org Admin
scaffold_org_admin(@template.org)
end
@@ -36,41 +36,41 @@
# admin_destroy_template DELETE /org/admin/templates/:id/admin_destroy(.:format) templates#admin_destroy
# admin_create_template POST /org/admin/templates/:id/admin_create(.:format) templates#admin_create
# admin_update_template PUT /org/admin/templates/:id/admin_update(.:format) templates#admin_update
-
-
+
+
# GET /org/admin/templates/:id/admin_index (admin_index_template_path) the :id here makes no sense!
# ----------------------------------------------------------
test "get the list of admin templates" do
# Should redirect user to the root path if they are not logged in!
get admin_index_template_path(@user.org)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
-
+
get admin_index_template_path(@user.org)
assert_response :success
-
+
assert assigns(:funder_templates)
assert assigns(:org_templates)
- end
-
+ end
+
# GET /org/admin/templates/:id/admin_template (admin_template_template_path)
# ----------------------------------------------------------
test "get the admin template" do
# Should redirect user to the root path if they are not logged in!
get admin_template_template_path(@template)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
-
+
get admin_template_template_path(@template)
assert_response :success
-
+
assert assigns(:template)
assert assigns(:hash)
assert assigns(:current)
end
-
+
# TODO: Why are we passing an :id here!? Its a new record but we seem to need the last template's id
# GET /org/admin/templates/:id/admin_new (admin_new_template_path)
# ----------------------------------------------------------
@@ -78,30 +78,30 @@
# Should redirect user to the root path if they are not logged in!
get admin_new_template_path(Template.last.id)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
-
+
get admin_new_template_path(Template.last.id)
assert_response :success
end
-
+
# GET /org/admin/templates/:id/admin_template_history (admin_template_history_template_path)
# ----------------------------------------------------------
test "get the admin template history page" do
# Should redirect user to the root path if they are not logged in!
get admin_template_history_template_path(@template)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
-
+
get admin_template_history_template_path(@template)
assert_response :success
-
+
assert assigns(:template)
assert assigns(:templates)
assert assigns(:current)
end
-
+
# DELETE /org/admin/templates/:id/admin_destroy (admin_destroy_template_path)
# ----------------------------------------------------------
test "delete the admin template" do
@@ -109,16 +109,16 @@
# Should redirect user to the root path if they are not logged in!
delete admin_destroy_template_path(@template)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
-
+
family = @template.dmptemplate_id
prior = Template.current(family)
-
+
version_the_template
-
+
current = Template.current(family)
-
+
# Try to delete a historical version should fail
delete admin_destroy_template_path(prior)
assert_equal _('You cannot delete historical versions of this template.'), flash[:notice]
@@ -130,31 +130,31 @@
delete admin_destroy_template_path(current)
assert_response :redirect
assert_redirected_to admin_index_template_path
- assert_raise ActiveRecord::RecordNotFound do
+ assert_raise ActiveRecord::RecordNotFound do
Template.find(current.id).nil?
end
assert_equal prior, Template.current(family), "expected the old version to now be the current version"
end
-
+
# TODO: Why are we passing an :id here!? Its a new record but we seem to need the last template's id
# POST /org/admin/templates/:id/admin_create (admin_create_template_path)
# ----------------------------------------------------------
test "create a template" do
params = {title: 'Testing create route'}
-
+
# Should redirect user to the root path if they are not logged in!
post admin_create_template_path(@user.org), {template: params}
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
-
+
post admin_create_template_path(@user.org), {template: params}
assert_equal _('Information was successfully created.'), flash[:notice]
assert_response :redirect
assert_redirected_to admin_template_template_url(Template.last.id)
assert assigns(:template)
assert_equal 'Testing create route', Template.last.title, "expected the record to have been created!"
-
+
# Invalid object
post admin_create_template_path(@user.org), {template: {title: nil, org_id: @user.org.id}}
assert flash[:notice].starts_with?(_('Could not create your'))
@@ -162,23 +162,25 @@
assert assigns(:template)
assert assigns(:hash)
end
-
+
# GET /org/admin/templates/:id/admin_update (admin_update_template_path)
# ----------------------------------------------------------
test "update the admin template" do
params = {title: 'ABCD'}
-
+
# Should redirect user to the root path if they are not logged in!
+ #get admin_template_template_path(@template) # Click on 'edit'
+ #@template = Template.current(@template.dmptemplate_id) # Edit working copy
put admin_update_template_path(@template), {template: params}
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
family = @template.dmptemplate_id
prior = Template.current(family)
-
+
version_the_template
-
+
current = Template.current(family)
# We shouldn't be able to edit a historical version
@@ -187,7 +189,7 @@
assert_response :redirect
assert_redirected_to admin_template_template_url(prior)
assert assigns(:template)
-
+
# Make sure we get the right response when editing an unpublished template
put admin_update_template_path(current), {template: params}
assert_equal _('Information was successfully updated.'), flash[:notice]
@@ -195,8 +197,8 @@
assert assigns(:template)
assert assigns(:hash)
assert_equal 'ABCD', current.reload.title, "expected the record to have been updated"
- assert current.reload.dirty?
-
+ assert current.reload.dirty?
+
# Make sure we get the right response when providing an invalid template
put admin_update_template_path(current), {template: {title: nil}}
assert flash[:notice].starts_with?(_('Could not update your'))
@@ -211,7 +213,7 @@
# Make sure we are redirected if we're not logged in
put admin_customize_template_path(@template)
assert_unauthorized_redirect_to_root_path
-
+
funder_template = Template.create(org: Org.funders.first, title: 'Testing integration')
# Sign in as the funder so that we cna publish the template
@@ -220,24 +222,24 @@
put admin_publish_template_path(funder_template)
assert_response :redirect
assert_redirected_to admin_index_template_path(funder_template.org)
-
+
# Sign in as the regular user so we can customize the funder template
sign_in @user
-
+
template = Template.live(funder_template.dmptemplate_id)
-
+
put admin_customize_template_path(template)
-
+
customization = Template.where(customization_of: template.dmptemplate_id).last
assert_response :redirect
assert_redirected_to admin_template_template_url(Template.last)
assert assigns(:template)
-
+
assert_equal 0, customization.version
assert_not customization.published?
assert_not customization.dirty?
-
+
# Make sure the funder templates data is not modifiable!
customization.phases.each do |p|
assert_not p.modifiable
@@ -249,21 +251,21 @@
end
end
end
-
+
# GET /org/admin/templates/:id/admin_publish (admin_publish_template_path)
# ----------------------------------------------------------
test "publish a template" do
# Should redirect user to the root path if they are not logged in!
put admin_publish_template_path(@template)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
family = @template.dmptemplate_id
prior = Template.current(family)
-
+
version_the_template
-
+
current = Template.current(family)
# We shouldn't be able to edit a historical version
@@ -272,18 +274,20 @@
assert_response :redirect
assert_redirected_to admin_template_template_url(prior)
assert assigns(:template)
-
+
# Publish the current template
put admin_publish_template_path(current)
assert_equal _('Your template has been published and is now available to users.'), flash[:notice]
assert_response :redirect
assert_redirected_to admin_index_template_path(@user.org)
-
+ current = Template.current(family)
+
# Update the description so that the template gets versioned
- put admin_update_template_path(current), {template: {description: "this is an update"}}
-
+ get admin_template_template_path(current) # Click on 'edit'
+ new_version = Template.current(family) # Edit working copy
+ put admin_update_template_path(new_version), {template: {description: "this is an update"}}
+
# Make sure it versioned properly
- current = Template.includes(:phases, :sections, :questions).find(current.id)
new_version = Template.current(family)
assert_not_equal current.id = new_version.id, "expected it to create a new version"
assert_equal (current.version + 1), new_version.version, "expected the version to have incremented"
@@ -293,33 +297,33 @@
assert new_version.dirty?, "expected the new dirty flag to be true"
assert_equal current.dmptemplate_id, new_version.dmptemplate_id, "expected the old and new versions to share the same dmptemplate_id"
end
-
+
# GET /org/admin/templates/:id/admin_unpublish (admin_unpublish_template_path)
# ----------------------------------------------------------
test "unpublish a template" do
# Should redirect user to the root path if they are not logged in!
put admin_unpublish_template_path(@template)
assert_unauthorized_redirect_to_root_path
-
+
sign_in @user
family = @template.dmptemplate_id
prior = Template.current(family)
-
+
version_the_template
-
+
current = Template.current(family)
-
+
# Publish it so we can unpublish
put admin_publish_template_path(current)
assert_not Template.live(family).nil?
-
+
put admin_unpublish_template_path(current)
assert_equal _('Your template is no longer published. Users will not be able to create new DMPs for this template until you re-publish it'), flash[:notice]
assert_response :redirect
assert_redirected_to admin_index_template_path(@user.org)
-
+
# Make sure there are no published versions
assert Template.live(family).nil?
end
-end
\ No newline at end of file
+end
diff --git a/test/integration/template_versioning_test.rb b/test/integration/template_versioning_test.rb
index 97e1cec..dd59836 100644
--- a/test/integration/template_versioning_test.rb
+++ b/test/integration/template_versioning_test.rb
@@ -6,68 +6,70 @@
setup do
scaffold_template
scaffold_org_admin(@template.org)
-
+
sign_in @user
-
+
# Make sure the template starts out as unpublished. The controller will not allow changes once its published
@template.published = false
@template.save!
-
+
@initial_id = @template.id
@initial_version = @template.version
@initial_title = @template.title
@dmptemplate_id = @template.dmptemplate_id
end
-
+
# ----------------------------------------------------------
test 'template gets versioned when its details are updated but it is already published' do
# Publish the template
put admin_publish_template_path(@template)
@template = Template.current(@dmptemplate_id)
+ get admin_template_template_path(@template) # Click on 'edit'
+ @template = Template.current(@dmptemplate_id) # Edit new version
# Change the title after its been published
put admin_update_template_path(@template), {template: {title: "Blah blah blah"}}
@template = Template.current(@dmptemplate_id)
-
+
# Make sure that the template was versioned
assert_equal (@initial_version + 1), @template.version, "expected the version to have incremented"
assert_not_equal @initial_id, @template.id, "expected the id to have changed"
assert_equal @dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match"
assert_equal false, @template.published?, "expected the new version to be unpublished"
assert_not_equal @initial_title, @template.title, "expected the title to have been updated"
-
+
# Now retrieve the published version and verify that it is unchanged
old = Template.live(@dmptemplate_id)
assert_equal @initial_version, old.version, "expected the version number of the published version to be the same"
assert_equal @initial_id, old.id, "expected the id of the published version to be the same"
assert_equal @initial_title, old.title, "expected the title of the published version to be the same"
end
-
+
# ----------------------------------------------------------
test 'template gets versioned when its phases are modified and it is already published' do
@template.dirty = false
@template.save!
-
+
put admin_update_phase_path @template.phases.first, {phase: {title: 'UPDATED'}}
@template.reload
assert @template.dirty
end
-
+
# ----------------------------------------------------------
test 'template gets versioned when its sections are modified and it is already published' do
@template.dirty = false
@template.save!
-
+
put admin_update_section_path @template.phases.first.sections.first, {section: {title: 'UPDATED'}}
@template.reload
assert @template.dirty
end
-
+
# ----------------------------------------------------------
test 'template gets versioned when its questions are modified and it is already published' do
@template.dirty = false
@template.save!
-
+
put admin_update_question_path @template.phases.first.sections.first.questions.first, {question: {text: 'UPDATED'}}
@template.reload
assert @template.dirty
@@ -84,7 +86,7 @@
assert_equal @dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match"
assert_equal false, @template.published?, "expected the version to have remained unpublished"
end
-
+
# ----------------------------------------------------------
test 'publishing a plan unpublishes the old published plan' do
put admin_publish_template_path(@template)
@@ -98,7 +100,7 @@
put admin_unpublish_template_path(@template)
assert Template.live(@dmptemplate_id).nil?
end
-
+
# ----------------------------------------------------------
test 'plans get attached to the appropriate template version' do
=begin
@@ -112,7 +114,7 @@
# Sign in as the funder so that we cna publish the template
sign_in User.find_by(org: funder_template.org)
-
+
# Publish the funder template
put admin_publish_template_path(funder_template)
assert_response :redirect
@@ -122,41 +124,41 @@
liveA = Template.live(funder_template.dmptemplate_id)
@dmptemplate_id = @template.dmptemplate_id
end
-
+
sign_in @user
-
+
# Plan A gets attached to the template v1
post plans_path, {plan: {funder_id: @template.org.id}}
planA = Plan.last
assert_equal liveA, planA.template, "expected the latest published version to have been assigned to PlanA"
-
+
# Template v2 is updated
put admin_update_template_path(@template), {template: {title: "Blah blah blah"}}
@template = Template.current(@dmptemplate_id)
-
+
# Plan B gets attached to the template v1 because v2 is not yet published
post plans_path, {plan: {funder_id: @template.org.id}}
planB = Plan.last
assert_equal liveA, planB.template, "expected the latest published version to have been assigned to PlanB"
-
+
# Plan A should still be attached to v1
assert_equal liveA, planA.template, "expected PlanA to still be attached to the original published version"
-
- # Sign back in as the funder
+
+ # Sign back in as the funder
sign_in User.find_by(org: funder_template.org)
-
+
# Template v2 is published
put admin_publish_template_path(@template)
@template = Template.current(@dmptemplate_id)
liveB = Template.live(@dmptemplate_id)
-
+
sign_in @user
-
+
# Plan C gets attached to template v2
post plans_path, {plan: {funder_id: @template.org.id}}
planC = Plan.last
assert_equal liveB, planC.template, "expected the latest published version to have been assigned to PlanA"
-
+
# Plan A and B are still attached to v1
assert_equal liveA, planA.template, "expected PlanA to still be attached to the original published version"
assert_equal liveA, planB.template, "expected PlanB to still be attached to the original published version"
diff --git a/test/test_helper.rb b/test/test_helper.rb
index f95c34d..43b64d3 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -11,16 +11,16 @@
class ActiveSupport::TestCase
include GlobalHelpers
-
+
# Suppress noisy ActiveRecord logs because fixtures load for each test
ActiveRecord::Base.logger.level = Logger::INFO
-
+
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
#fixtures :all
-
+
# Use the seeds.rb file to seed the test database
require_relative '../db/seeds.rb'
@@ -32,113 +32,115 @@
def scaffold_org_admin(org)
@user = User.create!(email: "admin-#{org.abbreviation.downcase}@example.com", firstname: "Org", surname: "Admin",
language: Language.find_by(abbreviation: FastGettext.locale),
- password: "password123", password_confirmation: "password123",
+ password: "password123", password_confirmation: "password123",
org: org, accept_terms: true, confirmed_at: Time.zone.now,
perms: Perm.where.not(name: ['admin', 'add_organisations', 'change_org_affiliation', 'grant_api_to_orgs']))
#perms: [Perm::GRANT_PERMISSIONS, Perm::MODIFY_TEMPLATES, Perm::MODIFY_GUIDANCE, Perm::CHANGE_ORG_DETAILS])
end
-
-
+
+
# Convert Ruby Class Names into attribute names (e.g. MyClass --> my_class)
# ----------------------------------------------------------------------
def class_name_to_attribute_name(name)
name.gsub(/([a-z]+)([A-Z])/, '\1_\2').gsub('-', '_').downcase
end
-
- # Scaffold a new Template with one Phase, one Section, and a Question for
- # each of the possible Question Formats.
+
+ # Scaffold a new Template with one Phase, one Section, and a Question for
+ # each of the possible Question Formats.
# ----------------------------------------------------------------------
def scaffold_template
- template = Template.new(title: 'Test template',
+ template = Template.new(title: 'Test template',
description: 'My test template',
org: Org.first)
-
- template.phases << Phase.new(title: 'Test phase',
- description: 'My test phase',
+
+ template.phases << Phase.new(title: 'Test phase',
+ description: 'My test phase',
number: 1)
-
- section = Section.new(title: 'Test section',
+
+ section = Section.new(title: 'Test section',
description: 'My test section',
number: 99, phase: template.phases.first)
-
+
i = 1
# Add each type of Question to the new section
QuestionFormat.all.each do |frmt|
- question = Question.new(text: "Test question - #{frmt.title}", number: i,
+ question = Question.new(text: "Test question - #{frmt.title}", number: i,
question_format: frmt)
-
+
if frmt.option_based?
3.times do |j|
question.question_options << QuestionOption.new(text: "Option #{j}", number: j)
end
end
-
+
section.questions << question
i += 1
end
-
+
template.phases.first.sections << section
template.save!
assert template.valid?, "unable to create new Template: #{template.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
-
+
@template = template.reload
end
-
+
# Version the template
# ----------------------------------------------------------------------
def version_the_template
put admin_publish_template_path(@template)
+ get admin_template_template_path(@template) # Click on 'edit'
+ @template = Template.current(@template.dmptemplate_id) # Edit working copy
put admin_update_template_path(@template), {template: {title: "#{@template.title} - VERSIONED"}}
end
-
- # Scaffold a new Plan based on the scaffolded Template
+
+ # Scaffold a new Plan based on the scaffolded Template
# ----------------------------------------------------------------------
def scaffold_plan
scaffold_template if @template.nil?
-
- @plan = Plan.new(template: @template, title: 'Test Plan', grant_number: 'Grant-123',
+
+ @plan = Plan.new(template: @template, title: 'Test Plan', grant_number: 'Grant-123',
principal_investigator: 'me', principal_investigator_identifier: 'me-1234',
description: "this is my plan's informative description",
identifier: '1234567890', data_contact: 'me@example.com', visibility: 0,
roles: [Role.new(user: User.last, creator: true)])
-
+
assert @plan.valid?, "unable to create new Plan: #{@plan.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
@plan.save!
end
-
-
+
+
# FUNCTIONAL/INTEGRATION TEST HELPERS
# ----------------------------------------------------------------------
def assert_unauthorized_redirect_to_root_path
assert_response :redirect
assert_match "#{root_url}", @response.redirect_url
-
+
follow_redirects
-
+
assert_response :success
assert_select '.welcome-message h2', _('Welcome.')
end
-
+
# ----------------------------------------------------------------------
def assert_authorized_redirect_to_plans_page
assert_response :redirect
assert_match "#{root_url}", @response.redirect_url
-
+
# Sometimes Devise has an intermediary step prior to sending the user to the final destination
follow_redirects
-
+
assert_response :success
assert_select '.main_page_content h1', _('My plans')
end
-
+
# ----------------------------------------------------------------------
def follow_redirects
while @response.status >= 300 && @response.status < 400
follow_redirect!
end
end
-
+
# UNIT TEST HELPERS
# ----------------------------------------------------------------------
def verify_deep_copy(object, exclusions)
@@ -154,40 +156,40 @@
end
end
end
-
+
# ----------------------------------------------------------------------
def verify_has_many_relationship(object, new_association, initial_expected_count)
# Assumes that the association name matches the pluralized name of the class
rel = "#{class_name_to_attribute_name(new_association.class.name).pluralize}"
-
+
assert_equal initial_expected_count, object.send(rel).count, "was expecting #{object.class.name} to initially have #{initial_expected_count} #{rel}"
-
+
# Add another association for the object
object.send(rel) << new_association
object.save!
assert_equal (initial_expected_count + 1), object.send(rel).count, "was expecting #{object.class.name} to have #{initial_expected_count + 1} #{rel} after adding a new one - #{new_association.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
-
+
# Remove the newly added association
object.send(rel).delete(new_association)
object.save!
assert_equal initial_expected_count, object.send(rel).count, "was expecting #{object.class.name} to have #{initial_expected_count} #{rel} after removing the new one we added"
end
-
+
# ----------------------------------------------------------------------
def verify_belongs_to_relationship(child, parent)
# Assumes that the association name matches the lower case name of the class
prnt = "#{class_name_to_attribute_name(parent.class.name)}"
chld = "#{class_name_to_attribute_name(child.class.name)}"
-
+
child.send("#{prnt}=", parent)
child.save!
assert_equal parent, child.send(prnt), "was expecting #{chld} to have a #{prnt}.id == #{parent.id}"
-
+
# Search the parent for the child
parent.reload
assert_includes parent.send("#{chld.pluralize}"), child, "was expecting the #{prnt}.#{chld.pluralize} to contain the #{chld}"
end
-
+
# STUBS FOR CALLS To EXTERNAL SITES
# ----------------------------------------------------------------------
def stub_blog_calls
@@ -207,7 +209,7 @@
"1 at http://www.example.com/stubbed/blog" +
"" +
""
-
+
stub_request(:get, "http://www.dcc.ac.uk/news/dmponline-0/feed").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => blog_feed, :headers => {})
|