Skip to content

Commit

Permalink
Add user creation to functional test setup (#3936)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder committed Apr 13, 2023
1 parent c882f98 commit 6dfe83f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jobs:
command: |
ddev xdebug on
$CIRCLE_WORKING_DIRECTORY/cc-test-reporter before-build
ddev dkan-test-phpunit \
ddev dkan-phpunit \
--coverage-clover /var/www/html/docroot/modules/contrib/dkan/clover.xml \
--coverage-html /var/www/html/docroot/modules/contrib/dkan/coverage-html \
--log-junit /var/www/html/docroot/modules/contrib/dkan/junit/junit.xml
Expand All @@ -177,7 +177,7 @@ jobs:
- run:
name: Run PHPUnit tests
command: |
ddev dkan-test-phpunit \
ddev dkan-phpunit \
--log-junit /var/www/html/docroot/modules/contrib/dkan/junit/junit.xml
- store_test_results:
path: dkan/junit
Expand Down
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 6dfe83f

Please sign in to comment.