diff --git a/lib/tasks/election.rake b/lib/tasks/election.rake index 719d1e2781c..590c09084ab 100644 --- a/lib/tasks/election.rake +++ b/lib/tasks/election.rake @@ -59,4 +59,34 @@ namespace :election do puts e.message end end + + desc " + Mark all documents of a given organisation as political + Usage: + rake election:mark_documents_as_political_for[organisation_slug, '30-12-2024'] + " + task :mark_documents_as_political_for, %i[slug date] => :environment do |_t, args| + date = Date.parse(args[:date]) + org = Organisation.find_by!(slug: args[:slug]) + puts "Marking all documents as political for #{org.name}..." + + editions = org.editions + published = editions + .published + .where("first_published_at >= ?", date) + + puts "Marking #{published.size} published editions as political..." + published.update_all(political: true) + + pre_published_editions = Edition.in_pre_publication_state.where(document_id: published.map(&:document_id)) + + puts "Updating #{pre_published_editions.size} pre-publication editions as political..." + pre_published_editions.update_all(political: true) + + puts "Done" + rescue Date::Error => _e + puts "The date is not on the right format [\"#{args[:date]}\"]" + rescue ActiveRecord::RecordNotFound => _e + puts "There is no Organisation with slug [\"#{args[:slug]}\"]" + end end diff --git a/test/unit/lib/tasks/election_mark_organisation_documents_as_political_test.rb b/test/unit/lib/tasks/election_mark_organisation_documents_as_political_test.rb new file mode 100644 index 00000000000..810fde92779 --- /dev/null +++ b/test/unit/lib/tasks/election_mark_organisation_documents_as_political_test.rb @@ -0,0 +1,47 @@ +require "test_helper" +require "rake" + +class MarkDocumentsAsPoliticalFor < ActiveSupport::TestCase + extend Minitest::Spec::DSL + + teardown { task.reenable } + + describe "#mark_as_political" do + let(:task) { Rake::Task["election:mark_documents_as_political_for"] } + let(:organisation) { create(:organisation) } + let(:document) { create(:document) } + let(:published_edition) { create(:edition, :published, document:, first_published_at: "01-01-2023") } + let(:draft_edition) { create(:edition, :draft, document:) } + let(:date) { "31-12-2022" } + + setup do + organisation.editions = [published_edition, draft_edition] + organisation.save! + end + + it "raises an error if organisation does not exist" do + out, _err = capture_io { task.invoke("non-existent-slug", date) } + assert_equal 'There is no Organisation with slug ["non-existent-slug"]', out.strip + end + + it "raises an error if date is not right format" do + out, _err = capture_io { task.invoke(organisation.slug, "not a date") } + assert_equal 'The date is not on the right format ["not a date"]', out.strip + end + + it "marks published editions of documents first published after the specified date and any associated drafts as political" do + capture_io { task.invoke(organisation.slug, date) } + assert published_edition.reload.political + assert draft_edition.reload.political + end + + it "does not mark editions unrelated to documents first published before the specified date as political" do + non_political_document = create(:document) + create(:edition, :published, document: non_political_document, first_published_at: "30-12-2022") + create(:edition, :draft, document: non_political_document) + capture_io { task.invoke(organisation.slug, date) } + assert_not non_political_document.reload.live_edition.political + assert_not non_political_document.reload.latest_edition.political + end + end +end