diff --git a/lib/wiki_assignment_output.rb b/lib/wiki_assignment_output.rb index 11c612aee6..d48dc31ae4 100644 --- a/lib/wiki_assignment_output.rb +++ b/lib/wiki_assignment_output.rb @@ -39,11 +39,13 @@ def build_talk_page_update # Do not post templates to disambugation pages return nil if includes_disambiguation_template?(initial_page_content) - # We only want to add assignment tags to non-existant talk pages if the + # We only want to add assignment tags to non-existent talk pages if the # article page actually exists, and is not a disambiguation page. - article_content = WikiApi.new(@wiki).get_page_content(@title) - return nil if article_content.blank? - return nil if includes_disambiguation_template?(article_content) + return nil unless normal_article? + + # We only want to remove assignments if talk page already exists. + # This is to avoid creating a new empty talk page. + return nil if @assignments.empty? && initial_page_content.empty? page_content = build_assignment_page_content(assignments_tag, initial_page_content) page_content @@ -52,6 +54,12 @@ def build_talk_page_update ################### # Helper methods # ################### + # Normal article means the article page actually exists, and is not a disambiguation page. + def normal_article? + article_content = WikiApi.new(@wiki).get_page_content(@title) + article_content.present? && !includes_disambiguation_template?(article_content) + end + def assignments_tag return '' if @assignments.empty? @@ -84,7 +92,7 @@ def assignments_tag def build_assignment_page_content(new_tag, page_content) page_content = page_content.dup.force_encoding('utf-8') # Return if tag already exists on page. - # However, if the tag is empty, that means to blank the prior tag (if any).z + # However, if the tag is empty, that means to blank the prior tag (if any). if new_tag.present? return nil if page_content.include? new_tag end diff --git a/spec/lib/wiki_assignment_output_spec.rb b/spec/lib/wiki_assignment_output_spec.rb index a8b0832a80..feceb5236f 100644 --- a/spec/lib/wiki_assignment_output_spec.rb +++ b/spec/lib/wiki_assignment_output_spec.rb @@ -264,13 +264,26 @@ let(:title) { 'Selfie' } let(:talk_title) { 'Talk:Selfie' } - context 'when the article exists but talk page does not' do + context 'when the article exists but talk page does not,' do let(:talk_title) { 'Talk:THIS PAGE DOES NOT EXIST' } - it 'returns content' do - VCR.use_cassette 'wiki_edits/talk_page_update' do - page_content = wiki_assignment_output.build_talk_page_update - expect(page_content).to include('{{dashboard.wikiedu.org assignment | course = ') + context 'adding or updating assignments' do + it 'returns content' do + VCR.use_cassette 'wiki_edits/talk_page_update' do + page_content = wiki_assignment_output.build_talk_page_update + expect(page_content).to include('{{dashboard.wikiedu.org assignment | course = ') + end + end + end + + context 'trying to remove assignments' do + let(:assignments) { [] } + + it 'returns nil' do + VCR.use_cassette 'wiki_edits/talk_page_update' do + page_content = wiki_assignment_output.build_talk_page_update + expect(page_content).to be_nil + end end end end