From 9658e94a769663176bf5cd8e84a11604d5d4d431 Mon Sep 17 00:00:00 2001 From: gabina Date: Sat, 21 Oct 2023 23:54:52 -0300 Subject: [PATCH 1/3] Start handing the specific situation of trying to remove a template from a page that doesn't exist in WikiAssignmentOutput, and add a spec for the new behavior. --- lib/wiki_assignment_output.rb | 15 +++++++++++++-- spec/lib/wiki_assignment_output_spec.rb | 23 ++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/wiki_assignment_output.rb b/lib/wiki_assignment_output.rb index 11c612aee6..e1ec97bc00 100644 --- a/lib/wiki_assignment_output.rb +++ b/lib/wiki_assignment_output.rb @@ -39,12 +39,16 @@ 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) + # Empty initial page content implies non-existent page. + talk_page_does_not_exist = initial_page_content.empty? + prevent_removal_of_assignments_from_non_existent_talk_pages(talk_page_does_not_exist) { return } + page_content = build_assignment_page_content(assignments_tag, initial_page_content) page_content end @@ -52,6 +56,13 @@ def build_talk_page_update ################### # Helper methods # ################### + # This is to avoid creating a new empty talk page. + def prevent_removal_of_assignments_from_non_existent_talk_pages(talk_page_does_not_exist) + # If assignments are empty, that means to remove assignments + return unless @assignments.empty? && talk_page_does_not_exist + yield + end + def assignments_tag return '' if @assignments.empty? @@ -84,7 +95,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..fdc9e52b28 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 From 170cc85b0878c5dd02e6a6a014d1a2d115685dcb Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 22 Oct 2023 00:17:22 -0300 Subject: [PATCH 2/3] Correct the new spec with the correct empty value for assignments. --- spec/lib/wiki_assignment_output_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/wiki_assignment_output_spec.rb b/spec/lib/wiki_assignment_output_spec.rb index fdc9e52b28..feceb5236f 100644 --- a/spec/lib/wiki_assignment_output_spec.rb +++ b/spec/lib/wiki_assignment_output_spec.rb @@ -277,7 +277,7 @@ end context 'trying to remove assignments' do - let(:assignments) { '' } + let(:assignments) { [] } it 'returns nil' do VCR.use_cassette 'wiki_edits/talk_page_update' do From 89a350151b55f6cf54f4ba8718edf6c5ecd0c145 Mon Sep 17 00:00:00 2001 From: gabina Date: Mon, 23 Oct 2023 17:46:51 -0300 Subject: [PATCH 3/3] Address suggested changes --- lib/wiki_assignment_output.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/wiki_assignment_output.rb b/lib/wiki_assignment_output.rb index e1ec97bc00..d48dc31ae4 100644 --- a/lib/wiki_assignment_output.rb +++ b/lib/wiki_assignment_output.rb @@ -41,13 +41,11 @@ def build_talk_page_update # 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? - # Empty initial page content implies non-existent page. - talk_page_does_not_exist = initial_page_content.empty? - prevent_removal_of_assignments_from_non_existent_talk_pages(talk_page_does_not_exist) { return } + # 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 @@ -56,11 +54,10 @@ def build_talk_page_update ################### # Helper methods # ################### - # This is to avoid creating a new empty talk page. - def prevent_removal_of_assignments_from_non_existent_talk_pages(talk_page_does_not_exist) - # If assignments are empty, that means to remove assignments - return unless @assignments.empty? && talk_page_does_not_exist - yield + # 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