Skip to content

Commit 976e9d6

Browse files
committed
Issue #3534 - Added a new RSpec test file plans_controller_templates_spec.rb. This deals with testing methods used by the new Plan creation functionality introduced.
1 parent 3aef6d2 commit 976e9d6

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require "rails_helper"
2+
3+
RSpec.describe PlansController, type: :controller do
4+
describe "templates availability helpers" do
5+
before do
6+
@org = create(:org, :organisation, name: "The User Org")
7+
@org_id = @org.id
8+
9+
@org_template = create(:template, :default, :organisationally_visible, :published, org: @org, title: "Org Template")
10+
11+
@funder_1 = create(:org, :funder, name: "Funder_1 Org")
12+
@funder_1_template = create(:template, :publicly_visible, :published,
13+
org: @funder_1, title: "Funder_1 Template")
14+
15+
@org_custom_funder_1_template = create(:template, :organisationally_visible, :published,
16+
org: @org, customization_of: @funder_1_template.family_id, title: "Customised Funder_1 Template for Org")
17+
18+
# # @org_custom_funder_1_template = create(:template, :organisationally_visible, :published,
19+
# org: @org, title: "Customised Funder_1 Template for Org")
20+
21+
@funder_2 = create(:org, :funder, name: "Funder_2 Org")
22+
@funder_2_template = create(:template, :publicly_visible, :published, org: @funder_2, title: "Funder_2 Template")
23+
24+
@global_template = create(:template, :default, :publicly_visible, :published, title: "Global Template")
25+
26+
@other_org = create(:org, :organisation, name: "The Other Org")
27+
@other_org_template = create(:template, :organisationally_visible, :published,
28+
org: @other_org, title: "Other Org Template")
29+
end
30+
31+
it "returns a grouped hash containing the available templates" do
32+
grouped = controller.send(:templates_available_to_org_user, @org_id)
33+
expect(grouped).to be_a(Hash)
34+
puts "Grouped templates available to org id #{@org_id}: #{grouped.keys.inspect}"
35+
36+
# flatten one level to inspect [title, id] entries across groups
37+
entries = grouped.values.flatten(1)
38+
puts entries.inspect
39+
# Available templates
40+
expect(entries).to include([@org_template.title, @org_template.id])
41+
expect(entries).to include([@org_custom_funder_1_template.title, @org_custom_funder_1_template.id])
42+
expect(entries).to include([@funder_2_template.title, @funder_2_template.id])
43+
expect(entries).to include([@global_template.title, @global_template.id])
44+
# Not available templates
45+
# As the org has already customised funder_1_template, it should not appear in the available list
46+
expect(entries).not_to include([@funder_1_template.title, @funder_1_template.id])
47+
# Templates from other orgs should not appear if only organisationally visible
48+
expect(entries).not_to include([@other_org_template.title, @other_org_template.id])
49+
end
50+
51+
it 'validates a template id that is available for the org' do
52+
expect(controller.send(:validate_template_available_to_org_user?, @org_template.id, @org_id)).to be true
53+
expect(controller.send(:validate_template_available_to_org_user?, @org_custom_funder_1_template.id, @org_id)).to be true
54+
expect(controller.send(:validate_template_available_to_org_user?, @funder_2_template.id, @org_id)).to be true
55+
expect(controller.send(:validate_template_available_to_org_user?, @global_template.id, @org_id)).to be true
56+
end
57+
58+
it 'returns false for a template id that is not available for the org' do
59+
# As the org has already customised funder_1_template, it should not be available
60+
expect(controller.send(:validate_template_available_to_org_user?, @funder_1_template.id, @org_id)).to be false
61+
# Templates from other orgs should not be available if only organisationally visible
62+
expect(controller.send(:validate_template_available_to_org_user?, @other_org_template.id, @org_id)).to be false
63+
end
64+
end
65+
end

spec/factories/templates.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
published { false }
4343
archived { false }
4444
sequence(:version)
45+
customization_of { nil }
46+
4547

4648
trait :publicly_visible do
4749
after(:create) do |template|

0 commit comments

Comments
 (0)