diff --git a/lib/tasks/reporting.rake b/lib/tasks/reporting.rake index 1d355be82f3..71136f9646a 100644 --- a/lib/tasks/reporting.rake +++ b/lib/tasks/reporting.rake @@ -12,4 +12,15 @@ namespace :reporting do task published_attachments_report: :environment do Reports::PublishedAttachmentsReport.new.report end + + desc "Prints a list of content IDs that documents whose live edition contains a given regular expression" + task :matching_docs, [:regex] => :environment do |_, args| + regex = Regexp.new(/#{args[:regex]}/) + + Document.where.not(live_edition_id: nil).find_in_batches(batch_size: 1000).each do |document| + next unless document.editions.published.any? + + puts document.content_id if regex.match?(document.editions.published.last.body) + end + end end diff --git a/test/unit/lib/tasks/reporting_test.rb b/test/unit/lib/tasks/reporting_test.rb new file mode 100644 index 00000000000..88e3a65b7b3 --- /dev/null +++ b/test/unit/lib/tasks/reporting_test.rb @@ -0,0 +1,26 @@ +require "test_helper" +require "rake" + +class ReportingRake < ActiveSupport::TestCase + setup do + @document_1 = create(:published_edition, body: "Some text 1") + @document_2 = create(:draft_edition, body: "Some text 2") + @document_3 = create(:published_edition, body: "Some other text 1") + end + + teardown do + Rake::Task["reporting:matching_docs"].reenable + end + + test "it prints the content IDs of the matching documents from published editions" do + assert_output(/#{@document_1.document.content_id}/) { Rake.application.invoke_task "reporting:matching_docs[Some text]" } + end + + test "it does not print the content IDs of the matching documents from draft editions" do + assert_output(/^(?!.*#{@document_2.document.content_id}).*$/) { Rake.application.invoke_task "reporting:matching_docs[Some text]" } + end + + test "it does not print the content IDs of the non-matching documents from published editions" do + assert_output(/^(?!.*#{@document_3.document.content_id}).*$/) { Rake.application.invoke_task "reporting:matching_docs[Some text]" } + end +end