diff --git a/plugins/community_track/lib/community_track_plugin/track.rb b/plugins/community_track/lib/community_track_plugin/track.rb index c267bdf..2f2f1ea 100644 --- a/plugins/community_track/lib/community_track_plugin/track.rb +++ b/plugins/community_track/lib/community_track_plugin/track.rb @@ -3,6 +3,12 @@ class CommunityTrackPlugin::Track < Folder settings_items :goals, :type => :string settings_items :expected_results, :type => :string + validate :validate_categories + + def validate_categories + errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank? + end + def self.icon_name(article = nil) 'community-track' end diff --git a/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb index 8ae47d3..ab48f9d 100644 --- a/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb +++ b/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb @@ -7,7 +7,7 @@ class CmsControllerTest < ActionController::TestCase def setup @profile = fast_create(Community) - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @profile) + @track = create_track('track', @profile) @step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today) user = create_user('testinguser') diff --git a/plugins/community_track/test/functional/community_track_plugin_content_viewer_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_content_viewer_controller_test.rb index 5ea9fec..42c4752 100644 --- a/plugins/community_track/test/functional/community_track_plugin_content_viewer_controller_test.rb +++ b/plugins/community_track/test/functional/community_track_plugin_content_viewer_controller_test.rb @@ -11,10 +11,7 @@ class ContentViewerControllerTest < ActionController::TestCase def setup @profile = fast_create(Community) - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @profile) - category = fast_create(Category, :name => "education") - @track.add_category(category) - + @track = create_track('track', @profile) @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) user = create_user('testinguser') @@ -110,7 +107,7 @@ class ContentViewerControllerTest < ActionController::TestCase @block = CommunityTrackPlugin::TrackListBlock.create!(:box => box) @profile.boxes << box get :view_page, @step.url - 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)}" } } } + 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)}" } } } end should 'render tracks in track card list block' do @@ -118,8 +115,8 @@ class ContentViewerControllerTest < ActionController::TestCase @block = CommunityTrackPlugin::TrackCardListBlock.create!(:box => box) @profile.boxes << box get :view_page, @step.url - assert_tag :tag => 'div', :attributes => { :class => 'item_card category_education' }, :descendant => { :tag => 'div', :attributes => { :class => 'track_content' } } - assert_tag :tag => 'div', :attributes => { :class => 'item_card category_education' }, :descendant => { :tag => 'div', :attributes => { :class => 'track_stats' } } + assert_tag :tag => 'div', :attributes => { :class => "item_card category_#{@track.category_name}" }, :descendant => { :tag => 'div', :attributes => { :class => 'track_content' } } + assert_tag :tag => 'div', :attributes => { :class => "item_card category_#{@track.category_name}" }, :descendant => { :tag => 'div', :attributes => { :class => 'track_stats' } } end should 'render link to display more tracks in track list block' do @@ -127,9 +124,7 @@ class ContentViewerControllerTest < ActionController::TestCase @block = CommunityTrackPlugin::TrackCardListBlock.create!(:box => box) @profile.boxes << box - (@block.limit+1).times do |i| - CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @profile) - end + (@block.limit+1).times { |i| create_track("track#{i}", @profile) } get :view_page, @step.url 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 @block.more_another_page = true @block.save! - (@block.limit+1).times do |i| - CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @profile) - end + (@block.limit+1).times { |i| create_track("track#{i}", @profile) } get :view_page, @step.url assert_tag :tag => 'div', :attributes => { :id => "track_list_more_#{@block.id}" }, :descendant => { :tag => 'div', :attributes => { :class => 'view_all' } } diff --git a/plugins/community_track/test/functional/community_track_plugin_myprofile_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_myprofile_controller_test.rb index a3a2887..faa5cde 100644 --- a/plugins/community_track/test/functional/community_track_plugin_myprofile_controller_test.rb +++ b/plugins/community_track/test/functional/community_track_plugin_myprofile_controller_test.rb @@ -12,7 +12,7 @@ class CommunityTrackPluginMyprofileControllerTest < ActionController::TestCase @response = ActionController::TestResponse.new @profile = fast_create(Community) - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @profile) + @track = create_track('track', @profile) @user = create_user('testinguser') login_as(@user.login) diff --git a/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb index 63660bb..18e9359 100644 --- a/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb +++ b/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb @@ -8,7 +8,7 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase def setup @community = fast_create(Community) - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @community) + @track = create_track('track', @community) box = fast_create(Box, :owner_id => @community.id, :owner_type => 'Community') @card_block = CommunityTrackPlugin::TrackCardListBlock.create!(:box => box) @@ -26,17 +26,13 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase end should 'display tracks with page size' do - 20.times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + 20.times { |i| create_track("track#{i}", @community) } xhr :get, :view_tracks, :id => @block.id, :page => 1, :per_page => 10 assert_equal 10, @response.body.scan(/item/).size end should 'default page size is the block limit' do - 20.times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + 20.times { |i| create_track("track#{i}", @community) } xhr :get, :view_tracks, :id => @block.id, :page => 1 assert_equal @block.limit, @response.body.scan(/item/).size end @@ -47,9 +43,7 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase end should 'show more link in all tracks if there is no more tracks to show' do - 10.times do |i| - CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + 10.times { |i| create_track("track#{i}", @community) } get :all_tracks, :id => @block.id assert assigns['show_more'] assert_match /track_list_more_#{@block.id}/, @response.body diff --git a/plugins/community_track/test/test_helper.rb b/plugins/community_track/test/test_helper.rb index cca1fd3..8e64f83 100644 --- a/plugins/community_track/test/test_helper.rb +++ b/plugins/community_track/test/test_helper.rb @@ -1 +1,8 @@ require File.dirname(__FILE__) + '/../../../test/test_helper' + +def create_track(name, profile) + track = CommunityTrackPlugin::Track.new(:abstract => 'abstract', :body => 'body', :name => name, :profile => profile) + track.add_category(fast_create(Category)) + track.save! + track +end diff --git a/plugins/community_track/test/unit/community_track_plugin/step_test.rb b/plugins/community_track/test/unit/community_track_plugin/step_test.rb index 376234f..cece41b 100644 --- a/plugins/community_track/test/unit/community_track_plugin/step_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/step_test.rb @@ -4,7 +4,11 @@ class StepTest < ActiveSupport::TestCase def setup @profile = fast_create(Community) - @track = CommunityTrackPlugin::Track.create(:profile_id => @profile.id, :name => 'track') + @track = CommunityTrackPlugin::Track.new(:profile_id => @profile.id, :name => 'track') + @category = fast_create(Category) + @track.add_category(@category) + @track.save! + @step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today) Delayed::Job.destroy_all end diff --git a/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb index 1135564..2e716e4 100644 --- a/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb @@ -3,13 +3,14 @@ require File.dirname(__FILE__) + '/../../test_helper' class TrackListBlockTest < ActiveSupport::TestCase def setup - @community = fast_create(Community) - @track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track', :profile => @community) - - box = fast_create(Box, :owner_id => @community.id, :owner_type => @community.class.name) + @profile = fast_create(Community) + @track = create_track('track', profile) + box = fast_create(Box, :owner_id => @profile.id, :owner_type => @profile.class.name) @block = CommunityTrackPlugin::TrackListBlock.create!(:box => box) end + attr_reader :profile + should 'describe yourself' do assert CommunityTrackPlugin::TrackListBlock.description end @@ -23,38 +24,30 @@ class TrackListBlockTest < ActiveSupport::TestCase end should 'list articles only of track type' do - article = fast_create(Article, :profile_id => @community.id) - assert_includes @community.articles, article + article = fast_create(Article, :profile_id => profile.id) + assert_includes profile.articles, article assert_equal [@track], @block.tracks end should 'list of articles be limited by block configuration' do - (@block.limit + 1).times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + (@block.limit + 1).times { |i| create_track("track#{i}", profile) } assert_equal @block.limit, @block.tracks.count end should 'return more link if has more tracks to show' do - @block.limit.times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + @block.limit.times { |i| create_track("track#{i}", profile) } assert @block.footer end should 'do not return more link if there is no more tracks to show' do - (@block.limit-1).times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + (@block.limit-1).times { |i| create_track("track#{i}", profile) } assert !@block.footer end should 'count all tracks' do @block.owner.articles.destroy_all tracks_to_insert = @block.limit + 1 - tracks_to_insert.times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + tracks_to_insert.times { |i| create_track("track#{i}", profile) } article = fast_create(Article, :profile_id => @block.owner.id) @block.reload assert_includes @block.owner.articles, article @@ -63,9 +56,7 @@ class TrackListBlockTest < ActiveSupport::TestCase should 'have a second page if there is more tracks than limit' do @block.owner.articles.destroy_all - (@block.limit+1).times do |i| - track = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => "track#{i}", :profile => @community) - end + (@block.limit+1).times { |i| create_track("track#{i}", profile) } assert @block.has_page?(2) assert !@block.has_page?(3) end @@ -74,9 +65,9 @@ class TrackListBlockTest < ActiveSupport::TestCase @block.owner.articles.destroy_all category = fast_create(Category) category2 = fast_create(Category) - track1 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track1', :profile => @community) - track2 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track2', :profile => @community) - track3 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track3', :profile => @community) + track1 = create_track("track1", profile) + track2 = create_track("track2", profile) + track3 = create_track("track3", profile) track1.add_category(category) @block.category_ids = [category.id] assert_equal [track1], @block.all_tracks @@ -85,8 +76,8 @@ class TrackListBlockTest < ActiveSupport::TestCase should 'return all tracks if block does not filter by category' do @block.owner.articles.destroy_all category = fast_create(Category) - track1 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track1', :profile => @community) - track2 = CommunityTrackPlugin::Track.create!(:abstract => 'abstract', :body => 'body', :name => 'track2', :profile => @community) + track1 = create_track("track1", profile) + track2 = create_track("track2", profile) track1.add_category(category) assert_includes @block.all_tracks, track1 assert_includes @block.all_tracks, track2 diff --git a/plugins/community_track/test/unit/community_track_plugin/track_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_test.rb index c323e8c..d939824 100644 --- a/plugins/community_track/test/unit/community_track_plugin/track_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/track_test.rb @@ -3,10 +3,10 @@ require File.dirname(__FILE__) + '/../../test_helper' class TrackTest < ActiveSupport::TestCase def setup - profile = fast_create(Community) - @track = CommunityTrackPlugin::Track.create!(:profile => profile, :name => 'track') - @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => profile) - @tool = fast_create(Article, :parent_id => @step.id, :profile_id => profile.id) + @profile = fast_create(Community) + @track = create_track('track', @profile) + @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => @profile) + @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id) end should 'describe yourself' do @@ -43,6 +43,7 @@ class TrackTest < ActiveSupport::TestCase top = fast_create(Category, :name => 'top category') category1 = fast_create(Category, :name => 'category1', :parent_id => top.id ) category2 = fast_create(Category, :name => 'category2', :parent_id => category1.id ) + @track.categories.delete_all @track.add_category(category2, true) assert_equal 'top category', @track.category_name end @@ -54,6 +55,7 @@ class TrackTest < ActiveSupport::TestCase should 'return category name of first category' do category = fast_create(Category, :name => 'category') + @track.categories.delete_all @track.add_category(category, true) category2 = fast_create(Category, :name => 'category2') @track.add_category(category2, true) @@ -126,4 +128,15 @@ class TrackTest < ActiveSupport::TestCase assert_equal '', @track.first_paragraph end + should 'not be able to create a track without category' do + track = CommunityTrackPlugin::Track.create(:profile => @profile, :name => 'track') + assert track.errors.invalid?(:categories) + end + + should 'not be able to save a track without category' do + @track.categories.delete_all + @track.save + assert @track.errors.invalid?(:categories) + end + end -- libgit2 0.21.2