dev/endpoints/endpoint_post_AAAAA/tests/src/Kernel/PostMemberRegistrationTest.php
2024-01-16 09:54:33 +02:00

81 lines
3.3 KiB
PHP

<?php
namespace Drupal\Tests\dotsoft\Kernel;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class PostMemberRegistrationTest extends KernelTestBase
{
use NodeCreationTrait;
protected static $modules = [
'dotsoft',
'dotsoft_config_user',
'field',
'node',
'user'
];
protected function setUp(): void
{
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installSchema('node', ['node_access']);
$this->installConfig([
'dotsoft_config_user'
]);
}
/** @test */
public function member_account_is_created_when_export_directory_exists(): void
{
$exportDirectory = $this->createNode(['type' => 'export_directory']);
$uri = Url::fromRoute('dotsoft.users.register')->toString();
$request = Request::create($uri, 'POST', [], [], [], [], '{"email":"johndoe@example.com","password":"12345","export_directory":' . $exportDirectory->id() . '}');
$response = $this->container->get('http_kernel')->handle($request);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$this->assertEquals('"Member account is created successfully"', $response->getContent());
$account = current(\Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['mail' => 'johndoe@example.com']));
$this->assertNotEmpty($account);
$this->assertEquals(['member'], $account->getRoles(true));
$this->assertNotEmpty($account->get('field_export_directory')->getValue());
$this->assertEquals($exportDirectory->id(), $account->get('field_export_directory')->getValue()[0]['target_id']);
}
/** @test */
public function member_account_is_created_when_export_directory_does_not_exist(): void
{
$uri = Url::fromRoute('dotsoft.users.register')->toString();
$request = Request::create($uri, 'POST', [], [], [], [], '{"email":"johndoe@example.com","password":"12345","export_directory_title":"DOTSOFT"}');
$response = $this->container->get('http_kernel')->handle($request);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), $response->getContent());
$this->assertEquals('"Member account is created successfully"', $response->getContent());
$account = current(\Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['mail' => 'johndoe@example.com']));
$this->assertNotEmpty($account);
$this->assertEquals(['member'], $account->getRoles(true));
$exportDirectory = current(\Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => 'export_directory',
'title' => 'DOTSOFT'
]));
$this->assertNotEmpty($exportDirectory);
$this->assertEquals($account->id(), $exportDirectory->get('uid')->getValue()[0]['target_id']);
$this->assertNotEmpty($account->get('field_export_directory')->getValue());
$this->assertEquals($exportDirectory->id(), $account->get('field_export_directory')->getValue()[0]['target_id']);
}
}