diff --git a/app/controllers/maintenance_tasks/application_controller.rb b/app/controllers/maintenance_tasks/application_controller.rb index 0140cd497..16486055e 100644 --- a/app/controllers/maintenance_tasks/application_controller.rb +++ b/app/controllers/maintenance_tasks/application_controller.rb @@ -20,6 +20,8 @@ class ApplicationController < MaintenanceTasks.parent_controller.constantize policy.script_src_elem( # +<% end %> diff --git a/config/routes.rb b/config/routes.rb index be607aa9f..b4031c576 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,9 @@ MaintenanceTasks::Engine.routes.draw do resources :tasks, only: [:index, :show], format: false do + member do + get :count + end resources :runs, only: [:create], format: false do member do post "pause" diff --git a/test/models/maintenance_tasks/task_data_show_test.rb b/test/models/maintenance_tasks/task_data_show_test.rb index 58645b46e..c42a5cf8c 100644 --- a/test/models/maintenance_tasks/task_data_show_test.rb +++ b/test/models/maintenance_tasks/task_data_show_test.rb @@ -91,6 +91,18 @@ class TaskDataShowTest < ActiveSupport::TestCase refute_predicate TaskDataShow.new("Maintenance::DoesNotExist"), :csv_task? end + test "#no_collection? returns true for a no-collection Task" do + assert_predicate TaskDataShow.new("Maintenance::NoCollectionTask"), :no_collection? + end + + test "#no_collection? returns false for a Task with a collection" do + refute_predicate TaskDataShow.new("Maintenance::UpdatePostsTask"), :no_collection? + end + + test "#no_collection? returns false if the Task is deleted" do + refute_predicate TaskDataShow.new("Maintenance::DoesNotExist"), :no_collection? + end + test "#refresh? returns true if there are active runs" do assert_predicate TaskDataShow.new("Maintenance::UpdatePostsTask"), :refresh? end @@ -153,5 +165,54 @@ class TaskDataShowTest < ActiveSupport::TestCase task_data = TaskDataShow.prepare("Maintenance::ParamsTask", arguments: { unknown: nil }) assert_nothing_raised { task_data.new } end + + test "#count returns the count for a Task with a count override" do + task_data = TaskDataShow.new("Maintenance::UpdatePostsThrottledTask") + assert_equal Post.count, task_data.count + end + + test "#count returns the count for a custom enumerating Task" do + task_data = TaskDataShow.new("Maintenance::CustomEnumeratingTask") + assert_equal 3, task_data.count + end + + test "#count falls back to collection count for a Task without a count override" do + task_data = TaskDataShow.new("Maintenance::TestTask") + assert_equal 2, task_data.count + end + + test "#count returns nil when collection count errors" do + task_data = TaskDataShow.new("Maintenance::ParamsTask") + assert_nil task_data.count + end + + test "#count returns the count for a no-collection Task" do + task_data = TaskDataShow.new("Maintenance::NoCollectionTask") + assert_equal 1, task_data.count + end + + test "#count returns nil for a CSV Task" do + task_data = TaskDataShow.new("Maintenance::ImportPostsTask") + assert_nil task_data.count + end + + test "#count returns nil for a deleted Task" do + task_data = TaskDataShow.new("Maintenance::DoesNotExist") + assert_nil task_data.count + end + + test "#count returns nil when collection count times out" do + task_data = TaskDataShow.new("Maintenance::UpdatePostsThrottledTask") + Task.named("Maintenance::UpdatePostsThrottledTask").any_instance.stubs(:count).raises(Timeout::Error) + assert_nil task_data.count + end + + test "#count returns the count for a Task with arguments" do + task_data = TaskDataShow.new( + "Maintenance::ParamsTask", + arguments: { post_ids: Post.first.id.to_s }, + ) + assert_equal 1, task_data.count + end end end diff --git a/test/system/maintenance_tasks/tasks_test.rb b/test/system/maintenance_tasks/tasks_test.rb index 3de7bd89e..4f8e8baff 100644 --- a/test/system/maintenance_tasks/tasks_test.rb +++ b/test/system/maintenance_tasks/tasks_test.rb @@ -258,6 +258,32 @@ class TasksTest < ApplicationSystemTestCase assert_button "Run", disabled: true end + test "show a Task with count indicator" do + visit maintenance_tasks.task_path("Maintenance::UpdatePostsThrottledTask") + assert_text "items expected to be processed" + end + + test "count updates dynamically when params change" do + visit maintenance_tasks.task_path("Maintenance::ParamsTask") + assert_selector "#task-count:not(.is-hidden)" + + fill_in "task[post_ids]", with: Post.first.id.to_s + find_field("task[post_ids]").send_keys(:tab) + within("#task-count") do + assert_text "1 item expected to be processed" + end + end + + test "show a Task without count indicator" do + visit maintenance_tasks.task_path("Maintenance::ImportPostsTask") + assert_no_text "expected to be processed" + end + + test "show a no-collection Task without count indicator" do + visit maintenance_tasks.task_path("Maintenance::NoCollectionTask") + assert_no_selector "#task-count" + end + test "visit main page through iframe" do visit root_path