-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #604 from City-of-Helsinki/UHF-10941
UHF-10941: Hakuvahti Sentry error
- Loading branch information
Showing
7 changed files
with
227 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
public/modules/custom/helfi_hakuvahti/templates/hakuvahti-confirmation.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{ title }} | ||
{{ message }} |
2 changes: 2 additions & 0 deletions
2
public/modules/custom/helfi_hakuvahti/templates/hakuvahti-form.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{ title }} | ||
{{ message }} |
109 changes: 109 additions & 0 deletions
109
public/modules/custom/helfi_hakuvahti/tests/src/Kernel/HakuvahtiControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\helfi_hakuvahti\Kernel; | ||
|
||
use Drupal\Core\Url; | ||
use Drupal\KernelTests\KernelTestBase; | ||
use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait; | ||
use Drupal\Tests\user\Traits\UserCreationTrait; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\Traits\PropertyTrait; | ||
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; | ||
|
||
/** | ||
* Tests for hakuvahti controller. | ||
*/ | ||
class HakuvahtiControllerTest extends KernelTestBase { | ||
|
||
use ApiTestTrait; | ||
use UserCreationTrait; | ||
use PropertyTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'user', | ||
'system', | ||
'helfi_hakuvahti', | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->setUpCurrentUser(permissions: ['access content']); | ||
} | ||
|
||
/** | ||
* Tests handleConfirmFormSubmission. | ||
*/ | ||
public function testHandleConfirmFormSubmission(): void { | ||
$this->container->set(ClientInterface::class, $this->createMockHttpClient([ | ||
new Response(200, body: 'success'), | ||
new Response(404, body: 'not found'), | ||
new Response(500, body: 'fail'), | ||
new RequestException("womp womp", new Request('POST', 'test')), | ||
])); | ||
|
||
$logger = $this->prophesize(LoggerInterface::class); | ||
$this->container->set('logger.channel.helfi_hakuvahti', $logger->reveal()); | ||
|
||
// Get request. | ||
$response = $this->makeRequest('GET', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertStringContainsString('Confirm saved search', $response->getContent() ?? ''); | ||
|
||
// Success. | ||
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertStringContainsString('Search saved successfully', $response->getContent() ?? ''); | ||
|
||
// Not found. | ||
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertStringContainsString('Confirmation failed', $response->getContent() ?? ''); | ||
|
||
// Server error. | ||
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertStringContainsString('Confirmation failed', $response->getContent() ?? ''); | ||
|
||
// Guzzle exception. | ||
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertStringContainsString('Confirmation failed', $response->getContent() ?? ''); | ||
} | ||
|
||
/** | ||
* Process a request. | ||
* | ||
* @param string $method | ||
* HTTP method. | ||
* @param string $route | ||
* Drupal route. | ||
* @param array $query | ||
* Query parameters. | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* Controller response. | ||
*/ | ||
private function makeRequest(string $method, string $route, array $query = []): SymfonyResponse { | ||
$url = Url::fromRoute($route, options: [ | ||
'query' => $query, | ||
]); | ||
|
||
$request = $this->getMockedRequest($url->toString(), $method); | ||
|
||
return $this->processRequest($request); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...les/custom/helfi_rekry_content/src/Plugin/search_api/processor/LanguageFieldProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_rekry_content\Plugin\search_api\processor; | ||
|
||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\search_api\Datasource\DatasourceInterface; | ||
use Drupal\search_api\Item\ItemInterface; | ||
use Drupal\search_api\Processor\ProcessorPluginBase; | ||
use Drupal\search_api\Processor\ProcessorProperty; | ||
|
||
/** | ||
* Adds `_language` to the index. | ||
* | ||
* This processor adds _language field to the search api index. The field was | ||
* renamed to `search_api_language` in elasticsearch_connector v8 update. We | ||
* cannot modify rekry search api index without breaking existing hakuvahti | ||
* queries. | ||
* | ||
* @SearchApiProcessor( | ||
* id = "language_field", | ||
* label = @Translation("Language field"), | ||
* description = @Translation("Add _language field to the index."), | ||
* stages = { | ||
* "add_properties" = 20, | ||
* }, | ||
* locked = true, | ||
* hidden = true, | ||
* ) | ||
*/ | ||
class LanguageFieldProcessor extends ProcessorPluginBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL): array { | ||
$properties = []; | ||
|
||
if (!$datasource) { | ||
$definition = [ | ||
'label' => $this->t('Language'), | ||
'description' => $this->t('The legacy _language field.'), | ||
'type' => 'string', | ||
'processor_id' => $this->getPluginId(), | ||
]; | ||
$properties['_language'] = new ProcessorProperty($definition); | ||
} | ||
|
||
return $properties; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFieldValues(ItemInterface $item): void { | ||
$object = $item->getOriginalObject()->getValue(); | ||
|
||
if ($object instanceof EntityInterface) { | ||
$indexableValue = $object->language()->getId(); | ||
|
||
$itemFields = $item->getFields(); | ||
$itemFields = $this->getFieldsHelper() | ||
->filterForPropertyPath($itemFields, NULL, '_language'); | ||
|
||
foreach ($itemFields as $itemField) { | ||
$itemField->addValue($indexableValue); | ||
} | ||
|
||
} | ||
} | ||
|
||
} |