Skip to content

Commit

Permalink
UHF-9937: Make sure removed job listings are not published
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Dec 2, 2024
1 parent bf85986 commit 7ab0445
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 24 deletions.
11 changes: 9 additions & 2 deletions docker/openshift/crons/migrate-changed-job-listings.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/bin/bash

migrations=(
"helfi_rekry_jobs:all"
)

echo "Starting job listing:changed migrations: $(date)"
while true
do
echo "Running job listing:changed migrations: $(date)"
# Allow migrations to be run every 10 minutes, reset stuck migrations every 30 minutes.
drush migrate:import helfi_rekry_jobs:changed --update --reset-threshold 1800 --interval 600 --no-progress

for migration in "${migrations[@]}"; do
# Allow migrations to be run every 10 minutes, reset stuck migrations every 30 minutes.
drush migrate:import "$migration" --reset-threshold 1800 --interval 600 --no-progress
done

# Sleep for 15 minutes.
sleep 900
Expand Down
1 change: 0 additions & 1 deletion docker/openshift/crons/migrate-job-listings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ migrations=(
"helfi_rekry_organizations:en"
"helfi_rekry_employments:all"
"helfi_rekry_employment_types:all"
"helfi_rekry_jobs:all"
)

echo "Starting job listing migrations: $(date)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
use Drush\Drupal\Migrate\MigrateMissingSourceRowsEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand Down Expand Up @@ -83,14 +84,21 @@ function ($values, $destinationValue) {
return;
}

// Query missing nodes that are still published.
$query = $this->entityTypeManager->getStorage('node')
->getQuery()
$query = $this->entityTypeManager
->getStorage('node')
->getQuery();

// Query missing nodes that are still published
// or still scheduled to be published.
$orCondition = $query->orConditionGroup();
$orCondition->condition('status', NodeInterface::PUBLISHED);
$orCondition->exists('publish_on');

$nids = $query
->accessCheck(FALSE)
->condition('nid', $destinationIds, 'IN')
->condition('status', 1)
->notExists('unpublish_on');
$nids = $query->execute();
->condition($orCondition)
->execute();

foreach ($nids as $nid) {
$job = ['nid' => $nid];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\Attribute\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Queue Worker for removing job listings not present in source.
*
* @QueueWorker(
* id = "job_listing_unpublish_worker",
* title = @Translation("Job listing unpublish worker"),
* cron = {"time" = 60}
* )
*/
#[QueueWorker(
id: 'job_listing_unpublish_worker',
title: new TranslatableMarkup('Job listing unpublish worker'),
cron: ['time' => 60],
)]
final class UnpublishWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

/**
Expand Down Expand Up @@ -74,23 +75,25 @@ public function processItem($data): void {

// Unpublish all translations.
if ($node instanceof NodeInterface && $node->getType() == 'job_listing') {
foreach (['fi', 'sv', 'en'] as $langcode) {
foreach ($node->getTranslationLanguages() as $language) {
$langcode = $language->getId();

// Unpublish the job listing node as it's still published, but it's
// no longer available at the source.
if (!$node->hasTranslation($langcode)) {
continue;
}

$translation = $node->getTranslation($langcode);
if ($translation->isPublished()) {
$translation->setUnpublished();
if ($translation->hasField('publish_on') && !empty($translation->get('publish_on')->getValue())) {
// Also clear the publish on date to make sure the translation is
// not going to be re-published.
$translation->set('publish_on', NULL);
}
$translation->save();
$translation->setUnpublished();

// Also clear the published on date so the translation
// is not going to be re-published.
if ($translation->hasField('publish_on') && !empty($translation->get('publish_on')->getValue())) {
$translation->set('publish_on', NULL);
}

$translation->save();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_rekry_content\Kernel;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\NodeInterface;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;

/**
* Unpublish worker test.
*/
class UnpublishWorkerTest extends KernelTestBase {

use NodeCreationTrait;
use ContentTypeCreationTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'node',
'field',
'user',
'text',
'filter',
'scheduler',
'language',
'content_lock',
'helfi_rekry_content',
];

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();

$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installSchema('node', 'node_access');
$this->installConfig(['node', 'filter']);
$this->createContentType(['type' => 'job_listing']);
}

/**
* Tests unpublish worker.
*/
public function testUnpublishWorker(): void {
$manager = $this->container->get('plugin.manager.queue_worker');
$sut = $manager->createInstance('job_listing_unpublish_worker');

$published_node = $this->createNode([
'type' => 'job_listing',
'status' => NodeInterface::PUBLISHED,
]);

$scheduled_node = $this->createNode([
'type' => 'job_listing',
'status' => NodeInterface::NOT_PUBLISHED,
]);

ConfigurableLanguage::createFromLangcode('sv')->save();
$translated_node = $this->createNode([
'type' => 'job_listing',
'langcode' => 'en',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$translated_node->addTranslation('sv', [
'title' => $this->randomMachineName(),
'status' => NodeInterface::PUBLISHED,
])->save();

$sut->processItem(['nid' => $published_node->id()]);
$this->assertNodeIsUnpublished($published_node->id());

$sut->processItem(['nid' => $scheduled_node->id()]);
$this->assertNodeIsUnpublished($scheduled_node->id());

$sut->processItem(['nid' => $translated_node->id()]);
$this->assertNodeIsUnpublished($translated_node->id());
}

/**
* Asserts that node is no longer published.
*/
private function assertNodeIsUnpublished(string $nid): void {
/** @var \Drupal\node\NodeInterface $node */
$node = $this->container->get(EntityTypeManagerInterface::class)
->getStorage('node')
->load($nid);

foreach ($node->getTranslationLanguages() as $language) {
$translation = $node->getTranslation($language->getId());
$this->assertFalse($translation->isPublished());
$this->assertEmpty($translation->get('publish_on')->getValue());
}
}

}

0 comments on commit 7ab0445

Please sign in to comment.