Skip to content

Commit

Permalink
UHF-9832: Added a separate test for the user sanitize drush command.
Browse files Browse the repository at this point in the history
  • Loading branch information
khalima committed May 2, 2024
1 parent 1fd55ad commit 03905a5
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 37 deletions.
102 changes: 102 additions & 0 deletions tests/src/Functional/UserSanitizeCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_api_base\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
use Drush\TestTraits\DrushTestTrait;

/**
* Tests user sanitation form and drush command.
*
* @group helfi_api_base
* @covers \Drupal\helfi_api_base\Commands\UserSanitizeCommands
*/
class UserSanitizeCommandsTest extends BrowserTestBase {

use DrushTestTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'user',
'helfi_api_base',
];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* User account for testing the sanitation form.
*
* @var \Drupal\user\Entity\User
*/
protected User $testUser;

/**
* Default values for the test user.
*
* @var array|string[]
*/
private array $defaultValues = [
'username' => 'Test',
'password' => 'test',
'email' => '[email protected]',
];

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
// Create a test user with default values.
$this->testUser = $this->createUser(
['access content'],
$this->defaultValues['username'],
FALSE,
[
'pass' => $this->defaultValues['password'],
'mail' => $this->defaultValues['email'],
],
);
$this->testUser->block()->save();
}

/**
* Tests helfi:yser-sanitize command with field options.
*/
public function testUserSanitizeCommandWithFields() {
$this->drush('helfi:user-sanitize', [$this->testUser->id()], ['fields' => 'username']);

$storage = \Drupal::entityTypeManager()->getStorage('user');
$storage->resetCache([$this->testUser->id()]);
$entity = $storage->load($this->testUser->id());

$this->assertEquals($entity->getAccountName() === $this->defaultValues['username'], FALSE);
$this->assertEquals($entity->getEmail() === $this->defaultValues['email'], TRUE);
$password_service = $this->container->get('password');
$this->assertEquals($password_service->check($this->defaultValues['password'], $entity->getPassword()), TRUE);
}

/**
* Tests helfi:yser-sanitize command without field options.
*/
public function testUserSanitizeCommandWithOutFields() {
$this->drush('helfi:user-sanitize', [$this->testUser->id()]);

$storage = \Drupal::entityTypeManager()->getStorage('user');
$storage->resetCache([$this->testUser->id()]);
$entity = $storage->load($this->testUser->id());

$this->assertEquals($entity->getAccountName() === $this->defaultValues['username'], FALSE);
$this->assertEquals($entity->getEmail() === $this->defaultValues['email'], FALSE);
$password_service = $this->container->get('password');
$this->assertEquals($password_service->check($this->defaultValues['password'], $entity->getPassword()), FALSE);
}

}
37 changes: 0 additions & 37 deletions tests/src/Functional/UserSanitizeFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
*
* @group helfi_api_base
* @covers \Drupal\helfi_api_base\Entity\Form\UserEntitySanitizeForm
* @covers \Drupal\helfi_api_base\Commands\UserSanitizeCommands
*/
class UserSanitizeFormTest extends BrowserTestBase {

use DrushTestTrait;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -140,38 +137,4 @@ public function testUserSanitizeForm(): void {
$this->assertSession()->pageTextContains("User account id {$this->testUser->id()} was sanitized.");
}

/**
* Tests helfi:yser-sanitize command with field options.
*/
public function testUserSanitizeCommandWithFields() {
$this->testUser->block()->save();
$this->drush('helfi:user-sanitize', [$this->testUser->id()], ['fields' => 'username']);

$storage = \Drupal::entityTypeManager()->getStorage('user');
$storage->resetCache([$this->testUser->id()]);
$entity = $storage->load($this->testUser->id());

$this->assertEquals($entity->getAccountName() === $this->defaultValues['username'], FALSE);
$this->assertEquals($entity->getEmail() === $this->defaultValues['email'], TRUE);
$password_service = $this->container->get('password');
$this->assertEquals($password_service->check($this->defaultValues['password'], $entity->getPassword()), TRUE);
}

/**
* Tests helfi:yser-sanitize command without field options.
*/
public function testUserSanitizeCommandWithOutFields() {
$this->testUser->block()->save();
$this->drush('helfi:user-sanitize', [$this->testUser->id()]);

$storage = \Drupal::entityTypeManager()->getStorage('user');
$storage->resetCache([$this->testUser->id()]);
$entity = $storage->load($this->testUser->id());

$this->assertEquals($entity->getAccountName() === $this->defaultValues['username'], FALSE);
$this->assertEquals($entity->getEmail() === $this->defaultValues['email'], FALSE);
$password_service = $this->container->get('password');
$this->assertEquals($password_service->check($this->defaultValues['password'], $entity->getPassword()), FALSE);
}

}

0 comments on commit 03905a5

Please sign in to comment.