config_preview_deploy-1.0.0-alpha3/tests/src/Kernel/OutgoingRequestIntegrationTest.php
tests/src/Kernel/OutgoingRequestIntegrationTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\config_preview_deploy\Kernel;
use Drupal\consumers\Entity\Consumer;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
/**
* Tests outgoing request integration and structure.
*
* @group config_preview_deploy
*/
class OutgoingRequestIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'config_preview_deploy',
'simple_oauth',
'simple_oauth_static_scope',
'consumers',
'key',
'system',
'user',
'serialization',
'options',
'file',
'image',
];
/**
* Test user.
*/
protected User $testUser;
/**
* OAuth consumer.
*/
protected Consumer $consumer;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('consumer');
$this->installEntitySchema('oauth2_token');
$this->installEntitySchema('oauth2_scope');
$this->installEntitySchema('oauth2_token_type');
$this->installEntitySchema('key');
$this->installConfig([
'system',
'user',
'simple_oauth',
'consumers',
'config_preview_deploy',
]);
// Create test user.
$this->testUser = User::create([
'name' => 'deploy_user',
'mail' => 'deploy@example.com',
'status' => 1,
]);
$this->testUser->save();
// Grant the user the required permission.
user_role_grant_permissions('authenticated', ['accept config deployments']);
// Run module install to create OAuth consumer.
\Drupal::moduleHandler()->loadInclude('config_preview_deploy', 'install');
config_preview_deploy_install();
// Load the created consumer.
$consumers = \Drupal::entityTypeManager()
->getStorage('consumer')
->loadByProperties(['client_id' => 'config_preview_deploy']);
$this->consumer = reset($consumers);
// Set up configuration.
$config = \Drupal::configFactory()->getEditable('config_preview_deploy.settings');
$config->set('production_url', 'https://production.example.com');
$config->save();
}
/**
* Tests OAuth consumer configuration.
*/
public function testOauthConsumerConfiguration(): void {
$this->assertNotNull($this->consumer, 'OAuth consumer should exist');
// Verify consumer configuration.
$this->assertEquals('config_preview_deploy', $this->consumer->getClientId());
$this->assertEquals('Config Preview Deploy', $this->consumer->label());
$this->assertFalse((bool) $this->consumer->get('third_party')->value, 'Consumer should not be third party');
$this->assertTrue((bool) $this->consumer->get('confidential')->value, 'Consumer should be confidential');
// Check grant types.
$grant_types = $this->consumer->get('grant_types')->getValue();
$this->assertContains(['value' => 'authorization_code'], $grant_types, 'Consumer should support authorization_code grant type');
}
/**
* Tests environment name detection.
*/
public function testEnvironmentNameDetection(): void {
$config_verifier = \Drupal::service('config_preview_deploy.config_verifier');
$environment = $config_verifier->getEnvironmentName();
$this->assertIsString($environment);
$this->assertNotEmpty($environment);
// In test environment, it should fall back to hostname or 'local'.
$this->assertTrue(in_array($environment, ['local', gethostname(), 'test']));
}
}
