Skip to content

Commit

Permalink
Add user creation to test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder committed Apr 10, 2023
1 parent ee82ea6 commit 774cd51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
9 changes: 7 additions & 2 deletions modules/common/tests/src/Functional/Api1TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use GuzzleHttp\RequestOptions;
use Opis\JsonSchema\Schema;
use Opis\JsonSchema\Validator;
use weitzman\DrupalTestTraits\Entity\UserCreationTrait;
use weitzman\DrupalTestTraits\ExistingSiteBase;

abstract class Api1TestBase extends ExistingSiteBase {
use CleanUp;
use UserCreationTrait;

protected $http;
protected $spec;
Expand All @@ -27,9 +29,12 @@ public function setUp(): void {
$this->removeFiles();
$this->removeDatastoreTables();
$this->setDefaultModerationState($state = 'published');

$user = $this->createUser([], "testapiuser", FALSE, ['roles' => ['api_user'], 'mail' => 'testapiuser@test.com']);

$this->baseUrl = getenv('SIMPLETEST_BASE_URL');
$this->http = new Client(['base_uri' => $this->baseUrl]);
$this->auth = ['testapiuser', 'testapiuser'];
$this->auth = ['testapiuser', $user->pass_raw];
$this->endpoint = $this->getEndpoint();

// Load the API spec for use by tests.
Expand All @@ -38,7 +43,7 @@ public function setUp(): void {
}

public function tearDown(): void {
parent::setUp();
parent::tearDown();
$this->http = NULL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Drupal\Core\Session\AccountProxy;
use Drupal\metastore\Service as Metastore;
use Drupal\metastore\ValidMetadataFactory;
use Drupal\metastore_admin\Plugin\Action\HideCurrentRevisionAction;
use Drupal\Tests\common\Traits\CleanUp;
use Drupal\Tests\metastore\Unit\ServiceTest;
use Drupal\user\Entity\User;
use RootedData\RootedJsonData;
use weitzman\DrupalTestTraits\Entity\UserCreationTrait;
use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
Expand All @@ -18,6 +21,11 @@
*/
class HideCurrentRevisionActionTest extends ExistingSiteBase {
use CleanUp;
use UserCreationTrait;

protected ValidMetadataFactory $validMetadataFactory;
protected User $testUser;
protected User $testApiUser;

private const S3_PREFIX = 'https://dkan-default-content-files.s3.amazonaws.com/phpunit';

Expand All @@ -32,6 +40,9 @@ public function setUp(): void {
$this->removeDatastoreTables();
$this->setDefaultModerationState("published");

$this->testUser = $this->createUser([], "testadmin", TRUE, ['mail' => 'testadmin@test.com']);
$this->testApiUser = $this->createUser([], "testapiuser", FALSE, ['roles' => ['api_user'], 'mail' => 'testapiuser@test.com']);

$this->validMetadataFactory = ServiceTest::getValidMetadataFactory($this);
}

Expand All @@ -45,20 +56,8 @@ public function testHide() {
$node = current($result);
$container = \Drupal::getContainer();

$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['name' => 'testadmin']
);
$testUser = reset($users);

$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['name' => 'testapiuser']
);
$testapiuser = reset($users);

$accountProxy = new AccountProxy($container->get('event_dispatcher'));
$accountProxy->setAccount($testUser);
$accountProxy->setAccount($this->testUser);
$container->set('current_user', $accountProxy);

$hideCurrentRevisionAction = HideCurrentRevisionAction::create(
Expand Down Expand Up @@ -95,7 +94,7 @@ public function testHide() {
$node = current($result);

$accountProxy = new AccountProxy($container->get('event_dispatcher'));
$accountProxy->setAccount($testapiuser);
$accountProxy->setAccount($this->testApiUser);
$container->set('current_user', $accountProxy);
$hideCurrentRevisionAction = HideCurrentRevisionAction::create(
$container,
Expand Down

0 comments on commit 774cd51

Please sign in to comment.