Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-8946: redirect to existing job listing translation #313

Merged
merged 15 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ jobs:
- name: Run PHPUnit tests
run: |
composer test-php public/modules/custom
[ -d "tests/" ] && composer test-php tests/ || echo "No DTT tests found. Ignoring..."
if [ -d "tests/" ]; then composer test-php tests/; else echo "No DTT tests found. Ignoring..."; fi
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<env name="DTT_MINK_DRIVER_ARGS" value='["chrome", {"chromeOptions":{"w3c": false }}, "http://127.0.0.1:4444"]'/>
<env name="DTT_API_OPTIONS" value='{"socketTimeout": 360, "domWaitTimeout": 3600000}' />
<env name="DTT_API_URL" value="http://127.0.0.1:9222"/>
<env name="DTT_BASE_URL" value="http://127.0.0.1:8080"/>
<env name="DTT_BASE_URL" value="http://127.0.0.1:8888"/>
</php>
<testsuites>
<testsuite name="unit">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- { name: 'event_subscriber' }
helfi_rekry_content.job_listing_redirect_subscriber:
class: Drupal\helfi_rekry_content\EventSubscriber\JobListingRedirectSubscriber
arguments: ['@config.factory', '@current_user']
arguments: ['@config.factory', '@current_user', '@entity_type.manager']
tags:
- { name: 'event_subscriber' }
logger.channel.helfi_rekry_content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
namespace Drupal\helfi_rekry_content\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;

/**
* Redirect job listing 403s for anonymous users.
* Http exception event subscribers for job listings.
*/
class JobListingRedirectSubscriber extends HttpExceptionSubscriberBase {

Expand All @@ -24,10 +25,13 @@ class JobListingRedirectSubscriber extends HttpExceptionSubscriberBase {
* The configuration factory.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* The entity type manager.
*/
public function __construct(
protected ConfigFactoryInterface $configFactory,
protected AccountInterface $currentUser
protected AccountInterface $currentUser,
protected EntityTypeManager $entityTypeManager,
) {}

/**
Expand Down Expand Up @@ -66,7 +70,52 @@ public function on403(ExceptionEvent $event): void {
$url = Url::fromRoute('entity.node.canonical', ['node' => $redirectNode])->toString();

// Set temporary redirect.
$response = new RedirectResponse($url, 307);
$response = new TrustedRedirectResponse($url, 307);
$event->setResponse($response);
}

/**
* The 404 exception listener.
*
* #UHF8946 External service's automation is only capable of creating links to
* finnish job listings. If finnish translation doesn't exist the user will be
* automatically redirected to existing translation with matching job ID.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The Event to process.
*/
public function on404(ExceptionEvent $event) : void {
$uri = $event->getRequest()->getRequestUri();
$redirectPaths = [
'fi' => 'avoimet-tyopaikat/avoimet-tyopaikat/',
'sv' => 'lediga-jobb/lediga-jobb/',
'en' => 'open-jobs/open-jobs/',
];

$redirectFrom = NULL;
foreach ($redirectPaths as $path) {
if (str_contains($uri, $path)) {
$redirectFrom = $path;
break;
}
}

if (!$redirectFrom) {
return;
}

$recruitmentId = array_reverse(explode('/', $uri))[0];
$nodes = $this->entityTypeManager
->getStorage('node')
->loadByProperties(['field_recruitment_id' => $recruitmentId]);

if (!$nodes || !$node = reset($nodes)) {
return;
}

$url = $node->toUrl('canonical', ['language' => $node->language()])->toString();
$response = new TrustedRedirectResponse($url);
$response->addCacheableDependency($url);
$event->setResponse($response);
}

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

declare(strict_types = 1);

namespace Drupal\helfi_rekry_content\Tests\dtt\src\ExistingSite;

use Drupal\Tests\helfi_api_base\Functional\ExistingSiteTestBase;

/**
* Test job listing redirect.
*
* @group dtt
*/
class JoblistingRedirectTest extends ExistingSiteTestBase {

/**
* Test job listing 404 redirect.
*/
public function test404Redirect(): void {
$node = $this->createNode([
'type' => 'job_listing',
'langcode' => 'sv',
'title' => 'en jobb',
'field_recruitment_id' => 'TESTI-1234-56-7890',
]);

$path = $node->toUrl()->toString();
$recruitmentId = array_reverse(explode('/', $path))[0];

$this->drupalGetWithLanguage("/fi/avoimet-tyopaikat/avoimet-tyopaikat/$recruitmentId", 'fi');
$url = $this->getSession()->getCurrentUrl();
$this->assertTrue(str_ends_with($url, '/sv/lediga-jobb/lediga-jobb/testi-1234-56-7890'));
}

}