Commit 77b54988c00876752a94b8a2ddbc670f927fcf2c
1 parent
cdae5167
Exists in
master
and in
28 other branches
Validates that a track has at least one category
Showing
9 changed files
with
64 additions
and
56 deletions
Show diff stats
plugins/community_track/lib/community_track_plugin/track.rb
... | ... | @@ -3,6 +3,12 @@ class CommunityTrackPlugin::Track < Folder |
3 | 3 | settings_items :goals, :type => :string |
4 | 4 | settings_items :expected_results, :type => :string |
5 | 5 | |
6 | + validate :validate_categories | |
7 | + | |
8 | + def validate_categories | |
9 | + errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank? | |
10 | + end | |
11 | + | |
6 | 12 | def self.icon_name(article = nil) |
7 | 13 | 'community-track' |
8 | 14 | end | ... | ... |
plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb
... | ... | @@ -7,7 +7,7 @@ class CmsControllerTest < ActionController::TestCase |
7 | 7 | |
8 | 8 | def setup |
9 | 9 | @profile = fast_create(Community) |
10 | - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @profile) | |
10 | + @track = create_track('track', @profile) | |
11 | 11 | @step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today) |
12 | 12 | |
13 | 13 | user = create_user('testinguser') | ... | ... |
plugins/community_track/test/functional/community_track_plugin_content_viewer_controller_test.rb
... | ... | @@ -11,10 +11,7 @@ class ContentViewerControllerTest < ActionController::TestCase |
11 | 11 | |
12 | 12 | def setup |
13 | 13 | @profile = fast_create(Community) |
14 | - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @profile) | |
15 | - category = fast_create(Category, :name => "education") | |
16 | - @track.add_category(category) | |
17 | - | |
14 | + @track = create_track('track', @profile) | |
18 | 15 | @step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today, :tool_type => TinyMceArticle.name) |
19 | 16 | |
20 | 17 | user = create_user('testinguser') |
... | ... | @@ -110,7 +107,7 @@ class ContentViewerControllerTest < ActionController::TestCase |
110 | 107 | @block = CommunityTrackPlugin::TrackListBlock.create!(:box => box) |
111 | 108 | @profile.boxes << box |
112 | 109 | get :view_page, @step.url |
113 | - assert_tag :tag => 'div', :attributes => { :class => 'item category_education' }, :descendant => { :tag => 'div', :attributes => { :class => 'steps' }, :descendant => { :tag => 'div', :attributes => { :class => "step #{@block.status_class(@step)}" } } } | |
110 | + assert_tag :tag => 'div', :attributes => { :class => "item category_#{@track.category_name}" }, :descendant => { :tag => 'div', :attributes => { :class => 'steps' }, :descendant => { :tag => 'div', :attributes => { :class => "step #{@block.status_class(@step)}" } } } | |
114 | 111 | end |
115 | 112 | |
116 | 113 | should 'render tracks in track card list block' do |
... | ... | @@ -118,8 +115,8 @@ class ContentViewerControllerTest < ActionController::TestCase |
118 | 115 | @block = CommunityTrackPlugin::TrackCardListBlock.create!(:box => box) |
119 | 116 | @profile.boxes << box |
120 | 117 | get :view_page, @step.url |
121 | - assert_tag :tag => 'div', :attributes => { :class => 'item_card category_education' }, :descendant => { :tag => 'div', :attributes => { :class => 'track_content' } } | |
122 | - assert_tag :tag => 'div', :attributes => { :class => 'item_card category_education' }, :descendant => { :tag => 'div', :attributes => { :class => 'track_stats' } } | |
118 | + assert_tag :tag => 'div', :attributes => { :class => "item_card category_#{@track.category_name}" }, :descendant => { :tag => 'div', :attributes => { :class => 'track_content' } } | |
119 | + assert_tag :tag => 'div', :attributes => { :class => "item_card category_#{@track.category_name}" }, :descendant => { :tag => 'div', :attributes => { :class => 'track_stats' } } | |
123 | 120 | end |
124 | 121 | |
125 | 122 | should 'render link to display more tracks in track list block' do |
... | ... | @@ -127,9 +124,7 @@ class ContentViewerControllerTest < ActionController::TestCase |
127 | 124 | @block = CommunityTrackPlugin::TrackCardListBlock.create!(:box => box) |
128 | 125 | @profile.boxes << box |
129 | 126 | |
130 | - (@block.limit+1).times do |i| | |
131 | - CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @profile) | |
132 | - end | |
127 | + (@block.limit+1).times { |i| create_track("track#{i}", @profile) } | |
133 | 128 | |
134 | 129 | get :view_page, @step.url |
135 | 130 | assert_tag :tag => 'div', :attributes => { :id => "track_list_more_#{@block.id}" }, :descendant => { :tag => 'div', :attributes => { :class => 'more' } } |
... | ... | @@ -142,9 +137,7 @@ class ContentViewerControllerTest < ActionController::TestCase |
142 | 137 | @block.more_another_page = true |
143 | 138 | @block.save! |
144 | 139 | |
145 | - (@block.limit+1).times do |i| | |
146 | - CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @profile) | |
147 | - end | |
140 | + (@block.limit+1).times { |i| create_track("track#{i}", @profile) } | |
148 | 141 | |
149 | 142 | get :view_page, @step.url |
150 | 143 | assert_tag :tag => 'div', :attributes => { :id => "track_list_more_#{@block.id}" }, :descendant => { :tag => 'div', :attributes => { :class => 'view_all' } } | ... | ... |
plugins/community_track/test/functional/community_track_plugin_myprofile_controller_test.rb
... | ... | @@ -12,7 +12,7 @@ class CommunityTrackPluginMyprofileControllerTest < ActionController::TestCase |
12 | 12 | @response = ActionController::TestResponse.new |
13 | 13 | |
14 | 14 | @profile = fast_create(Community) |
15 | - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @profile) | |
15 | + @track = create_track('track', @profile) | |
16 | 16 | |
17 | 17 | @user = create_user('testinguser') |
18 | 18 | login_as(@user.login) | ... | ... |
plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb
... | ... | @@ -8,7 +8,7 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase |
8 | 8 | |
9 | 9 | def setup |
10 | 10 | @community = fast_create(Community) |
11 | - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @community) | |
11 | + @track = create_track('track', @community) | |
12 | 12 | |
13 | 13 | box = fast_create(Box, :owner_id => @community.id, :owner_type => 'Community') |
14 | 14 | @card_block = CommunityTrackPlugin::TrackCardListBlock.create!(:box => box) |
... | ... | @@ -26,17 +26,13 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase |
26 | 26 | end |
27 | 27 | |
28 | 28 | should 'display tracks with page size' do |
29 | - 20.times do |i| | |
30 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
31 | - end | |
29 | + 20.times { |i| create_track("track#{i}", @community) } | |
32 | 30 | xhr :get, :view_tracks, :id => @block.id, :page => 1, :per_page => 10 |
33 | 31 | assert_equal 10, @response.body.scan(/item/).size |
34 | 32 | end |
35 | 33 | |
36 | 34 | should 'default page size is the block limit' do |
37 | - 20.times do |i| | |
38 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
39 | - end | |
35 | + 20.times { |i| create_track("track#{i}", @community) } | |
40 | 36 | xhr :get, :view_tracks, :id => @block.id, :page => 1 |
41 | 37 | assert_equal @block.limit, @response.body.scan(/item/).size |
42 | 38 | end |
... | ... | @@ -47,9 +43,7 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase |
47 | 43 | end |
48 | 44 | |
49 | 45 | should 'show more link in all tracks if there is no more tracks to show' do |
50 | - 10.times do |i| | |
51 | - CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
52 | - end | |
46 | + 10.times { |i| create_track("track#{i}", @community) } | |
53 | 47 | get :all_tracks, :id => @block.id |
54 | 48 | assert assigns['show_more'] |
55 | 49 | assert_match /track_list_more_#{@block.id}/, @response.body | ... | ... |
plugins/community_track/test/test_helper.rb
1 | 1 | require File.dirname(__FILE__) + '/../../../test/test_helper' |
2 | + | |
3 | +def create_track(name, profile) | |
4 | + track = CommunityTrackPlugin::Track.new(:abstract => 'abstract', :body => 'body', :name => name, :profile => profile) | |
5 | + track.add_category(fast_create(Category)) | |
6 | + track.save! | |
7 | + track | |
8 | +end | ... | ... |
plugins/community_track/test/unit/community_track_plugin/step_test.rb
... | ... | @@ -4,7 +4,11 @@ class StepTest < ActiveSupport::TestCase |
4 | 4 | |
5 | 5 | def setup |
6 | 6 | @profile = fast_create(Community) |
7 | - @track = CommunityTrackPlugin::Track.create(:profile_id => @profile.id, :name => 'track') | |
7 | + @track = CommunityTrackPlugin::Track.new(:profile_id => @profile.id, :name => 'track') | |
8 | + @category = fast_create(Category) | |
9 | + @track.add_category(@category) | |
10 | + @track.save! | |
11 | + | |
8 | 12 | @step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today) |
9 | 13 | Delayed::Job.destroy_all |
10 | 14 | end | ... | ... |
plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb
... | ... | @@ -3,13 +3,14 @@ require File.dirname(__FILE__) + '/../../test_helper' |
3 | 3 | class TrackListBlockTest < ActiveSupport::TestCase |
4 | 4 | |
5 | 5 | def setup |
6 | - @community = fast_create(Community) | |
7 | - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @community) | |
8 | - | |
9 | - box = fast_create(Box, :owner_id => @community.id, :owner_type => @community.class.name) | |
6 | + @profile = fast_create(Community) | |
7 | + @track = create_track('track', profile) | |
8 | + box = fast_create(Box, :owner_id => @profile.id, :owner_type => @profile.class.name) | |
10 | 9 | @block = CommunityTrackPlugin::TrackListBlock.create!(:box => box) |
11 | 10 | end |
12 | 11 | |
12 | + attr_reader :profile | |
13 | + | |
13 | 14 | should 'describe yourself' do |
14 | 15 | assert CommunityTrackPlugin::TrackListBlock.description |
15 | 16 | end |
... | ... | @@ -23,38 +24,30 @@ class TrackListBlockTest < ActiveSupport::TestCase |
23 | 24 | end |
24 | 25 | |
25 | 26 | should 'list articles only of track type' do |
26 | - article = fast_create(Article, :profile_id => @community.id) | |
27 | - assert_includes @community.articles, article | |
27 | + article = fast_create(Article, :profile_id => profile.id) | |
28 | + assert_includes profile.articles, article | |
28 | 29 | assert_equal [@track], @block.tracks |
29 | 30 | end |
30 | 31 | |
31 | 32 | should 'list of articles be limited by block configuration' do |
32 | - (@block.limit + 1).times do |i| | |
33 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
34 | - end | |
33 | + (@block.limit + 1).times { |i| create_track("track#{i}", profile) } | |
35 | 34 | assert_equal @block.limit, @block.tracks.count |
36 | 35 | end |
37 | 36 | |
38 | 37 | should 'return more link if has more tracks to show' do |
39 | - @block.limit.times do |i| | |
40 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
41 | - end | |
38 | + @block.limit.times { |i| create_track("track#{i}", profile) } | |
42 | 39 | assert @block.footer |
43 | 40 | end |
44 | 41 | |
45 | 42 | should 'do not return more link if there is no more tracks to show' do |
46 | - (@block.limit-1).times do |i| | |
47 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
48 | - end | |
43 | + (@block.limit-1).times { |i| create_track("track#{i}", profile) } | |
49 | 44 | assert !@block.footer |
50 | 45 | end |
51 | 46 | |
52 | 47 | should 'count all tracks' do |
53 | 48 | @block.owner.articles.destroy_all |
54 | 49 | tracks_to_insert = @block.limit + 1 |
55 | - tracks_to_insert.times do |i| | |
56 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
57 | - end | |
50 | + tracks_to_insert.times { |i| create_track("track#{i}", profile) } | |
58 | 51 | article = fast_create(Article, :profile_id => @block.owner.id) |
59 | 52 | @block.reload |
60 | 53 | assert_includes @block.owner.articles, article |
... | ... | @@ -63,9 +56,7 @@ class TrackListBlockTest < ActiveSupport::TestCase |
63 | 56 | |
64 | 57 | should 'have a second page if there is more tracks than limit' do |
65 | 58 | @block.owner.articles.destroy_all |
66 | - (@block.limit+1).times do |i| | |
67 | - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) | |
68 | - end | |
59 | + (@block.limit+1).times { |i| create_track("track#{i}", profile) } | |
69 | 60 | assert @block.has_page?(2) |
70 | 61 | assert !@block.has_page?(3) |
71 | 62 | end |
... | ... | @@ -74,9 +65,9 @@ class TrackListBlockTest < ActiveSupport::TestCase |
74 | 65 | @block.owner.articles.destroy_all |
75 | 66 | category = fast_create(Category) |
76 | 67 | category2 = fast_create(Category) |
77 | - track1 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track1', :profile => @community) | |
78 | - track2 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track2', :profile => @community) | |
79 | - track3 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track3', :profile => @community) | |
68 | + track1 = create_track("track1", profile) | |
69 | + track2 = create_track("track2", profile) | |
70 | + track3 = create_track("track3", profile) | |
80 | 71 | track1.add_category(category) |
81 | 72 | @block.category_ids = [category.id] |
82 | 73 | assert_equal [track1], @block.all_tracks |
... | ... | @@ -85,8 +76,8 @@ class TrackListBlockTest < ActiveSupport::TestCase |
85 | 76 | should 'return all tracks if block does not filter by category' do |
86 | 77 | @block.owner.articles.destroy_all |
87 | 78 | category = fast_create(Category) |
88 | - track1 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track1', :profile => @community) | |
89 | - track2 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track2', :profile => @community) | |
79 | + track1 = create_track("track1", profile) | |
80 | + track2 = create_track("track2", profile) | |
90 | 81 | track1.add_category(category) |
91 | 82 | assert_includes @block.all_tracks, track1 |
92 | 83 | assert_includes @block.all_tracks, track2 | ... | ... |
plugins/community_track/test/unit/community_track_plugin/track_test.rb
... | ... | @@ -3,10 +3,10 @@ require File.dirname(__FILE__) + '/../../test_helper' |
3 | 3 | class TrackTest < ActiveSupport::TestCase |
4 | 4 | |
5 | 5 | def setup |
6 | - profile = fast_create(Community) | |
7 | - @track = CommunityTrackPlugin::Track.create!(:profile => profile, :name => 'track') | |
8 | - @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => profile) | |
9 | - @tool = fast_create(Article, :parent_id => @step.id, :profile_id => profile.id) | |
6 | + @profile = fast_create(Community) | |
7 | + @track = create_track('track', @profile) | |
8 | + @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => @profile) | |
9 | + @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id) | |
10 | 10 | end |
11 | 11 | |
12 | 12 | should 'describe yourself' do |
... | ... | @@ -43,6 +43,7 @@ class TrackTest < ActiveSupport::TestCase |
43 | 43 | top = fast_create(Category, :name => 'top category') |
44 | 44 | category1 = fast_create(Category, :name => 'category1', :parent_id => top.id ) |
45 | 45 | category2 = fast_create(Category, :name => 'category2', :parent_id => category1.id ) |
46 | + @track.categories.delete_all | |
46 | 47 | @track.add_category(category2, true) |
47 | 48 | assert_equal 'top category', @track.category_name |
48 | 49 | end |
... | ... | @@ -54,6 +55,7 @@ class TrackTest < ActiveSupport::TestCase |
54 | 55 | |
55 | 56 | should 'return category name of first category' do |
56 | 57 | category = fast_create(Category, :name => 'category') |
58 | + @track.categories.delete_all | |
57 | 59 | @track.add_category(category, true) |
58 | 60 | category2 = fast_create(Category, :name => 'category2') |
59 | 61 | @track.add_category(category2, true) |
... | ... | @@ -126,4 +128,15 @@ class TrackTest < ActiveSupport::TestCase |
126 | 128 | assert_equal '', @track.first_paragraph |
127 | 129 | end |
128 | 130 | |
131 | + should 'not be able to create a track without category' do | |
132 | + track = CommunityTrackPlugin::Track.create(:profile => @profile, :name => 'track') | |
133 | + assert track.errors.invalid?(:categories) | |
134 | + end | |
135 | + | |
136 | + should 'not be able to save a track without category' do | |
137 | + @track.categories.delete_all | |
138 | + @track.save | |
139 | + assert @track.errors.invalid?(:categories) | |
140 | + end | |
141 | + | |
129 | 142 | end | ... | ... |