Repository: wagtail/wagtail

Issue:
Workflow - Disable Tasks warning shows an incorrect 'task is in progress on X pages' value
### Issue Summary

When disabling a Workflow Task, there is a warning that says `This task is in progress on # pages.`, however the number of pages is incorrect and appears to reflect the tasks in progress across ALL pages.


### Steps to Reproduce

1. In an existing Wagtail project (does not have to have custom Workflow Tasks)
2. Sign in as an admin user
3. Check you have at least one Workflow in progress (you can check this by viewing the home page), if not, start at least one.
3. On the sidebar click 'Settings' and then 'Workflow tasks'
4. Create a new workflow task (button at the top right)
5. Name this anything (e.g. 'Disable Me')
6. Do not use this Task in any Workflow but instead go straight to editing it
7. Now attempt to Disable this task
8. Expected: This page should not have a warning about any Task in progress
9. Actual: This page will show that the task is in progress on some pages

**Option** Maybe the warning message should not show at all when there are zero in progress, but the disable page itself should probably show to confirm the action.

<img width="1004" alt="Screen Shot 2021-09-03 at 6 45 02 am" src="https://user-images.githubusercontent.com/1396140/131924440-576c8f17-a5c1-40b2-b8d1-7ae20b615227.png">


### Technical details

* Python version: 3.6.9
* Django version: 3.2.6
* Wagtail version: 2.15 (alpha)
* Browser version: Firefox 91

### Potential root cause

The code below is what is used for Disabling a Task (view)
[wagtail/admin/views/workflows.py](https://github.com/wagtail/wagtail/blob/main/wagtail/admin/views/workflows.py#L447)
`states_in_progress = TaskState.objects.filter(status=TaskState.STATUS_IN_PROGRESS).count()`

Replacing that line with
`states_in_progress = TaskState.objects.filter(status=TaskState.STATUS_IN_PROGRESS, task=self.get_object().pk).count()`

Seems to fix it.




Discussion:

Resolving patch:
diff --git a/wagtail/admin/views/workflows.py b/wagtail/admin/views/workflows.py
--- a/wagtail/admin/views/workflows.py
+++ b/wagtail/admin/views/workflows.py
@@ -444,7 +444,7 @@ class DisableTask(DeleteView):
 
     def get_context_data(self, **kwargs):
         context = super().get_context_data(**kwargs)
-        states_in_progress = TaskState.objects.filter(status=TaskState.STATUS_IN_PROGRESS).count()
+        states_in_progress = TaskState.objects.filter(status=TaskState.STATUS_IN_PROGRESS, task=self.get_object().pk).count()
         context['warning_message'] = ngettext(
             'This task is in progress on %(states_in_progress)d page. Disabling this task will cause it to be skipped in the moderation workflow.',
             'This task is in progress on %(states_in_progress)d pages. Disabling this task will cause it to be skipped in the moderation workflow.',


Test patch:
diff --git a/wagtail/admin/tests/test_workflows.py b/wagtail/admin/tests/test_workflows.py
--- a/wagtail/admin/tests/test_workflows.py
+++ b/wagtail/admin/tests/test_workflows.py
@@ -1508,6 +1508,27 @@ def test_disable_workflow(self):
 
         self.assertEqual(TaskState.objects.filter(workflow_state__workflow=self.workflow, status=TaskState.STATUS_IN_PROGRESS).count(), 0)
 
+    def test_disable_task_view(self):
+        """Test that a view is shown before disabling a task that shows a warning"""
+        self.login(self.submitter)
+        self.submit()
+        self.login(self.superuser)
+
+        response = self.client.get(reverse('wagtailadmin_workflows:disable_task', args=(self.task_1.pk,)))
+
+        self.assertTemplateUsed(response, "wagtailadmin/workflows/confirm_disable_task.html")
+        self.assertEqual(response.context['warning_message'], "This task is in progress on 1 page. Disabling this task will cause it to be skipped in the moderation workflow.")
+
+        # create a new, unused, task and check the warning message is accurate
+        unused_task = GroupApprovalTask.objects.create(name='unused_task_3')
+        unused_task.groups.set(Group.objects.filter(name='Moderators'))
+
+        response = self.client.get(reverse('wagtailadmin_workflows:disable_task', args=(unused_task.pk,)))
+
+        self.assertEqual(response.context['warning_message'], "This task is in progress on 0 pages. Disabling this task will cause it to be skipped in the moderation workflow.")
+
+        unused_task.delete()  # clean up
+
     def test_disable_task(self):
         """Test that deactivating a task sets it to inactive and cancels in progress states"""
         self.login(self.submitter)