From 1e02530134a59365fd59fea7676c013a113c6d25 Mon Sep 17 00:00:00 2001
From: Konstantinos Samaras-Tsakiris <ksamtsak@gmail.com>
Date: Thu, 17 Mar 2022 13:53:13 +0100
Subject: [PATCH] Add scheduler patch

---
 composer.json                                 |   3 +
 .../scheduler-view-dependency-3224340.patch   | 118 ++++++++++++++++++
 2 files changed, 121 insertions(+)
 create mode 100644 patches/scheduler-view-dependency-3224340.patch

diff --git a/composer.json b/composer.json
index 69f7b68c9..25f52911a 100644
--- a/composer.json
+++ b/composer.json
@@ -305,6 +305,9 @@
       "drupal/captcha": {
         "Fixes CAPTCHAs not loading due to not being able to access font directory https://www.drupal.org/project/captcha/issues/3211385": "patches/captcha-white-image-captcha-3211385-12.patch"
       },
+      "drupal/scheduler": {
+        "Hardcoded local task dependency on view.scheduler_scheduled_content": "patches/scheduler-view-dependency-3224340.patch"
+      },
       "__comments__": {
       }
     },
diff --git a/patches/scheduler-view-dependency-3224340.patch b/patches/scheduler-view-dependency-3224340.patch
new file mode 100644
index 000000000..494f9df09
--- /dev/null
+++ b/patches/scheduler-view-dependency-3224340.patch
@@ -0,0 +1,118 @@
+diff --git a/scheduler.links.task.yml b/scheduler.links.task.yml
+index d1afd7e..cc7ad74 100644
+--- a/scheduler.links.task.yml
++++ b/scheduler.links.task.yml
+@@ -9,19 +9,3 @@ scheduler.cron_tab:
+   title: Lightweight cron
+   weight: 10
+   base_route: scheduler.admin_form
+-
+-content_moderation.content:
+-  # Use content_moderation.content which is the same key as is used in the core
+-  # Content Moderation module. If that modules is enabled this avoids two
+-  # 'Overview' links. If https://www.drupal.org/project/drupal/issues/3199682
+-  # gets committed then this route could be removed from here.
+-  title: 'Overview'
+-  route_name: system.admin_content
+-  parent_id: system.admin_content
+-
+-scheduler.scheduled_content:
+-  title: 'Scheduled content'
+-  route_name: view.scheduler_scheduled_content.overview
+-  parent_id: system.admin_content
+-  # Overview seems to have weight 0 and moderated content is weight 1.
+-  weight: 5
+diff --git a/scheduler.module b/scheduler.module
+index ff698b6..e173ac6 100644
+--- a/scheduler.module
++++ b/scheduler.module
+@@ -16,6 +16,7 @@ use Drupal\Core\Url;
+ use Drupal\node\Entity\NodeType;
+ use Drupal\scheduler\SchedulerEvent;
+ use Drupal\scheduler\SchedulerEvents;
++use Drupal\views\Entity\View;
+ 
+ /**
+  * Implements hook_help().
+@@ -729,3 +730,38 @@ function _scheduler_get_scheduler_enabled_node_types($action) {
+     return $bundle->getThirdPartySetting('scheduler', $action . '_enable', $config->get('default_' . $action . '_enable'));
+   });
+ }
++
++/**
++ * Implements hook_local_tasks_alter().
++ */
++function scheduler_local_tasks_alter(&$local_tasks) {
++  $view = View::load('scheduler_scheduled_content');
++  if ($view && $view->status() && $view->getDisplay('overview')) {
++    // Views do not currently support defining secondary local tasks, so define
++    // it dynamically if the view exists, is enabled and the display exists.
++    // Change this if https://www.drupal.org/node/2172307 gets fixed.
++    $local_tasks['scheduler.scheduled_content'] = [
++      'title' => t('Scheduled content'),
++      'route_name' => 'view.scheduler_scheduled_content.overview',
++      'parent_id' => 'system.admin_content',
++      'class' => 'Drupal\Core\Menu\LocalTaskDefault',
++      'options' => [],
++      // Overview seems to have weight 0 and moderated content is weight 1.
++      'weight' => 5,
++    ];
++
++    if (!\Drupal::moduleHandler()->moduleExists('content_moderation')) {
++      // Define a fallback overview local task if content_moderation is not
++      // enabled. If https://www.drupal.org/project/drupal/issues/3199682
++      // gets committed then this route could be removed from here.
++      $local_tasks['scheduler.content_overview'] = [
++        'title' => t('Overview'),
++        'route_name' => 'system.admin_content',
++        'parent_id' => 'system.admin_content',
++        'class' => 'Drupal\Core\Menu\LocalTaskDefault',
++        'options' => [],
++        'weight' => 0,
++      ];
++    }
++  }
++}
+diff --git a/tests/src/Functional/SchedulerScheduledContentListAccessTest.php b/tests/src/Functional/SchedulerScheduledContentListAccessTest.php
+index 33fd887..ca3910d 100644
+--- a/tests/src/Functional/SchedulerScheduledContentListAccessTest.php
++++ b/tests/src/Functional/SchedulerScheduledContentListAccessTest.php
+@@ -28,6 +28,9 @@ class SchedulerScheduledContentListAccessTest extends SchedulerBrowserTestBase {
+       'view own unpublished content',
+     ];
+ 
++    // Create three users, all of whom can access and create content. The editor
++    // can also view the admin overview page, and the two scheduler users each
++    // have one of the scheduler permissions but not the other.
+     $this->editorUser = $this->drupalCreateUser(array_merge($base_permissions, ['access content overview']));
+     $this->schedulerUser = $this->drupalCreateUser(array_merge($base_permissions, ['schedule publishing of nodes']));
+     $this->schedulerManager = $this->drupalCreateUser(array_merge($base_permissions, ['view scheduled content']));
+@@ -143,6 +146,28 @@ class SchedulerScheduledContentListAccessTest extends SchedulerBrowserTestBase {
+     $assert->pageTextContains('Node created by Scheduler User for unpublishing');
+     $assert->pageTextContains('Node created by Scheduler Manager for publishing');
+     $assert->pageTextContains('Node created by Scheduler Manager for unpublishing');
++
++    // Disable the scheduled content view.
++    $view = $this->container->get('entity_type.manager')->getStorage('view')->load('scheduler_scheduled_content');
++    $view->disable()->save();
++
++    // Attempt to view the scheduled content page. Interactively this gives a
++    // '404 page not found' error, but in phpunit it is served with a 200 code.
++    // However the page is empty so we can check that the content is not shown.
++    $this->drupalGet('admin/content/scheduled');
++    $assert->pageTextNotContains('Node created by Scheduler User for unpublishing');
++
++    // Check that access to the content overview page is unaffected.
++    $this->drupalLogin($this->editorUser);
++    $this->drupalGet('admin/content');
++    $assert->statusCodeEquals(200);
++    $assert->pageTextContains('Node created by Scheduler User for unpublishing');
++
++    // Delete the view and check again that the overview remains accessible.
++    $view->delete();
++    $this->drupalGet('admin/content');
++    $assert->statusCodeEquals(200);
++    $assert->pageTextContains('Node created by Scheduler User for unpublishing');
+   }
+ 
+ }
-- 
GitLab