diff --git a/app/controllers/dmptemplates_controller.rb b/app/controllers/dmptemplates_controller.rb index 9c7f757..e4b8785 100644 --- a/app/controllers/dmptemplates_controller.rb +++ b/app/controllers/dmptemplates_controller.rb @@ -8,10 +8,17 @@ # GET /dmptemplates def admin_index authorize Dmptemplate - #institutional templates + +# TODO: Wouldn't make more sense here to just do the following (using new model names here)?: +# @dmptemplates_own = current_user.org.templates + @dmptemplates_own = Dmptemplate.own_institutional_templates(current_user.organisation_id) - #funders templates + +# TODO: Wouldn't make more sense here to just do the following?: +# @@dmptemplates_funders = Org.where(org_type: :funder).templates + @dmptemplates_funders = Dmptemplate.funders_templates + respond_to do |format| format.html # index.html.erb end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index a653686..2acb8c3 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -71,6 +71,10 @@ @always_guidance = get_always_available_guidance @institutions = orgs_of_type(constant("organisation_types.institution")) +# TODO: Would be better to determine if the user's org has templates here than in the view. +# Replace the if Dmptemplate.own_institutional_templates check in views/projects/new with: +# @own_org_has_templates = current_user.organisation.templates.empty? + respond_to do |format| format.html # new.html.erb end diff --git a/app/models/dmptemplate.rb b/app/models/dmptemplate.rb index 1ef460c..789635e 100644 --- a/app/models/dmptemplate.rb +++ b/app/models/dmptemplate.rb @@ -1,33 +1,37 @@ class Dmptemplate < ActiveRecord::Base - include GlobalHelpers + include GlobalHelpers - attr_accessible :id, :organisation_id, :description, :published, :title, :user_id, :locale, - :phases, :projects, :organisation, - :is_default, :guidance_group_ids, :as => [:default, :admin] +# TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! + attr_accessible :id, :organisation_id, :description, :published, :title, :user_id, :locale, + :phases, :projects, :organisation, + :is_default, :guidance_group_ids, :as => [:default, :admin] - #associations between tables - has_many :phases - has_many :versions, through: :phases - has_many :sections, through: :versions - has_many :questions, through: :sections - has_many :projects + #associations between tables + has_many :phases + has_many :versions, through: :phases + has_many :sections, through: :versions + has_many :questions, through: :sections + has_many :projects - #has_many :guidances needs to be removed and checked + #has_many :guidances needs to be removed and checked - belongs_to :organisation + belongs_to :organisation has_and_belongs_to_many :guidance_groups, join_table: "dmptemplates_guidance_groups" - accepts_nested_attributes_for :guidance_groups - accepts_nested_attributes_for :phases - accepts_nested_attributes_for :organisation - accepts_nested_attributes_for :projects +# TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! + accepts_nested_attributes_for :guidance_groups + accepts_nested_attributes_for :phases + accepts_nested_attributes_for :organisation + accepts_nested_attributes_for :projects has_settings :export, class_name: 'Settings::Dmptemplate' do |s| s.key :export, defaults: Settings::Dmptemplate::DEFAULT_SETTINGS end + validates :organisation, :title, presence: true + ## # Converts a DMPtemplate object into a string containing it's title # @@ -36,6 +40,13 @@ "#{title}" end + +# TODO: Remove the following methods ... they are never called by anything and don't make sense here anyway: +# self.templates_org_type +# self.funders_templates +# self.own_institutional_templates +# self.funders_and_own_templates + ## # takes a type or organisation and returns all published templates from # organisations of that type @@ -116,6 +127,8 @@ return templates_list end +# TODO: Remove this! We should not be attempting to access an Org attribute through this class + ## # Returns the string name of the organisation type of the organisation who # owns this dmptemplate diff --git a/test/fixtures/organisations.yml b/test/fixtures/organisations.yml index df643f5..d98f1e2 100644 --- a/test/fixtures/organisations.yml +++ b/test/fixtures/organisations.yml @@ -15,7 +15,7 @@ name: 'Curation Centre' abbreviation: 'cc' organisation_type: organisation - contact_email: 'admin@example-curation-centre.org' + contact_email: 'admin@example.org' funder: name: 'Grant Funder' @@ -33,7 +33,6 @@ name: <%= "#{obj['name']} #{n}" %> domain: <%= "www.#{lbl}-#{n + 1}.org" %> organisation_type: <%= "#{lbl}" %> - contact_email: "admin@example-#{lbl}-#{n + 1}.org" <% end %> <% end %> @@ -54,4 +53,4 @@ language_id: en-UK logo_uid: '2016/08/30/1234abcd-complete-org.gif' logo_name: 'complete-org.gif' - contact_email: 'admin@example-complete-org.org' \ No newline at end of file + contact_email: 'admin@example.org' \ No newline at end of file diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb new file mode 100644 index 0000000..88ef51b --- /dev/null +++ b/test/unit/comment_test.rb @@ -0,0 +1,57 @@ +require 'test_helper' + +class CommentTest < ActiveSupport::TestCase + + setup do + @user = User.last + + # generate a template and plan + template = generate_complete_template + + project = Project.new({ + title: 'Test Project', + organisation: @user.organisation + }) + project.dmptemplate = template + project.save! + + @plan = project.plans.first + + qs = template.phases.first.versions.first.sections.first.questions + @text_area_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Area' ) }.first + @text_field_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Field' ) }.first + @radio_button_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Radio Button' ) }.first + @check_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Check Box' ) }.first + @select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Dropdown' ) }.first + @multi_select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Multi Select Box' ) }.first + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Comment.new.valid? + assert_not Comment.new(user: @user, question: @text_area_question).valid?, "expected the 'text' field to be required" + assert_not Comment.new(plan: @plan, question: @text_area_question, text: 'Testing').valid?, "expected the 'user' field to be required" + assert_not Comment.new(user: @user, question: @text_area_question, text: 'Testing').valid?, "expected the 'plan' field to be required" + assert_not Comment.new(user: @user, plan: @plan, text: 'Testing').valid?, "expected the 'question' field to be required" + + # Ensure the bar minimum and complete versions are valid + a = Comment.new(user: @user, plan: @plan, question: @text_area_question, text: 'Testing') + assert a.valid?, "expected the 'text', 'plan', 'user' and 'question' fields to be enough to create an Comment! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "can CRUD answers for text based questions" do + [@text_area_question, @text_field_question].each do |q| + cmnt = Comment.create(user: @user, plan: @plan, question: q, text: 'Tested ABC') + assert_not cmnt.id.nil?, "was expecting to be able to create a new Comment for a #{q.question_format.title} question: #{cmnt.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + cmnt.text = 'Testing an update' + cmnt.save! + cmnt.reload + assert_equal 'Testing an update', cmnt.text, "Was expecting to be able to update the text of the Comment for a #{q.question_format.title} question!" + + assert cmnt.destroy!, "Was unable to delete the Comment for a #{q.question_format.title} question!" + end + end + +end diff --git a/test/unit/dmptemplate_test.rb b/test/unit/dmptemplate_test.rb index 595d984..76a3617 100644 --- a/test/unit/dmptemplate_test.rb +++ b/test/unit/dmptemplate_test.rb @@ -4,13 +4,14 @@ setup do @template = Dmptemplate.first + + @organisation = Organisation.first end def settings(extras = {}) - { - margin: (@margin || { top: 10, bottom: 10, left: 10, right: 10 }), - font_face: (@font_face || Settings::Dmptemplate::VALID_FONT_FACES.first), - font_size: (@font_size || 11) + {margin: (@margin || { top: 10, bottom: 10, left: 10, right: 10 }), + font_face: (@font_face || Settings::Dmptemplate::VALID_FONT_FACES.first), + font_size: (@font_size || 11) }.merge(extras) end @@ -18,13 +19,31 @@ Settings::Dmptemplate::DEFAULT_SETTINGS[:formatting] end - # ---------- settings ---------- + # --------------------------------------------------- + test "required fields are required" do + assert_not Dmptemplate.new.valid? + assert_not Dmptemplate.new(title: 'Testing tmeplate').valid?, "expected the 'title' field to be required" + assert_not Dmptemplate.new(organisation: @organisation).valid?, "expected the 'organisation' field to be required" + + # Ensure the bar minimum and complete versions are valid + a = Dmptemplate.new(organisation: @organisation, title: 'Testing tmeplate') + assert a.valid?, "expected the 'title' and 'organisation' fields to be enough to create an Dmptemplate! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s method returns the title" do + assert_equal @template.title, @template.to_s + end + - test "settings should use defaults if none defined" do + # ---------- settings ---------- + # --------------------------------------------------- + test "settings should use defaults if none are defined" do assert(!@template.settings(:export).value?) assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "settings should use defined valid settings" do @template.settings(:export).formatting = settings @template.save! @@ -34,6 +53,7 @@ assert_not_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting negative margin should not be valid" do @margin = { top: -10, bottom: 10, left: 10, right: 10 } @@ -49,6 +69,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting unknown margin should not be valid" do @margin = { top: 10, bottom: 10, left: 10, right: 10, top_left: 10 } @@ -64,6 +85,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting negative font-size should not be valid" do @font_size = -11 @@ -80,6 +102,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting unknown key should not be valid" do @template.settings(:export).formatting = settings(foo: :bar) @@ -94,6 +117,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "not setting font_face should not be valid" do @template.settings(:export).formatting = settings.reject {|k,v| k == :font_face } @@ -108,6 +132,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "not setting font_size should not be valid" do @template.settings(:export).formatting = settings.reject {|k,v| k == :font_size } @@ -122,6 +147,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "not setting margin should not be valid" do @template.settings(:export).formatting = settings.reject {|k,v| k == :margin } @@ -136,6 +162,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting non-hash as margin should not be valid" do @margin = :foo @@ -152,6 +179,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting non-integer as font_size should not be valid" do @font_size = "foo" @@ -168,6 +196,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting non-string as font_face should not be valid" do @font_face = 1 @@ -184,6 +213,7 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end + # --------------------------------------------------- test "setting unknown string as font_face should not be valid" do @font_face = 'Monaco, Monospace, Sans-Serif' @@ -275,5 +305,40 @@ end end + # --------------------------------------------------- + test "can CRUD answers for text based questions" do + tmplt = Dmptemplate.create(organisation: @organisation, title: 'Testing tmeplate') + assert_not tmplt.id.nil?, "was expecting to be able to create a new Dmptemplate!" + tmplt.description = 'Testing an update' + tmplt.save! + tmplt.reload + assert_equal 'Testing an update', tmplt.description, "Was expecting to be able to update the description of the Dmptemplate!" + + assert tmplt.destroy!, "Was unable to delete the Dmptemplate!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with Phase" do + phase = Phase.new(title: 'Test Phase', number: 2) + verify_has_many_relationship(@template, phase, @template.phases.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Project" do + project = Project.new(title: 'Test Project', organisation: @organisation) + verify_has_many_relationship(@template, project, @template.projects.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with GuidanceGroup" do + grp = GuidanceGroup.new(name: 'Test Group', organisation: @organisation) + verify_has_many_relationship(@template, grp, @template.guidance_groups.count) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Organisation" do + verify_belongs_to_relationship(@template, @organisation) + end + end