-
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.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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 }} |
110 changes: 110 additions & 0 deletions
110
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,110 @@ | ||
<?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 Prophecy\Argument; | ||
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); | ||
} | ||
|
||
} |